@wheatley-ai/cli 0.1.0 → 0.2.0
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/README.md +192 -0
- package/dist/cli/chat-state.js +95 -0
- package/dist/cli/chat.js +32 -95
- package/dist/cli/io.js +22 -2
- package/dist/cli/personality.js +81 -0
- package/dist/cli/tui/app.js +159 -0
- package/dist/cli/tui/commands.js +98 -0
- package/dist/cli/tui/controller.js +504 -0
- package/dist/cli/tui/editor.js +125 -0
- package/dist/cli/tui/format.js +32 -0
- package/dist/cli/tui/markdown.js +147 -0
- package/dist/cli/tui/prompt.js +275 -0
- package/dist/cli/tui/run.js +63 -0
- package/dist/cli/tui/transcript.js +89 -0
- package/dist/node_modules/@wheatley-ai/sdk/package.json +1 -1
- package/package.json +8 -2
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Box, Static, Text, useApp, useInput, useWindowSize } from "ink";
|
|
3
|
+
import { useEffect, useState, useSyncExternalStore } from "react";
|
|
4
|
+
import { formatCost, formatCount, formatSeconds } from "./format.js";
|
|
5
|
+
import { wrapLines } from "./markdown.js";
|
|
6
|
+
import { Prompt } from "./prompt.js";
|
|
7
|
+
import { EntryView, LOGO_WIDTH } from "./transcript.js";
|
|
8
|
+
/** The frames of the spinning logo. */
|
|
9
|
+
const SPINNER_FRAMES = ["◐", "◓", "◑", "◒"];
|
|
10
|
+
const FRAME_MS = 120;
|
|
11
|
+
/** How long the spinner shows one thinking line. */
|
|
12
|
+
const LINE_MS = 3500;
|
|
13
|
+
const MS_PER_SECOND = 1000;
|
|
14
|
+
const APPROVAL_PREVIEW_LINES = 12;
|
|
15
|
+
/**
|
|
16
|
+
* Output stays out of the last terminal column. A line that fills it makes some terminals wrap early, which splits
|
|
17
|
+
* the last word of the line.
|
|
18
|
+
*/
|
|
19
|
+
const RIGHT_MARGIN = 1;
|
|
20
|
+
const MIN_WIDTH = 20;
|
|
21
|
+
/** Re-renders every `intervalMs` while `active`, and returns the current time. */
|
|
22
|
+
const useTicker = (active, intervalMs) => {
|
|
23
|
+
const [now, setNow] = useState(Date.now);
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
const timer = active
|
|
26
|
+
? setInterval(() => {
|
|
27
|
+
setNow(Date.now());
|
|
28
|
+
}, intervalMs)
|
|
29
|
+
: undefined;
|
|
30
|
+
return () => {
|
|
31
|
+
clearInterval(timer);
|
|
32
|
+
};
|
|
33
|
+
}, [active, intervalMs]);
|
|
34
|
+
return now;
|
|
35
|
+
};
|
|
36
|
+
const activityLabel = (activity, thinkingLines, elapsed) => {
|
|
37
|
+
if (activity.kind === "tool") {
|
|
38
|
+
return (_jsxs(Text, { children: [_jsx(Text, { children: "Running " }), _jsx(Text, { bold: true, children: activity.name }), _jsxs(Text, { dimColor: true, children: [" ", activity.input.split("\n")[0]] })] }));
|
|
39
|
+
}
|
|
40
|
+
if (activity.kind === "shell") {
|
|
41
|
+
return (_jsxs(Text, { children: [_jsx(Text, { bold: true, children: "$ " }), _jsx(Text, { children: activity.command })] }));
|
|
42
|
+
}
|
|
43
|
+
const line = thinkingLines[Math.floor(elapsed / LINE_MS) % thinkingLines.length] ?? "";
|
|
44
|
+
return _jsx(Text, { italic: true, children: line });
|
|
45
|
+
};
|
|
46
|
+
/** The spinning logo with what the Agent does now and the elapsed time. */
|
|
47
|
+
const ActivityLine = ({ activity, thinkingLines }) => {
|
|
48
|
+
const now = useTicker(true, FRAME_MS);
|
|
49
|
+
const elapsed = Math.max(0, now - activity.startedAt);
|
|
50
|
+
const frame = SPINNER_FRAMES[Math.floor(now / FRAME_MS) % SPINNER_FRAMES.length];
|
|
51
|
+
return (_jsxs(Box, { marginTop: 1, children: [_jsx(Box, { width: 2, children: _jsx(Text, { color: "cyan", children: frame }) }), _jsx(Box, { flexShrink: 1, children: _jsx(Text, { wrap: "truncate-end", children: activityLabel(activity, thinkingLines, elapsed) }) }), _jsxs(Text, { dimColor: true, children: [" ", "(", formatSeconds(elapsed / MS_PER_SECOND), " \u00B7 esc to interrupt)"] })] }));
|
|
52
|
+
};
|
|
53
|
+
const CHOICES = [
|
|
54
|
+
{ decision: "allow_once", hotkey: "o", label: "Yes, once" },
|
|
55
|
+
{
|
|
56
|
+
decision: "allow_conversation",
|
|
57
|
+
hotkey: "c",
|
|
58
|
+
label: "Yes, for this conversation",
|
|
59
|
+
},
|
|
60
|
+
{ decision: "allow_always", hotkey: "a", label: "Always allow this Tool" },
|
|
61
|
+
{ decision: "deny", hotkey: "d", label: "No, and tell Wheatley why" },
|
|
62
|
+
];
|
|
63
|
+
/** Asks for an Approval. Arrows and Enter, a hotkey, or a number choose. Esc denies. */
|
|
64
|
+
const ApprovalPanel = ({ approval, onDecide }) => {
|
|
65
|
+
const [selected, setSelected] = useState(0);
|
|
66
|
+
const [reason, setReason] = useState();
|
|
67
|
+
const choose = (index) => {
|
|
68
|
+
const choice = CHOICES[index];
|
|
69
|
+
if (choice === undefined) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (choice.decision === "deny") {
|
|
73
|
+
setReason("");
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
onDecide(choice.decision);
|
|
77
|
+
};
|
|
78
|
+
useInput((input, key) => {
|
|
79
|
+
if (reason !== undefined) {
|
|
80
|
+
if (key.return) {
|
|
81
|
+
onDecide("deny", reason.trim() === "" ? undefined : reason.trim());
|
|
82
|
+
}
|
|
83
|
+
else if (key.escape) {
|
|
84
|
+
onDecide("deny");
|
|
85
|
+
}
|
|
86
|
+
else if (key.backspace || key.delete) {
|
|
87
|
+
setReason(reason.slice(0, -1));
|
|
88
|
+
}
|
|
89
|
+
else if (!key.ctrl && !key.meta && input !== "") {
|
|
90
|
+
setReason(`${reason}${input}`);
|
|
91
|
+
}
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (key.upArrow || key.downArrow) {
|
|
95
|
+
setSelected((selected + (key.upArrow ? -1 : 1) + CHOICES.length) % CHOICES.length);
|
|
96
|
+
}
|
|
97
|
+
else if (key.return) {
|
|
98
|
+
choose(selected);
|
|
99
|
+
}
|
|
100
|
+
else if (key.escape) {
|
|
101
|
+
onDecide("deny");
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
const byKey = CHOICES.findIndex((choice, index) => choice.hotkey === input.toLowerCase() || `${index + 1}` === input);
|
|
105
|
+
if (byKey !== -1) {
|
|
106
|
+
choose(byKey);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
const lines = approval.inputPreview.split("\n");
|
|
111
|
+
const preview = lines.length > APPROVAL_PREVIEW_LINES
|
|
112
|
+
? [
|
|
113
|
+
...lines.slice(0, APPROVAL_PREVIEW_LINES),
|
|
114
|
+
`… ${lines.length - APPROVAL_PREVIEW_LINES} more lines`,
|
|
115
|
+
].join("\n")
|
|
116
|
+
: approval.inputPreview;
|
|
117
|
+
return (_jsxs(Box, { borderColor: "yellow", borderStyle: "round", flexDirection: "column", marginTop: 1, paddingX: 1, children: [_jsxs(Text, { children: [_jsx(Text, { color: "yellow", children: "\u26A0 " }), _jsx(Text, { bold: true, children: approval.toolName }), _jsx(Text, { children: " needs your approval" })] }), _jsx(Box, { marginY: 1, marginLeft: 2, children: _jsx(Text, { dimColor: true, children: preview }) }), reason === undefined ? (CHOICES.map((choice, index) => (_jsxs(Text, { children: [_jsx(Text, { color: "yellow", children: index === selected ? "› " : " " }), _jsxs(Text, { bold: index === selected, children: [index + 1, ". ", choice.label] }), _jsxs(Text, { dimColor: true, children: [" (", choice.hotkey, ")"] })] }, choice.decision)))) : (_jsxs(Text, { children: [_jsx(Text, { color: "yellow", children: "Why? " }), _jsx(Text, { children: reason }), _jsx(Text, { inverse: true, children: " " }), _jsx(Text, { dimColor: true, children: " Enter sends \u00B7 Esc denies without a reason" })] }))] }));
|
|
118
|
+
};
|
|
119
|
+
const StatusBar = ({ view, busy, columns }) => {
|
|
120
|
+
const { usage } = view;
|
|
121
|
+
const left = [
|
|
122
|
+
view.agent,
|
|
123
|
+
view.model ?? "model unknown",
|
|
124
|
+
`↑${formatCount(usage.inputTokens)} ↓${formatCount(usage.outputTokens)}`,
|
|
125
|
+
formatCost(usage.costUsd),
|
|
126
|
+
].join(" · ");
|
|
127
|
+
const right = busy
|
|
128
|
+
? "esc interrupt · enter sends next"
|
|
129
|
+
: "/ commands · ! shell · \\⏎ newline";
|
|
130
|
+
return (_jsxs(Box, { justifyContent: "space-between", paddingX: 1, width: columns, children: [_jsx(Text, { dimColor: true, wrap: "truncate-end", children: left }), _jsx(Text, { dimColor: true, wrap: "truncate-start", children: right })] }));
|
|
131
|
+
};
|
|
132
|
+
/** The interactive chat: the transcript, the live reply, the spinner, and the input box or the Approval panel. */
|
|
133
|
+
export const ChatApp = ({ controller, version, history, onHistory, }) => {
|
|
134
|
+
const view = useSyncExternalStore(controller.subscribe, controller.view);
|
|
135
|
+
const { exit } = useApp();
|
|
136
|
+
const { columns: terminalColumns } = useWindowSize();
|
|
137
|
+
const columns = Math.max(MIN_WIDTH, terminalColumns - RIGHT_MARGIN);
|
|
138
|
+
const { busy } = controller;
|
|
139
|
+
const onSubmit = (text) => {
|
|
140
|
+
controller
|
|
141
|
+
.submit(text)
|
|
142
|
+
.then((result) => {
|
|
143
|
+
if (result === "exit") {
|
|
144
|
+
exit();
|
|
145
|
+
}
|
|
146
|
+
return result;
|
|
147
|
+
})
|
|
148
|
+
.catch(() => {
|
|
149
|
+
// `submit` reports its own errors as transcript entries.
|
|
150
|
+
});
|
|
151
|
+
};
|
|
152
|
+
const onDecide = (decision, reason) => {
|
|
153
|
+
controller.decide(decision, reason).catch(() => {
|
|
154
|
+
// `decide` reports its own errors as transcript entries.
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
return (_jsxs(_Fragment, { children: [_jsx(Static, { items: [...view.entries], children: (entry) => (_jsx(Box, { width: columns, children: _jsx(EntryView, { columns: columns, entry: entry, version: version, view: view }) }, entry.id)) }), view.live.length === 0 ? undefined : (_jsx(Box, { marginLeft: LOGO_WIDTH, width: columns - LOGO_WIDTH, children: _jsx(Text, { children: wrapLines(view.live, columns - LOGO_WIDTH) }) })), view.activity === undefined ||
|
|
158
|
+
view.approval !== undefined ? undefined : (_jsx(ActivityLine, { activity: view.activity, thinkingLines: view.thinkingLines })), view.approval === undefined ? (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsx(Prompt, { busy: busy, columns: columns, history: history, onExit: exit, onHistory: onHistory, onInterrupt: controller.interrupt, onSubmit: onSubmit }), _jsx(StatusBar, { busy: busy, columns: columns, view: view })] })) : (_jsx(ApprovalPanel, { approval: view.approval, onDecide: onDecide }, view.approval.approvalId))] }));
|
|
159
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { BUILTIN_COMMANDS } from "@wheatley-ai/sdk";
|
|
2
|
+
/** The commands that the terminal runs without the Agent. */
|
|
3
|
+
export const LOCAL_COMMANDS = {
|
|
4
|
+
copy: "/copy",
|
|
5
|
+
exit: "/exit",
|
|
6
|
+
help: "/help",
|
|
7
|
+
quit: "/quit",
|
|
8
|
+
};
|
|
9
|
+
export const CHAT_COMMANDS = [
|
|
10
|
+
...BUILTIN_COMMANDS.map((command) => ({
|
|
11
|
+
argument: "argument" in command ? command.argument : undefined,
|
|
12
|
+
description: command.description,
|
|
13
|
+
local: false,
|
|
14
|
+
word: `/${command.name}`,
|
|
15
|
+
})),
|
|
16
|
+
{
|
|
17
|
+
description: "Copy the last reply to the clipboard",
|
|
18
|
+
local: true,
|
|
19
|
+
word: LOCAL_COMMANDS.copy,
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
description: "List the commands and keys",
|
|
23
|
+
local: true,
|
|
24
|
+
word: LOCAL_COMMANDS.help,
|
|
25
|
+
},
|
|
26
|
+
{ description: "End the chat", local: true, word: LOCAL_COMMANDS.exit },
|
|
27
|
+
];
|
|
28
|
+
/** The command name and its argument hint, for example `/model <model-id>`. */
|
|
29
|
+
export const commandLabel = (command) => command.argument === undefined
|
|
30
|
+
? command.word
|
|
31
|
+
: `${command.word} ${command.argument}`;
|
|
32
|
+
const COMMAND_NAME = /^\/[a-z-]*$/u;
|
|
33
|
+
const WORD_SPLIT = /\s+/u;
|
|
34
|
+
const RANK = {
|
|
35
|
+
descriptionPrefix: 3,
|
|
36
|
+
descriptionWord: 4,
|
|
37
|
+
namePrefix: 0,
|
|
38
|
+
nameSubstring: 1,
|
|
39
|
+
};
|
|
40
|
+
const rankOf = (command, query) => {
|
|
41
|
+
const name = command.word.slice(1);
|
|
42
|
+
if (name.startsWith(query)) {
|
|
43
|
+
return RANK.namePrefix;
|
|
44
|
+
}
|
|
45
|
+
if (name.includes(query)) {
|
|
46
|
+
return RANK.nameSubstring;
|
|
47
|
+
}
|
|
48
|
+
const words = command.description.toLowerCase().split(WORD_SPLIT);
|
|
49
|
+
if (words.some((word) => word.startsWith(query))) {
|
|
50
|
+
return words[0]?.startsWith(query) === true
|
|
51
|
+
? RANK.descriptionPrefix
|
|
52
|
+
: RANK.descriptionWord;
|
|
53
|
+
}
|
|
54
|
+
return undefined;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* The commands for the menu while `line` is a command name without arguments, best first: a name that starts with the
|
|
58
|
+
* query, then a name that contains it, then a description word that starts with it. Returns no commands otherwise.
|
|
59
|
+
*/
|
|
60
|
+
export const matchCommands = (line) => {
|
|
61
|
+
if (!COMMAND_NAME.test(line)) {
|
|
62
|
+
return [];
|
|
63
|
+
}
|
|
64
|
+
const query = line.slice(1);
|
|
65
|
+
return CHAT_COMMANDS.flatMap((command, index) => {
|
|
66
|
+
const rank = rankOf(command, query);
|
|
67
|
+
return rank === undefined ? [] : [{ command, index, rank }];
|
|
68
|
+
})
|
|
69
|
+
.toSorted((a, b) => a.rank - b.rank || a.index - b.index)
|
|
70
|
+
.map(({ command }) => command);
|
|
71
|
+
};
|
|
72
|
+
const editDistance = (a, b) => {
|
|
73
|
+
let previous = Array.from({ length: b.length + 1 }, (_, index) => index);
|
|
74
|
+
// Command words are ASCII, so code units are characters here.
|
|
75
|
+
for (let i = 0; i < a.length; i += 1) {
|
|
76
|
+
const current = [i + 1];
|
|
77
|
+
for (let j = 0; j < b.length; j += 1) {
|
|
78
|
+
current.push(Math.min((previous[j + 1] ?? 0) + 1, (current[j] ?? 0) + 1, (previous[j] ?? 0) + (a[i] === b[j] ? 0 : 1)));
|
|
79
|
+
}
|
|
80
|
+
previous = current;
|
|
81
|
+
}
|
|
82
|
+
return previous[b.length] ?? 0;
|
|
83
|
+
};
|
|
84
|
+
const MAX_SUGGESTION_DISTANCE = 2;
|
|
85
|
+
/** The known command closest to an unknown `word`, if one is at most two edits away. */
|
|
86
|
+
export const suggestCommand = (word) => {
|
|
87
|
+
const scored = CHAT_COMMANDS.map((command) => ({
|
|
88
|
+
distance: editDistance(word, command.word),
|
|
89
|
+
word: command.word,
|
|
90
|
+
})).toSorted((a, b) => a.distance - b.distance);
|
|
91
|
+
const [best] = scored;
|
|
92
|
+
return best !== undefined && best.distance <= MAX_SUGGESTION_DISTANCE
|
|
93
|
+
? best.word
|
|
94
|
+
: undefined;
|
|
95
|
+
};
|
|
96
|
+
const COMMAND_LINE = /^\/[a-z][a-z-]*(?:\s|$)/u;
|
|
97
|
+
/** True if `line` looks like a slash command, for example `/stop` or `/model x`, and not like a path such as `/etc/x`. */
|
|
98
|
+
export const isCommandLine = (line) => COMMAND_LINE.test(line);
|