@evref-bl/plexus 0.15.2 → 0.16.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 +8 -12
- package/dist/cli.js +8 -183
- package/dist/cli.js.map +1 -1
- package/dist/commandLine.d.ts +13 -0
- package/dist/commandLine.d.ts.map +1 -0
- package/dist/commandLine.js +247 -0
- package/dist/commandLine.js.map +1 -0
- package/dist/config/pharoMcpVersion.d.ts +6 -7
- package/dist/config/pharoMcpVersion.d.ts.map +1 -1
- package/dist/config/pharoMcpVersion.js +6 -7
- package/dist/config/pharoMcpVersion.js.map +1 -1
- package/dist/errors.d.ts +11 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +17 -0
- package/dist/errors.js.map +1 -0
- package/dist/forward/endpointHandoffFormat.d.ts +23 -0
- package/dist/forward/endpointHandoffFormat.d.ts.map +1 -0
- package/dist/forward/endpointHandoffFormat.js.map +1 -0
- package/dist/forward/index.d.ts +20 -0
- package/dist/forward/index.d.ts.map +1 -0
- package/dist/forward/index.js +77 -0
- package/dist/forward/index.js.map +1 -0
- package/dist/forward/jsonRpc.d.ts +20 -0
- package/dist/forward/jsonRpc.d.ts.map +1 -0
- package/dist/forward/jsonRpc.js +141 -0
- package/dist/forward/jsonRpc.js.map +1 -0
- package/dist/home.d.ts +15 -0
- package/dist/home.d.ts.map +1 -0
- package/dist/home.js +30 -0
- package/dist/home.js.map +1 -0
- package/dist/images/bases.d.ts +40 -0
- package/dist/images/bases.d.ts.map +1 -0
- package/dist/images/bases.js +190 -0
- package/dist/images/bases.js.map +1 -0
- package/dist/images/claims.d.ts +34 -0
- package/dist/images/claims.d.ts.map +1 -0
- package/dist/images/claims.js +119 -0
- package/dist/images/claims.js.map +1 -0
- package/dist/images/context.d.ts +22 -0
- package/dist/images/context.d.ts.map +1 -0
- package/dist/images/context.js +26 -0
- package/dist/images/context.js.map +1 -0
- package/dist/images/create.d.ts +19 -0
- package/dist/images/create.d.ts.map +1 -0
- package/dist/images/create.js +121 -0
- package/dist/images/create.js.map +1 -0
- package/dist/images/delete.d.ts +10 -0
- package/dist/images/delete.d.ts.map +1 -0
- package/dist/images/delete.js +31 -0
- package/dist/images/delete.js.map +1 -0
- package/dist/images/endpoint.d.ts +17 -0
- package/dist/images/endpoint.d.ts.map +1 -0
- package/dist/images/endpoint.js +58 -0
- package/dist/images/endpoint.js.map +1 -0
- package/dist/images/index.d.ts +28 -0
- package/dist/images/index.d.ts.map +1 -0
- package/dist/images/index.js +51 -0
- package/dist/images/index.js.map +1 -0
- package/dist/images/leftovers.d.ts +9 -0
- package/dist/images/leftovers.d.ts.map +1 -0
- package/dist/images/leftovers.js +38 -0
- package/dist/images/leftovers.js.map +1 -0
- package/dist/images/list.d.ts +12 -0
- package/dist/images/list.d.ts.map +1 -0
- package/dist/images/list.js +74 -0
- package/dist/images/list.js.map +1 -0
- package/dist/images/names.d.ts +15 -0
- package/dist/images/names.d.ts.map +1 -0
- package/dist/images/names.js +53 -0
- package/dist/images/names.js.map +1 -0
- package/dist/images/prepare.d.ts +18 -0
- package/dist/images/prepare.d.ts.map +1 -0
- package/dist/images/prepare.js +58 -0
- package/dist/images/prepare.js.map +1 -0
- package/dist/images/processes.d.ts +21 -0
- package/dist/images/processes.d.ts.map +1 -0
- package/dist/images/processes.js +83 -0
- package/dist/images/processes.js.map +1 -0
- package/dist/images/scripts.d.ts +39 -0
- package/dist/images/scripts.d.ts.map +1 -0
- package/dist/images/scripts.js +127 -0
- package/dist/images/scripts.js.map +1 -0
- package/dist/images/start.d.ts +23 -0
- package/dist/images/start.d.ts.map +1 -0
- package/dist/images/start.js +173 -0
- package/dist/images/start.js.map +1 -0
- package/dist/images/stop.d.ts +11 -0
- package/dist/images/stop.d.ts.map +1 -0
- package/dist/images/stop.js +42 -0
- package/dist/images/stop.js.map +1 -0
- package/dist/images/store.d.ts +62 -0
- package/dist/images/store.d.ts.map +1 -0
- package/dist/images/store.js +180 -0
- package/dist/images/store.js.map +1 -0
- package/dist/images/templates.d.ts +14 -0
- package/dist/images/templates.d.ts.map +1 -0
- package/dist/images/templates.js +84 -0
- package/dist/images/templates.js.map +1 -0
- package/dist/index.d.ts +12 -22
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -22
- package/dist/index.js.map +1 -1
- package/dist/launcher/pharoLauncherMcpClient.d.ts +22 -59
- package/dist/launcher/pharoLauncherMcpClient.d.ts.map +1 -1
- package/dist/launcher/pharoLauncherMcpClient.js +121 -98
- package/dist/launcher/pharoLauncherMcpClient.js.map +1 -1
- package/dist/launcher/pharoLauncherProfile.d.ts +5 -32
- package/dist/launcher/pharoLauncherProfile.d.ts.map +1 -1
- package/dist/launcher/pharoLauncherProfile.js +17 -115
- package/dist/launcher/pharoLauncherProfile.js.map +1 -1
- package/dist/mcp/server.d.ts +18 -0
- package/dist/mcp/server.d.ts.map +1 -0
- package/dist/mcp/server.js +206 -0
- package/dist/mcp/server.js.map +1 -0
- package/dist/types.d.ts +68 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +5 -0
- package/dist/version.js.map +1 -0
- package/package.json +3 -12
- package/dist/config/config.d.ts +0 -15
- package/dist/config/config.d.ts.map +0 -1
- package/dist/config/config.js +0 -74
- package/dist/config/config.js.map +0 -1
- package/dist/config/plexusHome.d.ts +0 -17
- package/dist/config/plexusHome.d.ts.map +0 -1
- package/dist/config/plexusHome.js +0 -30
- package/dist/config/plexusHome.js.map +0 -1
- package/dist/config/projectConfig.d.ts +0 -141
- package/dist/config/projectConfig.d.ts.map +0 -1
- package/dist/config/projectConfig.js +0 -722
- package/dist/config/projectConfig.js.map +0 -1
- package/dist/config/projectConfigAuthoring.d.ts +0 -53
- package/dist/config/projectConfigAuthoring.d.ts.map +0 -1
- package/dist/config/projectConfigAuthoring.js +0 -159
- package/dist/config/projectConfigAuthoring.js.map +0 -1
- package/dist/gateway/projectImageMcpEndpoint.d.ts +0 -23
- package/dist/gateway/projectImageMcpEndpoint.d.ts.map +0 -1
- package/dist/gateway/projectImageMcpEndpoint.js +0 -40
- package/dist/gateway/projectImageMcpEndpoint.js.map +0 -1
- package/dist/gateway/server/index.d.ts +0 -8
- package/dist/gateway/server/index.d.ts.map +0 -1
- package/dist/gateway/server/index.js +0 -34
- package/dist/gateway/server/index.js.map +0 -1
- package/dist/gateway/server/mcp/server.d.ts +0 -66
- package/dist/gateway/server/mcp/server.d.ts.map +0 -1
- package/dist/gateway/server/mcp/server.js +0 -458
- package/dist/gateway/server/mcp/server.js.map +0 -1
- package/dist/gateway/server/routing/endpointHandoffFormat.d.ts +0 -26
- package/dist/gateway/server/routing/endpointHandoffFormat.d.ts.map +0 -1
- package/dist/gateway/server/routing/endpointHandoffFormat.js.map +0 -1
- package/dist/gateway/server/routing/gateway.d.ts +0 -167
- package/dist/gateway/server/routing/gateway.d.ts.map +0 -1
- package/dist/gateway/server/routing/gateway.js +0 -863
- package/dist/gateway/server/routing/gateway.js.map +0 -1
- package/dist/gateway/server/routing/imageMcpRouter.d.ts +0 -59
- package/dist/gateway/server/routing/imageMcpRouter.d.ts.map +0 -1
- package/dist/gateway/server/routing/imageMcpRouter.js +0 -253
- package/dist/gateway/server/routing/imageMcpRouter.js.map +0 -1
- package/dist/gateway/server/routing/pharoFacade.d.ts +0 -12
- package/dist/gateway/server/routing/pharoFacade.d.ts.map +0 -1
- package/dist/gateway/server/routing/pharoFacade.js +0 -67
- package/dist/gateway/server/routing/pharoFacade.js.map +0 -1
- package/dist/gateway/server/routing/routeDiscovery.d.ts +0 -26
- package/dist/gateway/server/routing/routeDiscovery.d.ts.map +0 -1
- package/dist/gateway/server/routing/routeDiscovery.js +0 -46
- package/dist/gateway/server/routing/routeDiscovery.js.map +0 -1
- package/dist/gateway/server/routing/routingTable.d.ts +0 -120
- package/dist/gateway/server/routing/routingTable.d.ts.map +0 -1
- package/dist/gateway/server/routing/routingTable.js +0 -215
- package/dist/gateway/server/routing/routingTable.js.map +0 -1
- package/dist/gateway/server/support/processLiveness.d.ts +0 -10
- package/dist/gateway/server/support/processLiveness.d.ts.map +0 -1
- package/dist/gateway/server/support/processLiveness.js +0 -25
- package/dist/gateway/server/support/processLiveness.js.map +0 -1
- package/dist/gateway/server/support/sourceBuildPreflight.d.ts +0 -15
- package/dist/gateway/server/support/sourceBuildPreflight.d.ts.map +0 -1
- package/dist/gateway/server/support/sourceBuildPreflight.js +0 -88
- package/dist/gateway/server/support/sourceBuildPreflight.js.map +0 -1
- package/dist/gateway/server/support/toolResultFormatting.d.ts +0 -8
- package/dist/gateway/server/support/toolResultFormatting.d.ts.map +0 -1
- package/dist/gateway/server/support/toolResultFormatting.js +0 -74
- package/dist/gateway/server/support/toolResultFormatting.js.map +0 -1
- package/dist/images/homeImageCache.d.ts +0 -302
- package/dist/images/homeImageCache.d.ts.map +0 -1
- package/dist/images/homeImageCache.js +0 -1031
- package/dist/images/homeImageCache.js.map +0 -1
- package/dist/images/imageRescue.d.ts +0 -151
- package/dist/images/imageRescue.d.ts.map +0 -1
- package/dist/images/imageRescue.js +0 -748
- package/dist/images/imageRescue.js.map +0 -1
- package/dist/images/imageRuntime.d.ts +0 -60
- package/dist/images/imageRuntime.d.ts.map +0 -1
- package/dist/images/imageRuntime.js +0 -90
- package/dist/images/imageRuntime.js.map +0 -1
- package/dist/images/preparedImageCache.d.ts +0 -86
- package/dist/images/preparedImageCache.d.ts.map +0 -1
- package/dist/images/preparedImageCache.js +0 -229
- package/dist/images/preparedImageCache.js.map +0 -1
- package/dist/images/scopedTemplateCatalog.d.ts +0 -33
- package/dist/images/scopedTemplateCatalog.d.ts.map +0 -1
- package/dist/images/scopedTemplateCatalog.js +0 -363
- package/dist/images/scopedTemplateCatalog.js.map +0 -1
- package/dist/internal.d.ts +0 -20
- package/dist/internal.d.ts.map +0 -1
- package/dist/internal.js +0 -20
- package/dist/internal.js.map +0 -1
- package/dist/launcher/scopedPharoLauncherServer.d.ts +0 -349
- package/dist/launcher/scopedPharoLauncherServer.d.ts.map +0 -1
- package/dist/launcher/scopedPharoLauncherServer.js +0 -1307
- package/dist/launcher/scopedPharoLauncherServer.js.map +0 -1
- package/dist/lifecycle/startProjectImage.d.ts +0 -94
- package/dist/lifecycle/startProjectImage.d.ts.map +0 -1
- package/dist/lifecycle/startProjectImage.js +0 -1054
- package/dist/lifecycle/startProjectImage.js.map +0 -1
- package/dist/lifecycle/stopProjectImage.d.ts +0 -31
- package/dist/lifecycle/stopProjectImage.d.ts.map +0 -1
- package/dist/lifecycle/stopProjectImage.js +0 -201
- package/dist/lifecycle/stopProjectImage.js.map +0 -1
- package/dist/mcp/agentServer.d.ts +0 -15
- package/dist/mcp/agentServer.d.ts.map +0 -1
- package/dist/mcp/agentServer.js +0 -67
- package/dist/mcp/agentServer.js.map +0 -1
- package/dist/mcp/imageOperations.d.ts +0 -54
- package/dist/mcp/imageOperations.d.ts.map +0 -1
- package/dist/mcp/imageOperations.js +0 -223
- package/dist/mcp/imageOperations.js.map +0 -1
- package/dist/mcp/pharoMcpHealth.d.ts +0 -26
- package/dist/mcp/pharoMcpHealth.d.ts.map +0 -1
- package/dist/mcp/pharoMcpHealth.js +0 -97
- package/dist/mcp/pharoMcpHealth.js.map +0 -1
- package/dist/support/pathStyle.d.ts +0 -11
- package/dist/support/pathStyle.d.ts.map +0 -1
- package/dist/support/pathStyle.js +0 -61
- package/dist/support/pathStyle.js.map +0 -1
- package/dist/support/toolResultFormatting.d.ts +0 -8
- package/dist/support/toolResultFormatting.d.ts.map +0 -1
- package/dist/support/toolResultFormatting.js +0 -74
- package/dist/support/toolResultFormatting.js.map +0 -1
- package/dist/workspace/projectStartupScript.d.ts +0 -73
- package/dist/workspace/projectStartupScript.d.ts.map +0 -1
- package/dist/workspace/projectStartupScript.js +0 -607
- package/dist/workspace/projectStartupScript.js.map +0 -1
- package/dist/workspace/projectState.d.ts +0 -216
- package/dist/workspace/projectState.d.ts.map +0 -1
- package/dist/workspace/projectState.js +0 -172
- package/dist/workspace/projectState.js.map +0 -1
- /package/dist/{gateway/server/routing → forward}/endpointHandoffFormat.js +0 -0
|
@@ -1,1307 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
3
|
-
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
4
|
-
import { CallToolRequestSchema, ListToolsRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
|
|
5
|
-
import { loadProjectConfig, projectImageCreateCleanupPolicy, projectImageDisplayMode, projectConfigId, resolveProjectRuntimePolicy, } from "../config/projectConfig.js";
|
|
6
|
-
import { StopProjectImageError, stopProjectImage, } from "../lifecycle/stopProjectImage.js";
|
|
7
|
-
import { hostPharoProcesses, processesOnImage, StartProjectImageError, startProjectImage, } from "../lifecycle/startProjectImage.js";
|
|
8
|
-
import { formatToolResultPayload, toolResultDetailFromArguments, toolResultDetailSchema, } from "../support/toolResultFormatting.js";
|
|
9
|
-
import { homeImageCacheNetworkPolicy } from "../config/plexusHome.js";
|
|
10
|
-
import { materializeProjectImageFromHomeCache, recordHomeImageCacheBase, } from "../images/homeImageCache.js";
|
|
11
|
-
import { observeImageRuntime, } from "../images/imageRuntime.js";
|
|
12
|
-
import { openPharoLauncherClient, PharoLauncherMcpToolError, } from "./pharoLauncherMcpClient.js";
|
|
13
|
-
import { describePharoLauncherMcpProfile, pharoLauncherMcpProfileEnvironment, } from "./pharoLauncherProfile.js";
|
|
14
|
-
import { createProjectState, defaultWorkspaceId, loadProjectState, projectStatePath, renderProjectImageName, runtimeStatusForImages, sanitizeRuntimeId, saveProjectState, } from "../workspace/projectState.js";
|
|
15
|
-
import { projectScriptsDirectoryPath } from "../workspace/projectStartupScript.js";
|
|
16
|
-
import { dirnamePathLike, joinPathLike } from "../support/pathStyle.js";
|
|
17
|
-
import { prepareScopedTemplateCatalog, templateCategoryNames, } from "../images/scopedTemplateCatalog.js";
|
|
18
|
-
const stringSchema = { type: "string", minLength: 1 };
|
|
19
|
-
const displayModeSchema = {
|
|
20
|
-
type: "string",
|
|
21
|
-
enum: ["headless", "interactive"],
|
|
22
|
-
};
|
|
23
|
-
const displayModeSnapshotTimeoutMs = 60_000;
|
|
24
|
-
const displayModeSnapshotPollIntervalMs = 100;
|
|
25
|
-
function objectSchema(properties, required = []) {
|
|
26
|
-
return {
|
|
27
|
-
type: "object",
|
|
28
|
-
properties,
|
|
29
|
-
required,
|
|
30
|
-
additionalProperties: false,
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* A held client whose opening failed has nothing to close, and a close that
|
|
35
|
-
* fails must not become the operation's error: the work it was holding the
|
|
36
|
-
* client for is already done.
|
|
37
|
-
*/
|
|
38
|
-
async function closeHeldClient(held) {
|
|
39
|
-
if (!held) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
try {
|
|
43
|
-
await (await held).close?.();
|
|
44
|
-
}
|
|
45
|
-
catch {
|
|
46
|
-
// Nothing left to do with it.
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
export class ScopedPharoLauncherError extends Error {
|
|
50
|
-
details;
|
|
51
|
-
constructor(message, details) {
|
|
52
|
-
super(message);
|
|
53
|
-
this.details = details;
|
|
54
|
-
this.name = "ScopedPharoLauncherError";
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
function textResult(value, isError = false, detail = "summary") {
|
|
58
|
-
return {
|
|
59
|
-
content: [
|
|
60
|
-
{
|
|
61
|
-
type: "text",
|
|
62
|
-
text: JSON.stringify(formatToolResultPayload(value, detail), null, 2),
|
|
63
|
-
},
|
|
64
|
-
],
|
|
65
|
-
...(isError ? { isError } : {}),
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
function scopedErrorPayload(error) {
|
|
69
|
-
const payload = {
|
|
70
|
-
ok: false,
|
|
71
|
-
error: error instanceof Error ? error.message : String(error),
|
|
72
|
-
};
|
|
73
|
-
if (error instanceof PharoLauncherMcpToolError) {
|
|
74
|
-
return {
|
|
75
|
-
...payload,
|
|
76
|
-
cause: {
|
|
77
|
-
toolName: error.toolName,
|
|
78
|
-
result: error.result,
|
|
79
|
-
},
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
if (error instanceof ScopedPharoLauncherError &&
|
|
83
|
-
error.details !== undefined) {
|
|
84
|
-
return {
|
|
85
|
-
...payload,
|
|
86
|
-
cause: error.details,
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
return payload;
|
|
90
|
-
}
|
|
91
|
-
function objectInput(input) {
|
|
92
|
-
if (!input || typeof input !== "object" || Array.isArray(input)) {
|
|
93
|
-
return {};
|
|
94
|
-
}
|
|
95
|
-
return input;
|
|
96
|
-
}
|
|
97
|
-
function requireString(input, key) {
|
|
98
|
-
const value = input[key];
|
|
99
|
-
if (typeof value !== "string" || value.length === 0) {
|
|
100
|
-
throw new ScopedPharoLauncherError(`${key} is required`);
|
|
101
|
-
}
|
|
102
|
-
return value;
|
|
103
|
-
}
|
|
104
|
-
function optionalString(input, key) {
|
|
105
|
-
const value = input[key];
|
|
106
|
-
if (value === undefined) {
|
|
107
|
-
return undefined;
|
|
108
|
-
}
|
|
109
|
-
if (typeof value !== "string" || value.length === 0) {
|
|
110
|
-
throw new ScopedPharoLauncherError(`${key} must be a non-empty string`);
|
|
111
|
-
}
|
|
112
|
-
return value;
|
|
113
|
-
}
|
|
114
|
-
function optionalBoolean(input, key) {
|
|
115
|
-
const value = input[key];
|
|
116
|
-
if (value === undefined) {
|
|
117
|
-
return undefined;
|
|
118
|
-
}
|
|
119
|
-
if (typeof value !== "boolean") {
|
|
120
|
-
throw new ScopedPharoLauncherError(`${key} must be a boolean`);
|
|
121
|
-
}
|
|
122
|
-
return value;
|
|
123
|
-
}
|
|
124
|
-
function requireDisplayMode(input, key) {
|
|
125
|
-
const value = optionalDisplayMode(input, key);
|
|
126
|
-
if (value === undefined) {
|
|
127
|
-
throw new ScopedPharoLauncherError(`${key} is required`);
|
|
128
|
-
}
|
|
129
|
-
return value;
|
|
130
|
-
}
|
|
131
|
-
function optionalDisplayMode(input, key) {
|
|
132
|
-
const value = input[key];
|
|
133
|
-
if (value === undefined) {
|
|
134
|
-
return undefined;
|
|
135
|
-
}
|
|
136
|
-
if (value === "headless" || value === "interactive") {
|
|
137
|
-
return value;
|
|
138
|
-
}
|
|
139
|
-
throw new ScopedPharoLauncherError(`${key} must be headless or interactive`);
|
|
140
|
-
}
|
|
141
|
-
const leaseOwnerKinds = new Set([
|
|
142
|
-
"target",
|
|
143
|
-
"workspace",
|
|
144
|
-
"human",
|
|
145
|
-
"unknown",
|
|
146
|
-
]);
|
|
147
|
-
function nonEmptyEnv(value) {
|
|
148
|
-
const trimmed = value?.trim();
|
|
149
|
-
return trimmed && trimmed.length > 0 ? trimmed : undefined;
|
|
150
|
-
}
|
|
151
|
-
function leaseOwnerKindFromText(value) {
|
|
152
|
-
if (!value) {
|
|
153
|
-
return undefined;
|
|
154
|
-
}
|
|
155
|
-
if (!leaseOwnerKinds.has(value)) {
|
|
156
|
-
throw new ScopedPharoLauncherError(`PLEXUS_IMAGE_LEASE_OWNER_KIND must be one of ${[...leaseOwnerKinds].join(", ")}`);
|
|
157
|
-
}
|
|
158
|
-
return value;
|
|
159
|
-
}
|
|
160
|
-
function leaseTtlMsFromText(value) {
|
|
161
|
-
if (!value) {
|
|
162
|
-
return undefined;
|
|
163
|
-
}
|
|
164
|
-
const ttlMs = Number(value);
|
|
165
|
-
if (!Number.isInteger(ttlMs) || ttlMs <= 0) {
|
|
166
|
-
throw new ScopedPharoLauncherError("PLEXUS_IMAGE_LEASE_TTL_MS must be a positive integer");
|
|
167
|
-
}
|
|
168
|
-
return ttlMs;
|
|
169
|
-
}
|
|
170
|
-
export function scopedImageLeaseOptionsFromEnvironment(env) {
|
|
171
|
-
const ownerId = nonEmptyEnv(env.PLEXUS_IMAGE_LEASE_OWNER_ID);
|
|
172
|
-
const ownerKind = leaseOwnerKindFromText(nonEmptyEnv(env.PLEXUS_IMAGE_LEASE_OWNER_KIND));
|
|
173
|
-
const purpose = nonEmptyEnv(env.PLEXUS_IMAGE_LEASE_PURPOSE);
|
|
174
|
-
const repositoryPath = nonEmptyEnv(env.PLEXUS_IMAGE_LEASE_REPOSITORY_PATH);
|
|
175
|
-
const branch = nonEmptyEnv(env.PLEXUS_IMAGE_LEASE_BRANCH);
|
|
176
|
-
const ttlMs = leaseTtlMsFromText(nonEmptyEnv(env.PLEXUS_IMAGE_LEASE_TTL_MS));
|
|
177
|
-
const cleanupCommand = nonEmptyEnv(env.PLEXUS_IMAGE_LEASE_CLEANUP_COMMAND);
|
|
178
|
-
if (!ownerId &&
|
|
179
|
-
!ownerKind &&
|
|
180
|
-
!purpose &&
|
|
181
|
-
!repositoryPath &&
|
|
182
|
-
!branch &&
|
|
183
|
-
ttlMs === undefined &&
|
|
184
|
-
!cleanupCommand) {
|
|
185
|
-
return undefined;
|
|
186
|
-
}
|
|
187
|
-
return {
|
|
188
|
-
...(ownerId ? { ownerId } : {}),
|
|
189
|
-
...(ownerKind ? { ownerKind } : {}),
|
|
190
|
-
...(purpose ? { purpose } : {}),
|
|
191
|
-
...(repositoryPath ? { repositoryPath } : {}),
|
|
192
|
-
...(branch ? { branch } : {}),
|
|
193
|
-
...(ttlMs !== undefined ? { ttlMs } : {}),
|
|
194
|
-
...(cleanupCommand ? { cleanupCommand } : {}),
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
function assertLauncherOk(result, toolName) {
|
|
198
|
-
if (result && result.ok === false) {
|
|
199
|
-
throw new ScopedPharoLauncherError(`${toolName} returned ok: false`, {
|
|
200
|
-
toolName,
|
|
201
|
-
result,
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
async function ensureScopedTemplateCatalogReady(options) {
|
|
206
|
-
const runtime = resolveProjectRuntimePolicy(options.projectConfig);
|
|
207
|
-
const launcherProfile = describePharoLauncherMcpProfile({
|
|
208
|
-
projectRoot: options.scope.projectRoot,
|
|
209
|
-
config: options.projectConfig,
|
|
210
|
-
workspaceId: options.scope.workspaceId,
|
|
211
|
-
stateRoot: options.scope.stateRoot,
|
|
212
|
-
});
|
|
213
|
-
const bootstrap = await prepareScopedTemplateCatalog({
|
|
214
|
-
destinationDirectory: launcherProfile.templateSourcesDir,
|
|
215
|
-
policy: runtime.launcherProfile.templateCatalog,
|
|
216
|
-
networkPolicy: homeImageCacheNetworkPolicy(options.projectConfig),
|
|
217
|
-
fetch: options.fetch,
|
|
218
|
-
});
|
|
219
|
-
if (!bootstrap.ok) {
|
|
220
|
-
throw new ScopedPharoLauncherError(bootstrap.message, bootstrap.details);
|
|
221
|
-
}
|
|
222
|
-
if (!bootstrap.refreshTemplateCatalog) {
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
const result = await options.client.callTool("pharo_launcher_template_update", {});
|
|
226
|
-
assertLauncherOk(result, "pharo_launcher_template_update");
|
|
227
|
-
}
|
|
228
|
-
function scopedMutationApproval(approval, operation) {
|
|
229
|
-
return approval ?? { approved: true, approvalId: operation };
|
|
230
|
-
}
|
|
231
|
-
function resolveScope(options) {
|
|
232
|
-
const projectConfig = loadProjectConfig(options.projectRoot);
|
|
233
|
-
const workspaceId = options.workspaceId ?? defaultWorkspaceId(options.projectRoot);
|
|
234
|
-
const stateRoot = options.stateRoot;
|
|
235
|
-
return {
|
|
236
|
-
projectRoot: options.projectRoot,
|
|
237
|
-
projectId: projectConfigId(projectConfig),
|
|
238
|
-
projectName: projectConfig.name,
|
|
239
|
-
workspaceId,
|
|
240
|
-
...(stateRoot ? { stateRoot } : {}),
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
function scopeSummary(scope) {
|
|
244
|
-
return {
|
|
245
|
-
projectId: scope.projectId,
|
|
246
|
-
projectName: scope.projectName,
|
|
247
|
-
workspaceId: scope.workspaceId,
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
function launcherProfileSummary(profile) {
|
|
251
|
-
return {
|
|
252
|
-
ownership: profile.ownership,
|
|
253
|
-
mode: profile.mode,
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
function imageSummary(imageConfig, imageState, runtime) {
|
|
257
|
-
const defaultDisplayMode = projectImageDisplayMode(imageConfig);
|
|
258
|
-
const displayMode = imageState?.displayMode ?? defaultDisplayMode;
|
|
259
|
-
return {
|
|
260
|
-
imageId: imageConfig.id,
|
|
261
|
-
status: runtime.status,
|
|
262
|
-
runtime,
|
|
263
|
-
displayMode,
|
|
264
|
-
displayModes: {
|
|
265
|
-
default: defaultDisplayMode,
|
|
266
|
-
...(imageState?.displayMode ? { current: imageState.displayMode } : {}),
|
|
267
|
-
},
|
|
268
|
-
...(imageState?.pharoMcpContract
|
|
269
|
-
? { pharoMcpContract: imageState.pharoMcpContract }
|
|
270
|
-
: {}),
|
|
271
|
-
...(imageState?.lease ? { lease: imageState.lease } : {}),
|
|
272
|
-
};
|
|
273
|
-
}
|
|
274
|
-
function statePathForScope(scope, projectConfig = loadProjectConfig(scope.projectRoot)) {
|
|
275
|
-
return projectStatePath({
|
|
276
|
-
projectRoot: scope.projectRoot,
|
|
277
|
-
stateRoot: scope.stateRoot,
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
function leaseOwnerDescription(lease) {
|
|
281
|
-
return `${lease.ownerKind} ${lease.ownerId}`;
|
|
282
|
-
}
|
|
283
|
-
function leaseExpired(lease, now) {
|
|
284
|
-
if (!lease.expiresAt) {
|
|
285
|
-
return false;
|
|
286
|
-
}
|
|
287
|
-
const expiresAt = Date.parse(lease.expiresAt);
|
|
288
|
-
return Number.isFinite(expiresAt) && expiresAt <= now.getTime();
|
|
289
|
-
}
|
|
290
|
-
function sameLeaseOwner(left, right) {
|
|
291
|
-
return left.ownerKind === right.ownerKind && left.ownerId === right.ownerId;
|
|
292
|
-
}
|
|
293
|
-
function imageLeaseForMutation(scope, options, now, existingLease, mode = "mutable") {
|
|
294
|
-
const ownerId = options?.ownerId ?? scope.workspaceId;
|
|
295
|
-
const ownerKind = options?.ownerKind ?? "workspace";
|
|
296
|
-
const retainedLease = existingLease?.ownerId === ownerId && existingLease.ownerKind === ownerKind
|
|
297
|
-
? existingLease
|
|
298
|
-
: undefined;
|
|
299
|
-
const createdAt = retainedLease?.createdAt ?? now.toISOString();
|
|
300
|
-
const expiresAt = options?.ttlMs !== undefined
|
|
301
|
-
? new Date(now.getTime() + options.ttlMs).toISOString()
|
|
302
|
-
: retainedLease?.expiresAt;
|
|
303
|
-
return {
|
|
304
|
-
ownerId,
|
|
305
|
-
ownerKind,
|
|
306
|
-
mode,
|
|
307
|
-
purpose: options?.purpose ??
|
|
308
|
-
retainedLease?.purpose ??
|
|
309
|
-
"Scoped Pharo image lifecycle",
|
|
310
|
-
createdAt,
|
|
311
|
-
heartbeatAt: now.toISOString(),
|
|
312
|
-
...(expiresAt ? { expiresAt } : {}),
|
|
313
|
-
...(options?.repositoryPath ?? retainedLease?.repositoryPath
|
|
314
|
-
? {
|
|
315
|
-
repositoryPath: options?.repositoryPath ?? retainedLease?.repositoryPath,
|
|
316
|
-
}
|
|
317
|
-
: {}),
|
|
318
|
-
...(options?.branch ?? retainedLease?.branch
|
|
319
|
-
? { branch: options?.branch ?? retainedLease?.branch }
|
|
320
|
-
: {}),
|
|
321
|
-
...(options?.cleanupCommand ?? retainedLease?.cleanupCommand
|
|
322
|
-
? {
|
|
323
|
-
cleanupCommand: options?.cleanupCommand ?? retainedLease?.cleanupCommand,
|
|
324
|
-
}
|
|
325
|
-
: {}),
|
|
326
|
-
};
|
|
327
|
-
}
|
|
328
|
-
function assertImageLeaseAllowsMutation(imageId, existingLease, requestedLease, now) {
|
|
329
|
-
if (!existingLease ||
|
|
330
|
-
leaseExpired(existingLease, now) ||
|
|
331
|
-
sameLeaseOwner(existingLease, requestedLease)) {
|
|
332
|
-
return;
|
|
333
|
-
}
|
|
334
|
-
const expiry = existingLease.expiresAt
|
|
335
|
-
? ` until ${existingLease.expiresAt}`
|
|
336
|
-
: "";
|
|
337
|
-
throw new ScopedPharoLauncherError(`Image ${imageId} is leased to ${leaseOwnerDescription(existingLease)} for ${existingLease.purpose}${expiry}; mutation requires ${leaseOwnerDescription(requestedLease)}`);
|
|
338
|
-
}
|
|
339
|
-
function stateWithImageLease(state, imageId, lease, updatedAt) {
|
|
340
|
-
return {
|
|
341
|
-
...state,
|
|
342
|
-
images: state.images.map((image) => image.id === imageId ? { ...image, lease } : image),
|
|
343
|
-
updatedAt: updatedAt.toISOString(),
|
|
344
|
-
};
|
|
345
|
-
}
|
|
346
|
-
function findImageConfig(projectConfig, imageId) {
|
|
347
|
-
const imageConfig = projectConfig.images.find((image) => image.id === imageId);
|
|
348
|
-
if (!imageConfig) {
|
|
349
|
-
throw new ScopedPharoLauncherError(`Image ${imageId} is not declared in this PLexus workspace`);
|
|
350
|
-
}
|
|
351
|
-
return imageConfig;
|
|
352
|
-
}
|
|
353
|
-
function renderedImageName(scope, imageConfig) {
|
|
354
|
-
return renderProjectImageName(imageConfig.imageName, {
|
|
355
|
-
projectId: scope.projectId,
|
|
356
|
-
projectName: scope.projectName,
|
|
357
|
-
workspaceId: scope.workspaceId,
|
|
358
|
-
imageId: imageConfig.id,
|
|
359
|
-
});
|
|
360
|
-
}
|
|
361
|
-
function imageCreationState(imageConfig) {
|
|
362
|
-
const create = imageConfig.create;
|
|
363
|
-
if (!create) {
|
|
364
|
-
return undefined;
|
|
365
|
-
}
|
|
366
|
-
return {
|
|
367
|
-
...(create.role ? { role: create.role } : {}),
|
|
368
|
-
source: {
|
|
369
|
-
kind: create.kind,
|
|
370
|
-
...(create.profileId ? { profileId: create.profileId } : {}),
|
|
371
|
-
templateName: create.templateName,
|
|
372
|
-
...(create.templateCategory
|
|
373
|
-
? { templateCategory: create.templateCategory }
|
|
374
|
-
: {}),
|
|
375
|
-
},
|
|
376
|
-
cleanupPolicy: projectImageCreateCleanupPolicy(create),
|
|
377
|
-
route: {
|
|
378
|
-
serverName: "pharo_gateway",
|
|
379
|
-
targetKey: "workspaceId",
|
|
380
|
-
imageArgument: "mcpServerId",
|
|
381
|
-
imageId: imageConfig.id,
|
|
382
|
-
},
|
|
383
|
-
};
|
|
384
|
-
}
|
|
385
|
-
function assertPreviousStateBelongsToScope(previousState, scope) {
|
|
386
|
-
if (!previousState) {
|
|
387
|
-
return;
|
|
388
|
-
}
|
|
389
|
-
if (previousState.projectId !== scope.projectId) {
|
|
390
|
-
throw new ScopedPharoLauncherError(`State file belongs to project ${previousState.projectId}; scoped create for project ${scope.projectId} is rejected`);
|
|
391
|
-
}
|
|
392
|
-
if (previousState.workspaceId !== scope.workspaceId) {
|
|
393
|
-
throw new ScopedPharoLauncherError(`State file belongs to workspace ${previousState.workspaceId}; scoped create for workspace ${scope.workspaceId} is rejected`);
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
function assertWorkspaceImagePolicyAllowsCreate(projectConfig, previousState, imageId) {
|
|
397
|
-
const maxCount = resolveProjectRuntimePolicy(projectConfig).workspaceImages?.maxCount;
|
|
398
|
-
if (maxCount === undefined) {
|
|
399
|
-
return;
|
|
400
|
-
}
|
|
401
|
-
const currentImageCount = previousState?.images.length ?? 0;
|
|
402
|
-
if (currentImageCount >= maxCount) {
|
|
403
|
-
throw new ScopedPharoLauncherError(`Workspace image policy allows at most ${maxCount} image${maxCount === 1 ? "" : "s"}; scoped create for image ${imageId} is rejected`);
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
function imageMcpSnapshotEndpoint(imageState) {
|
|
407
|
-
return imageState.mcpServers?.find((server) => server.id === imageState.id)
|
|
408
|
-
?.endpoint;
|
|
409
|
-
}
|
|
410
|
-
function endpointUrl(endpoint) {
|
|
411
|
-
const host = endpoint.host.includes(":") && !endpoint.host.startsWith("[")
|
|
412
|
-
? `[${endpoint.host}]`
|
|
413
|
-
: endpoint.host;
|
|
414
|
-
return `http://${host}:${endpoint.port}${endpoint.path}`;
|
|
415
|
-
}
|
|
416
|
-
function smalltalkString(value) {
|
|
417
|
-
return `'${value.replaceAll("'", "''")}'`;
|
|
418
|
-
}
|
|
419
|
-
function smalltalkPath(value) {
|
|
420
|
-
return smalltalkString(value.replace(/\\/g, "/"));
|
|
421
|
-
}
|
|
422
|
-
function isObject(value) {
|
|
423
|
-
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
424
|
-
}
|
|
425
|
-
function defaultSleep(durationMs) {
|
|
426
|
-
return new Promise((resolve) => setTimeout(resolve, durationMs));
|
|
427
|
-
}
|
|
428
|
-
function displayModeSnapshotStatusPath(scope, imageId, now) {
|
|
429
|
-
const scriptsDirectory = projectScriptsDirectoryPath({
|
|
430
|
-
projectRoot: scope.projectRoot,
|
|
431
|
-
stateRoot: scope.stateRoot,
|
|
432
|
-
});
|
|
433
|
-
return joinPathLike(scriptsDirectory, `display-mode-snapshot-${sanitizeRuntimeId(imageId)}-${now.getTime()}.properties`);
|
|
434
|
-
}
|
|
435
|
-
function displayModeSnapshotScript(statusPath) {
|
|
436
|
-
return `| snapshotStatusFile |
|
|
437
|
-
snapshotStatusFile := ${smalltalkPath(statusPath)} asFileReference.
|
|
438
|
-
[
|
|
439
|
-
(Delay forMilliseconds: 500) wait.
|
|
440
|
-
[
|
|
441
|
-
| server |
|
|
442
|
-
server := Smalltalk globals at: #PLexusMCPServer ifAbsent: [ nil ].
|
|
443
|
-
server ifNotNil: [
|
|
444
|
-
[ server stop ] on: Error do: [ :error | nil ].
|
|
445
|
-
Smalltalk globals removeKey: #PLexusMCPServer ifAbsent: [ nil ] ].
|
|
446
|
-
Smalltalk snapshot: true andQuit: false.
|
|
447
|
-
snapshotStatusFile writeStreamDo: [ :stream |
|
|
448
|
-
stream nextPutAll: 'status=saved'; cr ]
|
|
449
|
-
] on: Error do: [ :error |
|
|
450
|
-
snapshotStatusFile writeStreamDo: [ :stream |
|
|
451
|
-
stream nextPutAll: 'status=error'; cr.
|
|
452
|
-
stream nextPutAll: 'message='; nextPutAll: error asString; cr ] ]
|
|
453
|
-
] forkAt: Processor userBackgroundPriority.
|
|
454
|
-
'display mode snapshot scheduled'.`;
|
|
455
|
-
}
|
|
456
|
-
function parseProperties(contents) {
|
|
457
|
-
const result = {};
|
|
458
|
-
for (const line of contents.split(/\r\n|\r|\n/)) {
|
|
459
|
-
const separator = line.indexOf("=");
|
|
460
|
-
if (separator < 0) {
|
|
461
|
-
continue;
|
|
462
|
-
}
|
|
463
|
-
result[line.slice(0, separator)] = line.slice(separator + 1);
|
|
464
|
-
}
|
|
465
|
-
return result;
|
|
466
|
-
}
|
|
467
|
-
async function waitForDisplayModeSnapshotStatus(imageId, statusPath) {
|
|
468
|
-
const deadline = Date.now() + displayModeSnapshotTimeoutMs;
|
|
469
|
-
while (Date.now() <= deadline) {
|
|
470
|
-
if (fs.existsSync(statusPath)) {
|
|
471
|
-
const status = parseProperties(fs.readFileSync(statusPath, "utf8"));
|
|
472
|
-
if (status.status === "saved") {
|
|
473
|
-
return;
|
|
474
|
-
}
|
|
475
|
-
if (status.status === "error") {
|
|
476
|
-
throw new ScopedPharoLauncherError(`Image ${imageId} snapshot failed: ${status.message ?? "unknown error"}`);
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
await defaultSleep(displayModeSnapshotPollIntervalMs);
|
|
480
|
-
}
|
|
481
|
-
throw new ScopedPharoLauncherError(`Image ${imageId} snapshot did not finish within ${displayModeSnapshotTimeoutMs}ms`);
|
|
482
|
-
}
|
|
483
|
-
function projectStateWithoutImage(state, imageId, updatedAt) {
|
|
484
|
-
const images = state.images.filter((image) => image.id !== imageId);
|
|
485
|
-
return {
|
|
486
|
-
...state,
|
|
487
|
-
images,
|
|
488
|
-
runtimeStatus: runtimeStatusForImages(images),
|
|
489
|
-
updatedAt: updatedAt.toISOString(),
|
|
490
|
-
};
|
|
491
|
-
}
|
|
492
|
-
function lifecycleStatus(image) {
|
|
493
|
-
return {
|
|
494
|
-
imageId: image.imageId,
|
|
495
|
-
status: image.status,
|
|
496
|
-
displayMode: image.displayMode,
|
|
497
|
-
};
|
|
498
|
-
}
|
|
499
|
-
function routeStatus(image, imageState) {
|
|
500
|
-
const endpointRecorded = Boolean(imageState?.mcpServers?.length);
|
|
501
|
-
const contractStatus = imageState?.pharoMcpContract?.status;
|
|
502
|
-
let status;
|
|
503
|
-
if (image.status !== "running" || !endpointRecorded) {
|
|
504
|
-
status = "image_unavailable";
|
|
505
|
-
}
|
|
506
|
-
else if (contractStatus === "mismatched" || contractStatus === "unsupported") {
|
|
507
|
-
status = "contract_mismatch";
|
|
508
|
-
}
|
|
509
|
-
else if (contractStatus === undefined || contractStatus === "unknown") {
|
|
510
|
-
status = "contract_unknown";
|
|
511
|
-
}
|
|
512
|
-
else {
|
|
513
|
-
status = "routable";
|
|
514
|
-
}
|
|
515
|
-
return {
|
|
516
|
-
serverName: "pharo_gateway",
|
|
517
|
-
requiredArgument: "mcpServerId",
|
|
518
|
-
mcpServerId: image.imageId,
|
|
519
|
-
status,
|
|
520
|
-
routable: status === "routable",
|
|
521
|
-
endpointRecorded,
|
|
522
|
-
...(contractStatus ? { contractStatus } : {}),
|
|
523
|
-
};
|
|
524
|
-
}
|
|
525
|
-
function resetStopError(imageId, error) {
|
|
526
|
-
const failure = error.result.failures.find((record) => record.imageId === imageId);
|
|
527
|
-
// Say why, not only that: the reset stops the image first, so the stop's own
|
|
528
|
-
// message is what tells a caller which process is in the way.
|
|
529
|
-
return new ScopedPharoLauncherError(failure
|
|
530
|
-
? `Image ${imageId} reset could not stop the image: ${failure.message}`
|
|
531
|
-
: `Image ${imageId} reset could not stop the image cleanly`);
|
|
532
|
-
}
|
|
533
|
-
function resetSummary(imageId, closeResult, deleted, started, image, imageState) {
|
|
534
|
-
return {
|
|
535
|
-
imageId,
|
|
536
|
-
closed: Boolean(closeResult),
|
|
537
|
-
deleted,
|
|
538
|
-
created: true,
|
|
539
|
-
started,
|
|
540
|
-
lifecycle: lifecycleStatus(image),
|
|
541
|
-
route: routeStatus(image, imageState),
|
|
542
|
-
};
|
|
543
|
-
}
|
|
544
|
-
function launcherHoldsImage(scope, projectConfig, imageName) {
|
|
545
|
-
const { imagesDir } = describePharoLauncherMcpProfile({
|
|
546
|
-
projectRoot: scope.projectRoot,
|
|
547
|
-
config: projectConfig,
|
|
548
|
-
workspaceId: scope.workspaceId,
|
|
549
|
-
stateRoot: scope.stateRoot,
|
|
550
|
-
});
|
|
551
|
-
// An external profile keeps its own paths, so we cannot look. Answer the way
|
|
552
|
-
// we always behaved there and let the launcher say whether the image is there.
|
|
553
|
-
return imagesDir === undefined || fs.existsSync(joinPathLike(imagesDir, imageName));
|
|
554
|
-
}
|
|
555
|
-
function stateWithCreatedImage(projectConfig, scope, previousState, imageId, now) {
|
|
556
|
-
const state = createProjectState(projectConfig, {
|
|
557
|
-
previousState,
|
|
558
|
-
workspaceId: scope.workspaceId,
|
|
559
|
-
updatedAt: now.toISOString(),
|
|
560
|
-
});
|
|
561
|
-
const existingImageIds = new Set(previousState?.images.map((image) => image.id) ?? []);
|
|
562
|
-
state.images = state.images.filter((image) => image.id === imageId || existingImageIds.has(image.id));
|
|
563
|
-
for (const image of state.images) {
|
|
564
|
-
const previousImage = previousState?.images.find((candidate) => candidate.id === image.id);
|
|
565
|
-
if (image.id === imageId) {
|
|
566
|
-
const imageConfig = findImageConfig(projectConfig, imageId);
|
|
567
|
-
image.status = "stopped";
|
|
568
|
-
delete image.pid;
|
|
569
|
-
image.creation = imageCreationState(imageConfig);
|
|
570
|
-
}
|
|
571
|
-
else if (previousImage) {
|
|
572
|
-
Object.assign(image, previousImage);
|
|
573
|
-
}
|
|
574
|
-
else {
|
|
575
|
-
image.status = "stopped";
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
state.runtimeStatus = runtimeStatusForImages(state.images);
|
|
579
|
-
return state;
|
|
580
|
-
}
|
|
581
|
-
async function listTemplatesInCategory(client, templateCategory) {
|
|
582
|
-
const result = await client.callTool("pharo_launcher_template_list", templateCategory ? { templateCategory } : {});
|
|
583
|
-
assertLauncherOk(result, "pharo_launcher_template_list");
|
|
584
|
-
const data = result?.data ?? result;
|
|
585
|
-
return Array.isArray(data) ? data : [];
|
|
586
|
-
}
|
|
587
|
-
export class ScopedPharoLauncher {
|
|
588
|
-
options;
|
|
589
|
-
constructor(options) {
|
|
590
|
-
this.options = options;
|
|
591
|
-
}
|
|
592
|
-
/** The client held for the operations in flight, and how many hold it. */
|
|
593
|
-
heldClient;
|
|
594
|
-
holdCount = 0;
|
|
595
|
-
currentImageState(scope, projectConfig, imageId) {
|
|
596
|
-
const state = loadProjectState(statePathForScope(scope, projectConfig));
|
|
597
|
-
return state?.images.find((image) => image.id === imageId);
|
|
598
|
-
}
|
|
599
|
-
imageLeaseForMutation(scope, projectConfig, imageId, now) {
|
|
600
|
-
const imageState = this.currentImageState(scope, projectConfig, imageId);
|
|
601
|
-
const lease = imageLeaseForMutation(scope, this.options.imageLease, now, imageState?.lease);
|
|
602
|
-
assertImageLeaseAllowsMutation(imageId, imageState?.lease, lease, now);
|
|
603
|
-
return lease;
|
|
604
|
-
}
|
|
605
|
-
recordImageLease(scope, projectConfig, imageId, lease, now) {
|
|
606
|
-
const statePath = statePathForScope(scope, projectConfig);
|
|
607
|
-
const state = loadProjectState(statePath);
|
|
608
|
-
if (!state?.images.some((image) => image.id === imageId)) {
|
|
609
|
-
return undefined;
|
|
610
|
-
}
|
|
611
|
-
const nextState = stateWithImageLease(state, imageId, lease, now);
|
|
612
|
-
saveProjectState(statePath, nextState);
|
|
613
|
-
return nextState;
|
|
614
|
-
}
|
|
615
|
-
async snapshotImageBeforeDisplayModeRestart(scope, imageState) {
|
|
616
|
-
const endpoint = imageMcpSnapshotEndpoint(imageState);
|
|
617
|
-
if (!endpoint) {
|
|
618
|
-
throw new ScopedPharoLauncherError(`Image ${imageState.id} has no routable Pharo MCP endpoint; display mode restart cannot snapshot before close`);
|
|
619
|
-
}
|
|
620
|
-
const statusPath = displayModeSnapshotStatusPath(scope, imageState.id, this.options.now?.() ?? new Date());
|
|
621
|
-
fs.mkdirSync(dirnamePathLike(statusPath), { recursive: true });
|
|
622
|
-
fs.rmSync(statusPath, { force: true });
|
|
623
|
-
const controller = new AbortController();
|
|
624
|
-
const timeout = setTimeout(() => controller.abort(), displayModeSnapshotTimeoutMs);
|
|
625
|
-
try {
|
|
626
|
-
const response = await (this.options.fetch ?? fetch)(endpointUrl(endpoint), {
|
|
627
|
-
method: "POST",
|
|
628
|
-
headers: {
|
|
629
|
-
accept: "application/json, text/event-stream",
|
|
630
|
-
"content-type": "application/json",
|
|
631
|
-
},
|
|
632
|
-
body: JSON.stringify({
|
|
633
|
-
jsonrpc: "2.0",
|
|
634
|
-
id: `plexus-display-mode-snapshot-${imageState.id}-${Date.now()}`,
|
|
635
|
-
method: "tools/call",
|
|
636
|
-
params: {
|
|
637
|
-
name: "evaluate",
|
|
638
|
-
arguments: {
|
|
639
|
-
code: displayModeSnapshotScript(statusPath),
|
|
640
|
-
},
|
|
641
|
-
},
|
|
642
|
-
}),
|
|
643
|
-
signal: controller.signal,
|
|
644
|
-
});
|
|
645
|
-
if (!response.ok) {
|
|
646
|
-
throw new ScopedPharoLauncherError(`Image ${imageState.id} snapshot failed with HTTP ${response.status}`);
|
|
647
|
-
}
|
|
648
|
-
const payload = (await response.json());
|
|
649
|
-
if (!isObject(payload)) {
|
|
650
|
-
throw new ScopedPharoLauncherError(`Image ${imageState.id} snapshot returned a non-object response`);
|
|
651
|
-
}
|
|
652
|
-
if ("error" in payload) {
|
|
653
|
-
throw new ScopedPharoLauncherError(`Image ${imageState.id} snapshot failed: ${JSON.stringify(payload.error)}`);
|
|
654
|
-
}
|
|
655
|
-
const result = payload.result;
|
|
656
|
-
if (isObject(result) && result.isError === true) {
|
|
657
|
-
throw new ScopedPharoLauncherError(`Image ${imageState.id} snapshot tool returned an error`);
|
|
658
|
-
}
|
|
659
|
-
await waitForDisplayModeSnapshotStatus(imageState.id, statusPath);
|
|
660
|
-
return {
|
|
661
|
-
attempted: true,
|
|
662
|
-
status: "saved",
|
|
663
|
-
endpoint,
|
|
664
|
-
};
|
|
665
|
-
}
|
|
666
|
-
finally {
|
|
667
|
-
clearTimeout(timeout);
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
/**
|
|
671
|
-
* What the host is running, asked once for the whole listing. image_start
|
|
672
|
-
* refuses a launch on this same answer, so a listing that did not ask it
|
|
673
|
-
* disagreed with the guard that acts on it: an image someone else started
|
|
674
|
-
* read as stopped, and a dead image whose port had been reused read as
|
|
675
|
-
* running.
|
|
676
|
-
*
|
|
677
|
-
* A listing still answers when the launcher cannot be reached. It says so
|
|
678
|
-
* rather than passing off the record as the host's answer.
|
|
679
|
-
*/
|
|
680
|
-
async hostProcessesForListing(scope, projectConfig) {
|
|
681
|
-
try {
|
|
682
|
-
return {
|
|
683
|
-
processes: await this.withLauncherClient(scope, projectConfig, async (client) => hostPharoProcesses(client)),
|
|
684
|
-
};
|
|
685
|
-
}
|
|
686
|
-
catch (error) {
|
|
687
|
-
return {
|
|
688
|
-
error: error instanceof Error ? error.message : String(error),
|
|
689
|
-
};
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
|
-
async listImages() {
|
|
693
|
-
const scope = resolveScope(this.options);
|
|
694
|
-
const projectConfig = loadProjectConfig(scope.projectRoot);
|
|
695
|
-
const state = loadProjectState(statePathForScope(scope, projectConfig));
|
|
696
|
-
const launcherProfile = describePharoLauncherMcpProfile({
|
|
697
|
-
projectRoot: scope.projectRoot,
|
|
698
|
-
config: projectConfig,
|
|
699
|
-
workspaceId: scope.workspaceId,
|
|
700
|
-
stateRoot: scope.stateRoot,
|
|
701
|
-
});
|
|
702
|
-
const host = await this.hostProcessesForListing(scope, projectConfig);
|
|
703
|
-
return {
|
|
704
|
-
scope: scopeSummary(scope),
|
|
705
|
-
launcherProfile: launcherProfileSummary(launcherProfile),
|
|
706
|
-
host: host.processes === undefined
|
|
707
|
-
? {
|
|
708
|
-
asked: false,
|
|
709
|
-
...(host.error === undefined ? {} : { error: host.error }),
|
|
710
|
-
}
|
|
711
|
-
: { asked: true, pharoProcesses: host.processes.length },
|
|
712
|
-
images: await Promise.all(projectConfig.images.map(async (imageConfig) => {
|
|
713
|
-
const imageState = state?.images.find((image) => image.id === imageConfig.id);
|
|
714
|
-
const imageName = renderProjectImageName(imageConfig.imageName, {
|
|
715
|
-
projectId: scope.projectId,
|
|
716
|
-
projectName: scope.projectName,
|
|
717
|
-
workspaceId: scope.workspaceId,
|
|
718
|
-
imageId: imageConfig.id,
|
|
719
|
-
});
|
|
720
|
-
return imageSummary(imageConfig, imageState, await observeImageRuntime({
|
|
721
|
-
...scope,
|
|
722
|
-
...(this.options.isPidAlive === undefined
|
|
723
|
-
? {}
|
|
724
|
-
: { isPidAlive: this.options.isPidAlive }),
|
|
725
|
-
...(this.options.isPortListening === undefined
|
|
726
|
-
? {}
|
|
727
|
-
: { isPortListening: this.options.isPortListening }),
|
|
728
|
-
imageId: imageConfig.id,
|
|
729
|
-
imageName,
|
|
730
|
-
...(launcherProfile.imagesDir === undefined
|
|
731
|
-
? {}
|
|
732
|
-
: { imagesDirectory: launcherProfile.imagesDir }),
|
|
733
|
-
...(imageState?.pid === undefined ? {} : { pid: imageState.pid }),
|
|
734
|
-
...(host.processes === undefined
|
|
735
|
-
? {}
|
|
736
|
-
: {
|
|
737
|
-
runningOnHost: processesOnImage(host.processes, imageName).length > 0,
|
|
738
|
-
}),
|
|
739
|
-
}));
|
|
740
|
-
})),
|
|
741
|
-
};
|
|
742
|
-
}
|
|
743
|
-
async imageInfo(imageId) {
|
|
744
|
-
const listed = await this.listImages();
|
|
745
|
-
const image = listed.images.find((candidate) => candidate.imageId === imageId);
|
|
746
|
-
if (!image) {
|
|
747
|
-
throw new ScopedPharoLauncherError(`Image ${imageId} is not declared in this PLexus workspace`);
|
|
748
|
-
}
|
|
749
|
-
return {
|
|
750
|
-
scope: listed.scope,
|
|
751
|
-
launcherProfile: listed.launcherProfile,
|
|
752
|
-
host: listed.host,
|
|
753
|
-
image,
|
|
754
|
-
};
|
|
755
|
-
}
|
|
756
|
-
/**
|
|
757
|
-
* One launcher client for one operation.
|
|
758
|
-
*
|
|
759
|
-
* This was worth a great deal when a client was a pharo-launcher-mcp
|
|
760
|
-
* subprocess and a reset opened four of them. The client is in process now,
|
|
761
|
-
* so what the hold saves is resolving the installation and the profile once
|
|
762
|
-
* per operation instead of once per step — a smaller reason, kept because
|
|
763
|
-
* the boundary it needs is already drawn and costs the caller one wrapper.
|
|
764
|
-
*
|
|
765
|
-
* The caller says where an operation begins and ends — a tool call, a CLI
|
|
766
|
-
* command — because this facade cannot tell one from a sequence of them.
|
|
767
|
-
* Operations that overlap share the client, and the last one out drops it.
|
|
768
|
-
*/
|
|
769
|
-
async runOperation(action) {
|
|
770
|
-
this.holdCount += 1;
|
|
771
|
-
try {
|
|
772
|
-
return await action();
|
|
773
|
-
}
|
|
774
|
-
finally {
|
|
775
|
-
this.holdCount -= 1;
|
|
776
|
-
if (this.holdCount === 0) {
|
|
777
|
-
const held = this.heldClient;
|
|
778
|
-
this.heldClient = undefined;
|
|
779
|
-
await closeHeldClient(held);
|
|
780
|
-
}
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
async openLauncherClient(scope, projectConfig) {
|
|
784
|
-
const profileEnvironment = pharoLauncherMcpProfileEnvironment({
|
|
785
|
-
projectRoot: scope.projectRoot,
|
|
786
|
-
config: projectConfig,
|
|
787
|
-
workspaceId: scope.workspaceId,
|
|
788
|
-
stateRoot: scope.stateRoot,
|
|
789
|
-
});
|
|
790
|
-
return this.options.createPharoLauncherMcpClient
|
|
791
|
-
? await this.options.createPharoLauncherMcpClient(profileEnvironment)
|
|
792
|
-
: openPharoLauncherClient({
|
|
793
|
-
...(profileEnvironment === undefined ? {} : { profileEnvironment }),
|
|
794
|
-
});
|
|
795
|
-
}
|
|
796
|
-
async withLauncherClient(scope, projectConfig, action) {
|
|
797
|
-
const injected = this.options.pharoLauncherMcpClient;
|
|
798
|
-
if (injected) {
|
|
799
|
-
return await action(injected);
|
|
800
|
-
}
|
|
801
|
-
if (this.holdCount > 0) {
|
|
802
|
-
this.heldClient ??= this.openLauncherClient(scope, projectConfig);
|
|
803
|
-
return await action(await this.heldClient);
|
|
804
|
-
}
|
|
805
|
-
const client = await this.openLauncherClient(scope, projectConfig);
|
|
806
|
-
try {
|
|
807
|
-
return await action(client);
|
|
808
|
-
}
|
|
809
|
-
finally {
|
|
810
|
-
await client.close?.();
|
|
811
|
-
}
|
|
812
|
-
}
|
|
813
|
-
async listTemplates(templateCategory) {
|
|
814
|
-
const scope = resolveScope(this.options);
|
|
815
|
-
const projectConfig = loadProjectConfig(scope.projectRoot);
|
|
816
|
-
const templates = await this.withLauncherClient(scope, projectConfig, async (client) => {
|
|
817
|
-
await ensureScopedTemplateCatalogReady({
|
|
818
|
-
client,
|
|
819
|
-
scope,
|
|
820
|
-
projectConfig,
|
|
821
|
-
fetch: this.options.fetch,
|
|
822
|
-
});
|
|
823
|
-
if (templateCategory) {
|
|
824
|
-
return await listTemplatesInCategory(client, templateCategory);
|
|
825
|
-
}
|
|
826
|
-
// Without a category the launcher answers from its default group only,
|
|
827
|
-
// so a caller naming a template from any other group is told it does
|
|
828
|
-
// not exist. Walk the categories and say which one each came from.
|
|
829
|
-
const categories = templateCategoryNames(describePharoLauncherMcpProfile({
|
|
830
|
-
projectRoot: scope.projectRoot,
|
|
831
|
-
config: projectConfig,
|
|
832
|
-
workspaceId: scope.workspaceId,
|
|
833
|
-
stateRoot: scope.stateRoot,
|
|
834
|
-
}).templateSourcesDir);
|
|
835
|
-
if (categories.length === 0) {
|
|
836
|
-
return await listTemplatesInCategory(client);
|
|
837
|
-
}
|
|
838
|
-
const listed = [];
|
|
839
|
-
for (const category of categories) {
|
|
840
|
-
let templates;
|
|
841
|
-
try {
|
|
842
|
-
templates = await listTemplatesInCategory(client, category);
|
|
843
|
-
}
|
|
844
|
-
catch {
|
|
845
|
-
// A group the sources list names but this launcher cannot open.
|
|
846
|
-
continue;
|
|
847
|
-
}
|
|
848
|
-
for (const template of templates) {
|
|
849
|
-
listed.push(isObject(template) ? { ...template, templateCategory: category } : template);
|
|
850
|
-
}
|
|
851
|
-
}
|
|
852
|
-
return listed;
|
|
853
|
-
});
|
|
854
|
-
return {
|
|
855
|
-
...this.templateScopeSummary(scope, projectConfig),
|
|
856
|
-
templates,
|
|
857
|
-
};
|
|
858
|
-
}
|
|
859
|
-
templateScopeSummary(scope, projectConfig) {
|
|
860
|
-
return {
|
|
861
|
-
scope: scopeSummary(scope),
|
|
862
|
-
launcherProfile: launcherProfileSummary(describePharoLauncherMcpProfile({
|
|
863
|
-
projectRoot: scope.projectRoot,
|
|
864
|
-
config: projectConfig,
|
|
865
|
-
workspaceId: scope.workspaceId,
|
|
866
|
-
stateRoot: scope.stateRoot,
|
|
867
|
-
})),
|
|
868
|
-
};
|
|
869
|
-
}
|
|
870
|
-
/**
|
|
871
|
-
* The launcher resolves a bare template name against its default group only,
|
|
872
|
-
* so an image naming a template from any other group could not be created
|
|
873
|
-
* until something said which group that was. `listTemplates` already walks
|
|
874
|
-
* the groups and stamps each row, so the answer was here all along and every
|
|
875
|
-
* consumer was finding it for itself.
|
|
876
|
-
*/
|
|
877
|
-
async templateCategoryFor(templateName) {
|
|
878
|
-
const { templates } = await this.listTemplates();
|
|
879
|
-
const categories = [
|
|
880
|
-
...new Set(templates
|
|
881
|
-
.filter((template) => template.name === templateName)
|
|
882
|
-
.map((template) => template.templateCategory)
|
|
883
|
-
.filter((category) => Boolean(category))),
|
|
884
|
-
];
|
|
885
|
-
if (categories.length === 0) {
|
|
886
|
-
throw new ScopedPharoLauncherError(`No template named ${templateName} in any category`);
|
|
887
|
-
}
|
|
888
|
-
if (categories.length > 1) {
|
|
889
|
-
throw new ScopedPharoLauncherError(`Template ${templateName} exists in several categories: ${categories.join(", ")}. `
|
|
890
|
-
+ "Set create.templateCategory to choose one.");
|
|
891
|
-
}
|
|
892
|
-
return categories[0];
|
|
893
|
-
}
|
|
894
|
-
async createImage(imageId, profileId) {
|
|
895
|
-
const scope = resolveScope(this.options);
|
|
896
|
-
const projectConfig = loadProjectConfig(scope.projectRoot);
|
|
897
|
-
const imageConfig = findImageConfig(projectConfig, imageId);
|
|
898
|
-
const createPolicy = imageConfig.create;
|
|
899
|
-
if (!createPolicy) {
|
|
900
|
-
throw new ScopedPharoLauncherError(`Image ${imageId} has no approved create policy in project config`);
|
|
901
|
-
}
|
|
902
|
-
if (profileId && profileId !== createPolicy.profileId) {
|
|
903
|
-
throw new ScopedPharoLauncherError(`Profile ${profileId} is not approved for image ${imageId}`);
|
|
904
|
-
}
|
|
905
|
-
const statePath = statePathForScope(scope, projectConfig);
|
|
906
|
-
const previousState = loadProjectState(statePath);
|
|
907
|
-
assertPreviousStateBelongsToScope(previousState, scope);
|
|
908
|
-
// Ask the profile, not the record. A record can name an image the launcher
|
|
909
|
-
// no longer holds, and refusing to create that one is refusing to fix it.
|
|
910
|
-
const imageName = renderProjectImageName(imageConfig.imageName, {
|
|
911
|
-
projectId: scope.projectId,
|
|
912
|
-
projectName: projectConfig.name,
|
|
913
|
-
workspaceId: scope.workspaceId,
|
|
914
|
-
imageId,
|
|
915
|
-
});
|
|
916
|
-
if (launcherHoldsImage(scope, projectConfig, imageName)) {
|
|
917
|
-
throw new ScopedPharoLauncherError(`Image ${imageId} already exists as ${imageName}`);
|
|
918
|
-
}
|
|
919
|
-
assertWorkspaceImagePolicyAllowsCreate(projectConfig, previousState, imageId);
|
|
920
|
-
const now = this.options.now?.() ?? new Date();
|
|
921
|
-
const lease = this.imageLeaseForMutation(scope, projectConfig, imageId, now);
|
|
922
|
-
const state = stateWithCreatedImage(projectConfig, scope, previousState, imageId, now);
|
|
923
|
-
const imageState = state.images.find((image) => image.id === imageId);
|
|
924
|
-
if (!imageState) {
|
|
925
|
-
throw new ScopedPharoLauncherError(`Image ${imageId} is not declared in this PLexus workspace`);
|
|
926
|
-
}
|
|
927
|
-
await this.withLauncherClient(scope, projectConfig, async (client) => {
|
|
928
|
-
const homeMaterialization = await materializeProjectImageFromHomeCache({
|
|
929
|
-
runtimeClient: client,
|
|
930
|
-
homeClient: this.options.homeImageCacheClient ??
|
|
931
|
-
(this.options.pharoLauncherMcpClient ? client : undefined),
|
|
932
|
-
projectRoot: scope.projectRoot,
|
|
933
|
-
config: projectConfig,
|
|
934
|
-
imageConfig,
|
|
935
|
-
imageState,
|
|
936
|
-
workspaceId: scope.workspaceId,
|
|
937
|
-
stateRoot: scope.stateRoot,
|
|
938
|
-
approval: scopedMutationApproval(this.options.homeImageCacheApproval, "scoped-pharo-launcher-create"),
|
|
939
|
-
now: this.options.now,
|
|
940
|
-
});
|
|
941
|
-
recordHomeImageCacheBase(imageState, homeMaterialization);
|
|
942
|
-
if (!homeMaterialization) {
|
|
943
|
-
await ensureScopedTemplateCatalogReady({
|
|
944
|
-
client,
|
|
945
|
-
scope,
|
|
946
|
-
projectConfig,
|
|
947
|
-
fetch: this.options.fetch,
|
|
948
|
-
});
|
|
949
|
-
const templateCategory = createPolicy.templateCategory ??
|
|
950
|
-
(await this.templateCategoryFor(createPolicy.templateName));
|
|
951
|
-
const result = await client.callTool("pharo_launcher_image_create", {
|
|
952
|
-
newImageName: renderedImageName(scope, imageConfig),
|
|
953
|
-
templateName: createPolicy.templateName,
|
|
954
|
-
...(templateCategory ? { templateCategory } : {}),
|
|
955
|
-
noLaunch: true,
|
|
956
|
-
});
|
|
957
|
-
assertLauncherOk(result, "pharo_launcher_image_create");
|
|
958
|
-
}
|
|
959
|
-
const nextState = stateWithImageLease(state, imageId, lease, now);
|
|
960
|
-
saveProjectState(statePath, nextState);
|
|
961
|
-
});
|
|
962
|
-
return await this.imageInfo(imageId);
|
|
963
|
-
}
|
|
964
|
-
async startImage(imageId, displayMode) {
|
|
965
|
-
const before = await this.imageInfo(imageId);
|
|
966
|
-
if (before.image.status === "running" || before.image.status === "starting") {
|
|
967
|
-
throw new ScopedPharoLauncherError(`Image ${imageId} is already ${before.image.status}; scoped start is rejected`);
|
|
968
|
-
}
|
|
969
|
-
const scope = resolveScope(this.options);
|
|
970
|
-
const projectConfig = loadProjectConfig(scope.projectRoot);
|
|
971
|
-
const now = this.options.now?.() ?? new Date();
|
|
972
|
-
const lease = this.imageLeaseForMutation(scope, projectConfig, imageId, now);
|
|
973
|
-
const openResult = await this.startingImage(imageId, () => (this.options.startImage ?? startProjectImage)({
|
|
974
|
-
projectRoot: scope.projectRoot,
|
|
975
|
-
workspaceId: scope.workspaceId,
|
|
976
|
-
stateRoot: scope.stateRoot,
|
|
977
|
-
imageId,
|
|
978
|
-
...(displayMode ? { displayMode } : {}),
|
|
979
|
-
homeImageCacheApproval: scopedMutationApproval(this.options.homeImageCacheApproval, "scoped-pharo-launcher-start"),
|
|
980
|
-
homeImageCacheClient: this.options.homeImageCacheClient ??
|
|
981
|
-
this.options.pharoLauncherMcpClient,
|
|
982
|
-
}));
|
|
983
|
-
const statePath = statePathForScope(scope, projectConfig);
|
|
984
|
-
const leasedState = this.recordImageLease(scope, projectConfig, imageId, lease, now);
|
|
985
|
-
return await this.imageInfo(imageId);
|
|
986
|
-
}
|
|
987
|
-
/**
|
|
988
|
-
* Raise the failure as this surface's own error, so a tool call reports it
|
|
989
|
-
* the way it reports every other refusal. The message already names what
|
|
990
|
-
* happened — which VM holds the image, which tool frees it.
|
|
991
|
-
*/
|
|
992
|
-
async startingImage(imageId, start) {
|
|
993
|
-
try {
|
|
994
|
-
return await start();
|
|
995
|
-
}
|
|
996
|
-
catch (error) {
|
|
997
|
-
if (!(error instanceof StartProjectImageError)) {
|
|
998
|
-
throw error;
|
|
999
|
-
}
|
|
1000
|
-
const failure = error.result.failures.find((candidate) => candidate.imageId === imageId);
|
|
1001
|
-
throw failure
|
|
1002
|
-
? new ScopedPharoLauncherError(failure.message)
|
|
1003
|
-
: error;
|
|
1004
|
-
}
|
|
1005
|
-
}
|
|
1006
|
-
async setImageDisplayMode(imageId, displayMode) {
|
|
1007
|
-
const scope = resolveScope(this.options);
|
|
1008
|
-
const projectConfig = loadProjectConfig(scope.projectRoot);
|
|
1009
|
-
const imageConfig = findImageConfig(projectConfig, imageId);
|
|
1010
|
-
const before = await this.imageInfo(imageId);
|
|
1011
|
-
const imageState = this.currentImageState(scope, projectConfig, imageId);
|
|
1012
|
-
const previousDisplayMode = imageState?.displayMode ?? projectImageDisplayMode(imageConfig);
|
|
1013
|
-
if (before.image.status === "starting") {
|
|
1014
|
-
throw new ScopedPharoLauncherError(`Image ${imageId} is starting; wait for startup to finish before changing display mode`);
|
|
1015
|
-
}
|
|
1016
|
-
if (before.image.status === "running" && previousDisplayMode === displayMode) {
|
|
1017
|
-
return {
|
|
1018
|
-
...before,
|
|
1019
|
-
previousDisplayMode,
|
|
1020
|
-
displayMode,
|
|
1021
|
-
restarted: false,
|
|
1022
|
-
runtimeStateUnchanged: true,
|
|
1023
|
-
};
|
|
1024
|
-
}
|
|
1025
|
-
this.imageLeaseForMutation(scope, projectConfig, imageId, this.options.now?.() ?? new Date());
|
|
1026
|
-
let snapshotBeforeRestart;
|
|
1027
|
-
if (before.image.status === "running") {
|
|
1028
|
-
if (imageState) {
|
|
1029
|
-
snapshotBeforeRestart =
|
|
1030
|
-
await this.snapshotImageBeforeDisplayModeRestart(scope, imageState);
|
|
1031
|
-
}
|
|
1032
|
-
await this.stopImage(imageId);
|
|
1033
|
-
}
|
|
1034
|
-
await this.startImage(imageId, displayMode);
|
|
1035
|
-
return {
|
|
1036
|
-
...(await this.imageInfo(imageId)),
|
|
1037
|
-
previousDisplayMode,
|
|
1038
|
-
displayMode,
|
|
1039
|
-
restarted: before.image.status === "running",
|
|
1040
|
-
runtimeStateUnchanged: false,
|
|
1041
|
-
...(snapshotBeforeRestart ? { snapshotBeforeRestart } : {}),
|
|
1042
|
-
};
|
|
1043
|
-
}
|
|
1044
|
-
async stopImage(imageId) {
|
|
1045
|
-
await this.imageInfo(imageId);
|
|
1046
|
-
const scope = resolveScope(this.options);
|
|
1047
|
-
const projectConfig = loadProjectConfig(scope.projectRoot);
|
|
1048
|
-
const now = this.options.now?.() ?? new Date();
|
|
1049
|
-
const lease = this.imageLeaseForMutation(scope, projectConfig, imageId, now);
|
|
1050
|
-
const closeResult = await (this.options.stopImage ?? stopProjectImage)({
|
|
1051
|
-
projectRoot: scope.projectRoot,
|
|
1052
|
-
workspaceId: scope.workspaceId,
|
|
1053
|
-
stateRoot: scope.stateRoot,
|
|
1054
|
-
imageId,
|
|
1055
|
-
});
|
|
1056
|
-
const statePath = statePathForScope(scope, projectConfig);
|
|
1057
|
-
const leasedState = this.recordImageLease(scope, projectConfig, imageId, lease, now);
|
|
1058
|
-
return await this.imageInfo(imageId);
|
|
1059
|
-
}
|
|
1060
|
-
assertResetImageConfig(imageId, imageConfig) {
|
|
1061
|
-
if (!imageConfig.create) {
|
|
1062
|
-
throw new ScopedPharoLauncherError(`Image ${imageId} has no approved create policy in project config; scoped reset is rejected`);
|
|
1063
|
-
}
|
|
1064
|
-
}
|
|
1065
|
-
async deleteLauncherImage(scope, projectConfig, imageName) {
|
|
1066
|
-
await this.withLauncherClient(scope, projectConfig, async (client) => {
|
|
1067
|
-
const deleteResult = await client.callTool("pharo_launcher_image_delete", { imageName, force: true });
|
|
1068
|
-
assertLauncherOk(deleteResult, "pharo_launcher_image_delete");
|
|
1069
|
-
});
|
|
1070
|
-
}
|
|
1071
|
-
async resetImage(imageId, options = {}) {
|
|
1072
|
-
const scope = resolveScope(this.options);
|
|
1073
|
-
const projectConfig = loadProjectConfig(scope.projectRoot);
|
|
1074
|
-
const imageConfig = findImageConfig(projectConfig, imageId);
|
|
1075
|
-
this.assertResetImageConfig(imageId, imageConfig);
|
|
1076
|
-
const statePath = statePathForScope(scope, projectConfig);
|
|
1077
|
-
const previousState = loadProjectState(statePath);
|
|
1078
|
-
const previousImage = previousState?.images.find((image) => image.id === imageId);
|
|
1079
|
-
if (previousImage) {
|
|
1080
|
-
this.imageLeaseForMutation(scope, projectConfig, imageId, this.options.now?.() ?? new Date());
|
|
1081
|
-
}
|
|
1082
|
-
if (previousImage?.status === "starting") {
|
|
1083
|
-
throw new ScopedPharoLauncherError(`Image ${imageId} is starting; wait for startup to finish before resetting`);
|
|
1084
|
-
}
|
|
1085
|
-
let closeResult;
|
|
1086
|
-
if (previousImage) {
|
|
1087
|
-
try {
|
|
1088
|
-
closeResult = await (this.options.stopImage ?? stopProjectImage)({
|
|
1089
|
-
projectRoot: scope.projectRoot,
|
|
1090
|
-
workspaceId: scope.workspaceId,
|
|
1091
|
-
stateRoot: scope.stateRoot,
|
|
1092
|
-
imageId,
|
|
1093
|
-
pharoLauncherMcpClient: this.options.pharoLauncherMcpClient,
|
|
1094
|
-
});
|
|
1095
|
-
}
|
|
1096
|
-
catch (error) {
|
|
1097
|
-
if (error instanceof StopProjectImageError) {
|
|
1098
|
-
throw resetStopError(imageId, error);
|
|
1099
|
-
}
|
|
1100
|
-
throw error;
|
|
1101
|
-
}
|
|
1102
|
-
}
|
|
1103
|
-
let deleted = false;
|
|
1104
|
-
if (previousImage) {
|
|
1105
|
-
// A record can outlive the image it names: an open that failed before
|
|
1106
|
-
// the launcher made the files leaves one behind. Deleting what is not
|
|
1107
|
-
// there fails, and a reset that cannot run is the only way out of that
|
|
1108
|
-
// record, so delete only what the profile actually holds.
|
|
1109
|
-
if (launcherHoldsImage(scope, projectConfig, previousImage.imageName)) {
|
|
1110
|
-
await this.deleteLauncherImage(scope, projectConfig, previousImage.imageName);
|
|
1111
|
-
deleted = true;
|
|
1112
|
-
}
|
|
1113
|
-
const currentState = loadProjectState(statePath);
|
|
1114
|
-
if (currentState) {
|
|
1115
|
-
saveProjectState(statePath, projectStateWithoutImage(currentState, imageId, this.options.now?.() ?? new Date()));
|
|
1116
|
-
}
|
|
1117
|
-
}
|
|
1118
|
-
await this.createImage(imageId, imageConfig.create.profileId);
|
|
1119
|
-
const shouldStart = options.start ?? true;
|
|
1120
|
-
if (shouldStart) {
|
|
1121
|
-
await this.startImage(imageId, options.displayMode);
|
|
1122
|
-
}
|
|
1123
|
-
const finalInfo = await this.imageInfo(imageId);
|
|
1124
|
-
return {
|
|
1125
|
-
...finalInfo,
|
|
1126
|
-
reset: resetSummary(imageId, closeResult, deleted, shouldStart, finalInfo.image, this.currentImageState(scope, projectConfig, imageId)),
|
|
1127
|
-
};
|
|
1128
|
-
}
|
|
1129
|
-
/**
|
|
1130
|
-
* Delete the image this project declares. No confirm flag: a destructive call
|
|
1131
|
-
* is guarded by refusing a state that conflicts with it, not by a boolean the
|
|
1132
|
-
* caller fills in anyway. Deleting a running image is that conflict.
|
|
1133
|
-
*/
|
|
1134
|
-
async deleteImage(imageId) {
|
|
1135
|
-
const before = await this.imageInfo(imageId);
|
|
1136
|
-
if (before.image.runtime?.status === "running" ||
|
|
1137
|
-
before.image.runtime?.status === "starting") {
|
|
1138
|
-
throw new ScopedPharoLauncherError(`Image ${imageId} is ${before.image.runtime.status}; stop it with ` +
|
|
1139
|
-
`pharo_launcher_image_stop before deleting it.`);
|
|
1140
|
-
}
|
|
1141
|
-
const scope = resolveScope(this.options);
|
|
1142
|
-
const projectConfig = loadProjectConfig(scope.projectRoot);
|
|
1143
|
-
const imageConfig = findImageConfig(projectConfig, imageId);
|
|
1144
|
-
if (!imageConfig) {
|
|
1145
|
-
throw new ScopedPharoLauncherError(`Image ${imageId} is not declared in this project`);
|
|
1146
|
-
}
|
|
1147
|
-
const statePath = statePathForScope(scope, projectConfig);
|
|
1148
|
-
const now = this.options.now?.() ?? new Date();
|
|
1149
|
-
this.imageLeaseForMutation(scope, projectConfig, imageId, now);
|
|
1150
|
-
// Ask the profile what it holds rather than believing the record: a record
|
|
1151
|
-
// can name an image the launcher never made, and deleting that fails. The
|
|
1152
|
-
// declaration's name is a template, and the launcher only knows the name
|
|
1153
|
-
// creating rendered from it.
|
|
1154
|
-
const imageName = renderedImageName(scope, imageConfig);
|
|
1155
|
-
const launcherImage = launcherHoldsImage(scope, projectConfig, imageName);
|
|
1156
|
-
if (launcherImage) {
|
|
1157
|
-
await this.deleteLauncherImage(scope, projectConfig, imageName);
|
|
1158
|
-
}
|
|
1159
|
-
const currentState = loadProjectState(statePath);
|
|
1160
|
-
const runtimeRecord = Boolean(currentState?.images.some((image) => image.id === imageId));
|
|
1161
|
-
if (currentState && runtimeRecord) {
|
|
1162
|
-
saveProjectState(statePath, projectStateWithoutImage(currentState, imageId, now));
|
|
1163
|
-
}
|
|
1164
|
-
const after = await this.imageInfo(imageId);
|
|
1165
|
-
return {
|
|
1166
|
-
scope: after.scope,
|
|
1167
|
-
launcherProfile: after.launcherProfile,
|
|
1168
|
-
deleted: { imageId, launcherImage, runtimeRecord },
|
|
1169
|
-
};
|
|
1170
|
-
}
|
|
1171
|
-
}
|
|
1172
|
-
export const scopedPharoLauncherTools = [
|
|
1173
|
-
{
|
|
1174
|
-
name: "pharo_launcher_image_list",
|
|
1175
|
-
description: "List Pharo images declared in the current PLexus project/workspace scope.",
|
|
1176
|
-
inputSchema: objectSchema({ detail: toolResultDetailSchema }),
|
|
1177
|
-
},
|
|
1178
|
-
{
|
|
1179
|
-
name: "pharo_launcher_image_info",
|
|
1180
|
-
description: "Return scoped state for one Pharo image handle in the current PLexus workspace.",
|
|
1181
|
-
inputSchema: objectSchema({ imageId: stringSchema, detail: toolResultDetailSchema }, ["imageId"]),
|
|
1182
|
-
},
|
|
1183
|
-
{
|
|
1184
|
-
name: "pharo_launcher_template_list",
|
|
1185
|
-
description: "List the Pharo Launcher image templates this project can create from. "
|
|
1186
|
-
+ "Each row names its category, so there is no separate category list.",
|
|
1187
|
-
inputSchema: objectSchema({
|
|
1188
|
-
templateCategory: stringSchema,
|
|
1189
|
-
detail: toolResultDetailSchema,
|
|
1190
|
-
}),
|
|
1191
|
-
},
|
|
1192
|
-
{
|
|
1193
|
-
name: "pharo_launcher_image_create",
|
|
1194
|
-
description: "Create a declared workspace-scoped image from an approved PLexus project create policy.",
|
|
1195
|
-
inputSchema: objectSchema({
|
|
1196
|
-
imageId: stringSchema,
|
|
1197
|
-
profileId: stringSchema,
|
|
1198
|
-
detail: toolResultDetailSchema,
|
|
1199
|
-
}, ["imageId"]),
|
|
1200
|
-
},
|
|
1201
|
-
{
|
|
1202
|
-
name: "pharo_launcher_image_start",
|
|
1203
|
-
description: "Start one image this project declares, in the project's launcher profile.",
|
|
1204
|
-
inputSchema: objectSchema({
|
|
1205
|
-
imageId: stringSchema,
|
|
1206
|
-
displayMode: displayModeSchema,
|
|
1207
|
-
detail: toolResultDetailSchema,
|
|
1208
|
-
}, ["imageId"]),
|
|
1209
|
-
},
|
|
1210
|
-
{
|
|
1211
|
-
name: "pharo_launcher_image_display",
|
|
1212
|
-
description: "Show or hide one image's window. A running image is restarted in the "
|
|
1213
|
-
+ "requested display mode; a stopped one starts in it next time.",
|
|
1214
|
-
inputSchema: objectSchema({
|
|
1215
|
-
imageId: stringSchema,
|
|
1216
|
-
displayMode: displayModeSchema,
|
|
1217
|
-
detail: toolResultDetailSchema,
|
|
1218
|
-
}, ["imageId", "displayMode"]),
|
|
1219
|
-
},
|
|
1220
|
-
{
|
|
1221
|
-
name: "pharo_launcher_image_stop",
|
|
1222
|
-
description: "Stop the process PLexus started for one workspace-scoped image. Stops only that process, never another VM on the same image.",
|
|
1223
|
-
inputSchema: objectSchema({
|
|
1224
|
-
imageId: stringSchema,
|
|
1225
|
-
detail: toolResultDetailSchema,
|
|
1226
|
-
}, ["imageId"]),
|
|
1227
|
-
},
|
|
1228
|
-
{
|
|
1229
|
-
name: "pharo_launcher_image_delete",
|
|
1230
|
-
description: "Delete one workspace-scoped image and its runtime record. Refused while the image is running.",
|
|
1231
|
-
inputSchema: objectSchema({ imageId: stringSchema, detail: toolResultDetailSchema }, ["imageId"]),
|
|
1232
|
-
},
|
|
1233
|
-
{
|
|
1234
|
-
name: "pharo_launcher_image_reset",
|
|
1235
|
-
description: "Replace one image with a fresh one from its template: stop it, delete it, create it again, and start it unless start is false.",
|
|
1236
|
-
inputSchema: objectSchema({
|
|
1237
|
-
imageId: stringSchema,
|
|
1238
|
-
start: { type: "boolean" },
|
|
1239
|
-
displayMode: displayModeSchema,
|
|
1240
|
-
detail: toolResultDetailSchema,
|
|
1241
|
-
}, ["imageId"]),
|
|
1242
|
-
},
|
|
1243
|
-
];
|
|
1244
|
-
export function createScopedPharoLauncherServer(options) {
|
|
1245
|
-
const facade = new ScopedPharoLauncher(options);
|
|
1246
|
-
const server = new Server({
|
|
1247
|
-
name: "pharo-launcher",
|
|
1248
|
-
version: "0.1.0",
|
|
1249
|
-
}, {
|
|
1250
|
-
capabilities: {
|
|
1251
|
-
tools: {},
|
|
1252
|
-
},
|
|
1253
|
-
});
|
|
1254
|
-
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
1255
|
-
tools: [...scopedPharoLauncherTools],
|
|
1256
|
-
}));
|
|
1257
|
-
server.setRequestHandler(CallToolRequestSchema, async (request) => callScopedPharoLauncherTool(facade, request.params.name, request.params.arguments));
|
|
1258
|
-
return server;
|
|
1259
|
-
}
|
|
1260
|
-
/** Shared so the composed `plexus mcp` server dispatches identically. */
|
|
1261
|
-
/**
|
|
1262
|
-
* One tool call is one operation, which is where a launcher client is opened
|
|
1263
|
-
* and closed. A call that reaches the launcher several times — a reset reports
|
|
1264
|
-
* a listing after each of its four steps — opens one, not four.
|
|
1265
|
-
*/
|
|
1266
|
-
export async function callScopedPharoLauncherTool(facade, name, argumentsValue) {
|
|
1267
|
-
return await facade.runOperation(async () => {
|
|
1268
|
-
const input = argumentsValue ?? {};
|
|
1269
|
-
const detail = toolResultDetailFromArguments(input);
|
|
1270
|
-
const request = { params: { name } };
|
|
1271
|
-
try {
|
|
1272
|
-
switch (name) {
|
|
1273
|
-
case "pharo_launcher_image_list":
|
|
1274
|
-
return textResult(await facade.listImages(), false, detail);
|
|
1275
|
-
case "pharo_launcher_image_info":
|
|
1276
|
-
return textResult(await facade.imageInfo(requireString(input, "imageId")), false, detail);
|
|
1277
|
-
case "pharo_launcher_template_list":
|
|
1278
|
-
return textResult(await facade.listTemplates(optionalString(input, "templateCategory")), false, detail);
|
|
1279
|
-
case "pharo_launcher_image_create":
|
|
1280
|
-
return textResult(await facade.createImage(requireString(input, "imageId"), optionalString(input, "profileId")), false, detail);
|
|
1281
|
-
case "pharo_launcher_image_start":
|
|
1282
|
-
return textResult(await facade.startImage(requireString(input, "imageId"), optionalDisplayMode(input, "displayMode")), false, detail);
|
|
1283
|
-
case "pharo_launcher_image_display":
|
|
1284
|
-
return textResult(await facade.setImageDisplayMode(requireString(input, "imageId"), requireDisplayMode(input, "displayMode")), false, detail);
|
|
1285
|
-
case "pharo_launcher_image_stop":
|
|
1286
|
-
return textResult(await facade.stopImage(requireString(input, "imageId")), false, detail);
|
|
1287
|
-
case "pharo_launcher_image_delete":
|
|
1288
|
-
return textResult(await facade.deleteImage(requireString(input, "imageId")), false, detail);
|
|
1289
|
-
case "pharo_launcher_image_reset":
|
|
1290
|
-
return textResult(await facade.resetImage(requireString(input, "imageId"), {
|
|
1291
|
-
start: optionalBoolean(input, "start"),
|
|
1292
|
-
displayMode: optionalDisplayMode(input, "displayMode"),
|
|
1293
|
-
}), false, detail);
|
|
1294
|
-
default:
|
|
1295
|
-
return textResult({ ok: false, error: `Unknown tool: ${request.params.name}` }, true, detail);
|
|
1296
|
-
}
|
|
1297
|
-
}
|
|
1298
|
-
catch (error) {
|
|
1299
|
-
return textResult(scopedErrorPayload(error), true, detail);
|
|
1300
|
-
}
|
|
1301
|
-
});
|
|
1302
|
-
}
|
|
1303
|
-
export async function startScopedPharoLauncherServer(options) {
|
|
1304
|
-
const server = createScopedPharoLauncherServer(options);
|
|
1305
|
-
await server.connect(new StdioServerTransport());
|
|
1306
|
-
}
|
|
1307
|
-
//# sourceMappingURL=scopedPharoLauncherServer.js.map
|