layermap 0.1.26 → 0.1.28
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 +6 -3
- package/analyzers/project-map-go-worker.mjs +1 -0
- package/analyzers/project-map-java/java-map.jar +0 -0
- package/analyzers/project-map-java-worker.mjs +1 -0
- package/analyzers/project-map-python-worker.mjs +1 -0
- package/analyzers/project-map-worker.mjs +428 -192
- package/dist/cli.mjs +415 -370
- package/package.json +1 -1
- package/pi/layermap.mjs +225 -243
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "layermap",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.28",
|
|
4
4
|
"mcpName": "io.github.coffeecoproject/layermap",
|
|
5
5
|
"description": "A layered map of a codebase for coding agents: directories, modules and call chains across TypeScript, JavaScript, Go, Python and Java.",
|
|
6
6
|
"license": "Apache-2.0",
|
package/pi/layermap.mjs
CHANGED
|
@@ -1,45 +1,194 @@
|
|
|
1
1
|
// Generated by scripts/plugins.ts from scripts/pi-extension.mjs.
|
|
2
|
-
//
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
// The map runs in LayerMap's own MCP server, started with Pi's Node in the session's directory; its
|
|
6
|
-
// tools become Pi tools of the same names. Like the Claude Code plugin's stop hook, the extension
|
|
7
|
-
// asks the agent once, before it settles, to check what its code edits affect.
|
|
8
|
-
import { execFile, spawn } from "node:child_process";
|
|
9
|
-
import { createHash } from "node:crypto";
|
|
10
|
-
import { readFile, stat } from "node:fs/promises";
|
|
11
|
-
import os from "node:os";
|
|
12
|
-
import path from "node:path";
|
|
2
|
+
// scripts/pi-extension.mjs
|
|
3
|
+
import { spawn } from "node:child_process";
|
|
4
|
+
import path2 from "node:path";
|
|
13
5
|
import { createInterface } from "node:readline";
|
|
14
6
|
import { fileURLToPath } from "node:url";
|
|
15
7
|
import { Type } from "typebox";
|
|
16
8
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
9
|
+
// src/agent/notes.ts
|
|
10
|
+
var AGENT_MAP_NOTE = "The layermap MCP server is available here. When a task needs to know what calls a function, what a change could affect beyond the code you are editing, or where code is used, use its project_explore_map tool (direction INCOMING, depth up to 8) instead of grep, then confirm in source; a change confined to code you have read does not need it. When the edits are done, call its project_check_changes tool once before reporting the work done.";
|
|
11
|
+
var AGENT_STOP_CHECK = "Code changed in this session. Before finishing, call the LayerMap tool project_check_changes: it lists the routes, jobs and commands the changes reach and the existing tests related to them. Check the affected entry points in source, and run the related tests if the change needs verifying. If it lists EXISTING TESTS CHANGED or code SHARED BY several areas, decide which of them the request is about; for the others, restore their tests and narrow the change. If it lists NEW MODULE DEPENDENCIES, check that each runs the way the project's layers do. If you already checked these changes, say so briefly and finish.";
|
|
12
|
+
var AGENT_STOP_CHECK_REPOSITORIES = "The changes are in more than one repository, or not in the one the session started in: call project_check_changes once for each, with its path set to the repository: ";
|
|
13
|
+
var projectHeading = (project) => `Project ${project} (paths are relative to it)`;
|
|
14
|
+
|
|
15
|
+
// src/agent/session-changes.ts
|
|
16
|
+
import { execFile } from "node:child_process";
|
|
17
|
+
import { createHash } from "node:crypto";
|
|
18
|
+
import { readFile, stat } from "node:fs/promises";
|
|
19
|
+
import os from "node:os";
|
|
20
|
+
import path from "node:path";
|
|
21
|
+
var CODE_EXTENSIONS = [
|
|
22
|
+
"ts",
|
|
23
|
+
"tsx",
|
|
24
|
+
"mts",
|
|
25
|
+
"cts",
|
|
26
|
+
"js",
|
|
27
|
+
"jsx",
|
|
28
|
+
"mjs",
|
|
29
|
+
"cjs",
|
|
30
|
+
"go",
|
|
31
|
+
"py",
|
|
32
|
+
"java",
|
|
33
|
+
"wxml"
|
|
34
|
+
];
|
|
35
|
+
var CODE_FILE = new RegExp(`\\.(?:${CODE_EXTENSIONS.join("|")})$`, "u");
|
|
36
|
+
var CODE_GLOBS = CODE_EXTENSIONS.map((extension) => `*.${extension}`);
|
|
37
|
+
var git = (cwd, args) => new Promise(
|
|
38
|
+
(resolve) => execFile(
|
|
39
|
+
"git",
|
|
40
|
+
["-C", cwd, ...args],
|
|
41
|
+
{ maxBuffer: 64 * 1024 * 1024, windowsHide: true },
|
|
42
|
+
(error, stdout) => resolve(error ? "" : stdout)
|
|
43
|
+
)
|
|
32
44
|
);
|
|
45
|
+
async function repositoryGit(repository, args) {
|
|
46
|
+
const drivers = new Set(
|
|
47
|
+
(await git(repository, ["config", "--name-only", "--get-regexp", "^filter\\."])).split("\n").flatMap((key) => /^filter\.(.+)\.[^.]+$/su.exec(key)?.[1] ?? [])
|
|
48
|
+
);
|
|
49
|
+
const blank = [...drivers].flatMap(
|
|
50
|
+
(driver) => ["clean=", "process=", "required=false"].flatMap((setting) => [
|
|
51
|
+
"-c",
|
|
52
|
+
`filter.${driver}.${setting}`
|
|
53
|
+
])
|
|
54
|
+
);
|
|
55
|
+
return git(repository, [
|
|
56
|
+
"-c",
|
|
57
|
+
"core.fsmonitor=false",
|
|
58
|
+
"-c",
|
|
59
|
+
"core.quotePath=false",
|
|
60
|
+
...blank,
|
|
61
|
+
...args
|
|
62
|
+
]);
|
|
63
|
+
}
|
|
64
|
+
var isDirectory = async (given) => Boolean((await stat(given).catch(() => void 0))?.isDirectory());
|
|
65
|
+
async function existingDirectory(given) {
|
|
66
|
+
let directory = given;
|
|
67
|
+
while (!await isDirectory(directory)) {
|
|
68
|
+
if (path.dirname(directory) === directory) return void 0;
|
|
69
|
+
directory = path.dirname(directory);
|
|
70
|
+
}
|
|
71
|
+
return directory;
|
|
72
|
+
}
|
|
73
|
+
async function repositoryAt(given) {
|
|
74
|
+
const directory = await existingDirectory(given);
|
|
75
|
+
return directory ? (await git(directory, ["rev-parse", "--show-toplevel"])).trim() : "";
|
|
76
|
+
}
|
|
77
|
+
async function codeEdits(files) {
|
|
78
|
+
const located = /* @__PURE__ */ new Map();
|
|
79
|
+
const locate = async (directory) => {
|
|
80
|
+
const [repository, prefix = ""] = (await git(directory, ["rev-parse", "--show-toplevel", "--show-prefix"])).split("\n");
|
|
81
|
+
return repository ? { repository, prefix } : void 0;
|
|
82
|
+
};
|
|
83
|
+
const edits = [];
|
|
84
|
+
for (const file of new Set(files)) {
|
|
85
|
+
if (!CODE_FILE.test(file)) continue;
|
|
86
|
+
const directory = await existingDirectory(path.dirname(file));
|
|
87
|
+
if (!directory) continue;
|
|
88
|
+
if (!located.has(directory)) located.set(directory, locate(directory));
|
|
89
|
+
const where = await located.get(directory);
|
|
90
|
+
if (!where) continue;
|
|
91
|
+
const rest = path.relative(directory, file).split(path.sep).join("/");
|
|
92
|
+
edits.push({ kind: "edit", repository: where.repository, file: `${where.prefix}${rest}` });
|
|
93
|
+
}
|
|
94
|
+
return edits;
|
|
95
|
+
}
|
|
96
|
+
async function commandFiles(cwd, command) {
|
|
97
|
+
const words = [
|
|
98
|
+
...new Set(
|
|
99
|
+
command.split(/[^A-Za-z0-9._/\\~:@+,%-]+/u).map((word) => word.replace(/[,:]+$/u, "")).filter(Boolean)
|
|
100
|
+
)
|
|
101
|
+
];
|
|
102
|
+
const resolve = (base, word) => word.startsWith("~/") ? path.join(os.homedir(), word.slice(2)) : path.resolve(base, word);
|
|
103
|
+
const bases = [cwd];
|
|
104
|
+
for (const word of words.filter((word2) => !CODE_FILE.test(word2)).slice(0, 300)) {
|
|
105
|
+
if (bases.length > 20) break;
|
|
106
|
+
const named = resolve(cwd, word);
|
|
107
|
+
if (!bases.includes(named) && await isDirectory(named)) bases.push(named);
|
|
108
|
+
}
|
|
109
|
+
const files = /* @__PURE__ */ new Set();
|
|
110
|
+
for (const word of words.filter((word2) => CODE_FILE.test(word2)))
|
|
111
|
+
for (const base of bases) {
|
|
112
|
+
const file = resolve(base, word);
|
|
113
|
+
if (files.size < 200 && await isDirectory(path.dirname(file))) files.add(file);
|
|
114
|
+
}
|
|
115
|
+
return [...files];
|
|
116
|
+
}
|
|
117
|
+
var hash = (content) => createHash("sha256").update(content).digest("hex");
|
|
118
|
+
async function fileStates(files) {
|
|
119
|
+
const states = {};
|
|
120
|
+
for (const file of files) {
|
|
121
|
+
const content = await readFile(file).catch(() => void 0);
|
|
122
|
+
states[file] = content ? hash(content) : "";
|
|
123
|
+
}
|
|
124
|
+
return states;
|
|
125
|
+
}
|
|
126
|
+
var changedFiles = (before, after) => Object.keys(before).filter((file) => before[file] !== after[file]);
|
|
127
|
+
async function changedCode(repository) {
|
|
128
|
+
if (!await git(repository, ["rev-parse", "--verify", "--quiet", "HEAD"])) return /* @__PURE__ */ new Set();
|
|
129
|
+
const listed = await Promise.all([
|
|
130
|
+
repositoryGit(repository, ["diff", "HEAD", "--name-only", "--no-renames", "--", ...CODE_GLOBS]),
|
|
131
|
+
repositoryGit(repository, ["ls-files", "--others", "--exclude-standard", "--", ...CODE_GLOBS])
|
|
132
|
+
]);
|
|
133
|
+
return new Set(listed.join("\n").split("\n").filter(Boolean));
|
|
134
|
+
}
|
|
135
|
+
async function checkRequest(changes, start, asked) {
|
|
136
|
+
const pending = /* @__PURE__ */ new Map();
|
|
137
|
+
for (const change of changes) {
|
|
138
|
+
if (change.kind === "check") pending.delete(change.repository);
|
|
139
|
+
else
|
|
140
|
+
pending.set(
|
|
141
|
+
change.repository,
|
|
142
|
+
(pending.get(change.repository) ?? /* @__PURE__ */ new Set()).add(change.file)
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
const due = [];
|
|
146
|
+
for (const [repository, files] of [...pending].sort(([a], [b]) => a < b ? -1 : 1)) {
|
|
147
|
+
const changed = await changedCode(repository);
|
|
148
|
+
const still = [...files].filter((file) => changed.has(file)).sort();
|
|
149
|
+
if (still.length)
|
|
150
|
+
due.push({
|
|
151
|
+
repository,
|
|
152
|
+
states: await fileStates(still.map((file) => path.join(repository, file)))
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
const fingerprint = due.length ? hash(JSON.stringify(due)) : "";
|
|
156
|
+
if (!fingerprint || fingerprint === asked) return void 0;
|
|
157
|
+
const elsewhere = due.some(({ repository }) => repository !== start);
|
|
158
|
+
return {
|
|
159
|
+
text: elsewhere ? `${AGENT_STOP_CHECK} ${AGENT_STOP_CHECK_REPOSITORIES}${due.map(({ repository }) => repository).join(", ")}.` : AGENT_STOP_CHECK,
|
|
160
|
+
fingerprint
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
var [HEADING_START, HEADING_END] = projectHeading("\0").split("\0");
|
|
164
|
+
function checkedRepository(result, depth = 0) {
|
|
165
|
+
if (typeof result === "string") {
|
|
166
|
+
const line = result.split("\n", 1)[0] ?? "";
|
|
167
|
+
return line.startsWith(HEADING_START) && line.endsWith(HEADING_END) ? line.slice(HEADING_START.length, line.length - HEADING_END.length) : void 0;
|
|
168
|
+
}
|
|
169
|
+
if (!result || typeof result !== "object" || depth > 3) return void 0;
|
|
170
|
+
for (const value of Object.values(result)) {
|
|
171
|
+
const repository = checkedRepository(value, depth + 1);
|
|
172
|
+
if (repository) return repository;
|
|
173
|
+
}
|
|
174
|
+
return void 0;
|
|
175
|
+
}
|
|
33
176
|
|
|
34
|
-
//
|
|
35
|
-
|
|
177
|
+
// scripts/pi-extension.mjs
|
|
178
|
+
var CHECK_TOOL = "project_check_changes";
|
|
179
|
+
var CLI = path2.join(path2.dirname(fileURLToPath(import.meta.url)), "..", "dist", "cli.mjs");
|
|
180
|
+
var READS = /* @__PURE__ */ new Set(["read", "grep", "find", "ls"]);
|
|
181
|
+
var WRITES = /* @__PURE__ */ new Set(["edit", "write"]);
|
|
182
|
+
var PATCH = "apply_patch";
|
|
183
|
+
var PATCH_FILES = /^\*\*\* (?:(?:Add|Update|Delete) File|Move to): (.+)$/gm;
|
|
184
|
+
var MapServer = class {
|
|
36
185
|
constructor(cwd) {
|
|
37
|
-
this.pending = new Map();
|
|
186
|
+
this.pending = /* @__PURE__ */ new Map();
|
|
38
187
|
this.next = 1;
|
|
39
188
|
this.child = spawn(process.execPath, [CLI, "mcp"], {
|
|
40
189
|
cwd,
|
|
41
190
|
stdio: ["pipe", "pipe", "ignore"],
|
|
42
|
-
windowsHide: true
|
|
191
|
+
windowsHide: true
|
|
43
192
|
});
|
|
44
193
|
this.exited = new Promise((resolve) => this.child.once("exit", resolve));
|
|
45
194
|
this.child.once("error", (error) => this.fail(error));
|
|
@@ -64,7 +213,8 @@ class MapServer {
|
|
|
64
213
|
this.dead = true;
|
|
65
214
|
}
|
|
66
215
|
send(message) {
|
|
67
|
-
this.child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", ...message })}
|
|
216
|
+
this.child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", ...message })}
|
|
217
|
+
`);
|
|
68
218
|
}
|
|
69
219
|
request(method, params, signal) {
|
|
70
220
|
if (this.dead) return Promise.reject(new Error("The LayerMap server is not running."));
|
|
@@ -78,7 +228,7 @@ class MapServer {
|
|
|
78
228
|
this.send({ method: "notifications/cancelled", params: { requestId: id } });
|
|
79
229
|
reject(new Error("Cancelled."));
|
|
80
230
|
},
|
|
81
|
-
{ once: true }
|
|
231
|
+
{ once: true }
|
|
82
232
|
);
|
|
83
233
|
this.send({ id, method, params });
|
|
84
234
|
});
|
|
@@ -87,155 +237,38 @@ class MapServer {
|
|
|
87
237
|
await this.request("initialize", {
|
|
88
238
|
protocolVersion: "2025-06-18",
|
|
89
239
|
capabilities: {},
|
|
90
|
-
clientInfo: { name: "layermap-pi", version }
|
|
240
|
+
clientInfo: { name: "layermap-pi", version }
|
|
91
241
|
});
|
|
92
|
-
// The server starts building the map as soon as it is told the client is ready.
|
|
93
242
|
this.send({ method: "notifications/initialized" });
|
|
94
243
|
return (await this.request("tools/list", {})).tools;
|
|
95
244
|
}
|
|
96
245
|
async stop() {
|
|
97
246
|
this.child.stdin.end();
|
|
98
|
-
const timer = setTimeout(() => this.child.kill(),
|
|
247
|
+
const timer = setTimeout(() => this.child.kill(), 3e3);
|
|
99
248
|
await this.exited;
|
|
100
249
|
clearTimeout(timer);
|
|
101
250
|
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const git = (cwd, args) =>
|
|
105
|
-
new Promise((resolve) =>
|
|
106
|
-
execFile("git", ["-C", cwd, ...args], { maxBuffer: 64 * 1024 * 1024 }, (error, stdout) =>
|
|
107
|
-
resolve(error ? "" : stdout),
|
|
108
|
-
),
|
|
109
|
-
);
|
|
110
|
-
|
|
111
|
-
// Git in a repository with no program its own config names: no fsmonitor, and no filter its
|
|
112
|
-
// attributes would put a changed file's content through.
|
|
113
|
-
async function repositoryGit(repository, args) {
|
|
114
|
-
const drivers = new Set(
|
|
115
|
-
(await git(repository, ["config", "--name-only", "--get-regexp", "^filter\\."]))
|
|
116
|
-
.split("\n")
|
|
117
|
-
.flatMap((key) => /^filter\.(.+)\.[^.]+$/su.exec(key)?.[1] ?? []),
|
|
118
|
-
);
|
|
119
|
-
const blank = [...drivers].flatMap((driver) =>
|
|
120
|
-
["clean=", "process=", "required=false"].flatMap((setting) => [
|
|
121
|
-
"-c",
|
|
122
|
-
`filter.${driver}.${setting}`,
|
|
123
|
-
]),
|
|
124
|
-
);
|
|
125
|
-
return git(repository, [
|
|
126
|
-
"-c",
|
|
127
|
-
"core.fsmonitor=false",
|
|
128
|
-
"-c",
|
|
129
|
-
"core.quotePath=false",
|
|
130
|
-
...blank,
|
|
131
|
-
...args,
|
|
132
|
-
]);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// The Git repository around a path, or the nearest directory of it that exists; empty outside one.
|
|
136
|
-
async function repositoryAt(given) {
|
|
137
|
-
let directory = given;
|
|
138
|
-
while (!(await stat(directory).catch(() => undefined))?.isDirectory()) {
|
|
139
|
-
if (path.dirname(directory) === directory) return "";
|
|
140
|
-
directory = path.dirname(directory);
|
|
141
|
-
}
|
|
142
|
-
return (await git(directory, ["rev-parse", "--show-toplevel"])).trim();
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// The files a tool call names to change, relative to the session's directory.
|
|
251
|
+
};
|
|
146
252
|
function namedFiles({ toolName, input }) {
|
|
147
253
|
if (WRITES.has(toolName)) return typeof input?.path === "string" ? [input.path] : [];
|
|
148
254
|
if (toolName === PATCH && typeof input?.patch === "string")
|
|
149
255
|
return [...input.patch.matchAll(PATCH_FILES)].map((match) => match[1].trim());
|
|
150
256
|
return [];
|
|
151
257
|
}
|
|
152
|
-
|
|
153
|
-
// The repository LayerMap's server routes a tool call's path to: a relative path that stays in the
|
|
154
|
-
// repository the session started in is that one's, and any other is resolved from where it started.
|
|
155
|
-
function routedRepository(start, given) {
|
|
156
|
-
const normal = path.normalize(given || ".");
|
|
157
|
-
const escapes = normal === ".." || normal.startsWith(`..${path.sep}`);
|
|
158
|
-
if (start.git && !path.isAbsolute(normal) && !escapes) return Promise.resolve(start.directory);
|
|
159
|
-
return repositoryAt(path.resolve(start.directory, normal));
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// A repository's uncommitted code files, each with a hash of its content ("" once deleted); empty
|
|
163
|
-
// without a commit.
|
|
164
|
-
async function changedCode(repository) {
|
|
165
|
-
const changed = new Map();
|
|
166
|
-
if (!(await git(repository, ["rev-parse", "--verify", "--quiet", "HEAD"]))) return changed;
|
|
167
|
-
const listed = await Promise.all([
|
|
168
|
-
repositoryGit(repository, ["diff", "HEAD", "--name-only", "--no-renames", "--", ...CODE_GLOBS]),
|
|
169
|
-
repositoryGit(repository, ["ls-files", "--others", "--exclude-standard", "--", ...CODE_GLOBS]),
|
|
170
|
-
]);
|
|
171
|
-
const files = [...new Set(listed.join("\n").split("\n").filter(Boolean))].sort();
|
|
172
|
-
for (const file of files) {
|
|
173
|
-
const content = await readFile(path.join(repository, file)).catch(() => undefined);
|
|
174
|
-
changed.set(file, content ? createHash("sha256").update(content).digest("hex") : "");
|
|
175
|
-
}
|
|
176
|
-
return changed;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// The changes as one hash, empty when there are none.
|
|
180
|
-
const fingerprint = (changed) =>
|
|
181
|
-
changed.size
|
|
182
|
-
? createHash("sha256")
|
|
183
|
-
.update(JSON.stringify([...changed]))
|
|
184
|
-
.digest("hex")
|
|
185
|
-
: "";
|
|
186
|
-
|
|
187
|
-
// How long a command runs (ms) before only the files it names count as its changes.
|
|
188
|
-
const LONG_COMMAND = 10_000;
|
|
189
|
-
|
|
190
|
-
// The folders a shell command names that exist, relative to the directory it runs in.
|
|
191
|
-
async function namedFolders(cwd, command) {
|
|
192
|
-
const folders = new Set();
|
|
193
|
-
for (const word of command.split(/[^A-Za-z0-9._/~:@+,%-]+/).slice(0, 300)) {
|
|
194
|
-
if (!word) continue;
|
|
195
|
-
const named = word.startsWith("~/")
|
|
196
|
-
? path.join(os.homedir(), word.slice(2))
|
|
197
|
-
: path.resolve(cwd, word);
|
|
198
|
-
for (const folder of [named, path.dirname(named)])
|
|
199
|
-
if ((await stat(folder).catch(() => undefined))?.isDirectory()) {
|
|
200
|
-
folders.add(folder);
|
|
201
|
-
break;
|
|
202
|
-
}
|
|
203
|
-
if (folders.size >= 20) break;
|
|
204
|
-
}
|
|
205
|
-
return folders;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
export default function layermap(pi) {
|
|
258
|
+
function layermap(pi) {
|
|
209
259
|
let server;
|
|
210
260
|
let starting;
|
|
211
261
|
let cwd;
|
|
212
|
-
const registered = new Set();
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
// edit, write or patch names, and those in which any other tool that can change files (bash, …)
|
|
218
|
-
// changed code files while it ran, among the repository the session started in, those of the
|
|
219
|
-
// folders its command names and those already edited.
|
|
220
|
-
// It compares a repository's uncommitted code changes with those it had before the session's
|
|
221
|
-
// first such tool there and those the agent last checked in it, and all of them with those it
|
|
222
|
-
// was last asked about; changes another session makes while this one reads do not count. A
|
|
223
|
-
// command that ran longer than ten seconds (tests, builds, scripts) leaves time for another
|
|
224
|
-
// session to change the same work tree, so only the files it names count; one that writes code
|
|
225
|
-
// without naming the file, such as a generator or formatter, takes a few seconds.
|
|
226
|
-
const edited = new Set();
|
|
227
|
-
const before = new Map();
|
|
228
|
-
const checked = new Map();
|
|
229
|
-
// The code changes of each repository a running tool may change, when it started and its
|
|
230
|
-
// command, by the tool call's id.
|
|
231
|
-
const running = new Map();
|
|
232
|
-
let askedAbout = "";
|
|
233
|
-
|
|
262
|
+
const registered = /* @__PURE__ */ new Set();
|
|
263
|
+
let start = "";
|
|
264
|
+
const changes = [];
|
|
265
|
+
const running = /* @__PURE__ */ new Map();
|
|
266
|
+
let asked = "";
|
|
234
267
|
const ensureServer = async () => {
|
|
235
268
|
if (server && !server.dead) return server;
|
|
236
269
|
starting ??= (async () => {
|
|
237
270
|
const next = new MapServer(cwd);
|
|
238
|
-
const tools = await next.start("0.1.
|
|
271
|
+
const tools = await next.start("0.1.28");
|
|
239
272
|
for (const tool of tools) {
|
|
240
273
|
if (registered.has(tool.name)) continue;
|
|
241
274
|
registered.add(tool.name);
|
|
@@ -244,129 +277,78 @@ export default function layermap(pi) {
|
|
|
244
277
|
label: tool.title ?? tool.name,
|
|
245
278
|
description: tool.description,
|
|
246
279
|
promptSnippet: tool.title,
|
|
247
|
-
promptGuidelines: tool.name === "project_explore_map" ? [
|
|
280
|
+
promptGuidelines: tool.name === "project_explore_map" ? [AGENT_MAP_NOTE] : [],
|
|
248
281
|
parameters: Type.Unsafe(tool.inputSchema),
|
|
249
282
|
async execute(_id, params, signal) {
|
|
250
|
-
const
|
|
251
|
-
const result = await
|
|
283
|
+
const running2 = await ensureServer();
|
|
284
|
+
const result = await running2.request(
|
|
252
285
|
"tools/call",
|
|
253
286
|
{ name: tool.name, arguments: params ?? {} },
|
|
254
|
-
signal
|
|
287
|
+
signal
|
|
255
288
|
);
|
|
256
289
|
const text = (result.content ?? []).map((part) => part.text ?? "").join("\n");
|
|
257
290
|
if (result.isError) throw new Error(text);
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
if (repository) checked.set(repository, fingerprint(await changedCode(repository)));
|
|
261
|
-
}
|
|
291
|
+
const repository = tool.name === CHECK_TOOL && checkedRepository(text);
|
|
292
|
+
if (repository) changes.push({ kind: "check", repository });
|
|
262
293
|
return { content: [{ type: "text", text }], details: {} };
|
|
263
|
-
}
|
|
294
|
+
}
|
|
264
295
|
});
|
|
265
296
|
}
|
|
266
297
|
server = next;
|
|
267
298
|
return next;
|
|
268
299
|
})().finally(() => {
|
|
269
|
-
starting =
|
|
300
|
+
starting = void 0;
|
|
270
301
|
});
|
|
271
302
|
return starting;
|
|
272
303
|
};
|
|
273
|
-
|
|
274
304
|
pi.on("session_start", async (_event, ctx) => {
|
|
275
305
|
cwd = ctx.cwd;
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
edited.clear();
|
|
279
|
-
before.clear();
|
|
280
|
-
checked.clear();
|
|
306
|
+
start = await repositoryAt(cwd);
|
|
307
|
+
changes.length = 0;
|
|
281
308
|
running.clear();
|
|
282
|
-
|
|
283
|
-
await ensureServer().catch(
|
|
284
|
-
ctx.ui?.notify?.(`LayerMap could not start: ${error.message}`, "warning")
|
|
309
|
+
asked = "";
|
|
310
|
+
await ensureServer().catch(
|
|
311
|
+
(error) => ctx.ui?.notify?.(`LayerMap could not start: ${error.message}`, "warning")
|
|
285
312
|
);
|
|
286
313
|
});
|
|
287
|
-
|
|
288
314
|
pi.on("tool_call", async (event) => {
|
|
289
315
|
if (READS.has(event.toolName) || registered.has(event.toolName) || !cwd) return;
|
|
290
|
-
const files = namedFiles(event);
|
|
291
|
-
if (files.length) {
|
|
292
|
-
const repositories = await Promise.all(
|
|
293
|
-
files.map((file) => repositoryAt(path.resolve(cwd, file))),
|
|
294
|
-
);
|
|
295
|
-
for (const repository of new Set(repositories)) {
|
|
296
|
-
if (!repository || edited.has(repository)) continue;
|
|
297
|
-
edited.add(repository);
|
|
298
|
-
before.set(repository, fingerprint(await changedCode(repository)));
|
|
299
|
-
}
|
|
300
|
-
return;
|
|
301
|
-
}
|
|
302
316
|
const command = event.input?.command;
|
|
303
|
-
|
|
304
|
-
const
|
|
305
|
-
|
|
306
|
-
...(await Promise.all([...folders].map(repositoryAt))),
|
|
307
|
-
...edited,
|
|
308
|
-
]);
|
|
309
|
-
const changes = new Map();
|
|
310
|
-
for (const repository of repositories)
|
|
311
|
-
if (repository) changes.set(repository, await changedCode(repository));
|
|
312
|
-
running.set(event.toolCallId, {
|
|
313
|
-
changes,
|
|
314
|
-
started: Date.now(),
|
|
315
|
-
command: typeof command === "string" ? command : "",
|
|
316
|
-
});
|
|
317
|
+
if (namedFiles(event).length || typeof command !== "string") return;
|
|
318
|
+
const files = await commandFiles(cwd, command);
|
|
319
|
+
if (files.length) running.set(event.toolCallId, await fileStates(files));
|
|
317
320
|
});
|
|
318
|
-
|
|
319
321
|
pi.on("tool_result", async (event) => {
|
|
320
|
-
|
|
321
|
-
|
|
322
|
+
if (!cwd) return;
|
|
323
|
+
const before = running.get(event.toolCallId);
|
|
322
324
|
running.delete(event.toolCallId);
|
|
323
|
-
const
|
|
324
|
-
|
|
325
|
-
if (edited.has(repository)) continue;
|
|
326
|
-
const now = await changedCode(repository);
|
|
327
|
-
if (
|
|
328
|
-
[...now].some(
|
|
329
|
-
([file, content]) =>
|
|
330
|
-
earlier.get(file) !== content && (!long || call.command.includes(path.basename(file))),
|
|
331
|
-
)
|
|
332
|
-
) {
|
|
333
|
-
edited.add(repository);
|
|
334
|
-
before.set(repository, fingerprint(earlier));
|
|
335
|
-
}
|
|
336
|
-
}
|
|
325
|
+
const files = before ? changedFiles(before, await fileStates(Object.keys(before))) : event.isError ? [] : namedFiles(event).map((file) => path2.resolve(cwd, file));
|
|
326
|
+
changes.push(...await codeEdits(files));
|
|
337
327
|
});
|
|
338
|
-
|
|
339
328
|
pi.on("agent_before_settle", async (event) => {
|
|
340
329
|
if (process.env.LAYERMAP_STOP_CHECK === "0") return;
|
|
341
|
-
const
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
if (changes && changes !== before.get(repository) && changes !== checked.get(repository))
|
|
345
|
-
due.push({ repository, changes });
|
|
346
|
-
}
|
|
347
|
-
const asked = JSON.stringify(due);
|
|
348
|
-
if (!due.length || asked === askedAbout) return;
|
|
349
|
-
askedAbout = asked;
|
|
350
|
-
const elsewhere = due.some(({ repository }) => !start.git || repository !== start.directory);
|
|
330
|
+
const request = await checkRequest(changes, start, asked);
|
|
331
|
+
if (!request) return;
|
|
332
|
+
asked = request.fingerprint;
|
|
351
333
|
return {
|
|
352
334
|
entries: [
|
|
353
335
|
...event.entries,
|
|
354
336
|
{
|
|
355
337
|
type: "custom_message",
|
|
356
338
|
customType: "layermap-check",
|
|
357
|
-
content:
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
display: false,
|
|
361
|
-
},
|
|
339
|
+
content: request.text,
|
|
340
|
+
display: false
|
|
341
|
+
}
|
|
362
342
|
],
|
|
363
|
-
continue: true
|
|
343
|
+
continue: true
|
|
364
344
|
};
|
|
365
345
|
});
|
|
366
|
-
|
|
367
346
|
pi.on("session_shutdown", async () => {
|
|
368
|
-
const
|
|
369
|
-
server =
|
|
370
|
-
await
|
|
347
|
+
const running2 = server;
|
|
348
|
+
server = void 0;
|
|
349
|
+
await running2?.stop();
|
|
371
350
|
});
|
|
372
351
|
}
|
|
352
|
+
export {
|
|
353
|
+
layermap as default
|
|
354
|
+
};
|