@evref-bl/pharo-launcher-mcp 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -20
- package/dist/answers.d.ts +43 -0
- package/dist/answers.d.ts.map +1 -0
- package/dist/answers.js +744 -0
- package/dist/answers.js.map +1 -0
- package/dist/commandCatalog.d.ts +13 -1
- package/dist/commandCatalog.d.ts.map +1 -1
- package/dist/commandCatalog.js +65 -89
- package/dist/commandCatalog.js.map +1 -1
- package/dist/config.d.ts +5 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +3 -1
- package/dist/config.js.map +1 -1
- package/dist/crossProfileCopy.d.ts +37 -0
- package/dist/crossProfileCopy.d.ts.map +1 -1
- package/dist/crossProfileCopy.js +77 -16
- package/dist/crossProfileCopy.js.map +1 -1
- package/dist/discovery.d.ts +8 -0
- package/dist/discovery.d.ts.map +1 -1
- package/dist/discovery.js +10 -0
- package/dist/discovery.js.map +1 -1
- package/dist/imageStore.d.ts +139 -0
- package/dist/imageStore.d.ts.map +1 -0
- package/dist/imageStore.js +385 -0
- package/dist/imageStore.js.map +1 -0
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/launcher.d.ts +64 -17
- package/dist/launcher.d.ts.map +1 -1
- package/dist/launcher.js +206 -167
- package/dist/launcher.js.map +1 -1
- package/dist/launcherCli.d.ts +13 -0
- package/dist/launcherCli.d.ts.map +1 -1
- package/dist/launcherCli.js +85 -91
- package/dist/launcherCli.js.map +1 -1
- package/dist/models.d.ts +117 -6
- package/dist/models.d.ts.map +1 -1
- package/dist/parser.d.ts +20 -7
- package/dist/parser.d.ts.map +1 -1
- package/dist/parser.js +48 -207
- package/dist/parser.js.map +1 -1
- package/dist/platform.d.ts +6 -0
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +14 -0
- package/dist/platform.js.map +1 -1
- package/dist/probe.d.ts +16 -0
- package/dist/probe.d.ts.map +1 -0
- package/dist/probe.js +31 -0
- package/dist/probe.js.map +1 -0
- package/dist/processTools.d.ts +22 -8
- package/dist/processTools.d.ts.map +1 -1
- package/dist/processTools.js +75 -58
- package/dist/processTools.js.map +1 -1
- package/dist/resultNormalizer.d.ts +9 -0
- package/dist/resultNormalizer.d.ts.map +1 -1
- package/dist/resultNormalizer.js +36 -79
- package/dist/resultNormalizer.js.map +1 -1
- package/dist/server.d.ts +10 -9
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +56 -55
- package/dist/server.js.map +1 -1
- package/dist/ston.d.ts +22 -0
- package/dist/ston.d.ts.map +1 -0
- package/dist/ston.js +127 -0
- package/dist/ston.js.map +1 -0
- package/docs/cleanup-hook-boundary.md +11 -9
- package/docs/development.md +51 -38
- package/docs/reference/tools.md +100 -40
- package/docs/troubleshooting.md +32 -23
- package/docs/user/getting-started.md +4 -4
- package/docs/user/profile-mode.md +30 -21
- package/package.json +1 -1
package/dist/answers.js
ADDED
|
@@ -0,0 +1,744 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What each MCP tool answers: plain text, rendered from the library's object
|
|
3
|
+
* the way MCP's own tools answer. A summary sentence, `Warning: …` lines,
|
|
4
|
+
* `key: value` lines, then indented lists.
|
|
5
|
+
*
|
|
6
|
+
* The library keeps its typed objects, which PLexus reads; nothing here reaches
|
|
7
|
+
* it. So every text that names a `pharo_launcher_*` tool is here, and so is
|
|
8
|
+
* every check made before a call: in profile mode, where this server wrote the
|
|
9
|
+
* images directory itself, a check refuses on its own; outside a profile the
|
|
10
|
+
* directory comes from settings that can be wrong, so a name the reader does
|
|
11
|
+
* not find goes to the Launcher, whose answer is the answer.
|
|
12
|
+
*/
|
|
13
|
+
import fs from "node:fs";
|
|
14
|
+
import path from "node:path";
|
|
15
|
+
import { buildLauncherCommandArgs, inputObject, optionalString, requiredString, ToolInputError, } from "./commandCatalog.js";
|
|
16
|
+
import { profileLabel } from "./crossProfileCopy.js";
|
|
17
|
+
import { findByName, processesOfImages, scanImages, whatItLacks, } from "./imageStore.js";
|
|
18
|
+
import { madeImageFailureText, } from "./launcher.js";
|
|
19
|
+
import { isLauncherInvocationFailure } from "./launcherCli.js";
|
|
20
|
+
import { isPidAlive } from "./processTools.js";
|
|
21
|
+
import { sentenceWithMessage } from "./resultNormalizer.js";
|
|
22
|
+
const outputTailLines = 20;
|
|
23
|
+
const defaultProcessExitTimeoutMs = 10_000;
|
|
24
|
+
const defaultProcessExitPollMs = 200;
|
|
25
|
+
// --- How an answer reads -------------------------------------------------
|
|
26
|
+
function textResult(lines, isError = false) {
|
|
27
|
+
const text = lines.filter((line) => typeof line === "string").join("\n");
|
|
28
|
+
return {
|
|
29
|
+
content: [{ type: "text", text }],
|
|
30
|
+
...(isError ? { isError } : {}),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function failure(...lines) {
|
|
34
|
+
return textResult(lines, true);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A value that carries whitespace, quoted so that where it ends is never a
|
|
38
|
+
* guess, `"` and `\` escaped: `MCPTool>>inlineValueTextFor:`.
|
|
39
|
+
*/
|
|
40
|
+
export function inlineValue(value) {
|
|
41
|
+
return /\s/.test(value) ? `"${value.replace(/["\\]/g, (char) => `\\${char}`)}"` : value;
|
|
42
|
+
}
|
|
43
|
+
function plural(count, singular, pluralForm = `${singular}s`) {
|
|
44
|
+
return count === 1 ? singular : pluralForm;
|
|
45
|
+
}
|
|
46
|
+
/** A sentence's end: no second period after a message that ends with one. */
|
|
47
|
+
function sentence(text) {
|
|
48
|
+
const trimmed = text.trim();
|
|
49
|
+
return trimmed.endsWith(".") ? trimmed : `${trimmed}.`;
|
|
50
|
+
}
|
|
51
|
+
/** A message set inside a sentence, in parentheses or before a semicolon. */
|
|
52
|
+
function clause(text) {
|
|
53
|
+
return text.trim().replace(/\.$/, "");
|
|
54
|
+
}
|
|
55
|
+
function capitalized(text) {
|
|
56
|
+
return text.charAt(0).toUpperCase() + text.slice(1);
|
|
57
|
+
}
|
|
58
|
+
/** `A, B or C`. */
|
|
59
|
+
function alternatives(items) {
|
|
60
|
+
return items.length <= 1
|
|
61
|
+
? (items[0] ?? "")
|
|
62
|
+
: `${items.slice(0, -1).join(", ")} or ${items.at(-1)}`;
|
|
63
|
+
}
|
|
64
|
+
/** The last lines a process wrote, under the label PLexus uses for them. */
|
|
65
|
+
function outputTail(label, text) {
|
|
66
|
+
const trimmed = text?.trim();
|
|
67
|
+
if (!trimmed) {
|
|
68
|
+
return [];
|
|
69
|
+
}
|
|
70
|
+
const lines = trimmed.split(/\r?\n/).slice(-outputTailLines);
|
|
71
|
+
return [label, ...lines.map((line) => (line.trimEnd() ? ` ${line.trimEnd()}` : ""))];
|
|
72
|
+
}
|
|
73
|
+
function outputTails(output) {
|
|
74
|
+
return [
|
|
75
|
+
...outputTail("Its output ends with:", output?.stdout),
|
|
76
|
+
...outputTail("Its error output ends with:", output?.stderr),
|
|
77
|
+
];
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* A list: its label, naming the attributes every entry has with the same
|
|
81
|
+
* value when there are two entries or more, then one entry per line, its name
|
|
82
|
+
* first. `MCPTool>>entriesTextOn:for:labelled:indent:`.
|
|
83
|
+
*/
|
|
84
|
+
function entries(label, list, shareable = []) {
|
|
85
|
+
if (list.length === 0) {
|
|
86
|
+
return [];
|
|
87
|
+
}
|
|
88
|
+
const shared = list.length < 2
|
|
89
|
+
? []
|
|
90
|
+
: shareable.filter((key) => {
|
|
91
|
+
const first = list[0].attributes.find(([name]) => name === key)?.[1];
|
|
92
|
+
return (first !== undefined &&
|
|
93
|
+
list.every((entry) => entry.attributes.find(([name]) => name === key)?.[1] === first));
|
|
94
|
+
});
|
|
95
|
+
const pair = ([key, value]) => `${key}=${inlineValue(value)}`;
|
|
96
|
+
const sharedPairs = shared.map((key) => pair([key, list[0].attributes.find(([name]) => name === key)[1]]));
|
|
97
|
+
return [
|
|
98
|
+
sharedPairs.length > 0 ? `${label} (each ${sharedPairs.join(" ")}):` : `${label}:`,
|
|
99
|
+
...list.map((entry) => [
|
|
100
|
+
` ${inlineValue(entry.name)}`,
|
|
101
|
+
...entry.attributes
|
|
102
|
+
.filter((attribute) => attribute[1] !== undefined && !shared.includes(attribute[0]))
|
|
103
|
+
.map(pair),
|
|
104
|
+
].join(" ")),
|
|
105
|
+
];
|
|
106
|
+
}
|
|
107
|
+
// --- Shared texts -----------------------------------------------------
|
|
108
|
+
function callSeconds(options) {
|
|
109
|
+
return Math.round((options.timeoutMs ?? 30_000) / 1000);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* A Launcher command that failed: past the call's timeout, never started, or
|
|
113
|
+
* refused, in its own words.
|
|
114
|
+
*/
|
|
115
|
+
function launcherFailureText(operation, result, context) {
|
|
116
|
+
if (result.command.timedOut) {
|
|
117
|
+
return `Pharo Launcher did not finish ${operation} within ${callSeconds(context.options)} s.`;
|
|
118
|
+
}
|
|
119
|
+
if (isLauncherInvocationFailure(result.diagnostic)) {
|
|
120
|
+
return result.diagnostic;
|
|
121
|
+
}
|
|
122
|
+
return sentenceWithMessage(`Pharo Launcher could not ${operation}`, result.diagnostic ??
|
|
123
|
+
(result.command.signal
|
|
124
|
+
? `it was killed by signal ${result.command.signal}`
|
|
125
|
+
: `it exited with ${result.command.exitCode ?? "no exit code"}`));
|
|
126
|
+
}
|
|
127
|
+
/** A sentence after a message, on a line of its own when the message has several. */
|
|
128
|
+
function followedBy(text, after) {
|
|
129
|
+
return `${text}${text.includes("\n") ? "\n" : " "}${after}`;
|
|
130
|
+
}
|
|
131
|
+
/** The text `image_info`, `image_start`, `image_delete` and a copy's source share. */
|
|
132
|
+
function noImageText(imageName, imagesDir, notAnImage) {
|
|
133
|
+
return notAnImage
|
|
134
|
+
? `There is no image ${inlineValue(imageName)} in ${imagesDir}: its directory ${whatItLacks(notAnImage)}.`
|
|
135
|
+
: `There is no image ${inlineValue(imageName)} in ${imagesDir}. pharo_launcher_image_list says which images there are.`;
|
|
136
|
+
}
|
|
137
|
+
function takenNameText(image, imagesDir) {
|
|
138
|
+
return (`There is already an image ${inlineValue(image.name)} in ${imagesDir}. ` +
|
|
139
|
+
"Pick another name, or delete it first with pharo_launcher_image_delete.");
|
|
140
|
+
}
|
|
141
|
+
function noTemplatesYet(config) {
|
|
142
|
+
if (!config.profile) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
return (fs.readFileSync(path.join(config.profile.templateSourcesDir, "sources.list"), "utf8").trim()
|
|
147
|
+
.length === 0);
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const noTemplatesYetText = "There are no templates yet: call pharo_launcher_template_update first.";
|
|
154
|
+
function rawOutput(result) {
|
|
155
|
+
return `${result.raw?.stdout ?? ""}\n${result.raw?.stderr ?? ""}\n${result.diagnostic ?? ""}`;
|
|
156
|
+
}
|
|
157
|
+
function timedOutWarning(result, context) {
|
|
158
|
+
return result.command.timedOut
|
|
159
|
+
? `Warning: Pharo Launcher needed more than the call's ${callSeconds(context.options)} s, but finished.`
|
|
160
|
+
: undefined;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* A create or copy that did not make its image. `operation` is what it was
|
|
164
|
+
* asked (`create image X`), `said` what the Launcher said it did (`created
|
|
165
|
+
* X`), and `newImageName` the image it made, names written as answers write
|
|
166
|
+
* them.
|
|
167
|
+
*/
|
|
168
|
+
function madeImageFailure(operation, said, newImageName, result, context) {
|
|
169
|
+
// Exited 0, but never listed or never opened.
|
|
170
|
+
if (result.failure) {
|
|
171
|
+
return failure(madeImageFailureText(result.failure, said, newImageName));
|
|
172
|
+
}
|
|
173
|
+
const output = rawOutput(result);
|
|
174
|
+
if (/not found, please enter a correct category name/i.test(output)) {
|
|
175
|
+
return noTemplatesYet(context.config)
|
|
176
|
+
? failure(noTemplatesYetText)
|
|
177
|
+
: failure(followedBy(launcherFailureText(operation, result, context), "pharo_launcher_template_category_list says which categories there are."));
|
|
178
|
+
}
|
|
179
|
+
if (/not found, please enter a correct (?:short )?template name/i.test(output)) {
|
|
180
|
+
return failure(followedBy(launcherFailureText(operation, result, context), "pharo_launcher_template_list says which templates there are."));
|
|
181
|
+
}
|
|
182
|
+
return failure(launcherFailureText(operation, result, context));
|
|
183
|
+
}
|
|
184
|
+
// --- The images ---------------------------------------------------------
|
|
185
|
+
/** The Pharo version as Pharo says it: "140" is 14.0 and "61" is 6.1; anything else as it is. */
|
|
186
|
+
export function pharoVersionText(raw) {
|
|
187
|
+
return raw !== undefined && /^\d{2,3}$/.test(raw) ? `${raw.slice(0, -1)}.${raw.slice(-1)}` : raw;
|
|
188
|
+
}
|
|
189
|
+
function imageAttributes(image) {
|
|
190
|
+
return [
|
|
191
|
+
["pharo", pharoVersionText(image.pharoVersion)],
|
|
192
|
+
["architecture", image.architecture],
|
|
193
|
+
["template", image.template?.name],
|
|
194
|
+
];
|
|
195
|
+
}
|
|
196
|
+
const answerConfig = async (context) => {
|
|
197
|
+
const report = context.launcher.describeConfig();
|
|
198
|
+
const launcherDir = path.resolve(report.launcherDir.path);
|
|
199
|
+
const imagesDir = report.imagesDir;
|
|
200
|
+
const profile = report.profile;
|
|
201
|
+
let first;
|
|
202
|
+
if (!report.discovery.selected.usable) {
|
|
203
|
+
const candidates = [
|
|
204
|
+
...new Set(report.discovery.candidates.map((candidate) => path.resolve(candidate.launcherDir.path))),
|
|
205
|
+
];
|
|
206
|
+
first =
|
|
207
|
+
`No usable Pharo Launcher in ${alternatives(candidates)}. ` +
|
|
208
|
+
"Install it, or set PHARO_LAUNCHER_DIR to where it is.";
|
|
209
|
+
}
|
|
210
|
+
else if (profile) {
|
|
211
|
+
first = `Pharo Launcher ${launcherDir}, profile ${inlineValue(profile.name)}.`;
|
|
212
|
+
}
|
|
213
|
+
else if (imagesDir.source === "default" && imagesDir.settingsFile && !fs.existsSync(imagesDir.settingsFile)) {
|
|
214
|
+
first = `Pharo Launcher ${launcherDir}, with its default settings: ${imagesDir.settingsFile} does not exist yet.`;
|
|
215
|
+
}
|
|
216
|
+
else if (imagesDir.source === "default") {
|
|
217
|
+
first = `Pharo Launcher ${launcherDir}, with the settings in ${imagesDir.settingsFile}, which name no images directory.`;
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
first = `Pharo Launcher ${launcherDir}, with the settings in ${imagesDir.settingsFile}.`;
|
|
221
|
+
}
|
|
222
|
+
return textResult([
|
|
223
|
+
first,
|
|
224
|
+
profile && `stateRoot: ${path.resolve(profile.stateRoot.path)}`,
|
|
225
|
+
`images: ${imagesDir.path}`,
|
|
226
|
+
]);
|
|
227
|
+
};
|
|
228
|
+
const answerImageList = async (context, input) => {
|
|
229
|
+
const nameFilter = optionalString(input, "nameFilter");
|
|
230
|
+
const list = await context.launcher.listImages(nameFilter === undefined ? {} : { nameFilter });
|
|
231
|
+
const scan = scanImages(list.imagesDir);
|
|
232
|
+
const dir = list.imagesDir;
|
|
233
|
+
const total = scan.images.length;
|
|
234
|
+
const shown = list.images.length;
|
|
235
|
+
const filter = nameFilter === undefined ? undefined : inlineValue(nameFilter);
|
|
236
|
+
const summary = !scan.exists
|
|
237
|
+
? `No images in ${dir}, which does not exist.`
|
|
238
|
+
: total === 0
|
|
239
|
+
? `No images in ${dir}.`
|
|
240
|
+
: filter === undefined
|
|
241
|
+
? `${total} ${plural(total, "image")} in ${dir}.`
|
|
242
|
+
: `${shown} of ${total} ${plural(total, "image")} in ${dir} match ${filter}.`;
|
|
243
|
+
return textResult([
|
|
244
|
+
summary,
|
|
245
|
+
...list.notImages.map((directory) => `Warning: ${inlineValue(directory.name)} is not an image: ${directory.directory} ${whatItLacks(directory)}.`),
|
|
246
|
+
list.processListFailure !== undefined &&
|
|
247
|
+
`Warning: cannot tell which images run: ${sentence(list.processListFailure)}`,
|
|
248
|
+
...entries("images", list.images.map((image) => ({
|
|
249
|
+
name: image.name,
|
|
250
|
+
attributes: [
|
|
251
|
+
...imageAttributes(image),
|
|
252
|
+
["pids", image.pids && image.pids.length > 0 ? image.pids.join(",") : undefined],
|
|
253
|
+
],
|
|
254
|
+
})), ["pharo", "architecture", "template"]),
|
|
255
|
+
]);
|
|
256
|
+
};
|
|
257
|
+
function runsText(image) {
|
|
258
|
+
const pids = image.pids ?? [];
|
|
259
|
+
return pids.length === 1 ? `runs as pid ${pids[0]}` : `runs as pids ${pids.join(", ")}`;
|
|
260
|
+
}
|
|
261
|
+
const answerImageInfo = async (context, input) => {
|
|
262
|
+
const imageName = requiredString(input, "imageName");
|
|
263
|
+
const info = await context.launcher.imageInfo({ imageName });
|
|
264
|
+
if (!info.found) {
|
|
265
|
+
return failure(noImageText(imageName, info.imagesDir, info.notAnImage));
|
|
266
|
+
}
|
|
267
|
+
const image = info.image;
|
|
268
|
+
const name = inlineValue(image.name);
|
|
269
|
+
const first = info.processListFailure !== undefined
|
|
270
|
+
? `Image ${name}.`
|
|
271
|
+
: (image.pids?.length ?? 0) === 0
|
|
272
|
+
? `Image ${name} is not running.`
|
|
273
|
+
: `Image ${name} ${runsText(image)}.`;
|
|
274
|
+
return textResult([
|
|
275
|
+
first,
|
|
276
|
+
info.processListFailure !== undefined &&
|
|
277
|
+
`Warning: cannot tell whether it runs: ${sentence(info.processListFailure)}`,
|
|
278
|
+
`path: ${image.imagePath}`,
|
|
279
|
+
image.pharoVersion && `pharo: ${pharoVersionText(image.pharoVersion)}`,
|
|
280
|
+
image.architecture && `architecture: ${image.architecture}`,
|
|
281
|
+
image.template && `template: ${image.template.name}`,
|
|
282
|
+
image.template?.url && `templateUrl: ${image.template.url}`,
|
|
283
|
+
`modified: ${image.modified}`,
|
|
284
|
+
]);
|
|
285
|
+
};
|
|
286
|
+
function madeImageLines(image, withVersions) {
|
|
287
|
+
if (!image) {
|
|
288
|
+
return [];
|
|
289
|
+
}
|
|
290
|
+
return [
|
|
291
|
+
`path: ${image.imagePath}`,
|
|
292
|
+
...(withVersions
|
|
293
|
+
? [
|
|
294
|
+
image.pharoVersion && `pharo: ${pharoVersionText(image.pharoVersion)}`,
|
|
295
|
+
image.architecture && `architecture: ${image.architecture}`,
|
|
296
|
+
].filter((line) => Boolean(line))
|
|
297
|
+
: []),
|
|
298
|
+
];
|
|
299
|
+
}
|
|
300
|
+
const answerImageCreate = async (context, input) => {
|
|
301
|
+
buildLauncherCommandArgs("pharo_launcher_image_create", input);
|
|
302
|
+
const newImageName = requiredString(input, "newImageName");
|
|
303
|
+
const templateName = optionalString(input, "templateName");
|
|
304
|
+
if (context.config.profile) {
|
|
305
|
+
const taken = findByName(scanImages(context.imagesDir).images, newImageName);
|
|
306
|
+
if (taken) {
|
|
307
|
+
return failure(takenNameText(taken, context.imagesDir));
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
const result = await context.launcher.createImage(input);
|
|
311
|
+
if (!result.ok) {
|
|
312
|
+
const name = inlineValue(newImageName);
|
|
313
|
+
return madeImageFailure(`create image ${name}`, `created ${name}`, name, result, context);
|
|
314
|
+
}
|
|
315
|
+
const image = result.data?.image;
|
|
316
|
+
const template = image?.template?.name ?? templateName;
|
|
317
|
+
const name = inlineValue(image?.name ?? newImageName);
|
|
318
|
+
return textResult([
|
|
319
|
+
template ? `Created image ${name} from template ${template}.` : `Created image ${name}.`,
|
|
320
|
+
timedOutWarning(result, context),
|
|
321
|
+
...madeImageLines(image, true),
|
|
322
|
+
]);
|
|
323
|
+
};
|
|
324
|
+
const answerImageCopy = async (context, input) => {
|
|
325
|
+
buildLauncherCommandArgs("pharo_launcher_image_copy", input);
|
|
326
|
+
const imageName = requiredString(input, "imageName");
|
|
327
|
+
const newImageName = requiredString(input, "newImageName");
|
|
328
|
+
if (context.config.profile) {
|
|
329
|
+
const scan = scanImages(context.imagesDir);
|
|
330
|
+
if (!findByName(scan.images, imageName)) {
|
|
331
|
+
return failure(noImageText(imageName, context.imagesDir, findByName(scan.notImages, imageName)));
|
|
332
|
+
}
|
|
333
|
+
const taken = findByName(scan.images, newImageName);
|
|
334
|
+
if (taken) {
|
|
335
|
+
return failure(takenNameText(taken, context.imagesDir));
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const result = await context.launcher.copyImage({ imageName, newImageName });
|
|
339
|
+
if (!result.ok) {
|
|
340
|
+
const copied = `${inlineValue(imageName)} to ${inlineValue(newImageName)}`;
|
|
341
|
+
return madeImageFailure(`copy image ${copied}`, `copied ${copied}`, inlineValue(newImageName), result, context);
|
|
342
|
+
}
|
|
343
|
+
const image = result.data?.image;
|
|
344
|
+
return textResult([
|
|
345
|
+
`Copied image ${inlineValue(imageName)} to ${inlineValue(image?.name ?? newImageName)}.`,
|
|
346
|
+
timedOutWarning(result, context),
|
|
347
|
+
...madeImageLines(image, false),
|
|
348
|
+
]);
|
|
349
|
+
};
|
|
350
|
+
const answerImageDelete = async (context, input) => {
|
|
351
|
+
buildLauncherCommandArgs("pharo_launcher_image_delete", input);
|
|
352
|
+
const imageName = requiredString(input, "imageName");
|
|
353
|
+
const info = await context.launcher.imageInfo({ imageName });
|
|
354
|
+
let name = inlineValue(imageName);
|
|
355
|
+
if (info.found) {
|
|
356
|
+
name = inlineValue(info.image.name);
|
|
357
|
+
if (info.processListFailure !== undefined) {
|
|
358
|
+
return failure(`Cannot tell whether ${name} runs (${clause(info.processListFailure)}), so it was not deleted.`);
|
|
359
|
+
}
|
|
360
|
+
if ((info.image.pids?.length ?? 0) > 0) {
|
|
361
|
+
return failure(`Image ${name} ${runsText(info.image)}: stop it with pharo_launcher_process_kill, then delete it.`);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
else if (context.config.profile) {
|
|
365
|
+
return failure(noImageText(imageName, info.imagesDir, info.notAnImage));
|
|
366
|
+
}
|
|
367
|
+
const result = await context.launcher.deleteImage({ imageName });
|
|
368
|
+
return result.ok
|
|
369
|
+
? textResult([`Deleted image ${name}.`])
|
|
370
|
+
: failure(launcherFailureText(`delete image ${name}`, result, context));
|
|
371
|
+
};
|
|
372
|
+
/** How a start the call waited for ended: past the call's time, by a signal, or with an exit code. */
|
|
373
|
+
function endedText(name, command, seconds) {
|
|
374
|
+
if (command.timedOut) {
|
|
375
|
+
return (`${name} did not quit within ${seconds} s and was stopped: a start that is not ` +
|
|
376
|
+
"detached lasts as long as the call. Pass detached: true to leave it running.");
|
|
377
|
+
}
|
|
378
|
+
return command.signal
|
|
379
|
+
? `${name} was killed by signal ${command.signal}.`
|
|
380
|
+
: `${name} quit with exit code ${command.exitCode}.`;
|
|
381
|
+
}
|
|
382
|
+
function endedWell(command) {
|
|
383
|
+
return !command.timedOut && !command.signal && command.exitCode === 0;
|
|
384
|
+
}
|
|
385
|
+
const answerImageStart = async (context, input) => {
|
|
386
|
+
buildLauncherCommandArgs("pharo_launcher_image_start", input);
|
|
387
|
+
const imageName = requiredString(input, "imageName");
|
|
388
|
+
const detached = input.detached === true;
|
|
389
|
+
const hasScript = input.script !== undefined;
|
|
390
|
+
const scan = scanImages(context.imagesDir);
|
|
391
|
+
const entry = findByName(scan.images, imageName);
|
|
392
|
+
if (context.config.profile && !entry) {
|
|
393
|
+
return failure(noImageText(imageName, context.imagesDir, findByName(scan.notImages, imageName)));
|
|
394
|
+
}
|
|
395
|
+
const result = await context.launcher.startImage(input);
|
|
396
|
+
const name = inlineValue(entry?.name ?? imageName);
|
|
397
|
+
// What ran, once something ran: none for a launch refused before it.
|
|
398
|
+
const launch = result.launch;
|
|
399
|
+
const logs = launch?.logs && `logs: ${launch.logs.stdout} and ${launch.logs.stderr}`;
|
|
400
|
+
if (launch?.by === "vm") {
|
|
401
|
+
const mode = launch.displayMode === "interactive" ? "with its window" : "headless";
|
|
402
|
+
const downloaded = launch.vmUpdated ? `Downloaded VM ${launch.vmId} for it first.` : undefined;
|
|
403
|
+
// Pharo 13 prints its usage and exits 0; Pharo 14 prints it and exits 1.
|
|
404
|
+
const nothingToRun = launch.displayMode === "headless" &&
|
|
405
|
+
!hasScript &&
|
|
406
|
+
"A headless image with no script has nothing to run and quits: " +
|
|
407
|
+
"pass a script, or displayMode: interactive.";
|
|
408
|
+
if (detached) {
|
|
409
|
+
return textResult([`Started ${name} ${mode}, detached, as pid ${launch.pid}.`, downloaded, nothingToRun, logs]);
|
|
410
|
+
}
|
|
411
|
+
return textResult([
|
|
412
|
+
endedText(name, result.command, callSeconds(context.options)),
|
|
413
|
+
downloaded,
|
|
414
|
+
nothingToRun,
|
|
415
|
+
...outputTails(launch.output),
|
|
416
|
+
], !endedWell(result.command));
|
|
417
|
+
}
|
|
418
|
+
if (launch?.by === "launcher" && detached && result.ok) {
|
|
419
|
+
return textResult([
|
|
420
|
+
`Pharo Launcher is starting ${name} (its own pid ${launch.pid}); ` +
|
|
421
|
+
"pharo_launcher_process_list shows the image's VM once it runs.",
|
|
422
|
+
logs,
|
|
423
|
+
]);
|
|
424
|
+
}
|
|
425
|
+
// Outside a profile the Launcher's exit code is the image's only when the
|
|
426
|
+
// reader knows the image; a timeout or a signal ended the Launcher.
|
|
427
|
+
if (launch?.by === "launcher" && !detached && entry && !result.command.timedOut && !result.command.signal) {
|
|
428
|
+
return textResult([`${name} quit with exit code ${result.command.exitCode}.`, ...outputTails(result.raw)], result.command.exitCode !== 0);
|
|
429
|
+
}
|
|
430
|
+
if (result.ok) {
|
|
431
|
+
return textResult([`${name} quit with exit code 0.`, ...outputTails(result.raw)]);
|
|
432
|
+
}
|
|
433
|
+
// A launch the library refused says why itself; the Launcher's refusal is quoted.
|
|
434
|
+
return failure(launch === undefined && result.diagnostic
|
|
435
|
+
? result.diagnostic
|
|
436
|
+
: launcherFailureText(`start image ${name}`, result, context));
|
|
437
|
+
};
|
|
438
|
+
// --- Between profiles ---------------------------------------------------------
|
|
439
|
+
function verificationClause(result) {
|
|
440
|
+
const diagnostic = result.verification?.diagnostic ?? result.diagnostic ?? "";
|
|
441
|
+
const text = clause(diagnostic);
|
|
442
|
+
return text.charAt(0).toLowerCase() + text.slice(1);
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* What to give instead of a profile path that does not exist. config prints
|
|
446
|
+
* this server's own stateRoot only, so another's is said by what it holds.
|
|
447
|
+
*/
|
|
448
|
+
function profilePathAdvice(result) {
|
|
449
|
+
const missing = result.missingProfilePaths ?? [];
|
|
450
|
+
const override = (each) => each.given && each.field !== "stateRoot" && result[each.profile].stateRoot !== undefined;
|
|
451
|
+
return [
|
|
452
|
+
missing.some((each) => !override(each)) &&
|
|
453
|
+
" Give as stateRoot the directory that holds the profile's images/ directory; " +
|
|
454
|
+
"pharo_launcher_config prints this server's own stateRoot.",
|
|
455
|
+
missing.some(override) &&
|
|
456
|
+
" Leave out every path but stateRoot unless the profile keeps it elsewhere: " +
|
|
457
|
+
"each defaults to its place inside stateRoot.",
|
|
458
|
+
]
|
|
459
|
+
.filter(Boolean)
|
|
460
|
+
.join("");
|
|
461
|
+
}
|
|
462
|
+
const answerCopyBetweenProfiles = async (context, input) => {
|
|
463
|
+
const result = context.launcher.copyImageBetweenProfiles(input);
|
|
464
|
+
const source = inlineValue(result.sourceImageName);
|
|
465
|
+
const destination = inlineValue(result.destinationImageName);
|
|
466
|
+
const from = profileLabel(result.sourceProfile);
|
|
467
|
+
const to = profileLabel(result.destinationProfile);
|
|
468
|
+
const renamed = result.destinationImageName === result.sourceImageName ? "" : ` as ${destination}`;
|
|
469
|
+
if (result.ok) {
|
|
470
|
+
return textResult([
|
|
471
|
+
`Copied image ${source} from ${from} to ${to}${renamed}.`,
|
|
472
|
+
`path: ${result.destinationImage.imageFile.path}`,
|
|
473
|
+
]);
|
|
474
|
+
}
|
|
475
|
+
const sourceDirectory = result.sourceImage.directory;
|
|
476
|
+
switch (result.failure) {
|
|
477
|
+
case "profile-path-missing":
|
|
478
|
+
return failure(`Cannot copy image ${source}: ${result.diagnostic}${profilePathAdvice(result)}`);
|
|
479
|
+
case "source-missing":
|
|
480
|
+
case "source-image-missing":
|
|
481
|
+
case "source-metadata-missing":
|
|
482
|
+
return failure(`Cannot copy image ${source}: ${sourceDirectory} is not a Launcher image ` +
|
|
483
|
+
`(${clause(result.diagnostic ?? "")}).`);
|
|
484
|
+
case "destination-exists":
|
|
485
|
+
return failure(`Cannot copy image ${source} to ${result.destinationImage.directory}: that path ` +
|
|
486
|
+
"already exists, and a copy never replaces it. Pick another destinationImageName, " +
|
|
487
|
+
"or remove it first.");
|
|
488
|
+
case "preflight":
|
|
489
|
+
return failure(`Cannot copy image ${source}: the copy did not check out (${verificationClause(result)}), ` +
|
|
490
|
+
"so it was removed.");
|
|
491
|
+
case "post-move":
|
|
492
|
+
return failure(`Copied image ${source}, but the copy at ${result.destinationImage.directory} is not a ` +
|
|
493
|
+
`Launcher image (${verificationClause(result)}). It was left in place; delete it ` +
|
|
494
|
+
"before copying again.");
|
|
495
|
+
case "exception": {
|
|
496
|
+
const left = result.cleanup.temporaryDirectory;
|
|
497
|
+
return failure(sentenceWithMessage(`Cannot copy image ${source} to ${to}`, result.error ?? result.diagnostic ?? "it failed"), left !== undefined && fs.existsSync(left) && `It left ${left} behind.`);
|
|
498
|
+
}
|
|
499
|
+
default:
|
|
500
|
+
return failure(result.message ?? result.diagnostic ?? `Cannot copy image ${source}.`);
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
/**
|
|
504
|
+
* The processes running an image of the images directory this server reads,
|
|
505
|
+
* in every mode: in profile mode the library's list already is; outside a
|
|
506
|
+
* profile it holds every Pharo VM, and the adapter keeps those.
|
|
507
|
+
*/
|
|
508
|
+
async function scopedProcesses(context) {
|
|
509
|
+
const result = await context.launcher.listProcesses();
|
|
510
|
+
if (!result.ok) {
|
|
511
|
+
return { failure: result.diagnostic ?? "the process list failed" };
|
|
512
|
+
}
|
|
513
|
+
const processes = result.data ?? [];
|
|
514
|
+
return {
|
|
515
|
+
processes: context.config.profile
|
|
516
|
+
? processes
|
|
517
|
+
: processesOfImages(scanImages(context.imagesDir).images, processes, context.options.nativeProcess?.platform),
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
function listFailureText(reason) {
|
|
521
|
+
return sentenceWithMessage("Could not list processes", reason);
|
|
522
|
+
}
|
|
523
|
+
const answerProcessList = async (context) => {
|
|
524
|
+
const scoped = await scopedProcesses(context);
|
|
525
|
+
if ("failure" in scoped) {
|
|
526
|
+
return failure(listFailureText(scoped.failure));
|
|
527
|
+
}
|
|
528
|
+
const count = scoped.processes.length;
|
|
529
|
+
const dir = context.imagesDir;
|
|
530
|
+
return textResult([
|
|
531
|
+
count === 0
|
|
532
|
+
? `No process runs an image in ${dir}.`
|
|
533
|
+
: count === 1
|
|
534
|
+
? `1 process runs an image in ${dir}.`
|
|
535
|
+
: `${count} processes run images in ${dir}.`,
|
|
536
|
+
...entries("processes", scoped.processes.map((process) => ({
|
|
537
|
+
name: process.imageName ?? "",
|
|
538
|
+
attributes: [["pid", String(process.pid)]],
|
|
539
|
+
}))),
|
|
540
|
+
]);
|
|
541
|
+
};
|
|
542
|
+
function sleep(milliseconds) {
|
|
543
|
+
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* Waits for each pid to exit, polling, as PLexus's stop waits: a delete
|
|
547
|
+
* right after a kill would otherwise meet a VM still exiting. The pids still
|
|
548
|
+
* there when the time is up.
|
|
549
|
+
*/
|
|
550
|
+
async function stillRunning(pids, options) {
|
|
551
|
+
const isAlive = options.nativeProcess?.isAlive ?? isPidAlive;
|
|
552
|
+
const timeoutMs = options.processExitTimeoutMs ?? defaultProcessExitTimeoutMs;
|
|
553
|
+
const pollMs = options.processExitPollMs ?? defaultProcessExitPollMs;
|
|
554
|
+
const wait = options.sleep ?? sleep;
|
|
555
|
+
let alive = pids.filter(isAlive);
|
|
556
|
+
for (let waited = 0; alive.length > 0 && waited < timeoutMs; waited += pollMs) {
|
|
557
|
+
await wait(Math.min(pollMs, timeoutMs - waited));
|
|
558
|
+
alive = alive.filter(isAlive);
|
|
559
|
+
}
|
|
560
|
+
return alive;
|
|
561
|
+
}
|
|
562
|
+
/** What a kill that failed said, without the pid it already names. */
|
|
563
|
+
function killMessage(pid, result) {
|
|
564
|
+
const diagnostic = result.diagnostic ?? `it exited with ${result.command.exitCode}`;
|
|
565
|
+
return clause(diagnostic.replace(new RegExp(`^Could not kill pid ${pid}: `), ""));
|
|
566
|
+
}
|
|
567
|
+
const answerProcessKill = async (context, input) => {
|
|
568
|
+
buildLauncherCommandArgs("pharo_launcher_process_kill", input);
|
|
569
|
+
const pidInput = input.pid;
|
|
570
|
+
const imageName = optionalString(input, "imageName");
|
|
571
|
+
let pid;
|
|
572
|
+
if (pidInput !== undefined) {
|
|
573
|
+
pid = Number(pidInput);
|
|
574
|
+
if (!Number.isSafeInteger(pid) || pid <= 0 || !/^\d+$/.test(String(pidInput).trim())) {
|
|
575
|
+
throw new ToolInputError("pid must be a process id, a whole number");
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
const scoped = await scopedProcesses(context);
|
|
579
|
+
if ("failure" in scoped) {
|
|
580
|
+
return failure(listFailureText(scoped.failure));
|
|
581
|
+
}
|
|
582
|
+
const image = imageName === undefined ? undefined : findByName(scanImages(context.imagesDir).images, imageName);
|
|
583
|
+
const targets = pid !== undefined
|
|
584
|
+
? scoped.processes.filter((process) => process.pid === pid)
|
|
585
|
+
: image
|
|
586
|
+
? scoped.processes.filter((process) => process.imageName === image.name)
|
|
587
|
+
: [];
|
|
588
|
+
if (targets.length === 0) {
|
|
589
|
+
return failure(pid !== undefined
|
|
590
|
+
? `No process of an image in ${context.imagesDir} has pid ${pid}. pharo_launcher_process_list says which run.`
|
|
591
|
+
: `No process runs image ${inlineValue(image?.name ?? imageName)}. pharo_launcher_process_list says which run.`);
|
|
592
|
+
}
|
|
593
|
+
// One pid per call: outside a profile the library sees every Pharo VM, and
|
|
594
|
+
// would match a name among all of them.
|
|
595
|
+
const isAlive = context.options.nativeProcess?.isAlive ?? isPidAlive;
|
|
596
|
+
const killed = [];
|
|
597
|
+
const failures = [];
|
|
598
|
+
for (const target of targets) {
|
|
599
|
+
const result = await context.launcher.killProcess({ pid: target.pid });
|
|
600
|
+
if (result.ok || !isAlive(target.pid)) {
|
|
601
|
+
killed.push(target.pid);
|
|
602
|
+
}
|
|
603
|
+
else {
|
|
604
|
+
failures.push(`could not kill pid ${target.pid}: ${killMessage(target.pid, result)}`);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
const seconds = Math.round((context.options.processExitTimeoutMs ?? defaultProcessExitTimeoutMs) / 1000);
|
|
608
|
+
const remaining = await stillRunning(killed, context.options);
|
|
609
|
+
const stopped = killed.filter((each) => !remaining.includes(each));
|
|
610
|
+
failures.push(...remaining.map((each) => `pid ${each} still runs ${seconds} s after being killed`));
|
|
611
|
+
const name = inlineValue(targets[0].imageName ?? "");
|
|
612
|
+
const killedClause = stopped.length === 0
|
|
613
|
+
? undefined
|
|
614
|
+
: `Killed ${name} (${plural(stopped.length, "pid")} ${stopped.join(", ")})`;
|
|
615
|
+
const clauses = [killedClause, ...failures].filter((each) => Boolean(each));
|
|
616
|
+
const text = `${capitalized(clauses.join("; "))}.`;
|
|
617
|
+
return failures.length === 0 ? textResult([text]) : failure(text);
|
|
618
|
+
};
|
|
619
|
+
// --- The templates ------------------------------------------------------------
|
|
620
|
+
const answerTemplateCategoryList = async (context) => {
|
|
621
|
+
const result = await context.launcher.listTemplateCategories();
|
|
622
|
+
if (!result.ok) {
|
|
623
|
+
return failure(launcherFailureText("list template categories", result, context));
|
|
624
|
+
}
|
|
625
|
+
const categories = result.data ?? [];
|
|
626
|
+
const count = categories.length;
|
|
627
|
+
if (count === 0) {
|
|
628
|
+
return textResult(["No template categories."]);
|
|
629
|
+
}
|
|
630
|
+
return textResult([
|
|
631
|
+
result.defaultCategory
|
|
632
|
+
? `${count} template ${plural(count, "category", "categories")}; ${result.defaultCategory} is the default.`
|
|
633
|
+
: `${count} template ${plural(count, "category", "categories")}.`,
|
|
634
|
+
"categories:",
|
|
635
|
+
...categories.map((category) => ` ${category}`),
|
|
636
|
+
]);
|
|
637
|
+
};
|
|
638
|
+
const answerTemplateList = async (context, input) => {
|
|
639
|
+
const templateCategory = optionalString(input, "templateCategory");
|
|
640
|
+
// What the Launcher lists, its copies of downloaded templates and the
|
|
641
|
+
// user's own included.
|
|
642
|
+
const result = await context.launcher.listTemplates({
|
|
643
|
+
...(templateCategory === undefined ? {} : { templateCategory }),
|
|
644
|
+
includeLocal: true,
|
|
645
|
+
});
|
|
646
|
+
if (!result.ok) {
|
|
647
|
+
const notFound = /Image template category '(.*?)' not found/.exec(rawOutput(result));
|
|
648
|
+
if (notFound && !result.command.timedOut) {
|
|
649
|
+
return noTemplatesYet(context.config)
|
|
650
|
+
? failure(noTemplatesYetText)
|
|
651
|
+
: failure(`There is no template category ${notFound[1]}. ` +
|
|
652
|
+
"pharo_launcher_template_category_list lists them.");
|
|
653
|
+
}
|
|
654
|
+
return failure(launcherFailureText("list templates", result, context));
|
|
655
|
+
}
|
|
656
|
+
const names = (result.data ?? []).map((template) => template.name ?? "");
|
|
657
|
+
const where = templateCategory ?? "the default category";
|
|
658
|
+
if (names.length === 0) {
|
|
659
|
+
return textResult([`No templates in ${where}.`]);
|
|
660
|
+
}
|
|
661
|
+
return textResult([
|
|
662
|
+
`${names.length} ${plural(names.length, "template")} in ${where}.`,
|
|
663
|
+
"templates:",
|
|
664
|
+
...names.map((name) => ` ${name}`),
|
|
665
|
+
]);
|
|
666
|
+
};
|
|
667
|
+
const answerTemplateUpdate = async (context) => {
|
|
668
|
+
const result = await context.launcher.updateTemplates();
|
|
669
|
+
const bootstrap = result.templateSourcesBootstrap;
|
|
670
|
+
if (result.ok) {
|
|
671
|
+
return textResult([
|
|
672
|
+
bootstrap?.action === "created"
|
|
673
|
+
? `Refreshed the template list, and downloaded this profile's sources.list from ${bootstrap.url}.`
|
|
674
|
+
: "Refreshed the template list.",
|
|
675
|
+
]);
|
|
676
|
+
}
|
|
677
|
+
if (bootstrap?.action === "failed") {
|
|
678
|
+
return failure("Refreshed the template list, but this profile still has no template sources: " +
|
|
679
|
+
`downloading ${bootstrap.url} failed (${clause(bootstrap.error ?? "")}). ` +
|
|
680
|
+
"Call pharo_launcher_template_update again, or put a sources.list in " +
|
|
681
|
+
`${path.dirname(bootstrap.path ?? "")}.`);
|
|
682
|
+
}
|
|
683
|
+
if (result.templateSourcesProbe && !result.templateSourcesProbe.ok && result.diagnostic) {
|
|
684
|
+
return failure(result.diagnostic);
|
|
685
|
+
}
|
|
686
|
+
return failure(launcherFailureText("refresh the template list", result, context));
|
|
687
|
+
};
|
|
688
|
+
/** The answer of each tool, by name: the table PLexus mirrors with functions. */
|
|
689
|
+
export const toolAnswers = {
|
|
690
|
+
pharo_launcher_config: answerConfig,
|
|
691
|
+
pharo_launcher_image_copy_between_profiles: answerCopyBetweenProfiles,
|
|
692
|
+
pharo_launcher_image_copy: answerImageCopy,
|
|
693
|
+
pharo_launcher_image_create: answerImageCreate,
|
|
694
|
+
pharo_launcher_image_delete: answerImageDelete,
|
|
695
|
+
pharo_launcher_image_info: answerImageInfo,
|
|
696
|
+
pharo_launcher_image_list: answerImageList,
|
|
697
|
+
pharo_launcher_image_start: answerImageStart,
|
|
698
|
+
pharo_launcher_process_kill: answerProcessKill,
|
|
699
|
+
pharo_launcher_process_list: answerProcessList,
|
|
700
|
+
pharo_launcher_template_category_list: answerTemplateCategoryList,
|
|
701
|
+
pharo_launcher_template_list: answerTemplateList,
|
|
702
|
+
pharo_launcher_template_update: answerTemplateUpdate,
|
|
703
|
+
};
|
|
704
|
+
/**
|
|
705
|
+
* Arguments an earlier release took and this one does not: an older caller
|
|
706
|
+
* still passes them, and they now change nothing, so they are dropped
|
|
707
|
+
* rather than refused.
|
|
708
|
+
*/
|
|
709
|
+
const retiredArguments = new Set([
|
|
710
|
+
"brief",
|
|
711
|
+
"confirm",
|
|
712
|
+
"delimiter",
|
|
713
|
+
"force",
|
|
714
|
+
"format",
|
|
715
|
+
"includeRaw",
|
|
716
|
+
"noLaunch",
|
|
717
|
+
"rowMode",
|
|
718
|
+
]);
|
|
719
|
+
/**
|
|
720
|
+
* Only the arguments a tool's schema names. One it never took is refused,
|
|
721
|
+
* as MCP and PLexus refuse it; one an earlier release took is dropped.
|
|
722
|
+
*/
|
|
723
|
+
export function toolArguments(schema, argumentsValue) {
|
|
724
|
+
const given = inputObject(argumentsValue);
|
|
725
|
+
const named = new Set(Object.keys(schema.properties ?? {}));
|
|
726
|
+
const unknown = Object.keys(given).filter((key) => !named.has(key) && !retiredArguments.has(key));
|
|
727
|
+
if (unknown.length > 0) {
|
|
728
|
+
throw new ToolInputError(`${unknown.join(", ")} ${unknown.length === 1 ? "is" : "are"} not accepted here`);
|
|
729
|
+
}
|
|
730
|
+
return Object.fromEntries(Object.keys(schema.properties ?? {})
|
|
731
|
+
.filter((key) => given[key] !== undefined)
|
|
732
|
+
.map((key) => [key, given[key]]));
|
|
733
|
+
}
|
|
734
|
+
/** `Invalid arguments for <tool>. <the error.> It takes <its arguments>.` */
|
|
735
|
+
export function invalidArgumentsText(toolName, schema, error) {
|
|
736
|
+
const properties = Object.keys(schema.properties ?? {});
|
|
737
|
+
return (`Invalid arguments for ${toolName}. ${sentence(error.message)} ` +
|
|
738
|
+
(properties.length === 0 ? "It takes no arguments." : `It takes ${properties.join(", ")}.`));
|
|
739
|
+
}
|
|
740
|
+
export function unknownToolText(toolName) {
|
|
741
|
+
return `There is no tool ${toolName}.`;
|
|
742
|
+
}
|
|
743
|
+
export { failure as failureResult };
|
|
744
|
+
//# sourceMappingURL=answers.js.map
|