@netnodeag/kraftwerk 0.32.0 → 0.33.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 +22 -17
- package/dist/cli/doctor.js +2 -3
- package/dist/cli/kraftwerk.js +3 -0
- package/dist/cli/projects.d.ts +2 -0
- package/dist/cli/projects.js +126 -0
- package/dist/cli/ui.js +28 -11
- package/dist/config.d.ts +2 -0
- package/dist/config.js +2 -1
- package/dist/inspector/instances.d.ts +64 -1
- package/dist/inspector/instances.js +257 -15
- package/dist/inspector/self-command.d.ts +15 -0
- package/dist/inspector/self-command.js +18 -0
- package/dist/inspector/server.js +43 -7
- package/inspector/dist/assets/{editor-DhNlc5vd.js → editor-BlAeAqt0.js} +3 -3
- package/inspector/dist/assets/{index-CT73nPrH.js → index--9LQl_DL.js} +1 -1
- package/inspector/dist/assets/{index-xM_-dBjg.js → index--wwp-KD0.js} +1 -1
- package/inspector/dist/assets/{index-CWIXOY1D.js → index-9rElziKS.js} +1 -1
- package/inspector/dist/assets/{index-C22iDz8w.js → index-AR7uGZPa.js} +1 -1
- package/inspector/dist/assets/{index--vNjDAEm.js → index-B0qGMqfM.js} +1 -1
- package/inspector/dist/assets/index-B7W4QbA2.css +1 -0
- package/inspector/dist/assets/{index-DYnto4lP.js → index-BKnMNAOc.js} +1 -1
- package/inspector/dist/assets/{index-DmZE5psD.js → index-BvCAG0mQ.js} +1 -1
- package/inspector/dist/assets/{index-Hlokid_B.js → index-BvGhLr64.js} +1 -1
- package/inspector/dist/assets/{index-C9y82Xkr.js → index-BxPjb1aG.js} +1 -1
- package/inspector/dist/assets/{index-BWQugB-m.js → index-C3ugp_Wf.js} +1 -1
- package/inspector/dist/assets/{index-IK4Vsg-q.js → index-C5IBvIVa.js} +44 -44
- package/inspector/dist/assets/{index-Dx4HOh5k.js → index-CV4Aiss-.js} +1 -1
- package/inspector/dist/assets/{index-BanLa7PQ.js → index-CdkhUV5I.js} +1 -1
- package/inspector/dist/assets/{index-BvrVgIw_.js → index-D3O4auYk.js} +1 -1
- package/inspector/dist/assets/{index-BmdO9i46.js → index-DcXZX6dj.js} +1 -1
- package/inspector/dist/assets/{index-Dgi1UC16.js → index-Dnqjpy0h.js} +1 -1
- package/inspector/dist/assets/{index-CK6xHVuA.js → index-LBoNKP4N.js} +1 -1
- package/inspector/dist/assets/{index-2RQVAOu3.js → index-e-11MUIo.js} +1 -1
- package/inspector/dist/index.html +2 -2
- package/package.json +1 -1
- package/inspector/dist/assets/index-iGaBYIin.css +0 -1
|
@@ -1,23 +1,48 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
1
3
|
import fsSync, { promises as fs } from "node:fs";
|
|
2
4
|
import os from "node:os";
|
|
3
5
|
import path from "node:path";
|
|
6
|
+
import { isDir, resolveProject } from "../config.js";
|
|
7
|
+
import { selfCommand } from "./self-command.js";
|
|
4
8
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
9
|
+
* Two registries under ~/.kraftwerk, two lifecycles:
|
|
10
|
+
*
|
|
11
|
+
* instances/<pid>.json — ephemeral. Every running inspector writes one so
|
|
12
|
+
* other local instances can discover it (the workspace switcher lists
|
|
13
|
+
* running workspaces automatically, no kraftwerk.yml `switcher:` needed).
|
|
14
|
+
* Registered ports are verified by probing /api/meta on read; entries
|
|
15
|
+
* that stop answering are pruned, so crashes leave no ghosts.
|
|
16
|
+
*
|
|
17
|
+
* projects/<hash(root)>.json — durable. One record per project root that
|
|
18
|
+
* ever ran the inspector on this machine, keyed by the root and never
|
|
19
|
+
* pruned automatically. Everything else (name, icon, port) is derived from
|
|
20
|
+
* the root's kraftwerk.yml at read time, so it is always current. A
|
|
21
|
+
* project that is not running can be started again from the switcher or
|
|
22
|
+
* `kraftwerk projects start` — that is what makes a killed UI findable.
|
|
11
23
|
*/
|
|
12
|
-
const
|
|
13
|
-
const
|
|
24
|
+
const HOME = path.join(os.homedir(), ".kraftwerk");
|
|
25
|
+
const INSTANCES_DIR = path.join(HOME, "instances");
|
|
26
|
+
const PROJECTS_DIR = path.join(HOME, "projects");
|
|
27
|
+
const LOGS_DIR = path.join(HOME, "logs");
|
|
28
|
+
const selfFile = () => path.join(INSTANCES_DIR, `${process.pid}.json`);
|
|
14
29
|
let selfPort = null;
|
|
30
|
+
let selfRoot = null;
|
|
31
|
+
const projectKey = (root) => createHash("sha1").update(path.resolve(root)).digest("hex").slice(0, 16);
|
|
32
|
+
const projectFile = (root) => path.join(PROJECTS_DIR, `${projectKey(root)}.json`);
|
|
33
|
+
/** ~/… for display. */
|
|
34
|
+
export const tildify = (p) => {
|
|
35
|
+
const home = os.homedir();
|
|
36
|
+
return p === home || p.startsWith(home + path.sep) ? "~" + p.slice(home.length) : p;
|
|
37
|
+
};
|
|
38
|
+
/* ---------- instances (ephemeral) ---------- */
|
|
15
39
|
/** Write this instance's registry file (call once the server listens). */
|
|
16
|
-
export async function registerInstance(port) {
|
|
40
|
+
export async function registerInstance(port, root) {
|
|
17
41
|
selfPort = port;
|
|
42
|
+
selfRoot = path.resolve(root);
|
|
18
43
|
try {
|
|
19
|
-
await fs.mkdir(
|
|
20
|
-
const rec = { pid: process.pid, port, startedAt: new Date().toISOString() };
|
|
44
|
+
await fs.mkdir(INSTANCES_DIR, { recursive: true });
|
|
45
|
+
const rec = { pid: process.pid, port, startedAt: new Date().toISOString(), root: selfRoot };
|
|
21
46
|
await fs.writeFile(selfFile(), JSON.stringify(rec));
|
|
22
47
|
}
|
|
23
48
|
catch { } // best-effort — without it discovery just won't see us
|
|
@@ -62,7 +87,7 @@ export async function discoverInstances() {
|
|
|
62
87
|
return cache.entries;
|
|
63
88
|
let files;
|
|
64
89
|
try {
|
|
65
|
-
files = await fs.readdir(
|
|
90
|
+
files = await fs.readdir(INSTANCES_DIR);
|
|
66
91
|
}
|
|
67
92
|
catch {
|
|
68
93
|
return [];
|
|
@@ -72,7 +97,7 @@ export async function discoverInstances() {
|
|
|
72
97
|
.map(async (f) => {
|
|
73
98
|
let rec;
|
|
74
99
|
try {
|
|
75
|
-
rec = JSON.parse(await fs.readFile(path.join(
|
|
100
|
+
rec = JSON.parse(await fs.readFile(path.join(INSTANCES_DIR, f), "utf8"));
|
|
76
101
|
}
|
|
77
102
|
catch {
|
|
78
103
|
return null;
|
|
@@ -81,10 +106,10 @@ export async function discoverInstances() {
|
|
|
81
106
|
return null;
|
|
82
107
|
const found = await probe(rec.port);
|
|
83
108
|
if (!found) {
|
|
84
|
-
await fs.unlink(path.join(
|
|
109
|
+
await fs.unlink(path.join(INSTANCES_DIR, f)).catch(() => { }); // stale — prune
|
|
85
110
|
return null;
|
|
86
111
|
}
|
|
87
|
-
return found;
|
|
112
|
+
return { ...found, root: rec.root };
|
|
88
113
|
}));
|
|
89
114
|
const entries = probed
|
|
90
115
|
.filter((e) => e != null)
|
|
@@ -92,3 +117,220 @@ export async function discoverInstances() {
|
|
|
92
117
|
cache = { at: Date.now(), entries };
|
|
93
118
|
return entries;
|
|
94
119
|
}
|
|
120
|
+
/* ---------- projects (durable) ---------- */
|
|
121
|
+
async function readProject(file) {
|
|
122
|
+
try {
|
|
123
|
+
const rec = JSON.parse(await fs.readFile(file, "utf8"));
|
|
124
|
+
if (typeof rec.root !== "string")
|
|
125
|
+
return null;
|
|
126
|
+
return {
|
|
127
|
+
root: rec.root,
|
|
128
|
+
firstSeen: rec.firstSeen ?? rec.lastStarted ?? "",
|
|
129
|
+
lastStarted: rec.lastStarted ?? rec.firstSeen ?? "",
|
|
130
|
+
lastStopped: rec.lastStopped,
|
|
131
|
+
startCount: rec.startCount ?? 1,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/** Upsert the project record for a root (call on every inspector start). */
|
|
139
|
+
export async function registerProject(root) {
|
|
140
|
+
const abs = path.resolve(root);
|
|
141
|
+
const now = new Date().toISOString();
|
|
142
|
+
try {
|
|
143
|
+
await fs.mkdir(PROJECTS_DIR, { recursive: true });
|
|
144
|
+
const prev = await readProject(projectFile(abs));
|
|
145
|
+
const rec = {
|
|
146
|
+
root: abs,
|
|
147
|
+
firstSeen: prev?.firstSeen || now,
|
|
148
|
+
lastStarted: now,
|
|
149
|
+
startCount: (prev?.startCount ?? 0) + 1,
|
|
150
|
+
};
|
|
151
|
+
await fs.writeFile(projectFile(abs), JSON.stringify(rec, null, 2));
|
|
152
|
+
}
|
|
153
|
+
catch { } // best-effort, like the instance file
|
|
154
|
+
}
|
|
155
|
+
/** Stamp lastStopped on clean shutdown. Sync so exit handlers can call it. */
|
|
156
|
+
export function markProjectStopped() {
|
|
157
|
+
if (!selfRoot)
|
|
158
|
+
return;
|
|
159
|
+
try {
|
|
160
|
+
const file = projectFile(selfRoot);
|
|
161
|
+
const rec = JSON.parse(fsSync.readFileSync(file, "utf8"));
|
|
162
|
+
rec.lastStopped = new Date().toISOString();
|
|
163
|
+
fsSync.writeFileSync(file, JSON.stringify(rec, null, 2));
|
|
164
|
+
}
|
|
165
|
+
catch { }
|
|
166
|
+
}
|
|
167
|
+
/** All known projects, most recently started first. */
|
|
168
|
+
export async function listProjects() {
|
|
169
|
+
let files;
|
|
170
|
+
try {
|
|
171
|
+
files = await fs.readdir(PROJECTS_DIR);
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
return [];
|
|
175
|
+
}
|
|
176
|
+
const recs = await Promise.all(files.filter((f) => f.endsWith(".json")).map((f) => readProject(path.join(PROJECTS_DIR, f))));
|
|
177
|
+
return recs
|
|
178
|
+
.filter((r) => r != null)
|
|
179
|
+
.sort((a, b) => b.lastStarted.localeCompare(a.lastStarted));
|
|
180
|
+
}
|
|
181
|
+
/** Drop a project record (the root itself is untouched). */
|
|
182
|
+
export async function forgetProject(root) {
|
|
183
|
+
try {
|
|
184
|
+
await fs.unlink(projectFile(root));
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Name, icon and port a root would run with right now — read from its
|
|
193
|
+
* kraftwerk.yml. `exists` is false when the directory is gone or when the
|
|
194
|
+
* project resolves to an ancestor (its kraftwerk.yml was removed, or it
|
|
195
|
+
* sits inside another project): resolveProject walks up and never fails,
|
|
196
|
+
* so without this check a stale record would wear its parent's identity.
|
|
197
|
+
*/
|
|
198
|
+
async function describeRoot(root) {
|
|
199
|
+
const fallback = { name: path.basename(root), port: 1981, exists: false };
|
|
200
|
+
if (!(await isDir(root)))
|
|
201
|
+
return fallback;
|
|
202
|
+
try {
|
|
203
|
+
const project = await resolveProject(root);
|
|
204
|
+
if (path.resolve(project.root) !== path.resolve(root))
|
|
205
|
+
return fallback;
|
|
206
|
+
return {
|
|
207
|
+
name: project.config.name ?? path.basename(project.root),
|
|
208
|
+
icon: project.config.icon || undefined,
|
|
209
|
+
port: project.config.port ?? 1981,
|
|
210
|
+
exists: true,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
return fallback;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
let wsCache = null;
|
|
218
|
+
/**
|
|
219
|
+
* The switcher's view: every known project joined with the live instances,
|
|
220
|
+
* plus running instances with no project record yet. The calling instance
|
|
221
|
+
* itself is left out. Live entries first, then by last start. Cached like
|
|
222
|
+
* discoverInstances — /api/meta is polled and this stats every root.
|
|
223
|
+
*/
|
|
224
|
+
export async function discoverWorkspaces() {
|
|
225
|
+
if (wsCache && Date.now() - wsCache.at < 5_000)
|
|
226
|
+
return wsCache.entries;
|
|
227
|
+
const [projects, live] = await Promise.all([listProjects(), discoverInstances()]);
|
|
228
|
+
const liveByRoot = new Map(live.filter((i) => i.root).map((i) => [i.root, i]));
|
|
229
|
+
const consumed = new Set();
|
|
230
|
+
const entries = (await Promise.all(projects
|
|
231
|
+
.filter((p) => p.root !== selfRoot)
|
|
232
|
+
.map(async (p) => {
|
|
233
|
+
const base = { root: p.root, rootLabel: tildify(p.root), lastStarted: p.lastStarted, lastStopped: p.lastStopped };
|
|
234
|
+
let running = liveByRoot.get(p.root);
|
|
235
|
+
let d;
|
|
236
|
+
if (!running) {
|
|
237
|
+
d = await describeRoot(p.root);
|
|
238
|
+
// A pre-0.33 inspector still serving this root registered no
|
|
239
|
+
// root — match it by the port the root would use, else the
|
|
240
|
+
// switcher shows the same project twice (live + stopped).
|
|
241
|
+
running = live.find((i) => !i.root && i.url === `http://localhost:${d.port}`);
|
|
242
|
+
}
|
|
243
|
+
if (running) {
|
|
244
|
+
consumed.add(running);
|
|
245
|
+
return { ...base, name: running.name, url: running.url, icon: running.icon, live: true, exists: true };
|
|
246
|
+
}
|
|
247
|
+
return { ...base, name: d.name, url: `http://localhost:${d.port}`, icon: d.icon, live: false, exists: d.exists };
|
|
248
|
+
}))).concat(await Promise.all(live
|
|
249
|
+
.filter((i) => !consumed.has(i))
|
|
250
|
+
.map(async (i) => ({
|
|
251
|
+
name: i.name,
|
|
252
|
+
url: i.url,
|
|
253
|
+
icon: i.icon,
|
|
254
|
+
live: true,
|
|
255
|
+
root: i.root,
|
|
256
|
+
rootLabel: i.root ? tildify(i.root) : undefined,
|
|
257
|
+
exists: i.root ? await isDir(i.root) : undefined,
|
|
258
|
+
}))));
|
|
259
|
+
entries.sort((a, b) => {
|
|
260
|
+
if (a.live !== b.live)
|
|
261
|
+
return a.live ? -1 : 1;
|
|
262
|
+
return (b.lastStarted ?? "").localeCompare(a.lastStarted ?? "") || a.name.localeCompare(b.name);
|
|
263
|
+
});
|
|
264
|
+
wsCache = { at: Date.now(), entries };
|
|
265
|
+
return entries;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Launch `kraftwerk ui` for a root as a detached process (selfCommand:
|
|
269
|
+
* the bin this process runs from), logging to ~/.kraftwerk/logs/<key>.log.
|
|
270
|
+
* The reported pid is the `kraftwerk ui` supervisor, which forwards
|
|
271
|
+
* SIGTERM/SIGINT to its server child, so `kill <pid>` stops the whole UI.
|
|
272
|
+
* Waits up to ~5s for the new inspector to answer so callers can link
|
|
273
|
+
* straight to it.
|
|
274
|
+
*/
|
|
275
|
+
export async function startProject(root) {
|
|
276
|
+
const abs = path.resolve(root);
|
|
277
|
+
const { name, port, exists } = await describeRoot(abs);
|
|
278
|
+
if (!exists)
|
|
279
|
+
return { ok: false, error: `not a project directory (missing or moved): ${abs}` };
|
|
280
|
+
const url = `http://localhost:${port}`;
|
|
281
|
+
if (abs === selfRoot)
|
|
282
|
+
return { ok: false, url, live: true, error: "that is this workspace" };
|
|
283
|
+
// Already running, or the port is taken by another kraftwerk?
|
|
284
|
+
cache = wsCache = null;
|
|
285
|
+
const live = await discoverInstances();
|
|
286
|
+
const byRoot = live.find((i) => i.root === abs);
|
|
287
|
+
if (byRoot)
|
|
288
|
+
return { ok: true, url: byRoot.url, live: true };
|
|
289
|
+
if (port === selfPort)
|
|
290
|
+
return { ok: false, url, error: `port ${port} is used by this workspace` };
|
|
291
|
+
const byPort = live.find((i) => i.url === url);
|
|
292
|
+
if (byPort)
|
|
293
|
+
return { ok: false, url, error: `port ${port} is used by "${byPort.name}"` };
|
|
294
|
+
let log;
|
|
295
|
+
let stdio = ["ignore", "ignore", "ignore"];
|
|
296
|
+
try {
|
|
297
|
+
await fs.mkdir(LOGS_DIR, { recursive: true });
|
|
298
|
+
log = path.join(LOGS_DIR, `${projectKey(abs)}.log`);
|
|
299
|
+
const fd = fsSync.openSync(log, "a");
|
|
300
|
+
fsSync.writeSync(fd, `\n--- ${new Date().toISOString()} start ${name} (${abs}) ---\n`);
|
|
301
|
+
stdio = ["ignore", fd, fd];
|
|
302
|
+
}
|
|
303
|
+
catch { }
|
|
304
|
+
const { cmd, args } = selfCommand(["ui"]);
|
|
305
|
+
let child;
|
|
306
|
+
try {
|
|
307
|
+
const env = { ...process.env };
|
|
308
|
+
delete env.KRAFTWERK_UI_SUPERVISED; // the new one runs its own supervisor
|
|
309
|
+
child = spawn(cmd, args, { cwd: abs, detached: true, stdio, env });
|
|
310
|
+
child.unref();
|
|
311
|
+
}
|
|
312
|
+
catch (err) {
|
|
313
|
+
return { ok: false, url, log, error: err.message };
|
|
314
|
+
}
|
|
315
|
+
if (typeof stdio[1] === "number") {
|
|
316
|
+
try {
|
|
317
|
+
fsSync.closeSync(stdio[1]);
|
|
318
|
+
}
|
|
319
|
+
catch { }
|
|
320
|
+
}
|
|
321
|
+
let exited = null;
|
|
322
|
+
child.once("exit", (code, signal) => {
|
|
323
|
+
exited = signal ? `killed by ${signal}` : `exited with code ${code}`;
|
|
324
|
+
});
|
|
325
|
+
const deadline = Date.now() + 5_000;
|
|
326
|
+
while (Date.now() < deadline) {
|
|
327
|
+
await new Promise((r) => setTimeout(r, 300));
|
|
328
|
+
if (exited)
|
|
329
|
+
return { ok: false, url, pid: child.pid, log, error: `kraftwerk ui ${exited}` };
|
|
330
|
+
if (await probe(port)) {
|
|
331
|
+
cache = wsCache = null;
|
|
332
|
+
return { ok: true, url, live: true, pid: child.pid, log };
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
return { ok: true, url, live: false, pid: child.pid, log };
|
|
336
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How to run "this same kraftwerk" as a new process: the node we run on
|
|
3
|
+
* plus the bin script we were started from. Identical for global installs,
|
|
4
|
+
* local node_modules, npx caches and a dev checkout (bin/kraftwerk.js
|
|
5
|
+
* picks src/ vs dist/ itself). Shared by the `kraftwerk ui` supervisor and
|
|
6
|
+
* the project-start feature so bin resolution lives in one place.
|
|
7
|
+
*
|
|
8
|
+
* If the bin file is gone (npx cache pruned, package removed) the
|
|
9
|
+
* fallback is `npx @netnodeag/kraftwerk`, i.e. the registry's latest —
|
|
10
|
+
* not the running version, but better than nothing.
|
|
11
|
+
*/
|
|
12
|
+
export declare function selfCommand(args: string[]): {
|
|
13
|
+
cmd: string;
|
|
14
|
+
args: string[];
|
|
15
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
/**
|
|
3
|
+
* How to run "this same kraftwerk" as a new process: the node we run on
|
|
4
|
+
* plus the bin script we were started from. Identical for global installs,
|
|
5
|
+
* local node_modules, npx caches and a dev checkout (bin/kraftwerk.js
|
|
6
|
+
* picks src/ vs dist/ itself). Shared by the `kraftwerk ui` supervisor and
|
|
7
|
+
* the project-start feature so bin resolution lives in one place.
|
|
8
|
+
*
|
|
9
|
+
* If the bin file is gone (npx cache pruned, package removed) the
|
|
10
|
+
* fallback is `npx @netnodeag/kraftwerk`, i.e. the registry's latest —
|
|
11
|
+
* not the running version, but better than nothing.
|
|
12
|
+
*/
|
|
13
|
+
export function selfCommand(args) {
|
|
14
|
+
const bin = process.argv[1];
|
|
15
|
+
if (bin && existsSync(bin))
|
|
16
|
+
return { cmd: process.execPath, args: [bin, ...args] };
|
|
17
|
+
return { cmd: "npx", args: ["-y", "@netnodeag/kraftwerk", ...args] };
|
|
18
|
+
}
|
package/dist/inspector/server.js
CHANGED
|
@@ -10,7 +10,7 @@ import { cancelChat, createChat, deleteChat, disposeAllBackends, getChat, listCh
|
|
|
10
10
|
import { bundleDetail, conceptDetail, createBundle, knowledgeIndex, putConcept, verifyFromUi, } from "./knowledge.js";
|
|
11
11
|
import { deleteMember, getMember, listMembers, saveMember, setMemberArchived, teamRoot, } from "./team.js";
|
|
12
12
|
import { deleteAgentSkill, getSkill, listAgentSkills, listSkills, readSkill, saveAgentSkill, skillsRoot, } from "./skills.js";
|
|
13
|
-
import {
|
|
13
|
+
import { discoverWorkspaces, forgetProject, markProjectStopped, registerInstance, registerProject, startProject, tildify, unregisterInstance, } from "./instances.js";
|
|
14
14
|
import { getSettings, saveSettings } from "./settings.js";
|
|
15
15
|
import { deleteRoutine, routineStatuses, runRoutineNow, saveRoutine, startRoutineScheduler, } from "./routines.js";
|
|
16
16
|
/**
|
|
@@ -98,15 +98,17 @@ async function handleApi(req, res, url) {
|
|
|
98
98
|
const project = await resolveProject(getProjectRoot()).catch(() => null);
|
|
99
99
|
const projectName = project?.config.name ?? (project ? path.basename(project.root) : "");
|
|
100
100
|
const manual = project?.config.switcher ?? [];
|
|
101
|
-
const discovered = url.searchParams.get("probe") === "1" ? [] : await
|
|
101
|
+
const discovered = url.searchParams.get("probe") === "1" ? [] : await discoverWorkspaces();
|
|
102
102
|
// Manual switcher entries keep their configured name/icon; discovered
|
|
103
|
-
//
|
|
103
|
+
// workspaces that duplicate one (same url, running) only contribute
|
|
104
|
+
// the live flag. Stopped projects never collide — they carry a root,
|
|
105
|
+
// and a manual entry pointing at the same port is just a link.
|
|
104
106
|
const norm = (u) => u.replace(/\/+$/, "").replace("127.0.0.1", "localhost").toLowerCase();
|
|
105
107
|
const manualUrls = new Set(manual.map((e) => norm(e.url)));
|
|
106
|
-
const liveUrls = new Set(discovered.map((d) => norm(d.url)));
|
|
108
|
+
const liveUrls = new Set(discovered.filter((d) => d.live).map((d) => norm(d.url)));
|
|
107
109
|
const switcher = [
|
|
108
110
|
...manual.map((e) => (liveUrls.has(norm(e.url)) ? { ...e, live: true } : e)),
|
|
109
|
-
...discovered.filter((d) => !manualUrls.has(norm(d.url))),
|
|
111
|
+
...discovered.filter((d) => !(d.live && manualUrls.has(norm(d.url)))),
|
|
110
112
|
];
|
|
111
113
|
return json(res, {
|
|
112
114
|
version: await getPkgVersion(),
|
|
@@ -116,9 +118,37 @@ async function handleApi(req, res, url) {
|
|
|
116
118
|
restartable: supervised(),
|
|
117
119
|
projectName,
|
|
118
120
|
projectIcon: project?.config.icon ?? "",
|
|
121
|
+
projectRoot: project?.root ?? getProjectRoot(),
|
|
122
|
+
projectRootLabel: tildify(project?.root ?? getProjectRoot()),
|
|
119
123
|
switcher,
|
|
120
124
|
});
|
|
121
125
|
}
|
|
126
|
+
// POST /api/projects/start {root} — launch `kraftwerk ui` for a known
|
|
127
|
+
// project as a detached process (the switcher's Start button).
|
|
128
|
+
if (seg.length === 3 && seg[1] === "projects" && seg[2] === "start" && method === "POST") {
|
|
129
|
+
try {
|
|
130
|
+
const { root } = JSON.parse(await readBody(req));
|
|
131
|
+
if (!root)
|
|
132
|
+
return json(res, { error: "root required" }, 400);
|
|
133
|
+
const result = await startProject(root);
|
|
134
|
+
return json(res, result, result.ok ? 200 : 409);
|
|
135
|
+
}
|
|
136
|
+
catch (err) {
|
|
137
|
+
return json(res, { error: err.message }, 400);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// POST /api/projects/forget {root} — drop a project from the registry
|
|
141
|
+
if (seg.length === 3 && seg[1] === "projects" && seg[2] === "forget" && method === "POST") {
|
|
142
|
+
try {
|
|
143
|
+
const { root } = JSON.parse(await readBody(req));
|
|
144
|
+
if (!root)
|
|
145
|
+
return json(res, { error: "root required" }, 400);
|
|
146
|
+
return json(res, { ok: await forgetProject(root) });
|
|
147
|
+
}
|
|
148
|
+
catch (err) {
|
|
149
|
+
return json(res, { error: err.message }, 400);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
122
152
|
// GET /api/settings — kraftwerk.yml (parsed + resolved paths) for the settings page
|
|
123
153
|
if (seg.length === 2 && seg[1] === "settings" && method === "GET") {
|
|
124
154
|
return json(res, await getSettings());
|
|
@@ -618,12 +648,16 @@ export function startInspector(opts) {
|
|
|
618
648
|
process.once(sig, () => {
|
|
619
649
|
disposeAllBackends();
|
|
620
650
|
unregisterInstance();
|
|
651
|
+
markProjectStopped();
|
|
621
652
|
process.exit(sig === "SIGINT" ? 130 : 143);
|
|
622
653
|
});
|
|
623
654
|
}
|
|
624
|
-
process.once("exit", () => {
|
|
655
|
+
process.once("exit", (code) => {
|
|
625
656
|
disposeAllBackends();
|
|
626
657
|
unregisterInstance();
|
|
658
|
+
// A self-restart (new version) is not a stop — the project stays "running".
|
|
659
|
+
if (code !== RESTART_EXIT_CODE)
|
|
660
|
+
markProjectStopped();
|
|
627
661
|
});
|
|
628
662
|
const server = http.createServer(async (req, res) => {
|
|
629
663
|
try {
|
|
@@ -641,7 +675,9 @@ export function startInspector(opts) {
|
|
|
641
675
|
server.once("error", reject);
|
|
642
676
|
server.listen(opts.port, () => {
|
|
643
677
|
const addr = server.address();
|
|
644
|
-
|
|
678
|
+
const port = typeof addr === "object" && addr ? addr.port : opts.port;
|
|
679
|
+
void registerInstance(port, getProjectRoot());
|
|
680
|
+
void registerProject(getProjectRoot());
|
|
645
681
|
resolve(server);
|
|
646
682
|
});
|
|
647
683
|
});
|