@koneeric/onetool 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +68 -2
- package/bin/onetool.js +4 -0
- package/core/src/index.js +732 -0
- package/guide/pages/INSTALL.md +636 -0
- package/guide/pages/settings.example.json +6 -0
- package/hosts/src/adapters/claude-code.js +136 -0
- package/hosts/src/index.js +107 -0
- package/library/src/index.js +544 -0
- package/package.json +35 -4
- package/practice/src/rows.js +15 -0
- package/practice/src/server.js +162 -0
- package/sandbox/src/child.mjs +421 -0
- package/sandbox/src/index.js +300 -0
- package/sandbox/src/last-expression.mjs +134 -0
- package/sandbox/src/probe.mjs +42 -0
- package/server/src/connect.js +128 -0
- package/server/src/descriptions.js +61 -0
- package/server/src/host.js +116 -0
- package/server/src/library.js +207 -0
- package/server/src/main.js +106 -0
- package/server/src/prompt.js +15 -0
- package/server/src/record.js +131 -0
- package/server/src/tools.js +461 -0
- package/shell/src/index.js +270 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// What onetool knows of the host program: its roots (the folders of the shell) and its own permission rules.
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import { RootsListChangedNotificationSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
4
|
+
import { rootFolders } from "../../core/src/index.js";
|
|
5
|
+
import { missingFolders } from "../../shell/src/index.js";
|
|
6
|
+
import { hostRules } from "../../hosts/src/index.js";
|
|
7
|
+
|
|
8
|
+
const say = (line) => process.stderr.write(`${line}\n`);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The host's side of one session. `server` is the `Server` that speaks to the host. Readings of the roots are made one
|
|
12
|
+
* after the other; `idle()` resolves when none is in progress or waits for its turn.
|
|
13
|
+
*/
|
|
14
|
+
export function makeHost(settings, server) {
|
|
15
|
+
const shell = settings.shell;
|
|
16
|
+
const wanted = settings.hostRules && (shell !== null || settings.hostServers.length > 0);
|
|
17
|
+
const needsFolders = shell !== null && shell.folders === null;
|
|
18
|
+
let folders = [];
|
|
19
|
+
let rules = null;
|
|
20
|
+
let begun = false;
|
|
21
|
+
let tail = Promise.resolve();
|
|
22
|
+
const unreadableTold = new Set();
|
|
23
|
+
|
|
24
|
+
const hasFolders = () => shell !== null && inEffect().length > 0;
|
|
25
|
+
const inEffect = () => (shell === null ? [] : (shell.folders ?? folders));
|
|
26
|
+
|
|
27
|
+
async function read() {
|
|
28
|
+
let found = [];
|
|
29
|
+
try {
|
|
30
|
+
const answer = await server.listRoots(undefined, { timeout: settings.connectSeconds * 1000 });
|
|
31
|
+
const list = rootFolders(answer.roots);
|
|
32
|
+
const missing = new Set(await missingFolders(list));
|
|
33
|
+
found = list.filter((_, position) => !missing.has(position));
|
|
34
|
+
} catch {
|
|
35
|
+
found = [];
|
|
36
|
+
}
|
|
37
|
+
folders = found;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function enqueue(task) {
|
|
41
|
+
tail = tail.then(task, task);
|
|
42
|
+
return tail;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function readRoots(first) {
|
|
46
|
+
if (!(needsFolders || rules !== null) || server.getClientCapabilities()?.roots === undefined) return;
|
|
47
|
+
const before = hasFolders();
|
|
48
|
+
await read();
|
|
49
|
+
if (!first && needsFolders && hasFolders() !== before) {
|
|
50
|
+
try {
|
|
51
|
+
await server.sendToolListChanged();
|
|
52
|
+
} catch {
|
|
53
|
+
// a failure of the sending is ignored
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function first() {
|
|
59
|
+
if (wanted) {
|
|
60
|
+
try {
|
|
61
|
+
rules = await hostRules(server.getClientVersion()?.name, { home: os.homedir() });
|
|
62
|
+
} catch {
|
|
63
|
+
rules = null;
|
|
64
|
+
}
|
|
65
|
+
if (rules === null) say("onetool: host rules: this host program is not known, onetool's own rules only");
|
|
66
|
+
}
|
|
67
|
+
await readRoots(true);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const self = {
|
|
71
|
+
/** The host's rules, or `null` when they are not in use. */
|
|
72
|
+
get rules() {
|
|
73
|
+
return rules;
|
|
74
|
+
},
|
|
75
|
+
/** The folders in effect, and whether the shell has some. */
|
|
76
|
+
folders: inEffect,
|
|
77
|
+
hasFolders,
|
|
78
|
+
/** Begins the first reading, once. */
|
|
79
|
+
start() {
|
|
80
|
+
if (begun) return;
|
|
81
|
+
begun = true;
|
|
82
|
+
void enqueue(first);
|
|
83
|
+
},
|
|
84
|
+
/** Resolves when no reading is in progress or waiting for its turn. */
|
|
85
|
+
async idle() {
|
|
86
|
+
let seen;
|
|
87
|
+
do {
|
|
88
|
+
seen = tail;
|
|
89
|
+
await seen;
|
|
90
|
+
} while (seen !== tail);
|
|
91
|
+
},
|
|
92
|
+
/** The host's files are read again, for a program about to start. */
|
|
93
|
+
async refresh() {
|
|
94
|
+
if (rules === null) return;
|
|
95
|
+
let unreadable = [];
|
|
96
|
+
try {
|
|
97
|
+
unreadable = await rules.refresh(folders);
|
|
98
|
+
} catch {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
for (const file of unreadable) {
|
|
102
|
+
if (unreadableTold.has(file)) continue;
|
|
103
|
+
unreadableTold.add(file);
|
|
104
|
+
say(`onetool: host rules: ${file} cannot be read, its rules are not applied`);
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
server.oninitialized = () => self.start();
|
|
110
|
+
server.setNotificationHandler(RootsListChangedNotificationSchema, () => {
|
|
111
|
+
if (!begun) return self.start();
|
|
112
|
+
void enqueue(() => readRoots(false));
|
|
113
|
+
return undefined;
|
|
114
|
+
});
|
|
115
|
+
return self;
|
|
116
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
// What onetool does with the library of saved programs: reading both folders, a save, a removal, the uses and the notes.
|
|
2
|
+
import { appendFile, mkdir, open } from "node:fs/promises";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import { archivedNote, sameAsNote, saveItNote } from "../../core/src/index.js";
|
|
6
|
+
import {
|
|
7
|
+
callSequence,
|
|
8
|
+
formatProgram,
|
|
9
|
+
joinLibraries,
|
|
10
|
+
noteUses,
|
|
11
|
+
readLibrary,
|
|
12
|
+
readSequences,
|
|
13
|
+
removeProgram,
|
|
14
|
+
reportText,
|
|
15
|
+
restoreProgram,
|
|
16
|
+
saveDecision,
|
|
17
|
+
saveProgram,
|
|
18
|
+
sequenceLine,
|
|
19
|
+
} from "../../library/src/index.js";
|
|
20
|
+
|
|
21
|
+
const say = (line) => process.stderr.write(`${line}\n`);
|
|
22
|
+
|
|
23
|
+
const FAILURES = 5;
|
|
24
|
+
const SAVE_IT_AT = 3;
|
|
25
|
+
|
|
26
|
+
// the last bytes of the file of the sequences that the report reads
|
|
27
|
+
const SEQUENCES_READ = 2000000;
|
|
28
|
+
|
|
29
|
+
const folderIn = (base) => join(base, ".onetool", "library");
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The library of the process. `host` gives the folders in effect; `session` is the text of the lines of the log and
|
|
33
|
+
* `failed()` tells, once, that a line cannot be written. Every reading and every writing is done in its turn, after the one
|
|
34
|
+
* before it has ended, so that a reading never meets a half-written file.
|
|
35
|
+
*/
|
|
36
|
+
export function makeLibrary(settings, host, { session, failed }) {
|
|
37
|
+
const userFolder = folderIn(os.homedir());
|
|
38
|
+
const sequencesFile = join(dirname(userFolder), "sequences.jsonl");
|
|
39
|
+
const toldBad = new Set();
|
|
40
|
+
const sequences = new Map();
|
|
41
|
+
let tail = Promise.resolve();
|
|
42
|
+
|
|
43
|
+
/** `fn` runs after every work queued before it has ended; resolves or rejects as `fn` does. */
|
|
44
|
+
function enqueue(fn) {
|
|
45
|
+
const run = tail.then(fn);
|
|
46
|
+
tail = run.then(
|
|
47
|
+
() => undefined,
|
|
48
|
+
() => undefined,
|
|
49
|
+
);
|
|
50
|
+
return run;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const projectFolder = () => {
|
|
54
|
+
const folders = host.folders();
|
|
55
|
+
return host.hasFolders() && folders.length > 0 ? folderIn(folders[0]) : null;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const told = (bad) => {
|
|
59
|
+
for (const path of bad) {
|
|
60
|
+
if (toldBad.has(path)) continue;
|
|
61
|
+
toldBad.add(path);
|
|
62
|
+
say(`onetool: library: ${path} is not a saved program, it is left out`);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/** One folder read, its bad files told. */
|
|
67
|
+
async function readFolder(folder) {
|
|
68
|
+
if (folder === null) return [];
|
|
69
|
+
const { programs, bad } = await readLibrary(folder);
|
|
70
|
+
told(bad);
|
|
71
|
+
return programs;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The library of the moment, and the project's folder it was read with (`null` when there is none). */
|
|
75
|
+
const read = () =>
|
|
76
|
+
enqueue(async () => {
|
|
77
|
+
const project = projectFolder();
|
|
78
|
+
const programs = joinLibraries(await readFolder(project), await readFolder(userFolder));
|
|
79
|
+
return { programs, project, user: userFolder };
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
/** `library.save`: `program` and `replace` are what `checkSave` accepted. */
|
|
83
|
+
const save = (program, replace) =>
|
|
84
|
+
enqueue(async () => {
|
|
85
|
+
const project = projectFolder();
|
|
86
|
+
const target = project ?? userFolder;
|
|
87
|
+
const saved = await readFolder(target);
|
|
88
|
+
const decision = saveDecision(saved, program, { replace, cap: settings.libraryCap });
|
|
89
|
+
if (!decision.save) throw new Error(decision.problem);
|
|
90
|
+
await saveProgram(target, program, { replaces: decision.replaces });
|
|
91
|
+
const place = project === null ? "user" : "project";
|
|
92
|
+
return typeof decision.replaces === "string"
|
|
93
|
+
? `lib.${program.name} is saved in the ${place} library; lib.${decision.replaces} went to the archive.`
|
|
94
|
+
: `lib.${program.name} is saved in the ${place} library.`;
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
/** `library.remove`: the project's library first, then the user's. */
|
|
98
|
+
const remove = (name) =>
|
|
99
|
+
enqueue(async () => {
|
|
100
|
+
const project = projectFolder();
|
|
101
|
+
if (project !== null && (await removeProgram(project, name))) return `lib.${name} went to the archive of the project library.`;
|
|
102
|
+
if (await removeProgram(userFolder, name)) return `lib.${name} went to the archive of the user library.`;
|
|
103
|
+
throw new Error(`library.remove failed: no program named ${name} is saved`);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
/** `library.read`: the text of the file of a saved program, as onetool would write it. */
|
|
107
|
+
const readProgram = (name) =>
|
|
108
|
+
enqueue(async () => {
|
|
109
|
+
const project = projectFolder();
|
|
110
|
+
const programs = joinLibraries(await readFolder(project), await readFolder(userFolder));
|
|
111
|
+
const found = programs.find((program) => program.name === name);
|
|
112
|
+
if (found === undefined) throw new Error(`library.read failed: no program named ${name} is saved`);
|
|
113
|
+
return formatProgram({ about: found.about, params: found.params, code: found.code });
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
/** The sequences of the file, the last 2,000,000 bytes of it at most; none when there is no log or no readable file. */
|
|
117
|
+
async function readSequencesFile() {
|
|
118
|
+
let handle;
|
|
119
|
+
try {
|
|
120
|
+
handle = await open(sequencesFile, "r");
|
|
121
|
+
const { size } = await handle.stat();
|
|
122
|
+
const length = Math.min(size, SEQUENCES_READ);
|
|
123
|
+
const buffer = Buffer.alloc(length);
|
|
124
|
+
let done = 0;
|
|
125
|
+
while (done < length) {
|
|
126
|
+
const { bytesRead } = await handle.read(buffer, done, length - done, size - length + done);
|
|
127
|
+
if (bytesRead === 0) break;
|
|
128
|
+
done += bytesRead;
|
|
129
|
+
}
|
|
130
|
+
return readSequences(buffer.toString("utf8", 0, done));
|
|
131
|
+
} catch {
|
|
132
|
+
return [];
|
|
133
|
+
} finally {
|
|
134
|
+
await handle?.close().catch(() => {});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** `library.report`: the account of the library of the moment. */
|
|
139
|
+
const report = () =>
|
|
140
|
+
enqueue(async () => {
|
|
141
|
+
const project = projectFolder();
|
|
142
|
+
const programs = joinLibraries(await readFolder(project), await readFolder(userFolder));
|
|
143
|
+
const sequences = settings.log ? await readSequencesFile() : [];
|
|
144
|
+
return reportText(programs, { sequences, log: settings.log });
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
/** `library.restore`: the project's archive first, then the user's; the last result asked for decides. */
|
|
148
|
+
const restore = (name) =>
|
|
149
|
+
enqueue(async () => {
|
|
150
|
+
const cap = settings.libraryCap;
|
|
151
|
+
const project = projectFolder();
|
|
152
|
+
let where = "project";
|
|
153
|
+
let result = project === null ? { restored: false, missing: true } : await restoreProgram(project, name, { cap });
|
|
154
|
+
if (project === null || (!result.restored && result.missing)) {
|
|
155
|
+
where = "user";
|
|
156
|
+
result = await restoreProgram(userFolder, name, { cap });
|
|
157
|
+
}
|
|
158
|
+
if (!result.restored) throw new Error(result.problem);
|
|
159
|
+
return `lib.${name} is back in the ${where} library.`;
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
/** One line of the sequences, appended whole; the folder is made at the first. */
|
|
163
|
+
async function writeSequence(sequence) {
|
|
164
|
+
try {
|
|
165
|
+
const line = `${sequenceLine(sequence, { time: Date.now(), session })}\n`;
|
|
166
|
+
await mkdir(dirname(sequencesFile), { recursive: true });
|
|
167
|
+
await appendFile(sequencesFile, line);
|
|
168
|
+
} catch {
|
|
169
|
+
failed();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The program's result is there: counts its uses and its sequence. `library` is what `read()` gave when it started,
|
|
175
|
+
* `uses` the `{ name, ok }` it was handed, `ok` whether the program ended well. Gives the notes of its trail.
|
|
176
|
+
*/
|
|
177
|
+
async function finish({ library, uses, code, ok }) {
|
|
178
|
+
const notes = [];
|
|
179
|
+
if (uses.length > 0) {
|
|
180
|
+
const byName = new Map(library.programs.map((program) => [program.name, program.where]));
|
|
181
|
+
const forProject = uses.filter((use) => byName.get(use.name) === "project");
|
|
182
|
+
const forUser = uses.filter((use) => byName.get(use.name) === "user");
|
|
183
|
+
const moved = [];
|
|
184
|
+
if (forProject.length > 0 && library.project !== null) {
|
|
185
|
+
moved.push(...(await enqueue(() => noteUses(library.project, forProject, { failuresLimit: FAILURES }))));
|
|
186
|
+
}
|
|
187
|
+
if (forUser.length > 0) moved.push(...(await enqueue(() => noteUses(library.user, forUser, { failuresLimit: FAILURES }))));
|
|
188
|
+
for (const name of moved) notes.push(archivedNote(name, FAILURES));
|
|
189
|
+
}
|
|
190
|
+
const sequence = callSequence(code);
|
|
191
|
+
if (sequence.length > 0) {
|
|
192
|
+
const same = (other) => other.length === sequence.length && other.every((id, at) => id === sequence[at]);
|
|
193
|
+
const twin = library.programs.find((program) => same(callSequence(program.code)));
|
|
194
|
+
if (twin !== undefined) notes.push(sameAsNote(twin.name));
|
|
195
|
+
else if (ok) {
|
|
196
|
+
const key = JSON.stringify(sequence);
|
|
197
|
+
const times = (sequences.get(key) ?? 0) + 1;
|
|
198
|
+
sequences.set(key, times);
|
|
199
|
+
if (settings.log) await enqueue(() => writeSequence(sequence));
|
|
200
|
+
if (times === SAVE_IT_AT) notes.push(saveItNote(SAVE_IT_AT));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return notes;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return { read, save, remove, report, restore, readProgram, finish };
|
|
207
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// The command: arguments, settings, the server on standard input and output, the end.
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
4
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
5
|
+
import {
|
|
6
|
+
CallToolRequestSchema,
|
|
7
|
+
ErrorCode,
|
|
8
|
+
GetPromptRequestSchema,
|
|
9
|
+
ListPromptsRequestSchema,
|
|
10
|
+
ListToolsRequestSchema,
|
|
11
|
+
McpError,
|
|
12
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
13
|
+
import { readSettings, SettingsError } from "../../core/src/index.js";
|
|
14
|
+
import { missingFolders } from "../../shell/src/index.js";
|
|
15
|
+
import { Connection } from "./connect.js";
|
|
16
|
+
import { makeHost } from "./host.js";
|
|
17
|
+
import { TIDY_DESCRIPTION, TIDY_NAME, TIDY_TEXT } from "./prompt.js";
|
|
18
|
+
import { makeTools } from "./tools.js";
|
|
19
|
+
|
|
20
|
+
const USAGE = "usage: onetool --config <settings file>";
|
|
21
|
+
const END_MS = 4500;
|
|
22
|
+
|
|
23
|
+
const version = JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8")).version;
|
|
24
|
+
|
|
25
|
+
const say = (line) => process.stderr.write(`${line}\n`);
|
|
26
|
+
|
|
27
|
+
export async function main(argv) {
|
|
28
|
+
if (argv.length !== 2 || argv[0] !== "--config") {
|
|
29
|
+
say(USAGE);
|
|
30
|
+
process.exitCode = 2;
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
let settings;
|
|
34
|
+
try {
|
|
35
|
+
settings = readSettings(argv[1]);
|
|
36
|
+
} catch (err) {
|
|
37
|
+
if (!(err instanceof SettingsError)) throw err;
|
|
38
|
+
for (const problem of err.problems) say(`onetool: ${problem}`);
|
|
39
|
+
process.exitCode = 2;
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (settings.shell !== null && settings.shell.folders !== null) {
|
|
44
|
+
const missing = await missingFolders(settings.shell.folders);
|
|
45
|
+
for (const position of missing) say(`onetool: onetool.shell.folders: item ${position + 1} is not a folder`);
|
|
46
|
+
if (missing.length > 0) {
|
|
47
|
+
process.exitCode = 2;
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const connections = settings.servers.map(
|
|
53
|
+
(entry) => new Connection(entry.name, entry, settings.connectSeconds, version),
|
|
54
|
+
);
|
|
55
|
+
// a list of tools that can change: the description of `run` follows the folders of the host program
|
|
56
|
+
const listChanged = settings.shell !== null && settings.shell.folders === null;
|
|
57
|
+
const server = new Server({ name: "onetool", version }, { capabilities: { tools: listChanged ? { listChanged: true } : {}, prompts: {} } });
|
|
58
|
+
const host = makeHost(settings, server);
|
|
59
|
+
const tools = makeTools(settings, connections, server, host);
|
|
60
|
+
|
|
61
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
62
|
+
host.start();
|
|
63
|
+
if (listChanged) await host.idle();
|
|
64
|
+
return { tools: await tools.list() };
|
|
65
|
+
});
|
|
66
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) =>
|
|
67
|
+
tools.call(request.params.name, request.params.arguments),
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
server.setRequestHandler(ListPromptsRequestSchema, async () => ({
|
|
71
|
+
prompts: [{ name: TIDY_NAME, description: TIDY_DESCRIPTION }],
|
|
72
|
+
}));
|
|
73
|
+
server.setRequestHandler(GetPromptRequestSchema, async (request) => {
|
|
74
|
+
if (request.params.name !== TIDY_NAME) throw new McpError(ErrorCode.InvalidParams, `unknown prompt: ${request.params.name}`);
|
|
75
|
+
return { description: TIDY_DESCRIPTION, messages: [{ role: "user", content: { type: "text", text: TIDY_TEXT } }] };
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
let ending = false;
|
|
79
|
+
const end = async () => {
|
|
80
|
+
if (ending) return;
|
|
81
|
+
ending = true;
|
|
82
|
+
const hard = setTimeout(() => {
|
|
83
|
+
for (const connection of connections) connection.kill();
|
|
84
|
+
process.exit(0);
|
|
85
|
+
}, END_MS);
|
|
86
|
+
await Promise.allSettled([server.close(), ...connections.map((connection) => connection.close())]);
|
|
87
|
+
clearTimeout(hard);
|
|
88
|
+
process.exit(0);
|
|
89
|
+
};
|
|
90
|
+
process.on("SIGINT", end);
|
|
91
|
+
process.on("SIGTERM", end);
|
|
92
|
+
process.stdout.on("error", () => {});
|
|
93
|
+
|
|
94
|
+
await server.connect(new StdioServerTransport());
|
|
95
|
+
process.stdin.on("end", end);
|
|
96
|
+
process.stdin.on("close", end);
|
|
97
|
+
|
|
98
|
+
// the entries that cannot be used are told first, before any connection is tried
|
|
99
|
+
for (const { name, reason } of settings.bad) say(`onetool: server ${name}: not connected (bad entry: ${reason})`);
|
|
100
|
+
tools.ready = Promise.all(
|
|
101
|
+
connections.map(async (connection) => {
|
|
102
|
+
await connection.connect();
|
|
103
|
+
if (connection.error !== null) say(`onetool: server ${connection.name}: not connected (${connection.error})`);
|
|
104
|
+
}),
|
|
105
|
+
);
|
|
106
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// The prompt `tidy-library`: fixed texts, the same whatever the settings and the library hold.
|
|
2
|
+
|
|
3
|
+
export const TIDY_NAME = "tidy-library";
|
|
4
|
+
|
|
5
|
+
export const TIDY_DESCRIPTION = "Tidy the library of saved programs: merge the ones that repeat, retire the ones not used, and rename the unclear.";
|
|
6
|
+
|
|
7
|
+
export const TIDY_TEXT = [
|
|
8
|
+
"Tidy the library of saved programs, with the person.",
|
|
9
|
+
"1. Run a program that returns `await library.report()`. It tells, for each saved program, its uses, its failures and its last use, the programs that share most of their tools, the programs not used for a long time, and the sequences of calls that ran again and again without being saved.",
|
|
10
|
+
"2. Open, with `library.read(name)`, only the programs worth comparing: those of a pair, those not used, those that fail. Do not read the others.",
|
|
11
|
+
"3. Propose to the person what to merge, retire, rename or save, with the reason of each. Do not apply any of it yet.",
|
|
12
|
+
"Change nothing before the person says yes.",
|
|
13
|
+
"4. When the person says yes, apply what they accepted, and only that: `library.save` to save a program, with `replace: true` to put it in the place of another, and `library.remove` to retire one.",
|
|
14
|
+
"5. Say that what was replaced or removed is in the archive, and that `library.restore(name)` brings it back.",
|
|
15
|
+
].join("\n");
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// The record of every call: one record per call a program makes, the trail at the end of an answer, the log file.
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { appendFile, mkdir } from "node:fs/promises";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { callSize, formatTrail, logFileName, logLine, shellLogArgs } from "../../core/src/index.js";
|
|
7
|
+
import { logArgs } from "../../library/src/index.js";
|
|
8
|
+
|
|
9
|
+
const DEFAULT_FOLDER = fileURLToPath(new URL("../../logs", import.meta.url));
|
|
10
|
+
const FAULT_LIMIT = 500;
|
|
11
|
+
|
|
12
|
+
/** An `Error` for a call that was refused: `why` is `"rules"` (deny, allow, ask) or `"outside"` (a path outside). */
|
|
13
|
+
export function refused(message, why) {
|
|
14
|
+
return Object.assign(new Error(message), { refusal: why });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const FAULTS = { rules: "refused by the rules", outside: "outside the listed folders" };
|
|
18
|
+
|
|
19
|
+
/** The log file: lines queued in the order the records were completed, each appended whole. */
|
|
20
|
+
function makeLog(settings, session, fail) {
|
|
21
|
+
if (!settings.log) return { write() {}, flush: async () => {} };
|
|
22
|
+
const folder = typeof settings.logFolder === "string" ? settings.logFolder : DEFAULT_FOLDER;
|
|
23
|
+
let tail = Promise.resolve();
|
|
24
|
+
return {
|
|
25
|
+
write(record) {
|
|
26
|
+
let text;
|
|
27
|
+
try {
|
|
28
|
+
text = `${logLine({ ...record, session })}\n`;
|
|
29
|
+
} catch {
|
|
30
|
+
fail();
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
tail = tail.then(async () => {
|
|
34
|
+
try {
|
|
35
|
+
await mkdir(folder, { recursive: true });
|
|
36
|
+
await appendFile(join(folder, logFileName(record.time)), text);
|
|
37
|
+
} catch {
|
|
38
|
+
fail();
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
},
|
|
42
|
+
flush: () => tail,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The records of the process: `program()` gives the records of one program. `session` is the text of the lines of the log;
|
|
48
|
+
* `failed()` says once, for the life of the process, that the log cannot be written (the file of the sequences shares it).
|
|
49
|
+
*/
|
|
50
|
+
export function makeRecords(settings) {
|
|
51
|
+
const session = randomUUID();
|
|
52
|
+
let told = false;
|
|
53
|
+
const failed = () => {
|
|
54
|
+
if (told) return;
|
|
55
|
+
told = true;
|
|
56
|
+
process.stderr.write("onetool: the log cannot be written\n");
|
|
57
|
+
};
|
|
58
|
+
const log = makeLog(settings, session, failed);
|
|
59
|
+
|
|
60
|
+
function program() {
|
|
61
|
+
const calls = [];
|
|
62
|
+
// the lines the program's trail ends with, after its calls
|
|
63
|
+
const notes = [];
|
|
64
|
+
let over = false;
|
|
65
|
+
|
|
66
|
+
function complete(record, fields) {
|
|
67
|
+
if (record.done) return;
|
|
68
|
+
record.done = true;
|
|
69
|
+
record.ms = Math.max(0, Math.floor(performance.now() - record.began));
|
|
70
|
+
Object.assign(record, fields);
|
|
71
|
+
log.write(record);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// what the log keeps of the arguments of a call of a built-in server: never the text of a file or of a program
|
|
75
|
+
const loggedArgs = (serverName, tool, args) => {
|
|
76
|
+
if (serverName === "shell" && settings.shell !== null) return shellLogArgs(tool, args);
|
|
77
|
+
if (serverName === "library") return logArgs(tool, args);
|
|
78
|
+
return args;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
calls,
|
|
83
|
+
notes,
|
|
84
|
+
/** A call begins: the record is kept, its decision is for the caller to set. */
|
|
85
|
+
begin(serverName, tool, args) {
|
|
86
|
+
const record = {
|
|
87
|
+
time: Date.now(),
|
|
88
|
+
began: performance.now(),
|
|
89
|
+
id: `${serverName}.${tool}`,
|
|
90
|
+
args: loggedArgs(serverName, tool, args),
|
|
91
|
+
decision: "allowed",
|
|
92
|
+
outcome: null,
|
|
93
|
+
ms: 0,
|
|
94
|
+
size: null,
|
|
95
|
+
fault: null,
|
|
96
|
+
done: over,
|
|
97
|
+
};
|
|
98
|
+
if (!over) calls.push(record);
|
|
99
|
+
return record;
|
|
100
|
+
},
|
|
101
|
+
resolved(record, value) {
|
|
102
|
+
if (record.id === "shell.run" && settings.shell !== null && value?.exitCode !== 0) {
|
|
103
|
+
complete(record, { outcome: "failed", fault: `exit code ${value?.exitCode}` });
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
complete(record, { outcome: "ok", size: callSize(value) });
|
|
107
|
+
},
|
|
108
|
+
rejected(record, err) {
|
|
109
|
+
if (err?.refusal !== undefined) {
|
|
110
|
+
complete(record, { outcome: "refused", fault: FAULTS[err.refusal] });
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
complete(record, { outcome: "failed", fault: String(err?.message ?? err).slice(0, FAULT_LIMIT) });
|
|
114
|
+
},
|
|
115
|
+
/** The program's result is there: what has not ended is complete now, and the file holds every line. */
|
|
116
|
+
async close() {
|
|
117
|
+
over = true;
|
|
118
|
+
for (const record of calls) complete(record, { outcome: "unfinished", fault: "unfinished" });
|
|
119
|
+
await log.flush();
|
|
120
|
+
},
|
|
121
|
+
trail() {
|
|
122
|
+
return formatTrail(
|
|
123
|
+
calls.map(({ id, ms, size, outcome }) => ({ id, ms, size, outcome })),
|
|
124
|
+
{ notes },
|
|
125
|
+
);
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return { program, session, failed };
|
|
131
|
+
}
|