agent-space-manager 0.0.0-stage → 1.0.1

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/index.mjs ADDED
@@ -0,0 +1,292 @@
1
+ // D:\Software\Magpie\agent-space-manager\index.mjs
2
+ // Magpie Plugin Entrypoint for Agent Space Manager & Smart Middleware
3
+
4
+ import { onRequest, onResponse } from "./smart-tools.middleware.js";
5
+ import { startApiServer, ensureBackgroundDaemon, handleOmnirouteRequest, runOmnirouteAction, writeConnectStatusFile } from "./server.mjs";
6
+ import {
7
+ loadHubConfig,
8
+ saveHubConfig,
9
+ scanPlugins,
10
+ connectAgent,
11
+ syncIdePlugins,
12
+ syncAllIdes,
13
+ unlinkAllForIde,
14
+ unlinkAllForProject,
15
+ detectAgentConfigPath,
16
+ createJunction,
17
+ removeJunction,
18
+ resolveDroppedTarget,
19
+ browseFileSystem,
20
+ inferNameFromExe,
21
+ resolveShortcutTarget,
22
+ buildCategoriesRegistry,
23
+ getEffectiveHubRoot,
24
+ listAgentProfilesForUi,
25
+ inferAgentType,
26
+ checkComponentsModifications,
27
+ checkAndAutoReconnectOnStartup,
28
+ getComponentsStorageSnapshot,
29
+ evaluateComponentAgentWorkability,
30
+ verifyPluginWorkabilityOnDisk,
31
+ recheckConnectedAgentsStatus,
32
+ removeComponentUpdateMechanisms,
33
+ checkCentralizedUpstreamUpdates,
34
+ loadCentralizedUpstreamUpdateState,
35
+ collectAllComponentsUpstream,
36
+ getOmniroutePort,
37
+ isOmnirouteInstalled,
38
+ checkOmnirouteStatus,
39
+ startOmniroute,
40
+ stopOmniroute,
41
+ isMagpieRunning,
42
+ openUrlInBrowser,
43
+ scanAgentForeignComponents,
44
+ cleanAgentForeignComponents,
45
+ autoEnableHubComponents,
46
+ } from "./manager.mjs";
47
+
48
+ if (!process.env.ASM_NO_SERVER && process.env.NODE_ENV !== "test" && process.env.ASM_TEST !== "1") {
49
+ try {
50
+ startApiServer();
51
+ } catch {}
52
+
53
+ const onProcessSignal = () => {
54
+ process.exit(0);
55
+ };
56
+ process.on("SIGINT", onProcessSignal);
57
+ process.on("SIGTERM", onProcessSignal);
58
+ }
59
+
60
+ export {
61
+ onRequest,
62
+ onResponse,
63
+ recheckConnectedAgentsStatus,
64
+ verifyPluginWorkabilityOnDisk,
65
+ };
66
+
67
+ export const AgentSpaceManagerPlugin = {
68
+ name: "agent-space-manager",
69
+ onRequest,
70
+ onResponse,
71
+
72
+ // Magpie IPC / Plugin API for the Web UI
73
+ api: {
74
+ getConfig: async () => {
75
+ return loadHubConfig();
76
+ },
77
+ getAgentProfiles: async () => {
78
+ return listAgentProfilesForUi();
79
+ },
80
+ saveConfig: async (config, options = {}) => {
81
+ saveHubConfig(config);
82
+ const effHub = getEffectiveHubRoot(config);
83
+ const plugins = scanPlugins(effHub);
84
+ const categories = buildCategoriesRegistry(plugins, effHub);
85
+ const shouldSync = Boolean(
86
+ options.sync === true ||
87
+ options.connect === true ||
88
+ config?.sync === true ||
89
+ config?.connect === true
90
+ );
91
+ const sync = shouldSync ? syncAllIdes(config, plugins) : null;
92
+ return {
93
+ success: true,
94
+ pluginCount: plugins.length,
95
+ plugins,
96
+ categories,
97
+ synced: shouldSync,
98
+ ...(sync ? { sync } : {}),
99
+ };
100
+ },
101
+ connectAgent: async (hubConfigOrIde, targetIdeOrOptions = {}, options = {}) => {
102
+ let hubConfig;
103
+ let targetIde;
104
+ let opts;
105
+ if (hubConfigOrIde && Array.isArray(hubConfigOrIde.ides)) {
106
+ hubConfig = hubConfigOrIde;
107
+ targetIde = targetIdeOrOptions;
108
+ opts = options || {};
109
+ } else {
110
+ hubConfig = loadHubConfig();
111
+ targetIde = hubConfigOrIde;
112
+ opts = targetIdeOrOptions || {};
113
+ }
114
+ const res = connectAgent(hubConfig, targetIde, opts);
115
+ try {
116
+ writeConnectStatusFile({
117
+ success: res.success !== false,
118
+ targetId: targetIde?.id || targetIde?.agentType,
119
+ targetName: targetIde?.name || targetIde?.id,
120
+ result: res.result || res.sync || res,
121
+ sync: res.sync || res.result || res,
122
+ timestamp: Date.now(),
123
+ });
124
+ } catch {}
125
+ return res;
126
+ },
127
+ getPlugins: async (customHubRoot) => {
128
+ if (customHubRoot !== undefined && customHubRoot !== null && customHubRoot !== "") {
129
+ return scanPlugins(customHubRoot);
130
+ }
131
+ const cfg = loadHubConfig();
132
+ return scanPlugins(getEffectiveHubRoot(cfg));
133
+ },
134
+ scanPlugins: async (customHubRoot) => {
135
+ if (customHubRoot !== undefined && customHubRoot !== null && customHubRoot !== "") {
136
+ return scanPlugins(customHubRoot);
137
+ }
138
+ const cfg = loadHubConfig();
139
+ return scanPlugins(getEffectiveHubRoot(cfg));
140
+ },
141
+ syncAll: async (customConfig) => {
142
+ const cfg = customConfig || loadHubConfig();
143
+ const plugins = scanPlugins(getEffectiveHubRoot(cfg));
144
+ return syncAllIdes(cfg, plugins);
145
+ },
146
+ detectConfigPath: async (name, exePath) => {
147
+ return detectAgentConfigPath(name, exePath);
148
+ },
149
+ inferAgentType: async (name, exePath, configPath) => {
150
+ return inferAgentType(name, exePath, configPath);
151
+ },
152
+ resolveDroppedTarget: async (rawNameOrPath, mode) => {
153
+ return resolveDroppedTarget(rawNameOrPath, mode);
154
+ },
155
+ browseFileSystem: async (type) => {
156
+ return browseFileSystem(type);
157
+ },
158
+ syncIde: async (ideId) => {
159
+ const cfg = loadHubConfig();
160
+ const ide = cfg.ides.find((i) => i.id === ideId);
161
+ if (!ide) return { success: false, error: "IDE not found" };
162
+ const plugins = scanPlugins(getEffectiveHubRoot(cfg));
163
+ return syncIdePlugins(ide, plugins);
164
+ },
165
+ removeIde: async (ideId, killProcesses = false) => {
166
+ const cfg = loadHubConfig();
167
+ const ideIndex = cfg.ides.findIndex((i) => i.id === ideId);
168
+ if (ideIndex === -1) return { success: false, error: "IDE not found" };
169
+
170
+ const ide = cfg.ides[ideIndex];
171
+
172
+ // Sever all junctions, MCP keys, rule blocks, and copied files created in this IDE
173
+ unlinkAllForIde(ide, killProcesses);
174
+
175
+ // Remove from config and persist
176
+ cfg.ides.splice(ideIndex, 1);
177
+ // Clean associated project overrides
178
+ cfg.projects = (cfg.projects || []).filter((p) => p.ideId !== ideId);
179
+ saveHubConfig(cfg);
180
+ const eff = getEffectiveHubRoot(cfg);
181
+ syncAllIdes(cfg, scanPlugins(eff));
182
+
183
+ return { success: true };
184
+ },
185
+ removeProject: async (projectId) => {
186
+ const cfg = loadHubConfig();
187
+ const projIdx = (cfg.projects || []).findIndex((p) => p.id === projectId);
188
+ if (projIdx === -1) return { success: false, error: "Project not found" };
189
+
190
+ const project = cfg.projects[projIdx];
191
+ const parentIde = (cfg.ides || []).find((i) => i.id === project.ideId) || null;
192
+ unlinkAllForProject(project, parentIde);
193
+
194
+ cfg.projects.splice(projIdx, 1);
195
+ saveHubConfig(cfg);
196
+ const eff = getEffectiveHubRoot(cfg);
197
+ syncAllIdes(cfg, scanPlugins(eff));
198
+
199
+ return { success: true };
200
+ },
201
+ rescan: async (customHubRoot) => {
202
+ const cfg = loadHubConfig();
203
+ if (customHubRoot && typeof customHubRoot === "string") {
204
+ cfg.hubRoot = customHubRoot.trim();
205
+ saveHubConfig(cfg);
206
+ }
207
+ const effHub = getEffectiveHubRoot(cfg);
208
+ const plugins = scanPlugins(effHub);
209
+ const categories = buildCategoriesRegistry(plugins, effHub);
210
+ const syncRes = syncAllIdes(cfg, plugins);
211
+ return { success: true, hubRoot: effHub, count: plugins.length, plugins, categories, sync: syncRes };
212
+ },
213
+ checkModifications: async (customHubRoot) => {
214
+ return checkComponentsModifications(customHubRoot);
215
+ },
216
+ startupSync: async (customConfig) => {
217
+ return checkAndAutoReconnectOnStartup(customConfig);
218
+ },
219
+ recheckStatus: async (customConfig) => {
220
+ const cfg = customConfig || loadHubConfig();
221
+ const plugins = scanPlugins(getEffectiveHubRoot(cfg));
222
+ return recheckConnectedAgentsStatus(cfg, plugins);
223
+ },
224
+ getComponentsSnapshot: async (customHubRoot) => {
225
+ return getComponentsStorageSnapshot(getEffectiveHubRoot(customHubRoot));
226
+ },
227
+ evaluateWorkability: (plugin, ide, options) => {
228
+ return evaluateComponentAgentWorkability(plugin, ide, options);
229
+ },
230
+ verifyPluginWorkabilityOnDisk: (plugin, ide, options) => {
231
+ return verifyPluginWorkabilityOnDisk(plugin, ide, options);
232
+ },
233
+ removeComponentUpdateMechanisms: async (customHubRoot) => {
234
+ return removeComponentUpdateMechanisms(customHubRoot);
235
+ },
236
+ checkCentralizedUpdates: async (customHubRoot, options) => {
237
+ return checkCentralizedUpstreamUpdates(customHubRoot, options);
238
+ },
239
+ getCentralizedUpdateStatus: async (customHubRoot) => {
240
+ return loadCentralizedUpstreamUpdateState(customHubRoot);
241
+ },
242
+ collectUpstreams: async (customHubRoot) => {
243
+ return collectAllComponentsUpstream(customHubRoot);
244
+ },
245
+ getOmnirouteStatus: async (customPort) => {
246
+ return runOmnirouteAction("status", customPort);
247
+ },
248
+ startOmniroute: async (customPort) => {
249
+ return runOmnirouteAction("start", customPort);
250
+ },
251
+ stopOmniroute: async (customPort) => {
252
+ return runOmnirouteAction("stop", customPort);
253
+ },
254
+ isMagpieRunning: async () => {
255
+ return isMagpieRunning();
256
+ },
257
+ openOmnirouteUrl: async (url, customPort) => {
258
+ const cfg = loadHubConfig();
259
+ const port = customPort || getOmniroutePort(cfg);
260
+ const targetUrl = url || `http://localhost:${port}`;
261
+ const ok = openUrlInBrowser(targetUrl);
262
+ return { success: ok, url: targetUrl };
263
+ },
264
+ scanForeignComponents: async (agent) => {
265
+ const cfg = loadHubConfig();
266
+ const effHub = getEffectiveHubRoot(cfg);
267
+ const plugins = scanPlugins(effHub);
268
+ return scanAgentForeignComponents(agent, cfg, plugins);
269
+ },
270
+ cleanForeignComponents: async (components) => {
271
+ return cleanAgentForeignComponents(components);
272
+ },
273
+ autoEnableHubComponents: async (agent) => {
274
+ const cfg = loadHubConfig();
275
+ const effHub = getEffectiveHubRoot(cfg);
276
+ const plugins = scanPlugins(effHub);
277
+ return autoEnableHubComponents(agent, cfg, plugins);
278
+ },
279
+ },
280
+ };
281
+
282
+ export async function server(input = {}, rawOptions = {}) {
283
+ const opts = rawOptions || {};
284
+ if (opts.omnirouteRequest) {
285
+ handleOmnirouteRequest(opts.omnirouteRequest);
286
+ }
287
+ return {};
288
+ }
289
+
290
+ AgentSpaceManagerPlugin.server = server;
291
+
292
+ export default AgentSpaceManagerPlugin;