@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,732 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { isAbsolute, resolve } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
5
|
+
// -- settings ----------------------------------------------------------------
|
|
6
|
+
|
|
7
|
+
export class SettingsError extends Error {
|
|
8
|
+
constructor(problems) {
|
|
9
|
+
super(problems.join("; "));
|
|
10
|
+
this.name = "SettingsError";
|
|
11
|
+
this.problems = problems;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
16
|
+
const isNumber = (value) => typeof value === "number" && Number.isFinite(value);
|
|
17
|
+
const isWhole = (value, min, max) => Number.isInteger(value) && value >= min && value <= max;
|
|
18
|
+
const isPattern = (value) => typeof value === "string" && value !== "" && !/\s/.test(value);
|
|
19
|
+
const isPatternList = (value) => Array.isArray(value) && value.every(isPattern);
|
|
20
|
+
const isCommandPattern = (value) => typeof value === "string" && /\S/.test(value);
|
|
21
|
+
const isCommandPatternList = (value) => Array.isArray(value) && value.every(isCommandPattern);
|
|
22
|
+
const isFullPath = (value) => typeof value === "string" && isAbsolute(value);
|
|
23
|
+
const isBoolean = (value) => typeof value === "boolean";
|
|
24
|
+
|
|
25
|
+
const ONETOOL_KEYS = {
|
|
26
|
+
answerLimit: {
|
|
27
|
+
fallback: 8000,
|
|
28
|
+
valid: (v) => isWhole(v, 100, 1000000),
|
|
29
|
+
problem: "must be a whole number from 100 to 1000000",
|
|
30
|
+
},
|
|
31
|
+
timeoutSeconds: {
|
|
32
|
+
fallback: 60,
|
|
33
|
+
valid: (v) => isNumber(v) && v > 0 && v <= 3600,
|
|
34
|
+
problem: "must be a number above 0, at most 3600",
|
|
35
|
+
},
|
|
36
|
+
connectSeconds: {
|
|
37
|
+
fallback: 30,
|
|
38
|
+
valid: (v) => isNumber(v) && v > 0 && v <= 600,
|
|
39
|
+
problem: "must be a number above 0, at most 600",
|
|
40
|
+
},
|
|
41
|
+
searchLimit: {
|
|
42
|
+
fallback: 10,
|
|
43
|
+
valid: (v) => isWhole(v, 1, 100),
|
|
44
|
+
problem: "must be a whole number from 1 to 100",
|
|
45
|
+
},
|
|
46
|
+
stateLimit: {
|
|
47
|
+
fallback: 10000000,
|
|
48
|
+
valid: (v) => isWhole(v, 1000, 100000000),
|
|
49
|
+
problem: "must be a whole number from 1000 to 100000000",
|
|
50
|
+
},
|
|
51
|
+
allow: { fallback: [], valid: isPatternList, problem: "must be a list of patterns" },
|
|
52
|
+
deny: { fallback: [], valid: isPatternList, problem: "must be a list of patterns" },
|
|
53
|
+
ask: { fallback: [], valid: isPatternList, problem: "must be a list of patterns" },
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const EXTRA_KEYS = ["shell", "trail", "log", "logFolder", "hostRules", "hostServers", "backgroundSeconds", "maxJobs", "libraryCap"];
|
|
57
|
+
|
|
58
|
+
const isTexts = (list) => Array.isArray(list) && list.every((item) => typeof item === "string");
|
|
59
|
+
const isObjectOfTexts = (value) => isObject(value) && Object.values(value).every((item) => typeof item === "string");
|
|
60
|
+
|
|
61
|
+
/** The reason an entry of mcpServers is unusable, or null when it is usable. */
|
|
62
|
+
function badReason(name, entry, shellOn) {
|
|
63
|
+
if (name.trim() === "") return "the name is empty";
|
|
64
|
+
if (name.includes(".")) return "the name holds a dot";
|
|
65
|
+
if (shellOn && name === "shell") return "the name is taken by the built-in shell";
|
|
66
|
+
if (name === "library") return "the name is taken by the built-in library";
|
|
67
|
+
if (!isObject(entry)) return "not an object";
|
|
68
|
+
const has = (key) => Object.hasOwn(entry, key);
|
|
69
|
+
if (has("command") && has("url")) return "both command and url";
|
|
70
|
+
if (has("url")) {
|
|
71
|
+
const { url } = entry;
|
|
72
|
+
if (typeof url !== "string" || !URL.canParse(url) || !/^https?:$/.test(new URL(url).protocol)) {
|
|
73
|
+
return "url is not an http address";
|
|
74
|
+
}
|
|
75
|
+
if (entry.type === "sse") return "the old SSE transport is not supported";
|
|
76
|
+
if (has("headers") && !isObjectOfTexts(entry.headers)) return "headers is not an object of texts";
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
if (typeof entry.command !== "string" || entry.command === "") return "no command";
|
|
80
|
+
if (has("args") && !isTexts(entry.args)) return "args is not a list of texts";
|
|
81
|
+
if (has("env") && !isObjectOfTexts(entry.env)) return "env is not an object of texts";
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const SHELL_KEYS = ["folders", "allow", "deny", "ask"];
|
|
86
|
+
|
|
87
|
+
/** The problems of the `shell` key of `onetool`, in their order. */
|
|
88
|
+
function shellProblems(shell) {
|
|
89
|
+
if (!isObject(shell)) return ["onetool.shell: not an object"];
|
|
90
|
+
const problems = [];
|
|
91
|
+
for (const key of Object.keys(shell)) {
|
|
92
|
+
if (!SHELL_KEYS.includes(key)) problems.push(`onetool.shell.${key}: unknown key`);
|
|
93
|
+
}
|
|
94
|
+
const { folders } = shell;
|
|
95
|
+
if (Object.hasOwn(shell, "folders") && (!Array.isArray(folders) || folders.length === 0 || !folders.every(isFullPath))) {
|
|
96
|
+
problems.push("onetool.shell.folders: must be a list of full paths, at least one");
|
|
97
|
+
}
|
|
98
|
+
for (const key of ["allow", "deny", "ask"]) {
|
|
99
|
+
if (Object.hasOwn(shell, key) && !isCommandPatternList(shell[key])) {
|
|
100
|
+
problems.push(`onetool.shell.${key}: must be a list of command patterns`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return problems;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function readSettings(path) {
|
|
107
|
+
let raw;
|
|
108
|
+
try {
|
|
109
|
+
raw = readFileSync(path, "utf8");
|
|
110
|
+
} catch {
|
|
111
|
+
throw new SettingsError([`settings file not found: ${path}`]);
|
|
112
|
+
}
|
|
113
|
+
let file;
|
|
114
|
+
try {
|
|
115
|
+
file = JSON.parse(raw);
|
|
116
|
+
} catch {
|
|
117
|
+
throw new SettingsError(["settings file is not JSON"]);
|
|
118
|
+
}
|
|
119
|
+
if (!isObject(file)) throw new SettingsError(["settings file is not a JSON object"]);
|
|
120
|
+
|
|
121
|
+
const problems = [];
|
|
122
|
+
if (!("mcpServers" in file)) problems.push("mcpServers: missing");
|
|
123
|
+
else if (!isObject(file.mcpServers)) problems.push("mcpServers: not an object");
|
|
124
|
+
|
|
125
|
+
const onetool = file.onetool === undefined ? {} : file.onetool;
|
|
126
|
+
const values = {};
|
|
127
|
+
let shell = null;
|
|
128
|
+
let trail = true;
|
|
129
|
+
let log = true;
|
|
130
|
+
let logFolder = null;
|
|
131
|
+
let hostRules = true;
|
|
132
|
+
let hostServers = [];
|
|
133
|
+
let backgroundSeconds = 7200;
|
|
134
|
+
let maxJobs = 3;
|
|
135
|
+
let libraryCap = 50;
|
|
136
|
+
if (!isObject(onetool)) {
|
|
137
|
+
problems.push("onetool: not an object");
|
|
138
|
+
} else {
|
|
139
|
+
for (const key of Object.keys(onetool)) {
|
|
140
|
+
if (!Object.hasOwn(ONETOOL_KEYS, key) && !EXTRA_KEYS.includes(key)) problems.push(`onetool.${key}: unknown key`);
|
|
141
|
+
}
|
|
142
|
+
for (const [key, rule] of Object.entries(ONETOOL_KEYS)) {
|
|
143
|
+
if (!Object.hasOwn(onetool, key)) values[key] = rule.fallback;
|
|
144
|
+
else if (rule.valid(onetool[key])) values[key] = onetool[key];
|
|
145
|
+
else problems.push(`onetool.${key}: ${rule.problem}`);
|
|
146
|
+
}
|
|
147
|
+
const shellOn = Object.hasOwn(onetool, "shell");
|
|
148
|
+
if (shellOn) {
|
|
149
|
+
const found = shellProblems(onetool.shell);
|
|
150
|
+
if (found.length === 0) {
|
|
151
|
+
const { folders = null, allow = [], deny = [], ask = [] } = onetool.shell;
|
|
152
|
+
shell = { folders, allow, deny, ask };
|
|
153
|
+
} else {
|
|
154
|
+
problems.push(...found);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (Object.hasOwn(onetool, "trail")) {
|
|
158
|
+
if (isBoolean(onetool.trail)) trail = onetool.trail;
|
|
159
|
+
else problems.push("onetool.trail: must be true or false");
|
|
160
|
+
}
|
|
161
|
+
if (Object.hasOwn(onetool, "log")) {
|
|
162
|
+
if (isBoolean(onetool.log)) log = onetool.log;
|
|
163
|
+
else problems.push("onetool.log: must be true or false");
|
|
164
|
+
}
|
|
165
|
+
if (Object.hasOwn(onetool, "logFolder")) {
|
|
166
|
+
if (isFullPath(onetool.logFolder)) logFolder = onetool.logFolder;
|
|
167
|
+
else problems.push("onetool.logFolder: must be a full path");
|
|
168
|
+
}
|
|
169
|
+
if (Object.hasOwn(onetool, "hostRules")) {
|
|
170
|
+
if (isBoolean(onetool.hostRules)) hostRules = onetool.hostRules;
|
|
171
|
+
else problems.push("onetool.hostRules: must be true or false");
|
|
172
|
+
}
|
|
173
|
+
if (Object.hasOwn(onetool, "hostServers")) {
|
|
174
|
+
const names = onetool.hostServers;
|
|
175
|
+
if (Array.isArray(names) && names.every((name) => typeof name === "string" && name.trim() !== "")) hostServers = names;
|
|
176
|
+
else problems.push("onetool.hostServers: must be a list of server names");
|
|
177
|
+
}
|
|
178
|
+
if (Object.hasOwn(onetool, "backgroundSeconds")) {
|
|
179
|
+
const seconds = onetool.backgroundSeconds;
|
|
180
|
+
if (isNumber(seconds) && seconds > 0 && seconds <= 86400) backgroundSeconds = seconds;
|
|
181
|
+
else problems.push("onetool.backgroundSeconds: must be a number above 0, at most 86400");
|
|
182
|
+
}
|
|
183
|
+
if (Object.hasOwn(onetool, "maxJobs")) {
|
|
184
|
+
if (isWhole(onetool.maxJobs, 0, 100)) maxJobs = onetool.maxJobs;
|
|
185
|
+
else problems.push("onetool.maxJobs: must be a whole number from 0 to 100");
|
|
186
|
+
}
|
|
187
|
+
if (Object.hasOwn(onetool, "libraryCap")) {
|
|
188
|
+
if (isWhole(onetool.libraryCap, 1, 1000)) libraryCap = onetool.libraryCap;
|
|
189
|
+
else problems.push("onetool.libraryCap: must be a whole number from 1 to 1000");
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (problems.length > 0) throw new SettingsError(problems);
|
|
193
|
+
|
|
194
|
+
const servers = [];
|
|
195
|
+
const bad = [];
|
|
196
|
+
for (const [name, entry] of Object.entries(file.mcpServers)) {
|
|
197
|
+
const reason = badReason(name, entry, shell !== null);
|
|
198
|
+
if (reason !== null) bad.push({ name, reason });
|
|
199
|
+
else if (Object.hasOwn(entry, "url")) servers.push({ name, url: entry.url, headers: entry.headers ?? {} });
|
|
200
|
+
else servers.push({ name, command: entry.command, args: entry.args ?? [], env: entry.env ?? {} });
|
|
201
|
+
}
|
|
202
|
+
const { answerLimit, timeoutSeconds, connectSeconds, searchLimit, stateLimit, allow, deny, ask } = values;
|
|
203
|
+
return { servers, bad, answerLimit, timeoutSeconds, connectSeconds, searchLimit, stateLimit, allow, deny, ask, shell, trail, log, logFolder, hostRules, hostServers, backgroundSeconds, maxJobs, libraryCap };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// -- allow and deny ----------------------------------------------------------
|
|
207
|
+
|
|
208
|
+
/** Whether `pattern` matches the whole of `text`; `*` stands for any run of characters. */
|
|
209
|
+
function globMatches(pattern, text) {
|
|
210
|
+
let p = 0;
|
|
211
|
+
let t = 0;
|
|
212
|
+
let starAt = -1;
|
|
213
|
+
let resume = 0;
|
|
214
|
+
while (t < text.length) {
|
|
215
|
+
if (p < pattern.length && pattern[p] === "*") {
|
|
216
|
+
starAt = p++;
|
|
217
|
+
resume = t;
|
|
218
|
+
} else if (p < pattern.length && pattern[p] === text[t]) {
|
|
219
|
+
p++;
|
|
220
|
+
t++;
|
|
221
|
+
} else if (starAt >= 0) {
|
|
222
|
+
p = starAt + 1;
|
|
223
|
+
t = ++resume;
|
|
224
|
+
} else {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
while (p < pattern.length && pattern[p] === "*") p++;
|
|
229
|
+
return p === pattern.length;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function decide(rules, server, tool) {
|
|
233
|
+
if (rules !== undefined && !isObject(rules)) throw new TypeError("decide: the rules must be an object");
|
|
234
|
+
const { allow = [], deny = [], ask = [] } = rules ?? {};
|
|
235
|
+
if (!Array.isArray(allow)) throw new TypeError("decide: allow must be a list");
|
|
236
|
+
if (!Array.isArray(deny)) throw new TypeError("decide: deny must be a list");
|
|
237
|
+
if (!Array.isArray(ask)) throw new TypeError("decide: ask must be a list");
|
|
238
|
+
const id = `${server}.${tool}`;
|
|
239
|
+
for (const rule of deny) {
|
|
240
|
+
if (globMatches(rule, id)) {
|
|
241
|
+
return { allowed: false, list: "deny", rule, message: `onetool: ${id} is denied by the rule deny "${rule}"` };
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (allow.length > 0 && !allow.some((rule) => globMatches(rule, id))) {
|
|
245
|
+
return {
|
|
246
|
+
allowed: false,
|
|
247
|
+
list: "allow",
|
|
248
|
+
rule: null,
|
|
249
|
+
message: `onetool: ${id} is not allowed: it matches no rule of allow`,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
const asked = ask.find((rule) => globMatches(rule, id));
|
|
253
|
+
return asked === undefined ? { allowed: true } : { allowed: true, ask: asked };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** The question the person reads before a call under a rule of `ask`. */
|
|
257
|
+
export function askMessage(id, rule, args, { limit = 300, file } = {}) {
|
|
258
|
+
const json = JSON.stringify(args);
|
|
259
|
+
const shown = json.length > limit ? `${json.slice(0, limit)}... (${json.length} characters)` : json;
|
|
260
|
+
return `onetool: a program wants to call ${id}.\nArguments: ${shown}\nThe rule ask "${rule}"${file === undefined ? "" : ` of the host program (${file})`} asks for your consent. Allow this call?`;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const ASK_CAUSES = {
|
|
264
|
+
unsupported: "the host program cannot ask the person",
|
|
265
|
+
decline: "the person said no",
|
|
266
|
+
cancel: "the person gave no answer",
|
|
267
|
+
failed: "the question failed",
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
/** The message of the error a program gets when a call under `ask` is not made. */
|
|
271
|
+
export function askRefusal(id, rule, why, file) {
|
|
272
|
+
if (typeof why !== "string" || !Object.hasOwn(ASK_CAUSES, why)) {
|
|
273
|
+
throw new TypeError("askRefusal: why must be unsupported, decline, cancel or failed");
|
|
274
|
+
}
|
|
275
|
+
return `onetool: ${id} is refused by the rule ask "${rule}"${file === undefined ? "" : ` of the host program (${file})`}: ${ASK_CAUSES[why]}`;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** The message of the error a program gets when a rule deny of the host program refuses its call. */
|
|
279
|
+
export function hostDenial(id, rule, file) {
|
|
280
|
+
return `onetool: ${id} is denied by the rule deny "${rule}" of the host program (${file})`;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// -- search ------------------------------------------------------------------
|
|
284
|
+
|
|
285
|
+
function typeOf(property) {
|
|
286
|
+
const type = property?.type;
|
|
287
|
+
if (typeof type === "string") return type;
|
|
288
|
+
if (Array.isArray(type)) return type.join("|");
|
|
289
|
+
return "any";
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function sentenceOf(description) {
|
|
293
|
+
if (typeof description !== "string") return "";
|
|
294
|
+
const text = description.replace(/\s+/g, " ").trim();
|
|
295
|
+
const end = text.search(/[.!?](?: |$)/);
|
|
296
|
+
const sentence = end === -1 ? text : text.slice(0, end + 1);
|
|
297
|
+
return sentence.length > 160 ? `${sentence.slice(0, 157)}...` : sentence;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export function toolLine(server, tool) {
|
|
301
|
+
const schema = tool.inputSchema;
|
|
302
|
+
const properties = isObject(schema?.properties) ? schema.properties : {};
|
|
303
|
+
const required = new Set(Array.isArray(schema?.required) ? schema.required : []);
|
|
304
|
+
const args = Object.keys(properties)
|
|
305
|
+
.map((name) => `${name}${required.has(name) ? "" : "?"}: ${typeOf(properties[name])}`)
|
|
306
|
+
.join(", ");
|
|
307
|
+
const sentence = sentenceOf(tool.description);
|
|
308
|
+
return `${server}.${tool.name}(${args})${sentence === "" ? "" : ` - ${sentence}`}`;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function score(server, tool, words) {
|
|
312
|
+
const id = `${server}.${tool.name}`.toLowerCase();
|
|
313
|
+
const description = typeof tool.description === "string" ? tool.description.toLowerCase() : "";
|
|
314
|
+
const properties = isObject(tool.inputSchema?.properties) ? tool.inputSchema.properties : {};
|
|
315
|
+
const argumentNames = Object.keys(properties).map((name) => name.toLowerCase());
|
|
316
|
+
let total = 0;
|
|
317
|
+
for (const word of words) {
|
|
318
|
+
if (id.includes(word)) total += 2;
|
|
319
|
+
else if (description.includes(word) || argumentNames.some((name) => name.includes(word))) total += 1;
|
|
320
|
+
}
|
|
321
|
+
return total;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** `lib.<name>(<arguments>) - <sentence>`: how a saved program is called. */
|
|
325
|
+
export function programLine(program) {
|
|
326
|
+
const params = program.params.length === 0 ? "" : `{ ${program.params.join(", ")} }`;
|
|
327
|
+
const about = program.about.replace(/\s+/g, " ").trim();
|
|
328
|
+
const sentence = about.length > 160 ? `${about.slice(0, 157)}...` : about;
|
|
329
|
+
return `lib.${program.name}(${params})${sentence === "" ? "" : ` - ${sentence}`}`;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function programScore(program, words) {
|
|
333
|
+
const id = `lib.${program.name}`.toLowerCase();
|
|
334
|
+
const about = program.about.toLowerCase();
|
|
335
|
+
const params = program.params.map((param) => param.toLowerCase());
|
|
336
|
+
let total = 0;
|
|
337
|
+
for (const word of words) {
|
|
338
|
+
if (id.includes(word)) total += 2;
|
|
339
|
+
else if (about.includes(word) || params.some((param) => param.includes(word))) total += 1;
|
|
340
|
+
}
|
|
341
|
+
return total;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
export function searchTools(catalog, words, { limit = 10, programs = [] } = {}) {
|
|
345
|
+
const query = typeof words === "string" ? words.trim() : "";
|
|
346
|
+
if (query === "") {
|
|
347
|
+
const lines = catalog.map((entry) =>
|
|
348
|
+
entry.error !== undefined && entry.error !== null
|
|
349
|
+
? `${entry.server}: not connected (${entry.error})`
|
|
350
|
+
: `${entry.server}: ${entry.tools.length} tools`,
|
|
351
|
+
);
|
|
352
|
+
if (programs.length > 0) lines.push(`library: ${programs.length} programs`);
|
|
353
|
+
return lines.length === 0 ? "No server is listed." : lines.join("\n");
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const terms = query.toLowerCase().split(/\s+/);
|
|
357
|
+
const matches = [];
|
|
358
|
+
for (const [order, program] of programs.entries()) {
|
|
359
|
+
const points = programScore(program, terms);
|
|
360
|
+
if (points > 0) matches.push({ points, line: programLine(program), order });
|
|
361
|
+
}
|
|
362
|
+
matches.sort((a, b) => b.points - a.points || a.order - b.order);
|
|
363
|
+
const found = [];
|
|
364
|
+
for (const entry of catalog) {
|
|
365
|
+
if (entry.error !== undefined && entry.error !== null) continue;
|
|
366
|
+
for (const tool of entry.tools) {
|
|
367
|
+
const points = score(entry.server, tool, terms);
|
|
368
|
+
if (points > 0) found.push({ points, line: toolLine(entry.server, tool), order: found.length });
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
found.sort((a, b) => b.points - a.points || a.order - b.order);
|
|
372
|
+
matches.push(...found);
|
|
373
|
+
if (matches.length === 0) return "No tool matches.";
|
|
374
|
+
const lines = matches.slice(0, limit).map((m) => m.line);
|
|
375
|
+
if (matches.length > limit) lines.push(`(${matches.length - limit} more match: add a word)`);
|
|
376
|
+
return lines.join("\n");
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// -- answers -----------------------------------------------------------------
|
|
380
|
+
|
|
381
|
+
export class ToolError extends Error {
|
|
382
|
+
constructor(message) {
|
|
383
|
+
super(message);
|
|
384
|
+
this.name = "ToolError";
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
export function toolValue(result) {
|
|
389
|
+
if (!isObject(result)) throw new TypeError("toolValue: the result must be an object");
|
|
390
|
+
const items = Array.isArray(result.content) ? result.content : [];
|
|
391
|
+
const texts = items.filter((item) => isObject(item) && item.type === "text").map((item) => String(item.text ?? ""));
|
|
392
|
+
const text = texts.join("\n");
|
|
393
|
+
if (result.isError === true) throw new ToolError(text === "" ? "the tool failed" : text);
|
|
394
|
+
if (result.structuredContent !== null && typeof result.structuredContent === "object") {
|
|
395
|
+
return result.structuredContent;
|
|
396
|
+
}
|
|
397
|
+
if (texts.length === 0) return null;
|
|
398
|
+
try {
|
|
399
|
+
return JSON.parse(text);
|
|
400
|
+
} catch {
|
|
401
|
+
return text;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
export function formatRun(result, { timeoutSeconds = 60 } = {}) {
|
|
406
|
+
let text;
|
|
407
|
+
if (result.ok) {
|
|
408
|
+
if (result.value === undefined) text = "undefined";
|
|
409
|
+
else if (typeof result.value === "string") text = result.value;
|
|
410
|
+
else text = JSON.stringify(result.value) ?? String(result.value);
|
|
411
|
+
} else {
|
|
412
|
+
const { kind, message } = result.error;
|
|
413
|
+
if (kind === "timeout") text = `The program was stopped after ${String(timeoutSeconds)} seconds.`;
|
|
414
|
+
else if (kind === "stopped") text = "The program was stopped on request.";
|
|
415
|
+
else if (kind === "crash") text = `The program's process ended before it answered (${message}).`;
|
|
416
|
+
else text = `The program failed: ${message}`;
|
|
417
|
+
}
|
|
418
|
+
const logs = result.logs ?? [];
|
|
419
|
+
if (logs.length > 0) text += `\n--- console.log ---\n${logs.join("\n")}`;
|
|
420
|
+
return text;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
export function cutAnswer(text, limit = 8000) {
|
|
424
|
+
if (typeof text !== "string") throw new TypeError("cutAnswer: text must be a string");
|
|
425
|
+
if (text.length <= limit) return text;
|
|
426
|
+
const notice = `\n[cut: the answer was ${text.length} characters, the limit is ${limit}]`;
|
|
427
|
+
return text.slice(0, limit - notice.length) + notice;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// -- jobs --------------------------------------------------------------------
|
|
431
|
+
|
|
432
|
+
const RUN_NEEDS_CODE = 'onetool: run needs "code", a text';
|
|
433
|
+
const flagProblem = (name) => `onetool: run takes "${name}", true or false`;
|
|
434
|
+
|
|
435
|
+
/** What a call of `run` asks for: a program, a job, the list of jobs, or a problem with the arguments. */
|
|
436
|
+
export function runRequest(args) {
|
|
437
|
+
if (!isObject(args)) return { problem: RUN_NEEDS_CODE };
|
|
438
|
+
const get = (key) => (Object.hasOwn(args, key) ? args[key] : undefined);
|
|
439
|
+
const flags = {};
|
|
440
|
+
for (const name of ["background", "stop", "jobs"]) {
|
|
441
|
+
const value = get(name);
|
|
442
|
+
if (value !== undefined && value !== null && !isBoolean(value)) return { problem: flagProblem(name) };
|
|
443
|
+
flags[name] = value === true;
|
|
444
|
+
}
|
|
445
|
+
const job = get("job");
|
|
446
|
+
if (job !== undefined && job !== null && typeof job !== "string") {
|
|
447
|
+
return { problem: 'onetool: run takes "job", the id of a job' };
|
|
448
|
+
}
|
|
449
|
+
const asksJob = typeof job === "string" && job !== "";
|
|
450
|
+
const asksJobs = flags.jobs;
|
|
451
|
+
const code = get("code");
|
|
452
|
+
const given = code !== undefined && !((asksJob || asksJobs) && (code === null || code === ""));
|
|
453
|
+
const asked = [given, asksJob, asksJobs].filter(Boolean).length;
|
|
454
|
+
if (asked === 0) return { problem: RUN_NEEDS_CODE };
|
|
455
|
+
if (asked > 1) return { problem: 'onetool: run takes one of "code", "job" and "jobs", not several' };
|
|
456
|
+
if (given) {
|
|
457
|
+
if (typeof code !== "string") return { problem: RUN_NEEDS_CODE };
|
|
458
|
+
if (flags.stop) return { problem: 'onetool: run takes "stop" only with "job"' };
|
|
459
|
+
return { mode: "run", code, background: flags.background };
|
|
460
|
+
}
|
|
461
|
+
if (asksJob) {
|
|
462
|
+
if (flags.background) return { problem: 'onetool: run takes "background" only with "code"' };
|
|
463
|
+
return { mode: "job", job, stop: flags.stop };
|
|
464
|
+
}
|
|
465
|
+
if (flags.background) return { problem: flagProblem("background") };
|
|
466
|
+
if (flags.stop) return { problem: flagProblem("stop") };
|
|
467
|
+
return { mode: "jobs" };
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/** The state a job ends in, from what part `sandbox` returned for its program. */
|
|
471
|
+
export function jobState(result) {
|
|
472
|
+
if (result.ok) return "done";
|
|
473
|
+
const { kind } = result.error;
|
|
474
|
+
return kind === "timeout" || kind === "stopped" ? "stopped" : "failed";
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
export const jobStarted = (id) =>
|
|
478
|
+
`Job ${String(id)} started. Its state: run({ job: "${String(id)}" }). To stop it: run({ job: "${String(id)}", stop: true }).`;
|
|
479
|
+
|
|
480
|
+
export const jobLimitMessage = (maxJobs) =>
|
|
481
|
+
`onetool: the job was not started: no more than ${String(maxJobs)} jobs may run at once (onetool.maxJobs). Wait for one to end, or stop one.`;
|
|
482
|
+
|
|
483
|
+
export const noJobMessage = (id) => `onetool: no job ${String(id)} in this session. run({ jobs: true }) lists them.`;
|
|
484
|
+
|
|
485
|
+
/** `<id>: <state>, <n> s`, the seconds the job has run. */
|
|
486
|
+
export function jobLine(job, now) {
|
|
487
|
+
const end = typeof job.endedAt === "number" ? job.endedAt : now;
|
|
488
|
+
const seconds = Math.max(0, Math.floor((end - job.startedAt) / 1000));
|
|
489
|
+
return `${job.id}: ${job.state}, ${seconds} s`;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
export function formatJobs(jobs, now) {
|
|
493
|
+
if (jobs.length === 0) return "No job in this session.";
|
|
494
|
+
return jobs.map((job) => jobLine(job, now)).join("\n");
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/** The text `run` answers for one job: its line, the progress kept, and its answer once it has ended. */
|
|
498
|
+
export function formatJob(job, now, { lineLimit = 200 } = {}) {
|
|
499
|
+
const lines = [`job ${jobLine(job, now)}`];
|
|
500
|
+
if (Array.isArray(job.progress) && job.progress.length > 0) {
|
|
501
|
+
lines.push("--- progress ---");
|
|
502
|
+
if (job.progressCount > job.progress.length) lines.push(`(${job.progressCount - job.progress.length} earlier lines not shown)`);
|
|
503
|
+
for (const item of job.progress) {
|
|
504
|
+
const flat = item.replace(/\r\n|\n|\r/g, " ");
|
|
505
|
+
lines.push(flat.length > lineLimit ? `${flat.slice(0, lineLimit)}...` : flat);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
if (typeof job.answer === "string") lines.push("--- answer ---", job.answer);
|
|
509
|
+
return lines.join("\n");
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// -- the library -------------------------------------------------------------
|
|
513
|
+
|
|
514
|
+
const LIBRARY_HOW = ". Call one as await lib.<name>(args); search finds them; library.save(name, { about, params, code }) keeps a new one.";
|
|
515
|
+
|
|
516
|
+
/** The text added to the description of `run`: how many programs are saved, the most used, and how to call and save one. */
|
|
517
|
+
export function libraryText(programs, { limit = 298, most = 5 } = {}) {
|
|
518
|
+
const head = `Saved programs: ${programs.length}`;
|
|
519
|
+
const names = [];
|
|
520
|
+
const sorted = [...programs].sort((a, b) => b.uses - a.uses || (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
521
|
+
for (const { name } of sorted.slice(0, most)) {
|
|
522
|
+
const text = `${head} (most used: ${[...names, name].join(", ")})${LIBRARY_HOW}`;
|
|
523
|
+
if (text.length > limit) break;
|
|
524
|
+
names.push(name);
|
|
525
|
+
}
|
|
526
|
+
return `${head}${names.length === 0 ? "" : ` (most used: ${names.join(", ")})`}${LIBRARY_HOW}`;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
export const saveItNote = (times) =>
|
|
530
|
+
`(this sequence of calls has run ${String(times)} times: save it as a program with library.save)`;
|
|
531
|
+
|
|
532
|
+
export const sameAsNote = (name) => `(lib.${String(name)} does this: call it)`;
|
|
533
|
+
|
|
534
|
+
export const archivedNote = (name, failures) =>
|
|
535
|
+
`(lib.${String(name)} failed ${String(failures)} times in a row: it was moved to the archive)`;
|
|
536
|
+
|
|
537
|
+
// -- state -------------------------------------------------------------------
|
|
538
|
+
|
|
539
|
+
export function nextState(kept, before, after, { limit = 10000000 } = {}) {
|
|
540
|
+
const removed = new Set();
|
|
541
|
+
const changed = new Set();
|
|
542
|
+
for (const key of Object.keys(after)) {
|
|
543
|
+
if (!Object.hasOwn(before, key) || JSON.stringify(before[key]) !== JSON.stringify(after[key])) changed.add(key);
|
|
544
|
+
}
|
|
545
|
+
for (const key of Object.keys(before)) {
|
|
546
|
+
if (!Object.hasOwn(after, key)) removed.add(key);
|
|
547
|
+
}
|
|
548
|
+
const entries = [];
|
|
549
|
+
for (const [key, value] of Object.entries(kept)) {
|
|
550
|
+
if (removed.has(key)) continue;
|
|
551
|
+
entries.push([key, changed.has(key) ? after[key] : value]);
|
|
552
|
+
}
|
|
553
|
+
for (const key of Object.keys(after)) {
|
|
554
|
+
if (changed.has(key) && !Object.hasOwn(kept, key)) entries.push([key, after[key]]);
|
|
555
|
+
}
|
|
556
|
+
if (removed.size === 0 && changed.size === 0) return { state: Object.fromEntries(Object.entries(kept)), problem: null };
|
|
557
|
+
const state = Object.fromEntries(entries);
|
|
558
|
+
const n = JSON.stringify(state).length;
|
|
559
|
+
if (n > limit) {
|
|
560
|
+
return { state: kept, problem: `state was not kept: it is ${n} characters as JSON, the limit is ${limit}` };
|
|
561
|
+
}
|
|
562
|
+
return { state, problem: null };
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// -- the shell ---------------------------------------------------------------
|
|
566
|
+
|
|
567
|
+
/** Whether a shell command may be run, by its text: `settings.shell` is the first argument. */
|
|
568
|
+
export function decideCommand(rules, command) {
|
|
569
|
+
if (!isObject(rules)) throw new TypeError("decideCommand: the rules must be an object");
|
|
570
|
+
const { allow = [], deny = [], ask = [] } = rules;
|
|
571
|
+
if (!Array.isArray(allow)) throw new TypeError("decideCommand: allow must be a list");
|
|
572
|
+
if (!Array.isArray(deny)) throw new TypeError("decideCommand: deny must be a list");
|
|
573
|
+
if (!Array.isArray(ask)) throw new TypeError("decideCommand: ask must be a list");
|
|
574
|
+
if (typeof command !== "string") throw new TypeError("decideCommand: the command must be a string");
|
|
575
|
+
const text = command.trim();
|
|
576
|
+
for (const rule of deny) {
|
|
577
|
+
if (globMatches(rule, text)) {
|
|
578
|
+
return {
|
|
579
|
+
allowed: false,
|
|
580
|
+
list: "deny",
|
|
581
|
+
rule,
|
|
582
|
+
message: `onetool: shell.run is denied by the rule deny "${rule}" of onetool.shell`,
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
if (allow.length > 0 && !allow.some((rule) => globMatches(rule, text))) {
|
|
587
|
+
return {
|
|
588
|
+
allowed: false,
|
|
589
|
+
list: "allow",
|
|
590
|
+
rule: null,
|
|
591
|
+
message: "onetool: shell.run is not allowed: the command matches no rule of allow of onetool.shell",
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
const asked = ask.find((rule) => globMatches(rule, text));
|
|
595
|
+
return asked === undefined ? { allowed: true } : { allowed: true, ask: asked };
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
const stringProperty = () => ({ type: "string" });
|
|
599
|
+
const shellTool = (name, description, properties) =>
|
|
600
|
+
Object.freeze({
|
|
601
|
+
name,
|
|
602
|
+
description,
|
|
603
|
+
inputSchema: Object.freeze({
|
|
604
|
+
type: "object",
|
|
605
|
+
properties: Object.freeze(Object.fromEntries(properties.map((property) => [property, stringProperty()]))),
|
|
606
|
+
required: Object.freeze([...properties]),
|
|
607
|
+
}),
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
export const SHELL_TOOLS = Object.freeze([
|
|
611
|
+
shellTool("run", "Runs a shell command in the first listed folder. In a program: $`command`.", ["command"]),
|
|
612
|
+
shellTool("read", "Reads a text file of the listed folders. In a program: read(path).", ["path"]),
|
|
613
|
+
shellTool(
|
|
614
|
+
"write",
|
|
615
|
+
"Writes a text file in the listed folders, replacing what it holds. In a program: write(path, text).",
|
|
616
|
+
["path", "text"],
|
|
617
|
+
),
|
|
618
|
+
shellTool(
|
|
619
|
+
"edit",
|
|
620
|
+
"Replaces one text by another in a file of the listed folders. In a program: edit(path, old, new).",
|
|
621
|
+
["path", "old", "new"],
|
|
622
|
+
),
|
|
623
|
+
]);
|
|
624
|
+
|
|
625
|
+
const SHELL_NAMES = ["$", "read", "write", "edit"];
|
|
626
|
+
|
|
627
|
+
export const SHELL_NO_FOLDERS = "no folder: onetool.shell.folders is not set and the host program gave none";
|
|
628
|
+
|
|
629
|
+
/** Says why a program that uses `$`, `read`, `write` or `edit` without a working shell got a ReferenceError. */
|
|
630
|
+
export function shellOffMessage(message, why = "off") {
|
|
631
|
+
if (why !== "off" && why !== "no-folders") throw new TypeError("shellOffMessage: why must be off or no-folders");
|
|
632
|
+
if (typeof message === "string" && SHELL_NAMES.some((name) => message === `ReferenceError: ${name} is not defined`)) {
|
|
633
|
+
return why === "off"
|
|
634
|
+
? `${message} (the shell is not switched on: see onetool.shell in the settings file)`
|
|
635
|
+
: `${message} (the shell has ${SHELL_NO_FOLDERS})`;
|
|
636
|
+
}
|
|
637
|
+
return message;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/** The full paths of the folders of a list of MCP roots, `file://` roots only, none twice. */
|
|
641
|
+
export function rootFolders(roots) {
|
|
642
|
+
if (!Array.isArray(roots)) return [];
|
|
643
|
+
const folders = [];
|
|
644
|
+
for (const root of roots) {
|
|
645
|
+
if (!isObject(root) || typeof root.uri !== "string" || !root.uri.startsWith("file://")) continue;
|
|
646
|
+
let folder;
|
|
647
|
+
try {
|
|
648
|
+
folder = resolve(fileURLToPath(root.uri));
|
|
649
|
+
} catch {
|
|
650
|
+
continue;
|
|
651
|
+
}
|
|
652
|
+
if (!folders.includes(folder)) folders.push(folder);
|
|
653
|
+
}
|
|
654
|
+
return folders;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
// -- the trail ---------------------------------------------------------------
|
|
658
|
+
|
|
659
|
+
/** The size of the answer of one call, in characters. */
|
|
660
|
+
export function callSize(value) {
|
|
661
|
+
if (value === undefined) return 0;
|
|
662
|
+
if (typeof value === "string") return value.length;
|
|
663
|
+
return JSON.stringify(value).length;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
/** The trail of one program: one line per call, in the order the calls started. */
|
|
667
|
+
export function formatTrail(calls, { limit = 20, notes = [] } = {}) {
|
|
668
|
+
if (calls.length === 0 && notes.length === 0) return "";
|
|
669
|
+
const lines = calls.slice(0, limit).map((call) =>
|
|
670
|
+
call.outcome === "ok" ? `${call.id} ${call.ms} ms ${call.size} chars` : `${call.id} ${call.ms} ms ${call.outcome}`,
|
|
671
|
+
);
|
|
672
|
+
if (calls.length > limit) lines.push(`(${calls.length - limit} more not shown)`);
|
|
673
|
+
return ["--- calls ---", ...lines, ...notes].join("\n");
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
/** The answer of `run` and its trail within the limit of an answer, the trail whole unless the limit is too small. */
|
|
677
|
+
export function withTrail(text, trail, limit = 8000) {
|
|
678
|
+
if (typeof text !== "string") throw new TypeError("withTrail: text must be a string");
|
|
679
|
+
if (typeof trail !== "string") throw new TypeError("withTrail: trail must be a string");
|
|
680
|
+
if (trail === "") return cutAnswer(text, limit);
|
|
681
|
+
const whole = `${text}\n${trail}`;
|
|
682
|
+
if (whole.length <= limit) return whole;
|
|
683
|
+
const notice = `\n[cut: the answer was ${whole.length} characters, the limit is ${limit}]`;
|
|
684
|
+
const room = limit - notice.length - 1 - trail.length;
|
|
685
|
+
if (room < 0) return cutAnswer(whole, limit);
|
|
686
|
+
return `${text.slice(0, room)}${notice}\n${trail}`;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
// -- the log -----------------------------------------------------------------
|
|
690
|
+
|
|
691
|
+
const lengthOrNull = (value) => (typeof value === "string" ? value.length : null);
|
|
692
|
+
|
|
693
|
+
/** What the log keeps of the arguments of a call of the built-in server `shell`: never the text of a file. */
|
|
694
|
+
export function shellLogArgs(tool, args) {
|
|
695
|
+
if (!isObject(args)) return null;
|
|
696
|
+
switch (tool) {
|
|
697
|
+
case "run":
|
|
698
|
+
return { command: args.command ?? null };
|
|
699
|
+
case "read":
|
|
700
|
+
return { path: args.path ?? null };
|
|
701
|
+
case "write":
|
|
702
|
+
return { path: args.path ?? null, size: lengthOrNull(args.text) };
|
|
703
|
+
case "edit":
|
|
704
|
+
return { path: args.path ?? null, oldSize: lengthOrNull(args.old), newSize: lengthOrNull(args.new) };
|
|
705
|
+
default:
|
|
706
|
+
return null;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/** One line of the log file, with no line break in it or after it. */
|
|
711
|
+
export function logLine(entry, { argsLimit = 2000 } = {}) {
|
|
712
|
+
const json = JSON.stringify(entry.args);
|
|
713
|
+
let args = null;
|
|
714
|
+
if (json !== undefined) {
|
|
715
|
+
args = json.length <= argsLimit ? entry.args : `${json.slice(0, argsLimit)}... (${json.length} characters)`;
|
|
716
|
+
}
|
|
717
|
+
return JSON.stringify({
|
|
718
|
+
time: new Date(entry.time).toISOString(),
|
|
719
|
+
session: entry.session,
|
|
720
|
+
id: entry.id,
|
|
721
|
+
args,
|
|
722
|
+
decision: entry.decision,
|
|
723
|
+
ms: entry.ms,
|
|
724
|
+
size: entry.size,
|
|
725
|
+
fault: entry.fault,
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
/** The name of the file a line goes to: one file per day, in UTC. */
|
|
730
|
+
export function logFileName(time) {
|
|
731
|
+
return `calls-${new Date(time).toISOString().slice(0, 10)}.jsonl`;
|
|
732
|
+
}
|