agent-from-zero 0.1.0 → 0.27.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 +37 -3
- package/dist/agent.js +500 -34
- package/dist/config.js +88 -12
- package/dist/context.js +4 -3
- package/dist/cost.js +93 -0
- package/dist/diff.js +135 -0
- package/dist/form.js +78 -0
- package/dist/hooks.js +92 -0
- package/dist/judge.js +77 -0
- package/dist/loop.js +267 -39
- package/dist/mcp.js +237 -0
- package/dist/memory.js +186 -0
- package/dist/mentions.js +62 -0
- package/dist/menu.js +154 -0
- package/dist/permissions.js +104 -6
- package/dist/prompt.js +16 -11
- package/dist/session.js +103 -0
- package/dist/skills.js +128 -0
- package/dist/telemetry.js +43 -0
- package/dist/tools.js +120 -18
- package/dist/tui.js +67 -0
- package/dist/ui.js +134 -0
- package/dist/undo.js +88 -0
- package/dist/validate.js +60 -0
- package/package.json +4 -3
package/dist/mcp.js
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { spawn } from "node:child_process"; // stdio servers are subprocesses
|
|
2
|
+
import chalk from "chalk"; // status lines
|
|
3
|
+
import { CONFIG } from "./config.js"; // configured servers
|
|
4
|
+
import { registerExternalTool } from "./tools.js"; // expose discovered tools
|
|
5
|
+
import { emit } from "./telemetry.js"; // observability
|
|
6
|
+
// MCP (Model Context Protocol) lets the agent borrow tools from external
|
|
7
|
+
// servers — a filesystem server, a GitHub server, a database server, anything.
|
|
8
|
+
// The wire protocol is JSON-RPC 2.0. We do the initialize handshake, ask for the
|
|
9
|
+
// server's tools, and register each one so it flows through the SAME dispatch and
|
|
10
|
+
// the SAME permission gate as a built-in. There is one execution path, period.
|
|
11
|
+
//
|
|
12
|
+
// Since Day 24 there are TWO ways to reach a server: spawn it and talk over
|
|
13
|
+
// stdio (local), or POST to a URL (remote, "Streamable HTTP"). The protocol is
|
|
14
|
+
// identical; only the pipe differs. That difference lives behind a Transport
|
|
15
|
+
// interface, so McpClient — handshake, ids, tool flattening — never has to care.
|
|
16
|
+
//
|
|
17
|
+
// Discovered tools are named mcp__<server>__<tool> so they never collide with
|
|
18
|
+
// built-ins and the user can see (and gate) exactly where each came from.
|
|
19
|
+
const INIT_TIMEOUT_MS = 15_000; // a server that doesn't initialize in 15s is skipped
|
|
20
|
+
const CALL_TIMEOUT_MS = 60_000; // a single tool call may take this long
|
|
21
|
+
// ---- stdio transport: one JSON object per line, to a subprocess --------------
|
|
22
|
+
class StdioTransport {
|
|
23
|
+
child;
|
|
24
|
+
pending = new Map(); // id → resolver (responses arrive async on one stream)
|
|
25
|
+
buffer = ""; // partial stdout, reassembled into whole lines
|
|
26
|
+
constructor(def) {
|
|
27
|
+
this.child = spawn(def.command, def.args ?? [], {
|
|
28
|
+
stdio: ["pipe", "pipe", "pipe"], // we own stdin/stdout; stderr is the server's log
|
|
29
|
+
env: { ...process.env, ...def.env }, // pass through + extras
|
|
30
|
+
});
|
|
31
|
+
// Reassemble newline-delimited JSON from the stdout stream.
|
|
32
|
+
this.child.stdout.on("data", (chunk) => {
|
|
33
|
+
this.buffer += chunk.toString();
|
|
34
|
+
let nl;
|
|
35
|
+
while ((nl = this.buffer.indexOf("\n")) >= 0) {
|
|
36
|
+
const line = this.buffer.slice(0, nl).trim(); // one complete message
|
|
37
|
+
this.buffer = this.buffer.slice(nl + 1); // keep the remainder
|
|
38
|
+
if (line)
|
|
39
|
+
this.onMessage(line);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
this.child.stderr.on("data", () => { }); // the server's stderr is its own logging — never our stdout
|
|
43
|
+
}
|
|
44
|
+
onMessage(line) {
|
|
45
|
+
let msg;
|
|
46
|
+
try {
|
|
47
|
+
msg = JSON.parse(line);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return; // ignore anything that isn't valid JSON-RPC
|
|
51
|
+
}
|
|
52
|
+
const resolver = this.pending.get(msg.id);
|
|
53
|
+
if (resolver) {
|
|
54
|
+
this.pending.delete(msg.id);
|
|
55
|
+
resolver(msg);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
request(payload, timeoutMs) {
|
|
59
|
+
const { id } = payload;
|
|
60
|
+
return new Promise((resolve) => {
|
|
61
|
+
let settled = false;
|
|
62
|
+
const finish = (r) => {
|
|
63
|
+
if (settled)
|
|
64
|
+
return;
|
|
65
|
+
settled = true;
|
|
66
|
+
clearTimeout(timer);
|
|
67
|
+
this.pending.delete(id);
|
|
68
|
+
resolve(r);
|
|
69
|
+
};
|
|
70
|
+
const timer = setTimeout(() => finish({ id, error: { message: `MCP request '${payload.method}' timed out` } }), timeoutMs);
|
|
71
|
+
this.pending.set(id, finish);
|
|
72
|
+
this.child.stdin.write(JSON.stringify(payload) + "\n"); // one line, newline-terminated
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
notify(method, params) {
|
|
76
|
+
this.child.stdin.write(JSON.stringify({ jsonrpc: "2.0", method, params }) + "\n");
|
|
77
|
+
}
|
|
78
|
+
close() {
|
|
79
|
+
this.child.kill(); // best-effort cleanup
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// Pull JSON-RPC messages out of a Server-Sent-Events body. SSE frames are
|
|
83
|
+
// separated by blank lines; the payload lives on one or more `data:` lines.
|
|
84
|
+
// Exported because parsing is the fiddly part worth testing on its own.
|
|
85
|
+
export function parseSseData(body) {
|
|
86
|
+
const out = [];
|
|
87
|
+
for (const frame of body.split(/\r?\n\r?\n/)) {
|
|
88
|
+
const data = frame
|
|
89
|
+
.split(/\r?\n/)
|
|
90
|
+
.filter((l) => l.startsWith("data:"))
|
|
91
|
+
.map((l) => l.slice(5).trim()) // strip "data:" and surrounding space
|
|
92
|
+
.join("\n");
|
|
93
|
+
if (data)
|
|
94
|
+
out.push(data);
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
// ---- HTTP transport: POST to a URL, read JSON or SSE back --------------------
|
|
99
|
+
class HttpTransport {
|
|
100
|
+
url;
|
|
101
|
+
headers;
|
|
102
|
+
sessionId; // assigned by the server on initialize, echoed on every later request
|
|
103
|
+
constructor(def) {
|
|
104
|
+
this.url = def.url;
|
|
105
|
+
this.headers = { ...def.headers };
|
|
106
|
+
}
|
|
107
|
+
// The response to a POST may come back as a single JSON object OR as an SSE
|
|
108
|
+
// stream carrying it. Either way we want the JSON-RPC message matching our id.
|
|
109
|
+
async post(body, timeoutMs, isRequest) {
|
|
110
|
+
const res = await fetch(this.url, {
|
|
111
|
+
method: "POST",
|
|
112
|
+
headers: {
|
|
113
|
+
"content-type": "application/json",
|
|
114
|
+
accept: "application/json, text/event-stream", // we accept either shape
|
|
115
|
+
...(this.sessionId ? { "mcp-session-id": this.sessionId } : {}),
|
|
116
|
+
...this.headers,
|
|
117
|
+
},
|
|
118
|
+
body: JSON.stringify(body),
|
|
119
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
120
|
+
});
|
|
121
|
+
const sid = res.headers.get("mcp-session-id"); // initialize hands us a session to keep
|
|
122
|
+
if (sid)
|
|
123
|
+
this.sessionId = sid;
|
|
124
|
+
if (!isRequest)
|
|
125
|
+
return null; // a notification expects no body (202 Accepted)
|
|
126
|
+
const raw = await res.text();
|
|
127
|
+
const payloads = res.headers.get("content-type")?.includes("text/event-stream") ? parseSseData(raw) : [raw];
|
|
128
|
+
for (const p of payloads) {
|
|
129
|
+
try {
|
|
130
|
+
const msg = JSON.parse(p);
|
|
131
|
+
if (msg && (msg.result !== undefined || msg.error !== undefined))
|
|
132
|
+
return msg; // the response we came for
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
/* skip non-JSON frames (SSE comments, keep-alives) */
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
async request(payload, timeoutMs) {
|
|
141
|
+
try {
|
|
142
|
+
const msg = await this.post(payload, timeoutMs, true);
|
|
143
|
+
return msg ?? { id: payload.id, error: { message: `MCP request '${payload.method}' returned no response` } };
|
|
144
|
+
}
|
|
145
|
+
catch (err) {
|
|
146
|
+
return { id: payload.id, error: { message: `MCP HTTP request '${payload.method}' failed: ${err.message}` } };
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
notify(method, params) {
|
|
150
|
+
// Fire-and-forget; errors on a notification are not worth surfacing.
|
|
151
|
+
void this.post({ jsonrpc: "2.0", method, params }, INIT_TIMEOUT_MS, false).catch(() => { });
|
|
152
|
+
}
|
|
153
|
+
close() {
|
|
154
|
+
// Best-effort: tell the server to drop the session. No await, no error care.
|
|
155
|
+
if (this.sessionId) {
|
|
156
|
+
void fetch(this.url, { method: "DELETE", headers: { "mcp-session-id": this.sessionId, ...this.headers } }).catch(() => { });
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// Choose the pipe from the config: a url means HTTP, otherwise spawn over stdio.
|
|
161
|
+
function makeTransport(def) {
|
|
162
|
+
return def.url ? new HttpTransport(def) : new StdioTransport(def);
|
|
163
|
+
}
|
|
164
|
+
// The protocol layer: handshake, request ids, tool flattening. It talks to a
|
|
165
|
+
// Transport and is blind to whether that's a subprocess or an HTTP endpoint.
|
|
166
|
+
class McpClient {
|
|
167
|
+
transport;
|
|
168
|
+
nextId = 1; // JSON-RPC request id counter
|
|
169
|
+
constructor(def) {
|
|
170
|
+
this.transport = makeTransport(def);
|
|
171
|
+
}
|
|
172
|
+
request(method, params, timeoutMs) {
|
|
173
|
+
return this.transport.request({ jsonrpc: "2.0", id: this.nextId++, method, params }, timeoutMs);
|
|
174
|
+
}
|
|
175
|
+
// The MCP startup dance: initialize → initialized → list tools.
|
|
176
|
+
async start() {
|
|
177
|
+
const init = await this.request("initialize", { protocolVersion: "2024-11-05", capabilities: {}, clientInfo: { name: "mini-agent", version: "0" } }, INIT_TIMEOUT_MS);
|
|
178
|
+
if (init.error)
|
|
179
|
+
throw new Error(init.error.message); // server refused to initialize
|
|
180
|
+
this.transport.notify("notifications/initialized", {}); // tell the server we're ready
|
|
181
|
+
const list = await this.request("tools/list", {}, INIT_TIMEOUT_MS); // ask what it offers
|
|
182
|
+
if (list.error)
|
|
183
|
+
throw new Error(list.error.message);
|
|
184
|
+
return list.result?.tools ?? [];
|
|
185
|
+
}
|
|
186
|
+
// Invoke one tool and flatten its content into plain text for the model.
|
|
187
|
+
async callTool(name, args) {
|
|
188
|
+
const res = await this.request("tools/call", { name, arguments: args }, CALL_TIMEOUT_MS);
|
|
189
|
+
if (res.error)
|
|
190
|
+
return `[error] MCP tool failed: ${res.error.message}`; // surface, don't throw
|
|
191
|
+
const text = (res.result?.content ?? []).map((c) => c.text ?? `[${c.type} content]`).join("\n"); // flatten parts
|
|
192
|
+
const body = text || "(no output)";
|
|
193
|
+
return res.result?.isError ? `[error] ${body}` : body; // MCP can flag a logical error in a successful response
|
|
194
|
+
}
|
|
195
|
+
kill() {
|
|
196
|
+
this.transport.close();
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
// Connect to every configured MCP server, register their tools, and return a
|
|
200
|
+
// cleanup function. Failures are isolated: a server that won't start is logged
|
|
201
|
+
// and skipped — it never stops the agent or the other servers.
|
|
202
|
+
export async function connectMcpServers() {
|
|
203
|
+
const entries = Object.entries(CONFIG.mcpServers); // [name, def] pairs
|
|
204
|
+
if (!entries.length)
|
|
205
|
+
return () => { }; // the common case: nothing configured
|
|
206
|
+
const clients = []; // for cleanup
|
|
207
|
+
for (const [serverName, def] of entries) {
|
|
208
|
+
const client = new McpClient(def);
|
|
209
|
+
try {
|
|
210
|
+
const specs = await client.start(); // handshake + discover
|
|
211
|
+
clients.push(client);
|
|
212
|
+
for (const spec of specs) {
|
|
213
|
+
const toolName = `mcp__${serverName}__${spec.name}`; // namespaced, collision-proof
|
|
214
|
+
const tool = {
|
|
215
|
+
definition: {
|
|
216
|
+
type: "function",
|
|
217
|
+
function: {
|
|
218
|
+
name: toolName,
|
|
219
|
+
description: `[MCP:${serverName}] ${spec.description ?? spec.name}`, // mark the origin in the manual
|
|
220
|
+
parameters: spec.inputSchema ?? { type: "object", properties: {} }, // pass the server's schema straight through
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
run: async (args) => client.callTool(spec.name, args), // dispatch routes here; the permission gate ran first
|
|
224
|
+
};
|
|
225
|
+
registerExternalTool(tool); // now indistinguishable from a built-in to the rest of the system
|
|
226
|
+
}
|
|
227
|
+
emit("agent_mcp_connected", { server: serverName, tools: specs.length, transport: def.url ? "http" : "stdio" });
|
|
228
|
+
console.log(chalk.dim(`(mcp: ${serverName} — ${specs.length} tools${def.url ? " over http" : ""})`)); // visible at startup
|
|
229
|
+
}
|
|
230
|
+
catch (err) {
|
|
231
|
+
client.kill(); // don't leak the process/session
|
|
232
|
+
emit("agent_mcp_failed", { server: serverName });
|
|
233
|
+
console.error(chalk.yellow(` [mcp] ${serverName} failed to start (skipped): ${err.message}`)); // loud, but not fatal
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return () => clients.forEach((c) => c.kill()); // the caller calls this on exit
|
|
237
|
+
}
|
package/dist/memory.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import fs from "node:fs"; // memory lives in a file on disk
|
|
2
|
+
import path from "node:path"; // path joining
|
|
3
|
+
// Long-term memory: facts the agent should carry ACROSS sessions, not just
|
|
4
|
+
// within one conversation. Session persistence (Day 12) restores "what we were
|
|
5
|
+
// talking about"; this restores "what I should always know about this project".
|
|
6
|
+
//
|
|
7
|
+
// One Markdown file, project-local, human-readable and human-editable. Not a
|
|
8
|
+
// vector database, not embeddings: a file you can open, read, and fix.
|
|
9
|
+
//
|
|
10
|
+
// Two ways facts get in: the model calls `remember` explicitly, OR — when
|
|
11
|
+
// auto-extract is on — after each turn a cheap model call reads the conversation
|
|
12
|
+
// and saves what's worth keeping (especially turns where the USER corrected the
|
|
13
|
+
// assistant: 90% the model gets right isn't worth saving, the 10% it got wrong
|
|
14
|
+
// and was corrected on is gold). Every fact is TYPED so the model can tell "this
|
|
15
|
+
// is who the user is" from "this is a correction" from "this is a project fact".
|
|
16
|
+
export const MEMORY_PATH = path.resolve(".mini-agent", "MEMORY.md");
|
|
17
|
+
// The four memory types (handbook §17). feedback is the most valuable.
|
|
18
|
+
export const MEMORY_TYPES = ["user", "feedback", "project", "reference"];
|
|
19
|
+
// Bounds, because unbounded memory dilutes the model's attention (Ch6: too many
|
|
20
|
+
// rules → worse compliance) and grows the cached prefix forever. On overflow we
|
|
21
|
+
// drop the OLDEST — recent facts are likelier still true.
|
|
22
|
+
const MAX_ENTRIES = 40; // at most this many remembered facts
|
|
23
|
+
const MAX_BYTES = 12_000; // ...and at most this many bytes total
|
|
24
|
+
const HEADER = "# mini-agent long-term memory\n# One fact per line: - [type] fact. Injected at session start. Edit freely.\n";
|
|
25
|
+
// Coerce any string to a valid type, defaulting to "project".
|
|
26
|
+
function asType(t) {
|
|
27
|
+
return MEMORY_TYPES.includes(t ?? "") ? t : "project";
|
|
28
|
+
}
|
|
29
|
+
// Parse one bullet's text into a typed entry. "[feedback] foo" → feedback; a
|
|
30
|
+
// bare "foo" (legacy / hand-written) → project.
|
|
31
|
+
function parseEntry(text) {
|
|
32
|
+
const m = text.match(/^\[(\w+)\]\s*(.*)$/);
|
|
33
|
+
if (m && MEMORY_TYPES.includes(m[1]))
|
|
34
|
+
return { type: m[1], fact: m[2].trim() };
|
|
35
|
+
return { type: "project", fact: text.trim() };
|
|
36
|
+
}
|
|
37
|
+
// Read facts WITH their types. The fiddly parsing lives here.
|
|
38
|
+
export function readMemoryTyped() {
|
|
39
|
+
try {
|
|
40
|
+
return fs
|
|
41
|
+
.readFileSync(MEMORY_PATH, "utf8")
|
|
42
|
+
.split("\n")
|
|
43
|
+
.filter((l) => l.startsWith("- "))
|
|
44
|
+
.map((l) => parseEntry(l.slice(2).trim()))
|
|
45
|
+
.filter((e) => e.fact);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return []; // no memory file yet — fine
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// Read just the fact text (no type). Kept for callers that only want the facts.
|
|
52
|
+
export function readMemory() {
|
|
53
|
+
return readMemoryTyped().map((e) => e.fact);
|
|
54
|
+
}
|
|
55
|
+
// The block injected into the system message, grouped by type so the model can
|
|
56
|
+
// weigh "a correction" differently from "a project fact". Empty when there's
|
|
57
|
+
// nothing, so the cached prefix stays clean for projects that never use it.
|
|
58
|
+
export function memoryContext() {
|
|
59
|
+
const entries = readMemoryTyped();
|
|
60
|
+
if (!entries.length)
|
|
61
|
+
return "";
|
|
62
|
+
const labels = {
|
|
63
|
+
user: "About the user",
|
|
64
|
+
feedback: "Guidance & corrections the user gave you (follow these)",
|
|
65
|
+
project: "Project facts & decisions",
|
|
66
|
+
reference: "References",
|
|
67
|
+
};
|
|
68
|
+
let out = "\n\nLong-term project memory (background from earlier sessions; verify against the code before relying on a specific claim):";
|
|
69
|
+
for (const type of MEMORY_TYPES) {
|
|
70
|
+
const group = entries.filter((e) => e.type === type);
|
|
71
|
+
if (group.length)
|
|
72
|
+
out += `\n[${labels[type]}]\n${group.map((e) => `- ${e.fact}`).join("\n")}`;
|
|
73
|
+
}
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
// Append a typed fact, then enforce the caps. Self-healing: every write re-applies
|
|
77
|
+
// the bounds, so the file can never grow past them even if hand-edited huge.
|
|
78
|
+
export function addFact(fact, type = "project") {
|
|
79
|
+
const clean = fact.replace(/\s+/g, " ").trim(); // one line, normalized whitespace
|
|
80
|
+
if (!clean)
|
|
81
|
+
return "[error] nothing to remember (empty fact).";
|
|
82
|
+
const t = asType(type); // coerce a bad type to "project" so the on-disk format stays parseable
|
|
83
|
+
let entries = readMemoryTyped();
|
|
84
|
+
if (entries.some((e) => e.fact === clean))
|
|
85
|
+
return "Already remembered — no change."; // idempotent
|
|
86
|
+
entries.push({ type: t, fact: clean });
|
|
87
|
+
if (entries.length > MAX_ENTRIES)
|
|
88
|
+
entries = entries.slice(entries.length - MAX_ENTRIES); // drop oldest
|
|
89
|
+
let body = entries.map((e) => `- [${e.type}] ${e.fact}`).join("\n");
|
|
90
|
+
while (Buffer.byteLength(HEADER + body, "utf8") > MAX_BYTES && entries.length > 1) {
|
|
91
|
+
entries.shift(); // still too big — drop the oldest until it fits
|
|
92
|
+
body = entries.map((e) => `- [${e.type}] ${e.fact}`).join("\n");
|
|
93
|
+
}
|
|
94
|
+
fs.mkdirSync(path.dirname(MEMORY_PATH), { recursive: true });
|
|
95
|
+
fs.writeFileSync(MEMORY_PATH, HEADER + body + "\n", "utf8");
|
|
96
|
+
return `Remembered (${t}). (${entries.length} fact${entries.length === 1 ? "" : "s"} stored.)`;
|
|
97
|
+
}
|
|
98
|
+
// The tool the model calls to save something worth keeping.
|
|
99
|
+
export const rememberTool = {
|
|
100
|
+
definition: {
|
|
101
|
+
type: "function",
|
|
102
|
+
function: {
|
|
103
|
+
name: "remember",
|
|
104
|
+
description: `Save a durable fact to long-term memory (persists across sessions, shown to you at the start of every future session).
|
|
105
|
+
Use it for things still true next time: the build/test command, a gotcha, a convention, a decision and its reason, a user preference, or a correction the user gave you.
|
|
106
|
+
Do NOT use it for transient state (current task, today's bug). Keep each fact to one sentence. Pick the right "type".`,
|
|
107
|
+
parameters: {
|
|
108
|
+
type: "object",
|
|
109
|
+
properties: {
|
|
110
|
+
fact: { type: "string", description: "One durable fact, as a single sentence" },
|
|
111
|
+
type: { type: "string", enum: [...MEMORY_TYPES], description: "user (who they are/prefs) · feedback (a correction/guidance) · project (a decision/convention/gotcha) · reference (a URL/doc)" },
|
|
112
|
+
},
|
|
113
|
+
required: ["fact"],
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
run: (args) => addFact(args.fact ?? "", asType(args.type)),
|
|
118
|
+
};
|
|
119
|
+
// ---- auto-extract -----------------------------------------------------------
|
|
120
|
+
// After a turn, read the conversation and save what's worth keeping. The high-
|
|
121
|
+
// frequency write path from §17 — so memory captures itself instead of relying
|
|
122
|
+
// on the model to remember to call `remember`.
|
|
123
|
+
const EXTRACT_SYSTEM = `You extract DURABLE memories from a slice of a coding-assistant conversation — facts worth carrying into FUTURE sessions.
|
|
124
|
+
Output ONLY a JSON array. Each item: {"type": "...", "fact": "one sentence"}. No prose, no markdown fences.
|
|
125
|
+
"type" is one of:
|
|
126
|
+
- "user": who the user is — role, preferences, how they like to work.
|
|
127
|
+
- "feedback": guidance or a CORRECTION the user gave the assistant. These are the MOST valuable — prioritize any turn where the user corrected or redirected the assistant.
|
|
128
|
+
- "project": a goal, decision, convention, or non-obvious gotcha about THIS project.
|
|
129
|
+
- "reference": a pointer to an external resource (a URL, a doc, a ticket).
|
|
130
|
+
Do NOT save: transient task state, the bug currently being fixed, or anything obvious from reading the code. Keep each fact to one sentence.
|
|
131
|
+
If nothing is worth saving, output [].`;
|
|
132
|
+
// Parse the model's extraction output into typed entries. Lenient: finds the
|
|
133
|
+
// JSON array even if wrapped in prose/fences, drops malformed items. Pure.
|
|
134
|
+
export function parseExtractedMemories(text) {
|
|
135
|
+
const m = text.match(/\[[\s\S]*\]/); // the first […] — tolerate surrounding chatter
|
|
136
|
+
if (!m)
|
|
137
|
+
return [];
|
|
138
|
+
let arr;
|
|
139
|
+
try {
|
|
140
|
+
arr = JSON.parse(m[0]);
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
return [];
|
|
144
|
+
}
|
|
145
|
+
if (!Array.isArray(arr))
|
|
146
|
+
return [];
|
|
147
|
+
return arr
|
|
148
|
+
.filter((x) => !!x && typeof x === "object")
|
|
149
|
+
.filter((x) => typeof x.fact === "string" && x.fact.trim())
|
|
150
|
+
.map((x) => ({ type: asType(x.type), fact: String(x.fact).replace(/\s+/g, " ").trim() }));
|
|
151
|
+
}
|
|
152
|
+
// Build the transcript handed to the extractor: the last few user/assistant
|
|
153
|
+
// turns (string content only), each capped, so the extraction stays cheap.
|
|
154
|
+
function buildTranscript(messages) {
|
|
155
|
+
const turns = messages
|
|
156
|
+
.filter((m) => (m.role === "user" || m.role === "assistant") && typeof m.content === "string" && m.content.trim())
|
|
157
|
+
.slice(-12) // recent context is what matters
|
|
158
|
+
.map((m) => `${m.role}: ${String(m.content).split("\n\n[Referenced files")[0].slice(0, 600)}`);
|
|
159
|
+
return turns.join("\n\n");
|
|
160
|
+
}
|
|
161
|
+
// Run one extraction pass: a single (cheap) model call, parse, save. Returns the
|
|
162
|
+
// facts it saved. Never throws — a failed extraction must not break the session.
|
|
163
|
+
export async function extractMemories(client, model, messages) {
|
|
164
|
+
const transcript = buildTranscript(messages);
|
|
165
|
+
if (!transcript.trim())
|
|
166
|
+
return [];
|
|
167
|
+
let text = "";
|
|
168
|
+
try {
|
|
169
|
+
const res = await client.chat.completions.create({
|
|
170
|
+
model,
|
|
171
|
+
messages: [
|
|
172
|
+
{ role: "system", content: EXTRACT_SYSTEM },
|
|
173
|
+
{ role: "user", content: transcript },
|
|
174
|
+
],
|
|
175
|
+
stream: false,
|
|
176
|
+
});
|
|
177
|
+
text = res.choices?.[0]?.message?.content ?? "";
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
return []; // network/model error — skip silently, try again next turn
|
|
181
|
+
}
|
|
182
|
+
const extracted = parseExtractedMemories(text);
|
|
183
|
+
for (const e of extracted)
|
|
184
|
+
addFact(e.fact, e.type); // addFact dedups internally
|
|
185
|
+
return extracted;
|
|
186
|
+
}
|
package/dist/mentions.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import fs from "node:fs"; // read referenced files
|
|
2
|
+
import path from "node:path"; // resolve mention paths
|
|
3
|
+
import { checkPermission } from "./permissions.js"; // reuse the read_file gate — secret files must never leak in
|
|
4
|
+
// @file mentions: let the user pull a file into the conversation just by naming
|
|
5
|
+
// it — "explain @src/loop.ts" attaches the file's content so the model sees it
|
|
6
|
+
// without a separate read_file round-trip. The point is convenience, but the
|
|
7
|
+
// SAME rule still holds: a secret file (.env, *.pem) must never enter the
|
|
8
|
+
// context. So every mention is routed through the read_file permission gate
|
|
9
|
+
// (Day 4) — if it would deny a read, the mention is refused, not read.
|
|
10
|
+
// @ must start the token (line start or after whitespace) so emails (a@b) and
|
|
11
|
+
// npm scopes inside words don't trigger. The path runs to the next space; we
|
|
12
|
+
// trim trailing sentence punctuation so "see @cart.js." doesn't include the dot.
|
|
13
|
+
const MENTION_RE = /(?:^|\s)@(\S+)/g;
|
|
14
|
+
const MAX_MENTION_BYTES = 100_000; // a referenced file bigger than this is truncated
|
|
15
|
+
// Pull the raw @tokens out of a line (trailing , ; : ! ? ) stripped).
|
|
16
|
+
export function findMentions(line) {
|
|
17
|
+
const out = [];
|
|
18
|
+
for (const m of line.matchAll(MENTION_RE)) {
|
|
19
|
+
const raw = m[1].replace(/[.,;:!?)\]]+$/, ""); // drop trailing punctuation, keep inner dots (cart.js)
|
|
20
|
+
if (raw)
|
|
21
|
+
out.push(raw);
|
|
22
|
+
}
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
// Resolve one mention to a file we may attach. Never throws.
|
|
26
|
+
function resolveMention(raw) {
|
|
27
|
+
const abs = path.resolve(raw);
|
|
28
|
+
// The gate decides if a read is allowed; a secret file comes back "deny".
|
|
29
|
+
if (checkPermission("read_file", JSON.stringify({ path: raw })).decision === "deny") {
|
|
30
|
+
return { raw, path: abs, status: "denied" };
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
const stat = fs.statSync(abs);
|
|
34
|
+
if (stat.isDirectory())
|
|
35
|
+
return { raw, path: abs, status: "dir" };
|
|
36
|
+
let content = fs.readFileSync(abs, "utf8");
|
|
37
|
+
if (content.length > MAX_MENTION_BYTES)
|
|
38
|
+
content = content.slice(0, MAX_MENTION_BYTES) + `\n... (truncated, file is ${content.length} chars)`;
|
|
39
|
+
return { raw, path: abs, status: "ok", content };
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return { raw, path: abs, status: "missing" }; // not a file — probably not a reference at all
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// Expand a user line: find @mentions, attach the readable ones, and refuse the
|
|
46
|
+
// secret ones out loud. Returns the augmented message (file blocks appended) and
|
|
47
|
+
// the mention list (for the REPL to summarize). A line with no resolvable
|
|
48
|
+
// mention is returned unchanged — @tokens that don't name a real file (an email,
|
|
49
|
+
// a stray @) are ignored silently so we don't nag on every "@" in prose.
|
|
50
|
+
export function expandMentions(line) {
|
|
51
|
+
const mentions = findMentions(line).map(resolveMention);
|
|
52
|
+
// Only attach files we actually read, and announce refusals (security-relevant).
|
|
53
|
+
// "missing"/"dir" are dropped silently — likely not file references.
|
|
54
|
+
const attach = mentions.filter((m) => m.status === "ok" || m.status === "denied");
|
|
55
|
+
if (!attach.length)
|
|
56
|
+
return { augmented: line, mentions };
|
|
57
|
+
const blocks = attach.map((m) => m.status === "ok"
|
|
58
|
+
? `--- ${m.raw} ---\n${m.content}`
|
|
59
|
+
: `--- ${m.raw} (refused: secret file, not included) ---`);
|
|
60
|
+
const augmented = `${line}\n\n[Referenced files — the user attached these with @mentions]\n${blocks.join("\n\n")}`;
|
|
61
|
+
return { augmented, mentions };
|
|
62
|
+
}
|
package/dist/menu.js
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import readline from "node:readline"; // raw-mode keypress events + the interface to pause
|
|
2
|
+
import { renderMenu, MENU_HINT } from "./ui.js"; // the pure drawing + the footer hint
|
|
3
|
+
import { initFormState, reduceForm, renderForm, collectAnswers } from "./form.js"; // the multi-question form
|
|
4
|
+
// An arrow-key selection menu. The whole point: an approval is one keystroke
|
|
5
|
+
// (↑/↓ then Enter), not "type y, then N, then realize you meant the other".
|
|
6
|
+
// Selecting costs the user less than typing.
|
|
7
|
+
//
|
|
8
|
+
// Coordinating with the session's readline (which owns stdin) is the tricky
|
|
9
|
+
// part: we pause it, switch stdin to raw mode, read keypresses ourselves, then
|
|
10
|
+
// hand stdin back exactly as we found it. The `input` parameter is injected so
|
|
11
|
+
// tests can drive it with a fake stream instead of a real terminal.
|
|
12
|
+
//
|
|
13
|
+
// Returns the chosen index, or -1 if cancelled (Esc / Ctrl+C) or not a TTY —
|
|
14
|
+
// callers treat -1 as "no" (fail closed for an approval).
|
|
15
|
+
export function promptSelect(rl, // the session's line reader, paused while we own the keys
|
|
16
|
+
options, // the choices, top to bottom
|
|
17
|
+
input = process.stdin) {
|
|
18
|
+
return new Promise((resolve) => {
|
|
19
|
+
if (!input.isTTY)
|
|
20
|
+
return resolve(-1); // no interactive selection without a terminal — caller falls back
|
|
21
|
+
let selected = 0;
|
|
22
|
+
let done = false;
|
|
23
|
+
const wasRaw = input.isRaw === true; // the session's readline keeps stdin raw — restore THIS, don't force false
|
|
24
|
+
// Redraw the menu in place: move the cursor back up over the previous
|
|
25
|
+
// render (options + the one footer line), clear downward, reprint. The
|
|
26
|
+
// question above the menu stays put.
|
|
27
|
+
const lineCount = options.length + 1; // options, plus the hint footer
|
|
28
|
+
const draw = (first) => {
|
|
29
|
+
if (!first)
|
|
30
|
+
process.stdout.write(`\x1b[${lineCount}A`); // up to the menu's top
|
|
31
|
+
process.stdout.write(`\x1b[J${renderMenu(options, selected)}\n${MENU_HINT}\n`); // clear down, repaint + hint
|
|
32
|
+
};
|
|
33
|
+
const finish = (result) => {
|
|
34
|
+
if (done)
|
|
35
|
+
return;
|
|
36
|
+
done = true;
|
|
37
|
+
input.off("keypress", onKey); // stop listening
|
|
38
|
+
try {
|
|
39
|
+
input.setRawMode(wasRaw); // restore the prior mode — forcing false breaks the live readline → it closes → the REPL exits
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
/* not all streams support it; ignore */
|
|
43
|
+
}
|
|
44
|
+
rl.resume(); // the line reader takes over again
|
|
45
|
+
resolve(result);
|
|
46
|
+
};
|
|
47
|
+
const onKey = (_str, key) => {
|
|
48
|
+
if (!key || done)
|
|
49
|
+
return;
|
|
50
|
+
switch (key.name) {
|
|
51
|
+
case "up":
|
|
52
|
+
case "k":
|
|
53
|
+
selected = (selected - 1 + options.length) % options.length; // wrap around the top
|
|
54
|
+
draw(false);
|
|
55
|
+
break;
|
|
56
|
+
case "down":
|
|
57
|
+
case "j":
|
|
58
|
+
selected = (selected + 1) % options.length; // wrap around the bottom
|
|
59
|
+
draw(false);
|
|
60
|
+
break;
|
|
61
|
+
case "return":
|
|
62
|
+
case "enter":
|
|
63
|
+
finish(selected); // commit the choice
|
|
64
|
+
break;
|
|
65
|
+
case "escape":
|
|
66
|
+
finish(-1); // bail
|
|
67
|
+
break;
|
|
68
|
+
default:
|
|
69
|
+
if (key.ctrl && key.name === "c")
|
|
70
|
+
finish(-1); // Ctrl+C in raw mode arrives as a keypress, not a signal
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
try {
|
|
74
|
+
rl.pause(); // stop the line reader from turning our keystrokes into lines
|
|
75
|
+
if (input === process.stdin)
|
|
76
|
+
readline.emitKeypressEvents(input); // turn raw bytes into named keys (real stdin only)
|
|
77
|
+
input.setRawMode(true); // keys arrive immediately, no Enter needed
|
|
78
|
+
input.resume(); // CRUCIAL: rl.pause() paused the stream — resume it or no keypresses reach us (menu hangs)
|
|
79
|
+
input.on("keypress", onKey);
|
|
80
|
+
draw(true); // first paint
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
finish(-1); // anything unexpected in setup → cancel, caller falls back to typing
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
// The multi-question form (Day 30): the agent asks several questions at once,
|
|
88
|
+
// the user answers each and submits, and we hand back organized answers. Same
|
|
89
|
+
// raw-mode borrow/return dance as promptSelect; the state machine is in form.ts.
|
|
90
|
+
// Returns the answers, or null if cancelled / not a TTY.
|
|
91
|
+
export function promptForm(rl, questions, input = process.stdin) {
|
|
92
|
+
return new Promise((resolve) => {
|
|
93
|
+
if (!input.isTTY || !questions.length)
|
|
94
|
+
return resolve(null); // no form without a terminal or questions
|
|
95
|
+
let state = initFormState(questions);
|
|
96
|
+
let done = false;
|
|
97
|
+
const wasRaw = input.isRaw === true; // restore this on exit, not false (see promptSelect)
|
|
98
|
+
const lineCount = renderForm(questions, state).split("\n").length; // constant for these questions
|
|
99
|
+
const draw = (first) => {
|
|
100
|
+
if (!first)
|
|
101
|
+
process.stdout.write(`\x1b[${lineCount}A`); // up over the whole form
|
|
102
|
+
process.stdout.write(`\x1b[J${renderForm(questions, state)}\n`); // clear down, repaint
|
|
103
|
+
};
|
|
104
|
+
const finish = (result) => {
|
|
105
|
+
if (done)
|
|
106
|
+
return;
|
|
107
|
+
done = true;
|
|
108
|
+
input.off("keypress", onKey);
|
|
109
|
+
try {
|
|
110
|
+
input.setRawMode(wasRaw); // restore prior mode; forcing false breaks the live readline → REPL exits
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
/* ignore */
|
|
114
|
+
}
|
|
115
|
+
rl.resume();
|
|
116
|
+
resolve(result);
|
|
117
|
+
};
|
|
118
|
+
const onKey = (_str, key) => {
|
|
119
|
+
if (!key || done)
|
|
120
|
+
return;
|
|
121
|
+
if (key.name === "up" || key.name === "k") {
|
|
122
|
+
state = reduceForm(questions, state, "up").state;
|
|
123
|
+
draw(false);
|
|
124
|
+
}
|
|
125
|
+
else if (key.name === "down" || key.name === "j" || key.name === "tab") {
|
|
126
|
+
state = reduceForm(questions, state, "down").state;
|
|
127
|
+
draw(false);
|
|
128
|
+
}
|
|
129
|
+
else if (key.name === "return" || key.name === "enter" || key.name === "space") {
|
|
130
|
+
const next = reduceForm(questions, state, "select");
|
|
131
|
+
state = next.state;
|
|
132
|
+
if (next.done)
|
|
133
|
+
finish(collectAnswers(questions, state)); // submitted with all answered
|
|
134
|
+
else
|
|
135
|
+
draw(false);
|
|
136
|
+
}
|
|
137
|
+
else if (key.name === "escape" || (key.ctrl && key.name === "c")) {
|
|
138
|
+
finish(null); // cancel
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
try {
|
|
142
|
+
rl.pause();
|
|
143
|
+
if (input === process.stdin)
|
|
144
|
+
readline.emitKeypressEvents(input);
|
|
145
|
+
input.setRawMode(true);
|
|
146
|
+
input.resume(); // resume the stream rl.pause() paused, or no keypresses reach the form
|
|
147
|
+
input.on("keypress", onKey);
|
|
148
|
+
draw(true);
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
finish(null);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|