reasonally 0.5.0 → 0.7.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 +62 -12
- package/dist/agent/bundled-commands.js +24 -0
- package/dist/agent/command-files.js +334 -0
- package/dist/agent/command-template.js +179 -0
- package/dist/agent/compact.js +58 -8
- package/dist/agent/context.js +82 -0
- package/dist/agent/loop.js +128 -31
- package/dist/agent/permissions.js +91 -0
- package/dist/agent/session.js +73 -5
- package/dist/api.js +12 -3
- package/dist/browser/guards.js +15 -0
- package/dist/browser/messages.js +61 -0
- package/dist/browser/sessions.js +2 -60
- package/dist/cli.js +5 -0
- package/dist/login.js +20 -4
- package/dist/sync/client.js +789 -0
- package/dist/sync/command.js +105 -0
- package/dist/sync/entries.js +139 -0
- package/dist/sync/live.js +107 -0
- package/dist/sync/protocol.js +14 -0
- package/dist/sync/redact.js +35 -0
- package/dist/sync/store.js +214 -0
- package/dist/sync/tail.js +66 -0
- package/dist/tui/app.js +227 -43
- package/dist/tui/commands.js +71 -15
- package/dist/tui/start.js +21 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -87,18 +87,49 @@ The agent looks around and writes a plan, and nothing changes until you approve
|
|
|
87
87
|
|
|
88
88
|
### Commands
|
|
89
89
|
|
|
90
|
-
| Command
|
|
91
|
-
|
|
|
92
|
-
| `/model`
|
|
93
|
-
| `/mode`
|
|
94
|
-
| `/
|
|
95
|
-
| `/
|
|
96
|
-
| `/
|
|
97
|
-
| `/
|
|
98
|
-
| `/
|
|
99
|
-
| `/
|
|
100
|
-
| `/
|
|
101
|
-
| `/
|
|
90
|
+
| Command | Does |
|
|
91
|
+
| ------------------ | ---------------------------------------------------------- |
|
|
92
|
+
| `/model [name]` | Switch model (only models that can use tools are shown) |
|
|
93
|
+
| `/mode [mode]` | Switch mode (`/mode plan` goes straight to Plan mode) |
|
|
94
|
+
| `/plan [task]` | Plan first, starting on the task if you give one |
|
|
95
|
+
| `/new` | Start a new conversation (also `/clear`) |
|
|
96
|
+
| `/resume` | Continue an earlier conversation in this folder |
|
|
97
|
+
| `/compact [focus]` | Summarize the conversation so far, keeping `focus` in mind |
|
|
98
|
+
| `/usage` | This session's cost, the context used and your balance |
|
|
99
|
+
| `/review [focus]` | Review your uncommitted changes, changing nothing |
|
|
100
|
+
| `/init` | Write an `AGENTS.md` for this project |
|
|
101
|
+
| `/mcp` | MCP servers and their tools |
|
|
102
|
+
| `/login` | Log in again |
|
|
103
|
+
| `/help` | Commands, your saved commands, and keys |
|
|
104
|
+
| `/exit` | Quit |
|
|
105
|
+
|
|
106
|
+
A message that starts with a path (`/usr/bin/env is missing`) is sent as a message. A command name
|
|
107
|
+
that doesn't exist is never sent: you're told, with the closest names.
|
|
108
|
+
|
|
109
|
+
### Saved commands
|
|
110
|
+
|
|
111
|
+
Save a prompt you use often as a Markdown file and run it as `/name` (here and in the desktop
|
|
112
|
+
app). What you type after the name fills `$ARGUMENTS`:
|
|
113
|
+
|
|
114
|
+
```markdown
|
|
115
|
+
---
|
|
116
|
+
description: Fix a GitHub issue
|
|
117
|
+
argument-hint: <issue>
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
Fix issue $ARGUMENTS: reproduce it, fix it, and add a test.
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Saved as `.reasonally/commands/fix-issue.md`, that's `/fix-issue 482`. Commands are found in
|
|
124
|
+
(first match wins): the project's `.reasonally/commands/` (from the folder you're in up to the
|
|
125
|
+
repository root), your own `~/.config/reasonally/commands/`, Claude Code's `.claude/commands/`
|
|
126
|
+
and `.claude/skills/` (the project's, then `~/.claude`), and Codex's `~/.codex/prompts/`.
|
|
127
|
+
|
|
128
|
+
- `$0`, `$1`… are single words; an `arguments` list in the frontmatter names them. With no
|
|
129
|
+
placeholder, what you typed is added at the end. Codex prompts keep Codex's rules.
|
|
130
|
+
- Subfolders become part of the name: `frontend/component.md` is `/frontend:component`.
|
|
131
|
+
- Files are only read as text: shell lines, tool permissions and model settings in them are
|
|
132
|
+
ignored, and the agent still asks as usual. `/help` lists files it skipped, and why.
|
|
102
133
|
|
|
103
134
|
### Conversations
|
|
104
135
|
|
|
@@ -112,6 +143,24 @@ reasonally --resume # choose one
|
|
|
112
143
|
When a conversation fills about 80% of the model's context window, earlier turns are summarized
|
|
113
144
|
automatically (or now, with `/compact`).
|
|
114
145
|
|
|
146
|
+
### On the web
|
|
147
|
+
|
|
148
|
+
Your conversations also show in the web chat's sidebar, under this computer, where you can follow
|
|
149
|
+
them live. Only summaries are sent: what you typed, the agent's replies, and one line for each step
|
|
150
|
+
(commands with secrets masked, addresses without their query). Command output, file contents,
|
|
151
|
+
tool arguments and the agent's reasoning never leave the computer. It's on unless you turn it off,
|
|
152
|
+
and the desktop app on the same computer shares the setting.
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
reasonally sync # whether they show on the web, and the last upload
|
|
156
|
+
reasonally sync off # stop, and take them off the web (they stay here)
|
|
157
|
+
reasonally sync on # show them again
|
|
158
|
+
reasonally sync off --here # only for this folder
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Uploads happen in the background and never slow a turn down; offline, they catch up later.
|
|
162
|
+
Replies and approvals from the web go to the desktop app, never to a terminal.
|
|
163
|
+
|
|
115
164
|
### Project instructions
|
|
116
165
|
|
|
117
166
|
The agent follows `AGENTS.md` and `CLAUDE.md` files from your repository's root down to the folder
|
|
@@ -179,6 +228,7 @@ Generated files live in `~/.config/reasonally/harness/`.
|
|
|
179
228
|
```bash
|
|
180
229
|
reasonally models [search] # models you can use, with context and price
|
|
181
230
|
reasonally whoami # account and balance
|
|
231
|
+
reasonally sync [on|off] # whether your conversations show on the web (--here: this folder)
|
|
182
232
|
reasonally logout # log out and revoke this computer's key
|
|
183
233
|
reasonally login --no-browser # print the approval link instead of opening it
|
|
184
234
|
```
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prompt commands that come with Reasonally, in both apps. A saved command with the same name
|
|
3
|
+
* (a team's own `/review`) takes its place.
|
|
4
|
+
*/
|
|
5
|
+
const REVIEW = "Review my uncommitted changes, including new files, and point out anything risky or " +
|
|
6
|
+
"unfinished. Don't change anything.";
|
|
7
|
+
const INIT = "Look around this folder and write an AGENTS.md at its root for the coding agents who'll work " +
|
|
8
|
+
"here: how to set up, build, test and lint (the exact commands), how the code is laid out, and " +
|
|
9
|
+
"the conventions to follow. Keep it short and specific to this project, and leave out generic " +
|
|
10
|
+
"advice. If AGENTS.md or CLAUDE.md already exists, improve it instead of starting over.";
|
|
11
|
+
export const BUNDLED_COMMANDS = [
|
|
12
|
+
{
|
|
13
|
+
name: "review",
|
|
14
|
+
description: "Review your changes for anything risky",
|
|
15
|
+
argumentHint: "[focus]",
|
|
16
|
+
prompt: (args) => (args ? `${REVIEW}\n\nFocus on: ${args}` : REVIEW),
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
name: "init",
|
|
20
|
+
description: "Write an AGENTS.md for this folder",
|
|
21
|
+
argumentHint: null,
|
|
22
|
+
prompt: (args) => (args ? `${INIT}\n\nAlso: ${args}` : INIT),
|
|
23
|
+
},
|
|
24
|
+
];
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Saved commands: prompts kept as Markdown files, run by typing `/name`. Found in (first wins):
|
|
3
|
+
*
|
|
4
|
+
* - the project's `.reasonally/commands/`, in each folder from `cwd` up to the repository root;
|
|
5
|
+
* - the user's own `<config>/commands/`;
|
|
6
|
+
* - Claude Code's `.claude/commands/` and `.claude/skills/<name>/SKILL.md` (project, then
|
|
7
|
+
* `~/.claude`), and Codex's `~/.codex/prompts/`, so files made for those tools work here too;
|
|
8
|
+
* - the prompts that come with Reasonally (`/review`, `/init`).
|
|
9
|
+
*
|
|
10
|
+
* Files are only read as text. Nothing in them runs, and nothing they say changes what the agent
|
|
11
|
+
* may do: shell lines, tool pre-approvals and model choices in them are ignored.
|
|
12
|
+
*/
|
|
13
|
+
import { open, readdir, readFile, realpath, stat } from "node:fs/promises";
|
|
14
|
+
import { dirname, join, relative, sep } from "node:path";
|
|
15
|
+
import { BUNDLED_COMMANDS } from "./bundled-commands.js";
|
|
16
|
+
import { expandClaude, expandCodex, firstLine, hasClaudePlaceholder, hasCodexPlaceholder, hintIsOptional, parseFrontmatter, } from "./command-template.js";
|
|
17
|
+
import { isInside } from "./permissions.js";
|
|
18
|
+
import { findRepoRoot } from "./prompt.js";
|
|
19
|
+
export { commandLabel, parseSlash } from "./command-template.js";
|
|
20
|
+
/** A command's name: lowercase words, and up to two `folder:` parts in front. */
|
|
21
|
+
export const COMMAND_NAME = /^[a-z0-9][a-z0-9_-]{0,47}(?::[a-z0-9][a-z0-9_-]{0,47}){0,2}$/;
|
|
22
|
+
/**
|
|
23
|
+
* The apps' own commands and their aliases, in the terminal and the desktop app. A saved command
|
|
24
|
+
* can't take one of these names (it's skipped, and the reason is reported).
|
|
25
|
+
*/
|
|
26
|
+
export const RESERVED_COMMAND_NAMES = new Set([
|
|
27
|
+
"archive",
|
|
28
|
+
"browser",
|
|
29
|
+
"clear",
|
|
30
|
+
"compact",
|
|
31
|
+
"context",
|
|
32
|
+
"copy",
|
|
33
|
+
"cost",
|
|
34
|
+
"delete",
|
|
35
|
+
"diff",
|
|
36
|
+
"duplicate",
|
|
37
|
+
"exit",
|
|
38
|
+
"fork",
|
|
39
|
+
"help",
|
|
40
|
+
"login",
|
|
41
|
+
"mcp",
|
|
42
|
+
"mode",
|
|
43
|
+
"model",
|
|
44
|
+
"new",
|
|
45
|
+
"plan",
|
|
46
|
+
"quit",
|
|
47
|
+
"rename",
|
|
48
|
+
"resume",
|
|
49
|
+
"settings",
|
|
50
|
+
"usage",
|
|
51
|
+
]);
|
|
52
|
+
export const MAX_COMMAND_FILE_BYTES = 64 * 1024;
|
|
53
|
+
export const MAX_COMMANDS = 200;
|
|
54
|
+
/** What can be typed after a command's name. */
|
|
55
|
+
export const MAX_COMMAND_ARGS = 10_000;
|
|
56
|
+
/** How much of each file a listing reads (the frontmatter and enough text to see placeholders). */
|
|
57
|
+
const HEAD_BYTES = 8 * 1024;
|
|
58
|
+
/** Folders below a commands folder that count (`a/b/c.md` is `/a:b:c`). */
|
|
59
|
+
const MAX_DEPTH = 2;
|
|
60
|
+
async function placesFor(roots) {
|
|
61
|
+
const root = (await findRepoRoot(roots.cwd, roots.home)) ?? roots.cwd;
|
|
62
|
+
// Nearest first: a folder's own commands beat the repository root's.
|
|
63
|
+
const dirs = [];
|
|
64
|
+
for (let dir = roots.cwd;; dir = dirname(dir)) {
|
|
65
|
+
dirs.push(dir);
|
|
66
|
+
if (dir === root || !isInside(dir, root) || dirname(dir) === dir)
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
const env = roots.env ?? process.env;
|
|
70
|
+
const claudeHome = env.CLAUDE_CONFIG_DIR || join(roots.home, ".claude");
|
|
71
|
+
const codexHome = env.CODEX_HOME || join(roots.home, ".codex");
|
|
72
|
+
const project = (dir, layout, source) => ({
|
|
73
|
+
dir,
|
|
74
|
+
source,
|
|
75
|
+
format: "claude",
|
|
76
|
+
layout,
|
|
77
|
+
within: root,
|
|
78
|
+
});
|
|
79
|
+
const places = [
|
|
80
|
+
...dirs.map((d) => project(join(d, ".reasonally", "commands"), "tree", "project")),
|
|
81
|
+
{
|
|
82
|
+
dir: join(roots.configDir, "commands"),
|
|
83
|
+
source: "personal",
|
|
84
|
+
format: "claude",
|
|
85
|
+
layout: "tree",
|
|
86
|
+
within: null,
|
|
87
|
+
},
|
|
88
|
+
...dirs.flatMap((d) => [
|
|
89
|
+
project(join(d, ".claude", "commands"), "tree", "claude"),
|
|
90
|
+
project(join(d, ".claude", "skills"), "skills", "claude"),
|
|
91
|
+
]),
|
|
92
|
+
{
|
|
93
|
+
dir: join(claudeHome, "commands"),
|
|
94
|
+
source: "claude",
|
|
95
|
+
format: "claude",
|
|
96
|
+
layout: "tree",
|
|
97
|
+
within: null,
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
dir: join(claudeHome, "skills"),
|
|
101
|
+
source: "claude",
|
|
102
|
+
format: "claude",
|
|
103
|
+
layout: "skills",
|
|
104
|
+
within: null,
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
dir: join(codexHome, "prompts"),
|
|
108
|
+
source: "codex",
|
|
109
|
+
format: "codex",
|
|
110
|
+
layout: "flat",
|
|
111
|
+
within: null,
|
|
112
|
+
},
|
|
113
|
+
];
|
|
114
|
+
// The home folder's `.claude` is both a project folder and the personal one: list it once.
|
|
115
|
+
const seen = new Set();
|
|
116
|
+
return places.filter((p) => !seen.has(p.dir) && seen.add(p.dir));
|
|
117
|
+
}
|
|
118
|
+
const isMarkdown = (name) => name.toLowerCase().endsWith(".md");
|
|
119
|
+
/** Whether an entry is a file or a folder, following a link. */
|
|
120
|
+
async function kindOf(path, isLink, isDir, isFile) {
|
|
121
|
+
if (!isLink)
|
|
122
|
+
return isDir ? "dir" : isFile ? "file" : null;
|
|
123
|
+
const info = await stat(path).catch(() => null);
|
|
124
|
+
return info?.isDirectory() ? "dir" : info?.isFile() ? "file" : null;
|
|
125
|
+
}
|
|
126
|
+
async function entries(dir) {
|
|
127
|
+
const list = await readdir(dir, { withFileTypes: true }).catch(() => []);
|
|
128
|
+
return list.filter((e) => !e.name.startsWith(".")).sort((a, b) => a.name.localeCompare(b.name));
|
|
129
|
+
}
|
|
130
|
+
async function filesIn(place) {
|
|
131
|
+
const out = [];
|
|
132
|
+
if (place.layout === "skills") {
|
|
133
|
+
for (const e of await entries(place.dir)) {
|
|
134
|
+
const folder = join(place.dir, e.name);
|
|
135
|
+
if ((await kindOf(folder, e.isSymbolicLink(), e.isDirectory(), e.isFile())) !== "dir") {
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
const file = join(folder, "SKILL.md");
|
|
139
|
+
if ((await stat(file).catch(() => null))?.isFile()) {
|
|
140
|
+
out.push({ path: file, name: e.name, place });
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return out;
|
|
144
|
+
}
|
|
145
|
+
const walk = async (dir, depth) => {
|
|
146
|
+
for (const e of await entries(dir)) {
|
|
147
|
+
const path = join(dir, e.name);
|
|
148
|
+
const kind = await kindOf(path, e.isSymbolicLink(), e.isDirectory(), e.isFile());
|
|
149
|
+
if (kind === "dir" && place.layout === "tree" && depth < MAX_DEPTH) {
|
|
150
|
+
await walk(path, depth + 1);
|
|
151
|
+
}
|
|
152
|
+
else if (kind === "file" && isMarkdown(e.name)) {
|
|
153
|
+
const rel = relative(place.dir, path).slice(0, -3);
|
|
154
|
+
out.push({ path, name: rel.split(sep).join(":"), place });
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
await walk(place.dir, 0);
|
|
159
|
+
return out;
|
|
160
|
+
}
|
|
161
|
+
async function readHead(path) {
|
|
162
|
+
const handle = await open(path, "r");
|
|
163
|
+
try {
|
|
164
|
+
const buffer = Buffer.alloc(HEAD_BYTES);
|
|
165
|
+
const { bytesRead } = await handle.read(buffer, 0, HEAD_BYTES, 0);
|
|
166
|
+
return buffer.subarray(0, bytesRead).toString("utf8");
|
|
167
|
+
}
|
|
168
|
+
finally {
|
|
169
|
+
await handle.close();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** Why a name can't be used, or null when it can. */
|
|
173
|
+
function nameProblem(name) {
|
|
174
|
+
if (/\s/.test(name))
|
|
175
|
+
return "names can't have spaces";
|
|
176
|
+
if (!COMMAND_NAME.test(name))
|
|
177
|
+
return "names can only use letters, numbers, - and _";
|
|
178
|
+
if (RESERVED_COMMAND_NAMES.has(name))
|
|
179
|
+
return `/${name} is one of Reasonally's own commands`;
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
const insideAny = (path, dirs) => dirs.some((d) => path === d || isInside(path, d));
|
|
183
|
+
/** Every saved command for `cwd`, best first by name, and the files that were left out. */
|
|
184
|
+
export async function listCommands(roots) {
|
|
185
|
+
const commands = [];
|
|
186
|
+
const problems = [];
|
|
187
|
+
const taken = new Set();
|
|
188
|
+
// Both as given and resolved, so a link can't lead around them.
|
|
189
|
+
const forbidden = [
|
|
190
|
+
...(roots.forbidden ?? []),
|
|
191
|
+
...(await Promise.all((roots.forbidden ?? []).map((f) => realpath(f).catch(() => f)))),
|
|
192
|
+
];
|
|
193
|
+
const realRoots = new Map();
|
|
194
|
+
let full = false;
|
|
195
|
+
for (const place of await placesFor(roots)) {
|
|
196
|
+
for (const found of await filesIn(place)) {
|
|
197
|
+
if (full)
|
|
198
|
+
break;
|
|
199
|
+
const fail = (reason) => problems.push({ path: found.path, reason });
|
|
200
|
+
let real;
|
|
201
|
+
try {
|
|
202
|
+
real = await realpath(found.path);
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (insideAny(found.path, forbidden) || insideAny(real, forbidden))
|
|
208
|
+
continue;
|
|
209
|
+
if (place.within) {
|
|
210
|
+
let root = realRoots.get(place.within);
|
|
211
|
+
if (!root) {
|
|
212
|
+
root = await realpath(place.within).catch(() => place.within);
|
|
213
|
+
realRoots.set(place.within, root);
|
|
214
|
+
}
|
|
215
|
+
if (!isInside(real, root)) {
|
|
216
|
+
fail("it links outside this project");
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
let head;
|
|
221
|
+
let size;
|
|
222
|
+
try {
|
|
223
|
+
size = (await stat(real)).size;
|
|
224
|
+
head = size > MAX_COMMAND_FILE_BYTES ? "" : await readHead(real);
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
fail("it couldn't be read");
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
const { meta, body } = parseFrontmatter(head);
|
|
231
|
+
if (place.layout === "skills" && meta.userInvocable === false)
|
|
232
|
+
continue;
|
|
233
|
+
const name = (place.layout === "skills" && meta.name ? meta.name : found.name).toLowerCase();
|
|
234
|
+
const problem = nameProblem(name);
|
|
235
|
+
if (problem) {
|
|
236
|
+
fail(problem);
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
if (taken.has(name))
|
|
240
|
+
continue;
|
|
241
|
+
if (size > MAX_COMMAND_FILE_BYTES) {
|
|
242
|
+
fail("it's over 64 KB");
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
if (commands.length >= MAX_COMMANDS) {
|
|
246
|
+
fail(`only the first ${MAX_COMMANDS} commands are used`);
|
|
247
|
+
full = true;
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
const hint = meta.argumentHint?.trim() || null;
|
|
251
|
+
const placeholders = place.format === "codex"
|
|
252
|
+
? hasCodexPlaceholder(body)
|
|
253
|
+
: hasClaudePlaceholder(body, meta.arguments);
|
|
254
|
+
taken.add(name);
|
|
255
|
+
commands.push({
|
|
256
|
+
name,
|
|
257
|
+
description: meta.description?.trim() || firstLine(body),
|
|
258
|
+
argumentHint: hint,
|
|
259
|
+
source: place.source,
|
|
260
|
+
format: place.format,
|
|
261
|
+
requiresArguments: placeholders && !(hint && hintIsOptional(hint)),
|
|
262
|
+
path: found.path,
|
|
263
|
+
skill: place.layout === "skills",
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
for (const b of BUNDLED_COMMANDS) {
|
|
268
|
+
if (taken.has(b.name))
|
|
269
|
+
continue;
|
|
270
|
+
commands.push({
|
|
271
|
+
name: b.name,
|
|
272
|
+
description: b.description,
|
|
273
|
+
argumentHint: b.argumentHint,
|
|
274
|
+
source: "bundled",
|
|
275
|
+
format: "claude",
|
|
276
|
+
requiresArguments: false,
|
|
277
|
+
path: null,
|
|
278
|
+
skill: false,
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
commands.sort((a, b) => a.name.localeCompare(b.name));
|
|
282
|
+
return { commands, problems };
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* The prompt for running a saved command: its file is found and read again now, so an edit
|
|
286
|
+
* applies on the very next run.
|
|
287
|
+
*/
|
|
288
|
+
export async function expandCommand(ref, roots) {
|
|
289
|
+
const args = ref.args.trim();
|
|
290
|
+
if (args.length > MAX_COMMAND_ARGS) {
|
|
291
|
+
return { ok: false, message: `That's too much text for /${ref.name}.` };
|
|
292
|
+
}
|
|
293
|
+
const { commands } = await listCommands(roots);
|
|
294
|
+
const command = commands.find((c) => c.name === ref.name);
|
|
295
|
+
const gone = `/${ref.name} isn't available anymore. Its file was moved or deleted.`;
|
|
296
|
+
if (!command)
|
|
297
|
+
return { ok: false, message: gone };
|
|
298
|
+
if (command.requiresArguments && !args) {
|
|
299
|
+
return { ok: false, message: `/${ref.name} needs ${command.argumentHint ?? "text after it"}.` };
|
|
300
|
+
}
|
|
301
|
+
if (!command.path) {
|
|
302
|
+
const bundled = BUNDLED_COMMANDS.find((b) => b.name === command.name);
|
|
303
|
+
return { ok: true, prompt: bundled.prompt(args), command };
|
|
304
|
+
}
|
|
305
|
+
let raw;
|
|
306
|
+
try {
|
|
307
|
+
if ((await stat(command.path)).size > MAX_COMMAND_FILE_BYTES) {
|
|
308
|
+
return { ok: false, message: `/${ref.name} is over 64 KB.` };
|
|
309
|
+
}
|
|
310
|
+
raw = await readFile(command.path, "utf8");
|
|
311
|
+
}
|
|
312
|
+
catch {
|
|
313
|
+
return { ok: false, message: gone };
|
|
314
|
+
}
|
|
315
|
+
const { meta, body } = parseFrontmatter(raw);
|
|
316
|
+
let prompt;
|
|
317
|
+
if (command.format === "codex") {
|
|
318
|
+
const result = expandCodex(body, args);
|
|
319
|
+
if (!result.ok) {
|
|
320
|
+
const needs = result.missing.map((k) => `${k}=…`).join(" ");
|
|
321
|
+
return { ok: false, message: `/${ref.name} needs ${needs}.` };
|
|
322
|
+
}
|
|
323
|
+
prompt = result.text;
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
prompt = expandClaude(body, args, meta.arguments);
|
|
327
|
+
}
|
|
328
|
+
if (command.skill) {
|
|
329
|
+
prompt += `\n\nThe files for this command are in ${dirname(command.path)}.`;
|
|
330
|
+
}
|
|
331
|
+
if (!prompt.trim())
|
|
332
|
+
return { ok: false, message: `/${ref.name} is empty.` };
|
|
333
|
+
return { ok: true, prompt: prompt.trim(), command };
|
|
334
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/** `/review the auth flow` as it's shown: in the transcript, a title, the terminal. */
|
|
2
|
+
export const commandLabel = (ref) => `/${ref.name}${ref.args ? ` ${ref.args}` : ""}`;
|
|
3
|
+
/**
|
|
4
|
+
* A message that starts with `/name`: the name (lowercase) and what follows it. Null for anything
|
|
5
|
+
* else, including a path (`/Users/me/app is slow`), whose first word has another `/` in it.
|
|
6
|
+
*/
|
|
7
|
+
export function parseSlash(text) {
|
|
8
|
+
const match = /^\/(\S*)(?:\s+([\s\S]*))?$/.exec(text.trim());
|
|
9
|
+
if (!match || match[1].includes("/"))
|
|
10
|
+
return null;
|
|
11
|
+
return { name: match[1].toLowerCase(), args: (match[2] ?? "").trim() };
|
|
12
|
+
}
|
|
13
|
+
const unquote = (value) => {
|
|
14
|
+
const v = value.trim();
|
|
15
|
+
if (v.length >= 2 && ((v[0] === '"' && v.endsWith('"')) || (v[0] === "'" && v.endsWith("'")))) {
|
|
16
|
+
return v.slice(1, -1);
|
|
17
|
+
}
|
|
18
|
+
return v;
|
|
19
|
+
};
|
|
20
|
+
const listValue = (value) => {
|
|
21
|
+
const v = value.trim();
|
|
22
|
+
const inner = v.startsWith("[") && v.endsWith("]") ? v.slice(1, -1) : v;
|
|
23
|
+
return inner
|
|
24
|
+
.split(inner.includes(",") ? "," : /\s+/)
|
|
25
|
+
.map(unquote)
|
|
26
|
+
.filter(Boolean);
|
|
27
|
+
};
|
|
28
|
+
const TRUE = new Set(["true", "yes", "on", "1"]);
|
|
29
|
+
const FALSE = new Set(["false", "no", "off", "0"]);
|
|
30
|
+
/**
|
|
31
|
+
* A file's frontmatter (between `---` lines at the very top) and the text after it. Only simple
|
|
32
|
+
* `key: value` lines and `- item` lists are understood; that's all a command file needs.
|
|
33
|
+
*/
|
|
34
|
+
export function parseFrontmatter(raw) {
|
|
35
|
+
const text = raw.replace(/^/, "").replace(/\r\n?/g, "\n");
|
|
36
|
+
if (!text.startsWith("---\n"))
|
|
37
|
+
return { meta: {}, body: text.trim() };
|
|
38
|
+
const end = text.indexOf("\n---", 3);
|
|
39
|
+
const close = end === -1 ? -1 : text.indexOf("\n", end + 1);
|
|
40
|
+
if (end === -1 || (close !== -1 && text.slice(end + 4, close).trim())) {
|
|
41
|
+
return { meta: {}, body: text.trim() };
|
|
42
|
+
}
|
|
43
|
+
const lines = text.slice(4, end).split("\n");
|
|
44
|
+
const body = close === -1 ? "" : text.slice(close + 1).trim();
|
|
45
|
+
const meta = {};
|
|
46
|
+
for (let i = 0; i < lines.length; i++) {
|
|
47
|
+
const match = /^([A-Za-z][\w-]*)\s*:\s*(.*)$/.exec(lines[i]);
|
|
48
|
+
if (!match)
|
|
49
|
+
continue;
|
|
50
|
+
const key = match[1].toLowerCase();
|
|
51
|
+
let value = match[2];
|
|
52
|
+
// A list on the lines below: `arguments:` then ` - name`.
|
|
53
|
+
const items = [];
|
|
54
|
+
while (!value.trim() && i + 1 < lines.length && /^\s+-\s+/.test(lines[i + 1])) {
|
|
55
|
+
items.push(unquote(lines[++i].replace(/^\s+-\s+/, "")));
|
|
56
|
+
}
|
|
57
|
+
if (items.length)
|
|
58
|
+
value = `[${items.join(",")}]`;
|
|
59
|
+
switch (key) {
|
|
60
|
+
case "name":
|
|
61
|
+
meta.name = unquote(value);
|
|
62
|
+
break;
|
|
63
|
+
case "description":
|
|
64
|
+
meta.description = unquote(value);
|
|
65
|
+
break;
|
|
66
|
+
case "argument-hint":
|
|
67
|
+
meta.argumentHint = unquote(value);
|
|
68
|
+
break;
|
|
69
|
+
case "arguments":
|
|
70
|
+
meta.arguments = listValue(value);
|
|
71
|
+
break;
|
|
72
|
+
case "user-invocable": {
|
|
73
|
+
const flag = unquote(value).toLowerCase();
|
|
74
|
+
if (TRUE.has(flag))
|
|
75
|
+
meta.userInvocable = true;
|
|
76
|
+
else if (FALSE.has(flag))
|
|
77
|
+
meta.userInvocable = false;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return { meta, body };
|
|
83
|
+
}
|
|
84
|
+
/** What was typed after the name, split into words; quotes keep words together. */
|
|
85
|
+
export function splitArgs(args) {
|
|
86
|
+
const out = [];
|
|
87
|
+
const re = /"([^"]*)"|'([^']*)'|(\S+)/g;
|
|
88
|
+
for (let m = re.exec(args); m; m = re.exec(args))
|
|
89
|
+
out.push(m[1] ?? m[2] ?? m[3]);
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
92
|
+
/** A description when the file has none: its first line of text, without heading marks. */
|
|
93
|
+
export function firstLine(body) {
|
|
94
|
+
const line = body.split("\n").find((l) => l.trim()) ?? "";
|
|
95
|
+
const text = line.replace(/^#+\s*/, "").trim();
|
|
96
|
+
return text.length > 100 ? `${text.slice(0, 99)}…` : text;
|
|
97
|
+
}
|
|
98
|
+
const CLAUDE_PLACEHOLDER = /\\\$|\$ARGUMENTS\[(\d+)\]|\$ARGUMENTS(?![\w[])|\$(\d+)|\$([A-Za-z_][A-Za-z0-9_]*)/g;
|
|
99
|
+
/**
|
|
100
|
+
* Claude Code's rules (used for Reasonally's own files too): `$ARGUMENTS` is everything typed,
|
|
101
|
+
* `$ARGUMENTS[N]` and `$N` one word (from 0), `$name` a named argument by position. An unmatched
|
|
102
|
+
* `$N` stays as written, an unmatched name becomes empty, `\$` is a dollar sign. When nothing in
|
|
103
|
+
* the text takes the arguments, they're added at the end as `ARGUMENTS: …`.
|
|
104
|
+
*/
|
|
105
|
+
export function expandClaude(body, args, named = []) {
|
|
106
|
+
const words = splitArgs(args);
|
|
107
|
+
let used = false;
|
|
108
|
+
const out = body.replace(CLAUDE_PLACEHOLDER, (all, index, n, name) => {
|
|
109
|
+
if (all === "\\$")
|
|
110
|
+
return "$";
|
|
111
|
+
if (index !== undefined) {
|
|
112
|
+
used = true;
|
|
113
|
+
return words[Number(index)] ?? "";
|
|
114
|
+
}
|
|
115
|
+
if (all === "$ARGUMENTS") {
|
|
116
|
+
used = true;
|
|
117
|
+
return args;
|
|
118
|
+
}
|
|
119
|
+
if (n !== undefined) {
|
|
120
|
+
used = true;
|
|
121
|
+
return words[Number(n)] ?? all;
|
|
122
|
+
}
|
|
123
|
+
const at = name === undefined ? -1 : named.indexOf(name);
|
|
124
|
+
if (at === -1)
|
|
125
|
+
return all;
|
|
126
|
+
used = true;
|
|
127
|
+
return words[at] ?? "";
|
|
128
|
+
});
|
|
129
|
+
return !used && args ? `${out}\n\nARGUMENTS: ${args}` : out;
|
|
130
|
+
}
|
|
131
|
+
/** Whether a Claude-style text takes arguments anywhere. */
|
|
132
|
+
export function hasClaudePlaceholder(body, named = []) {
|
|
133
|
+
for (const m of body.matchAll(CLAUDE_PLACEHOLDER)) {
|
|
134
|
+
if (m[0] === "\\$")
|
|
135
|
+
continue;
|
|
136
|
+
if (m[3] !== undefined && !named.includes(m[3]))
|
|
137
|
+
continue;
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
const CODEX_PLACEHOLDER = /\$\$|\$([1-9])|\$ARGUMENTS(?!\w)|\$([A-Z][A-Z0-9_]*)/g;
|
|
143
|
+
/**
|
|
144
|
+
* Codex's rules for its prompts: `$1`…`$9` one word each, `$ARGUMENTS` everything, `$KEY` from a
|
|
145
|
+
* `KEY=value` word, `$$` a dollar sign. A `$KEY` with no value means the prompt can't run yet.
|
|
146
|
+
*/
|
|
147
|
+
export function expandCodex(body, args) {
|
|
148
|
+
const named = new Map();
|
|
149
|
+
const positional = [];
|
|
150
|
+
for (const word of splitArgs(args)) {
|
|
151
|
+
const kv = /^([A-Z][A-Z0-9_]*)=([\s\S]*)$/.exec(word);
|
|
152
|
+
if (kv)
|
|
153
|
+
named.set(kv[1], kv[2]);
|
|
154
|
+
else
|
|
155
|
+
positional.push(word);
|
|
156
|
+
}
|
|
157
|
+
const missing = new Set();
|
|
158
|
+
const text = body.replace(CODEX_PLACEHOLDER, (all, n, key) => {
|
|
159
|
+
if (all === "$$")
|
|
160
|
+
return "$";
|
|
161
|
+
if (n !== undefined)
|
|
162
|
+
return positional[Number(n) - 1] ?? "";
|
|
163
|
+
if (all === "$ARGUMENTS")
|
|
164
|
+
return positional.join(" ");
|
|
165
|
+
if (key === undefined)
|
|
166
|
+
return all;
|
|
167
|
+
const value = named.get(key);
|
|
168
|
+
if (value === undefined)
|
|
169
|
+
missing.add(key);
|
|
170
|
+
return value ?? all;
|
|
171
|
+
});
|
|
172
|
+
return missing.size ? { ok: false, missing: [...missing] } : { ok: true, text };
|
|
173
|
+
}
|
|
174
|
+
/** Whether a Codex prompt takes arguments anywhere. */
|
|
175
|
+
export function hasCodexPlaceholder(body) {
|
|
176
|
+
return [...body.matchAll(CODEX_PLACEHOLDER)].some((m) => m[0] !== "$$");
|
|
177
|
+
}
|
|
178
|
+
/** Whether a hint only lists optional parts (`[focus]`, `[a] [b]`), so it can run without any. */
|
|
179
|
+
export const hintIsOptional = (hint) => /^(\s*\[[^\]]*\])+\s*$/.test(hint);
|