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
|
@@ -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);
|
package/dist/agent/compact.js
CHANGED
|
@@ -2,29 +2,49 @@
|
|
|
2
2
|
* Compaction: when the conversation nears the model's context window, earlier turns are replaced
|
|
3
3
|
* by a model-written summary. The pure parts (what to keep, what to send) live here.
|
|
4
4
|
*/
|
|
5
|
+
import { commandLabel } from "./command-template.js";
|
|
5
6
|
import { truncateMiddle } from "./tools/types.js";
|
|
6
7
|
/** Compact once the context is this full. */
|
|
7
8
|
export const COMPACT_AT = 0.8;
|
|
9
|
+
/** What an image counts for in a token estimate (a large screenshot, roughly). */
|
|
10
|
+
export const IMAGE_TOKENS = 1_600;
|
|
8
11
|
/** A rough token count (4 characters per token), for before the server has reported usage. */
|
|
9
12
|
export function estimateTokens(messages) {
|
|
10
13
|
let chars = 0;
|
|
14
|
+
let images = 0;
|
|
11
15
|
for (const m of messages) {
|
|
12
|
-
|
|
16
|
+
if (typeof m.content === "string")
|
|
17
|
+
chars += m.content.length;
|
|
18
|
+
else if (Array.isArray(m.content)) {
|
|
19
|
+
for (const part of m.content) {
|
|
20
|
+
if (part.type === "text")
|
|
21
|
+
chars += part.text.length;
|
|
22
|
+
else
|
|
23
|
+
images++;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
13
26
|
if (m.role === "assistant" && m.tool_calls) {
|
|
14
27
|
for (const c of m.tool_calls)
|
|
15
28
|
chars += c.function.name.length + c.function.arguments.length;
|
|
16
29
|
}
|
|
17
30
|
}
|
|
18
|
-
return Math.ceil(chars / 4);
|
|
31
|
+
return Math.ceil(chars / 4) + images * IMAGE_TOKENS;
|
|
32
|
+
}
|
|
33
|
+
/** A user message's text, with a placeholder for each image. */
|
|
34
|
+
export function contentText(content) {
|
|
35
|
+
if (typeof content === "string")
|
|
36
|
+
return content;
|
|
37
|
+
return content.map((p) => (p.type === "text" ? p.text : "[image]")).join("\n");
|
|
19
38
|
}
|
|
20
39
|
export const shouldCompact = (contextTokens, contextLength) => contextTokens !== null && contextTokens >= contextLength * COMPACT_AT;
|
|
21
40
|
/**
|
|
22
41
|
* Splits the conversation (without the system message) into what gets summarized and what's kept:
|
|
23
42
|
* everything from the latest user message on. Cutting at a user message never separates a tool
|
|
24
|
-
* call from its result
|
|
43
|
+
* call from its result; one the agent added (a tool call's images) isn't a place to cut, since it
|
|
44
|
+
* belongs with the results before it. Null when there's nothing earlier to summarize.
|
|
25
45
|
*/
|
|
26
46
|
export function splitForCompaction(messages) {
|
|
27
|
-
const lastUser = messages.
|
|
47
|
+
const lastUser = messages.findLastIndex((m) => m.role === "user" && !m.synthetic);
|
|
28
48
|
if (lastUser <= 0)
|
|
29
49
|
return null;
|
|
30
50
|
return { head: messages.slice(0, lastUser), tail: messages.slice(lastUser) };
|
|
@@ -34,8 +54,15 @@ const clip = (text, max) => truncateMiddle(text, max);
|
|
|
34
54
|
export function transcriptForSummary(head, maxChars) {
|
|
35
55
|
const parts = [];
|
|
36
56
|
for (const m of head) {
|
|
37
|
-
if (m.role === "user")
|
|
38
|
-
|
|
57
|
+
if (m.role === "user") {
|
|
58
|
+
// A tool call's images: the summarizer only reads text.
|
|
59
|
+
if (m.synthetic)
|
|
60
|
+
parts.push(`TOOL IMAGES:\n${clip(contentText(m.content), 2000)}`);
|
|
61
|
+
else {
|
|
62
|
+
const label = m.command ? `USER (${commandLabel(m.command)})` : "USER";
|
|
63
|
+
parts.push(`${label}:\n${clip(contentText(m.content), 8000)}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
39
66
|
else if (m.role === "assistant") {
|
|
40
67
|
if (m.content)
|
|
41
68
|
parts.push(`ASSISTANT:\n${clip(m.content, 8000)}`);
|
|
@@ -57,6 +84,13 @@ export const SUMMARY_REQUEST = `Summarize the conversation above. Include:
|
|
|
57
84
|
- The current state: what works, what's broken, errors still open (quote key messages).
|
|
58
85
|
- What remains to do next.
|
|
59
86
|
Keep specifics (paths, names, versions, error text). Leave out pleasantries and repeated detail.`;
|
|
87
|
+
/** The summary request, asked to keep what the user named (`/compact keep the API decisions`). */
|
|
88
|
+
export function summaryRequest(focus) {
|
|
89
|
+
const keep = focus?.trim();
|
|
90
|
+
return keep
|
|
91
|
+
? `${SUMMARY_REQUEST}\nPay particular attention to: ${clip(keep, 2000)}`
|
|
92
|
+
: SUMMARY_REQUEST;
|
|
93
|
+
}
|
|
60
94
|
export const SUMMARY_PREFIX = "[Summary of the conversation so far]";
|
|
61
95
|
/** The conversation after compaction: the summary leads the first kept user message. */
|
|
62
96
|
export function compactedMessages(summary, tail, todos) {
|
|
@@ -65,10 +99,26 @@ export function compactedMessages(summary, tail, todos) {
|
|
|
65
99
|
: "";
|
|
66
100
|
const [first, ...rest] = tail;
|
|
67
101
|
const lead = `${SUMMARY_PREFIX}\n${summary.trim()}${todoText}\n\n[Latest message]\n`;
|
|
68
|
-
if (first?.role !== "user") {
|
|
102
|
+
if (first?.role !== "user" || first.synthetic) {
|
|
69
103
|
return [{ role: "user", content: `${lead}(none)` }, ...tail];
|
|
70
104
|
}
|
|
71
|
-
return [
|
|
105
|
+
return [
|
|
106
|
+
{
|
|
107
|
+
role: "user",
|
|
108
|
+
content: withLead(lead, first.content),
|
|
109
|
+
...(first.command ? { command: first.command } : {}),
|
|
110
|
+
},
|
|
111
|
+
...rest,
|
|
112
|
+
];
|
|
113
|
+
}
|
|
114
|
+
/** `lead` in front of a message's text (its first text part, when it has parts). */
|
|
115
|
+
function withLead(lead, content) {
|
|
116
|
+
if (typeof content === "string")
|
|
117
|
+
return lead + content;
|
|
118
|
+
const i = content.findIndex((p) => p.type === "text");
|
|
119
|
+
if (i === -1)
|
|
120
|
+
return [{ type: "text", text: lead.trimEnd() }, ...content];
|
|
121
|
+
return content.map((p, j) => (j === i && p.type === "text" ? { ...p, text: lead + p.text } : p));
|
|
72
122
|
}
|
|
73
123
|
/** Whether an error says the request was too long for the model's context. */
|
|
74
124
|
export function isContextOverflow(error) {
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
export const WIRE_ELISION = { keepImages: 2, budgetChars: 60_000 };
|
|
2
|
+
/** What an image counts for against the budget: about 1,600 tokens, at 4 characters a token. */
|
|
3
|
+
export const IMAGE_CHARS = 1_600 * 4;
|
|
4
|
+
/** What the model sees in place of an old screenshot. */
|
|
5
|
+
export const IMAGE_ELIDED = "[image no longer shown]";
|
|
6
|
+
/** What a model that doesn't take images sees in place of one. */
|
|
7
|
+
export const IMAGE_NOT_SENT = "[image not shown: this model doesn't take images]";
|
|
8
|
+
/** The conversation with every image replaced by a note, for a model that doesn't take images. */
|
|
9
|
+
export function withoutImages(messages) {
|
|
10
|
+
if (!messages.some((m) => m.role === "user" && Array.isArray(m.content)))
|
|
11
|
+
return messages;
|
|
12
|
+
return messages.map((m) => {
|
|
13
|
+
if (m.role !== "user" || typeof m.content === "string")
|
|
14
|
+
return m;
|
|
15
|
+
if (!m.content.some((p) => p.type === "image_url"))
|
|
16
|
+
return m;
|
|
17
|
+
const content = m.content.map((p) => p.type === "image_url" ? { type: "text", text: IMAGE_NOT_SENT } : p);
|
|
18
|
+
return { ...m, content };
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/** The conversation as it's sent: old replaceable results and images replaced (see above). */
|
|
22
|
+
export function elideForWire(messages, { keepImages, budgetChars } = WIRE_ELISION) {
|
|
23
|
+
const elided = new Set();
|
|
24
|
+
/** Sent whole so far: kept by the last batch, or added since. */
|
|
25
|
+
let pending = [];
|
|
26
|
+
/** Added since the last batch. */
|
|
27
|
+
let added = 0;
|
|
28
|
+
for (const [index, m] of messages.entries()) {
|
|
29
|
+
const units = [];
|
|
30
|
+
if (m.role === "tool" && m.elide !== undefined) {
|
|
31
|
+
units.push({ kind: "result", index, chars: m.content.length });
|
|
32
|
+
}
|
|
33
|
+
else if (m.role === "user" && m.synthetic === "tool-images" && Array.isArray(m.content)) {
|
|
34
|
+
m.content.forEach((p, part) => {
|
|
35
|
+
if (p.type === "image_url")
|
|
36
|
+
units.push({ kind: "image", index, part, chars: IMAGE_CHARS });
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
for (const unit of units) {
|
|
40
|
+
pending.push(unit);
|
|
41
|
+
added += unit.chars;
|
|
42
|
+
if (added <= budgetChars)
|
|
43
|
+
continue;
|
|
44
|
+
// Over budget: keep the newest result and the newest images, replace the rest of the batch.
|
|
45
|
+
const kept = new Set();
|
|
46
|
+
const results = pending.filter((u) => u.kind === "result");
|
|
47
|
+
if (results.length)
|
|
48
|
+
kept.add(results.at(-1));
|
|
49
|
+
const images = pending.filter((u) => u.kind === "image");
|
|
50
|
+
for (const u of images.slice(Math.max(0, images.length - keepImages)))
|
|
51
|
+
kept.add(u);
|
|
52
|
+
for (const u of pending)
|
|
53
|
+
if (!kept.has(u))
|
|
54
|
+
elided.add(u);
|
|
55
|
+
pending = pending.filter((u) => kept.has(u));
|
|
56
|
+
added = 0;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (!elided.size)
|
|
60
|
+
return messages;
|
|
61
|
+
const results = new Set();
|
|
62
|
+
const images = new Map();
|
|
63
|
+
for (const u of elided) {
|
|
64
|
+
if (u.kind === "result")
|
|
65
|
+
results.add(u.index);
|
|
66
|
+
else
|
|
67
|
+
images.set(u.index, (images.get(u.index) ?? new Set()).add(u.part));
|
|
68
|
+
}
|
|
69
|
+
return messages.map((m, index) => {
|
|
70
|
+
if (results.has(index)) {
|
|
71
|
+
const tool = m;
|
|
72
|
+
return { ...tool, content: tool.elide };
|
|
73
|
+
}
|
|
74
|
+
const parts = images.get(index);
|
|
75
|
+
if (parts) {
|
|
76
|
+
const user = m;
|
|
77
|
+
const content = user.content.map((p, i) => parts.has(i) ? { type: "text", text: IMAGE_ELIDED } : p);
|
|
78
|
+
return { ...user, content };
|
|
79
|
+
}
|
|
80
|
+
return m;
|
|
81
|
+
});
|
|
82
|
+
}
|