reasonally 0.4.0 → 0.6.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 +56 -12
- package/dist/agent/bundled-commands.js +24 -0
- package/dist/agent/client.js +53 -3
- 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 +163 -32
- package/dist/agent/permissions.js +151 -1
- package/dist/agent/session.js +75 -6
- package/dist/agent/tools/index.js +9 -0
- package/dist/agent/tools/task.js +1 -1
- package/dist/agent/tools/web/ddg.js +131 -0
- package/dist/agent/tools/web/dom.js +9 -0
- package/dist/agent/tools/web/html.js +90 -0
- package/dist/agent/tools/web/page.js +218 -0
- package/dist/agent/tools/web-fetch.js +175 -0
- package/dist/agent/tools/web-search.js +158 -0
- 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 +1 -1
- package/dist/tui/app.js +172 -41
- package/dist/tui/args.js +4 -0
- package/dist/tui/commands.js +71 -15
- package/dist/tui/start.js +2 -0
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -48,6 +48,19 @@ the conversation stays on screen after you quit.
|
|
|
48
48
|
what a command does before approving it, and use "Don't ask" only in projects you trust.
|
|
49
49
|
- Commands run without your Reasonally key in their environment, and pagers are turned off.
|
|
50
50
|
|
|
51
|
+
### The web
|
|
52
|
+
|
|
53
|
+
The agent can search the web and read pages, for current docs, releases and error messages. Both
|
|
54
|
+
are free.
|
|
55
|
+
|
|
56
|
+
- **Searching** runs without asking. Searches start from your computer; if that doesn't work,
|
|
57
|
+
Reasonally searches for you.
|
|
58
|
+
- **Reading a page** asks once per site (`y` yes, `a` yes and don't ask again for that site, `n` no).
|
|
59
|
+
Links you paste open without asking, and "Don't ask" (`--yes`) never asks.
|
|
60
|
+
- Pages are read from your computer. Addresses on your computer or local network are never opened;
|
|
61
|
+
the agent can use `curl` for those, which asks like any command.
|
|
62
|
+
- `reasonally --no-web` starts without either.
|
|
63
|
+
|
|
51
64
|
### Plan mode
|
|
52
65
|
|
|
53
66
|
The agent looks around and writes a plan, and nothing changes until you approve it:
|
|
@@ -74,18 +87,49 @@ The agent looks around and writes a plan, and nothing changes until you approve
|
|
|
74
87
|
|
|
75
88
|
### Commands
|
|
76
89
|
|
|
77
|
-
| Command
|
|
78
|
-
|
|
|
79
|
-
| `/model`
|
|
80
|
-
| `/mode`
|
|
81
|
-
| `/
|
|
82
|
-
| `/
|
|
83
|
-
| `/
|
|
84
|
-
| `/
|
|
85
|
-
| `/
|
|
86
|
-
| `/
|
|
87
|
-
| `/
|
|
88
|
-
| `/
|
|
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.
|
|
89
133
|
|
|
90
134
|
### Conversations
|
|
91
135
|
|
|
@@ -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
|
+
];
|
package/dist/agent/client.js
CHANGED
|
@@ -16,6 +16,10 @@ export class StreamError extends Error {
|
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
let userAgent;
|
|
19
|
+
/** The user agent for our own requests: the host app's name and version, or the CLI's. */
|
|
20
|
+
export const userAgentFor = (client) => client ? `${client.name}/${client.version}` : cliUserAgent();
|
|
21
|
+
/** For requests to websites: browser-compatible, and names the app honestly. */
|
|
22
|
+
export const webUserAgent = (client) => `Mozilla/5.0 (compatible; ${userAgentFor(client)})`;
|
|
19
23
|
function cliUserAgent() {
|
|
20
24
|
try {
|
|
21
25
|
userAgent ??= `reasonally-cli/${createRequire(import.meta.url)("../../package.json").version}`;
|
|
@@ -76,9 +80,7 @@ export async function* streamChat(options) {
|
|
|
76
80
|
authorization: `Bearer ${options.apiKey}`,
|
|
77
81
|
"content-type": "application/json",
|
|
78
82
|
accept: "text/event-stream",
|
|
79
|
-
"user-agent": options.client
|
|
80
|
-
? `${options.client.name}/${options.client.version}`
|
|
81
|
-
: cliUserAgent(),
|
|
83
|
+
"user-agent": userAgentFor(options.client),
|
|
82
84
|
[CANCEL_ON_DISCONNECT_HEADER]: "1",
|
|
83
85
|
},
|
|
84
86
|
body: JSON.stringify(options.body),
|
|
@@ -149,6 +151,54 @@ export async function* streamChat(options) {
|
|
|
149
151
|
options.signal.removeEventListener("abort", abort);
|
|
150
152
|
}
|
|
151
153
|
}
|
|
154
|
+
/** How long the fallback search may take (the server gives its provider 6 s). */
|
|
155
|
+
const SEARCH_TIMEOUT_MS = 12_000;
|
|
156
|
+
/**
|
|
157
|
+
* Runs `POST /web/search`. Throws `ApiError` for HTTP errors (status 0 when the server can't be
|
|
158
|
+
* reached or took too long), and the abort reason when `signal` fires.
|
|
159
|
+
*/
|
|
160
|
+
export async function searchWeb(options) {
|
|
161
|
+
const doFetch = options.fetch ?? fetch;
|
|
162
|
+
const timeout = AbortSignal.timeout(SEARCH_TIMEOUT_MS);
|
|
163
|
+
let res;
|
|
164
|
+
try {
|
|
165
|
+
res = await doFetch(`${options.apiBaseUrl}/web/search`, {
|
|
166
|
+
method: "POST",
|
|
167
|
+
headers: {
|
|
168
|
+
authorization: `Bearer ${options.apiKey}`,
|
|
169
|
+
"content-type": "application/json",
|
|
170
|
+
"user-agent": userAgentFor(options.client),
|
|
171
|
+
},
|
|
172
|
+
body: JSON.stringify(options.body),
|
|
173
|
+
signal: AbortSignal.any([options.signal, timeout]),
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
if (options.signal.aborted)
|
|
178
|
+
throw options.signal.reason;
|
|
179
|
+
const why = timeout.aborted ? "no response" : error.message;
|
|
180
|
+
throw new ApiError(`Couldn't reach Reasonally's search (${why}).`, 0, null);
|
|
181
|
+
}
|
|
182
|
+
if (!res.ok)
|
|
183
|
+
throw await errorFrom(res);
|
|
184
|
+
const body = (await res.json().catch(() => null));
|
|
185
|
+
if (!body || !Array.isArray(body.results)) {
|
|
186
|
+
throw new ApiError("Reasonally's search sent an unexpected response.", res.status, null);
|
|
187
|
+
}
|
|
188
|
+
return body.results.flatMap((r) => {
|
|
189
|
+
const item = r;
|
|
190
|
+
if (typeof item.url !== "string" || typeof item.title !== "string")
|
|
191
|
+
return [];
|
|
192
|
+
return [
|
|
193
|
+
{
|
|
194
|
+
title: item.title,
|
|
195
|
+
url: item.url,
|
|
196
|
+
snippet: typeof item.snippet === "string" ? item.snippet : "",
|
|
197
|
+
published: typeof item.published === "string" ? item.published : null,
|
|
198
|
+
},
|
|
199
|
+
];
|
|
200
|
+
});
|
|
201
|
+
}
|
|
152
202
|
const RETRYABLE_STATUS = new Set([0, 408, 409, 425, 429, 500, 502, 503, 504, 529]);
|
|
153
203
|
export const MAX_RETRIES = 5;
|
|
154
204
|
/** A server asking us to wait longer than this is reported instead of waited out. */
|
|
@@ -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
|
+
}
|