wizards 0.2.45 → 0.2.47
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/apps/runtime/dist/app.js +34 -1
- package/apps/runtime/dist/cli/features.js +50 -0
- package/apps/runtime/dist/cli/main.js +12 -2
- package/apps/runtime/dist/cli/report.js +81 -0
- package/apps/runtime/dist/debug.js +196 -0
- package/apps/runtime/dist/features.js +123 -0
- package/apps/runtime/dist/harness/model.js +2 -0
- package/apps/runtime/dist/index.js +3 -0
- package/apps/runtime/dist/log.js +106 -0
- package/apps/runtime/dist/manage.js +13 -0
- package/apps/runtime/dist/plugins/discovery.js +11 -0
- package/apps/runtime/dist/plugins/loader.js +10 -0
- package/apps/runtime/dist/routes/debug.js +42 -0
- package/apps/runtime/dist/routes/internal.js +21 -5
- package/apps/runtime/dist/routes/runs.js +1 -1
- package/apps/runtime/dist/routes/studio.js +7 -5
- package/apps/runtime/dist/tenants/control.js +7 -0
- package/apps/web/dist/assets/{AboutDialog-Dx_ged8B.js → AboutDialog-t7bpiasT.js} +1 -1
- package/apps/web/dist/assets/{ChangelogDialog-CbEGxGZG.js → ChangelogDialog-B87yS6aN.js} +1 -1
- package/apps/web/dist/assets/{ChatView-DZm3GK6k.js → ChatView-Dm764bYy.js} +4 -4
- package/apps/web/dist/assets/EditorPage-BG_Ue2iC.js +3 -0
- package/apps/web/dist/assets/{FlowPreview-t7hVutQM.js → FlowPreview-ChXfiMn_.js} +1 -1
- package/apps/web/dist/assets/{Harness-DnUFOVKn.js → Harness-zKMptCzB.js} +2 -2
- package/apps/web/dist/assets/HomePage-BXSg5P5k.js +1 -0
- package/apps/web/dist/assets/{Marketplace-SLjncUHh.js → Marketplace-BWhUJ4UX.js} +2 -2
- package/apps/web/dist/assets/{NewWizardPage-D24mz6nu.js → NewWizardPage-C3NqEGmg.js} +1 -1
- package/apps/web/dist/assets/ProjectPage-D6Fy7uTV.js +2 -0
- package/apps/web/dist/assets/PublicRunner-ChgJs0C9.js +1 -0
- package/apps/web/dist/assets/{ReadOnly-BokOQXGX.js → ReadOnly-C9P9aiU6.js} +1 -1
- package/apps/web/dist/assets/{RunDrawer-BLxTUquf.js → RunDrawer-CvIKLphz.js} +3 -3
- package/apps/web/dist/assets/RunnerView-Bmayop7s.js +2 -0
- package/apps/web/dist/assets/SettingsPage-QkwtRFoD.js +1 -0
- package/apps/web/dist/assets/SetupPage-B3IOQLO3.js +1 -0
- package/apps/web/dist/assets/{SharePage-D30PD4st.js → SharePage-C3QEzicl.js} +1 -1
- package/apps/web/dist/assets/ThemeSwitch-3IzIOlNz.js +1 -0
- package/apps/web/dist/assets/arrow-right-Dpp_mpHe.js +1 -0
- package/apps/web/dist/assets/changelog-Du2gWzlN.js +1 -0
- package/apps/web/dist/assets/{circle-alert-DmnHyrXp.js → circle-alert-CPo3Naay.js} +1 -1
- package/apps/web/dist/assets/{definition-TTirPrTQ.js → definition-CQGiBQ4X.js} +1 -1
- package/apps/web/dist/assets/{external-link-CbsKNS8Z.js → external-link-CpU8aJY2.js} +1 -1
- package/apps/web/dist/assets/index-Cz6c1S5g.js +78 -0
- package/apps/web/dist/assets/{lock-CjhgdsXt.js → lock-C_WlXIQ0.js} +1 -1
- package/apps/web/dist/assets/{refresh-cw-Obopmbfz.js → refresh-cw-Cli5WGnW.js} +1 -1
- package/apps/web/dist/assets/{session-OPJXuQ-4.js → session-miDEdw2c.js} +1 -1
- package/apps/web/dist/assets/{settings-menu-Dme1HWda.js → settings-menu-ROT8Lbbj.js} +1 -1
- package/apps/web/dist/assets/{sparkles-DQnR0qxG.js → sparkles-CP5H-O1E.js} +1 -1
- package/apps/web/dist/assets/{trash-CDGh6DEg.js → trash-DU-hZpV0.js} +1 -1
- package/apps/web/dist/assets/ui-D92EkdEB.js +9 -0
- package/apps/web/dist/assets/{upload-BzfKj43F.js → upload-DMq5N6Fw.js} +1 -1
- package/apps/web/dist/assets/{volume-2-Dca_TstC.js → volume-2-0SfkvOho.js} +1 -1
- package/apps/web/dist/index.html +10 -10
- package/apps/web/dist/mcp/flow.html +41 -41
- package/node_modules/@engenty-wizards/plugin-sdk/dist/index.d.ts +21 -0
- package/node_modules/@engenty-wizards/shared/dist/feature-flags.d.ts +54 -0
- package/node_modules/@engenty-wizards/shared/dist/feature-flags.js +46 -0
- package/node_modules/@engenty-wizards/shared/dist/features.d.ts +36 -0
- package/node_modules/@engenty-wizards/shared/dist/features.js +78 -0
- package/package.json +1 -1
- package/apps/web/dist/assets/EditorPage-DYmr37s6.js +0 -3
- package/apps/web/dist/assets/HomePage-BtDteNOG.js +0 -1
- package/apps/web/dist/assets/ProjectPage-BrJp7tGj.js +0 -2
- package/apps/web/dist/assets/PublicRunner-c3A01WoK.js +0 -1
- package/apps/web/dist/assets/RunnerView-BCXVgQJg.js +0 -2
- package/apps/web/dist/assets/SettingsPage-CUZcrDWr.js +0 -1
- package/apps/web/dist/assets/SetupPage-DVM5r96i.js +0 -1
- package/apps/web/dist/assets/ThemeSwitch-BeoISyt6.js +0 -1
- package/apps/web/dist/assets/arrow-right-BeQQ3XNo.js +0 -1
- package/apps/web/dist/assets/changelog-CpOw96D-.js +0 -1
- package/apps/web/dist/assets/index-C67UNafO.js +0 -78
- package/apps/web/dist/assets/ui-BJwZ9JV4.js +0 -9
package/apps/runtime/dist/app.js
CHANGED
|
@@ -12,6 +12,8 @@ import { ZodError } from "zod";
|
|
|
12
12
|
import { authRoutes, principalOf } from "./auth/index.js";
|
|
13
13
|
import { db, schema, withTenant } from "./db/client.js";
|
|
14
14
|
import { basePath, env } from "./env.js";
|
|
15
|
+
import { features } from "./features.js";
|
|
16
|
+
import { allowed, hashIp } from "./limits.js";
|
|
15
17
|
import { linkPreview } from "./link-preview.js";
|
|
16
18
|
import { discovery, managed } from "./manage.js";
|
|
17
19
|
import { bridgeRequest } from "./mcp/bridge.js";
|
|
@@ -19,11 +21,12 @@ import { mcpHandler } from "./mcp/handler.js";
|
|
|
19
21
|
import { MCP_RESOURCE, SCOPES } from "./mcp/scopes.js";
|
|
20
22
|
import { PLUGIN_NAME, pluginArchive, pluginMarketplace } from "./plugin.js";
|
|
21
23
|
import { apiRoutes } from "./routes/api.js";
|
|
24
|
+
import { browserReport, localDebugRoutes } from "./routes/debug.js";
|
|
22
25
|
import { internalRoutes } from "./routes/internal.js";
|
|
23
26
|
import { marketplaceStudio } from "./routes/marketplace.js";
|
|
24
27
|
import { publicPluginRoutes } from "./routes/plugin-public.js";
|
|
25
28
|
import { pluginRoutes } from "./routes/plugins.js";
|
|
26
|
-
import { connectCallback, publicRoutes, runRoutes, shareRoutes } from "./routes/runs.js";
|
|
29
|
+
import { clientIp, connectCallback, publicRoutes, runRoutes, shareRoutes } from "./routes/runs.js";
|
|
27
30
|
import { studio } from "./routes/studio.js";
|
|
28
31
|
import { wizardStream } from "./routes/wizard-stream.js";
|
|
29
32
|
import { runTicketValid } from "./secrets/signing.js";
|
|
@@ -99,12 +102,41 @@ app.use("*", async (c, next) => {
|
|
|
99
102
|
return next();
|
|
100
103
|
});
|
|
101
104
|
app.get("/api/health", (c) => c.json({ ok: true }));
|
|
105
|
+
/**
|
|
106
|
+
* A page in a browser (the studio, a wizard's own page) tells of an error it met: the line
|
|
107
|
+
* goes into this process's log like the server's own, so a look from afar sees both sides
|
|
108
|
+
* (docs/plans/remote-debug.md). Anyone may send; the size and the count per address are capped.
|
|
109
|
+
*/
|
|
110
|
+
app.post("/api/log", bodyLimit({ maxSize: 8 * 1024 }), async (c) => {
|
|
111
|
+
const ip = hashIp(clientIp(c));
|
|
112
|
+
if (!allowed(`log:${ip ?? "?"}`, 60, 60 * 60 * 1000)) {
|
|
113
|
+
return c.json({ ok: false }, 429);
|
|
114
|
+
}
|
|
115
|
+
const parsed = browserReport.safeParse(await c.req.json().catch(() => null));
|
|
116
|
+
if (!parsed.success) {
|
|
117
|
+
return c.json({ ok: false }, 400);
|
|
118
|
+
}
|
|
119
|
+
const report = parsed.data;
|
|
120
|
+
const who = await principalOf(c).catch(() => null);
|
|
121
|
+
const tag = who ? `${who.tenantId}/${who.id}` : "visitor";
|
|
122
|
+
const where = report.url ? ` at ${report.url}` : "";
|
|
123
|
+
const line = `[browser ${tag}]${where}: ${report.message}${report.stack ? `\n${report.stack}` : ""}${report.agent ? `\n${report.agent}` : ""}`;
|
|
124
|
+
if (report.level === "warn") {
|
|
125
|
+
console.warn(line);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
console.error(line);
|
|
129
|
+
}
|
|
130
|
+
return c.json({ ok: true });
|
|
131
|
+
});
|
|
102
132
|
app.route("/api", authRoutes);
|
|
103
133
|
app.get("/api/config", (c) => c.json({
|
|
104
134
|
mode: managed ? "managed" : "local",
|
|
105
135
|
devLogin: !managed && env.devLogin,
|
|
106
136
|
/** The product's own site, for a runtime that runs alone. */
|
|
107
137
|
site: managed ? null : env.local.cloudUrl,
|
|
138
|
+
/** The feature switches before anyone is signed in: the code's, the environment's, the file's. */
|
|
139
|
+
features: features(),
|
|
108
140
|
}));
|
|
109
141
|
// OAuth discovery for MCP clients (RFC 9728): the Manage-App is the authorization server.
|
|
110
142
|
app.get("/.well-known/oauth-protected-resource/*", async (c) => {
|
|
@@ -184,6 +216,7 @@ app.route("/api/studio/plugins", pluginRoutes);
|
|
|
184
216
|
app.route("/api/studio", studio);
|
|
185
217
|
app.route("/api/v1", apiRoutes);
|
|
186
218
|
app.route("/api/internal", internalRoutes);
|
|
219
|
+
app.route("/api/local/debug", localDebugRoutes);
|
|
187
220
|
app.route("/api/public/plugins", publicPluginRoutes);
|
|
188
221
|
app.route("/api/public", publicRoutes);
|
|
189
222
|
app.route("/api/runs", runRoutes);
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { defaultFeatures, FEATURE_NAME, parseFeatures, resolveFeatures, } from "@engenty-wizards/shared/features";
|
|
2
|
+
import { declaredFeatures, featuresFile, fileFeatures, writeFeature } from "../features.js";
|
|
3
|
+
import { discoverPlugins } from "../plugins/discovery.js";
|
|
4
|
+
import { pluginDirs } from "../plugins/loader.js";
|
|
5
|
+
import { proModulesDir } from "../plugins/pro-state.js";
|
|
6
|
+
import { command } from "./home.js";
|
|
7
|
+
/**
|
|
8
|
+
* `wizards features` — the feature switches of this install, from its `.features` file. Not in
|
|
9
|
+
* the help: for whoever works on a part before it is on for everyone.
|
|
10
|
+
*
|
|
11
|
+
* features what is on, and where that comes from
|
|
12
|
+
* features <name> on|off put a switch into the file
|
|
13
|
+
* features <name> default take it out again
|
|
14
|
+
*/
|
|
15
|
+
export function featuresCommand(args) {
|
|
16
|
+
const [name, value] = args;
|
|
17
|
+
const file = featuresFile();
|
|
18
|
+
if (!name) {
|
|
19
|
+
// The core's switches and those the plugins here declare, without loading them.
|
|
20
|
+
const found = discoverPlugins([...pluginDirs(), proModulesDir()]).found;
|
|
21
|
+
const declared = declaredFeatures(found);
|
|
22
|
+
const fromEnv = parseFeatures(process.env.FEATURES ?? "");
|
|
23
|
+
const fromFile = fileFeatures(file);
|
|
24
|
+
const all = resolveFeatures(defaultFeatures(declared), fromEnv, fromFile);
|
|
25
|
+
const names = [
|
|
26
|
+
...new Set([...Object.keys(declared), ...Object.keys(fromEnv), ...Object.keys(fromFile)]),
|
|
27
|
+
];
|
|
28
|
+
for (const n of names) {
|
|
29
|
+
const source = n in fromFile ? "file" : n in fromEnv ? "FEATURES" : "default";
|
|
30
|
+
const d = declared[n];
|
|
31
|
+
const kind = d ? d.kind : "?";
|
|
32
|
+
console.log(`${all[n] ? "on " : "off"} ${n.padEnd(28)} ${kind.padEnd(8)} ${source.padEnd(9)} ${d?.label ?? ""}`);
|
|
33
|
+
}
|
|
34
|
+
console.log(`\nfile: ${file}\nAn account's or a team's switches come on top of these while the runtime runs.`);
|
|
35
|
+
return 0;
|
|
36
|
+
}
|
|
37
|
+
if (!FEATURE_NAME.test(name)) {
|
|
38
|
+
console.error(`"${name}" is no switch name (teamPlans, contacts.leadScoring).`);
|
|
39
|
+
return 2;
|
|
40
|
+
}
|
|
41
|
+
if (value === "on" || value === "off" || value === "default") {
|
|
42
|
+
writeFeature(name, value === "default" ? null : value === "on", file);
|
|
43
|
+
console.log(value === "default"
|
|
44
|
+
? `${name} follows the default again (${file}). A running runtime reads it at once.`
|
|
45
|
+
: `${name} is ${value} on this machine (${file}). A running runtime reads it at once.`);
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
console.error(`${command()} features ${name} on|off|default`);
|
|
49
|
+
return 2;
|
|
50
|
+
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { apps, connectCommand } from "./connect.js";
|
|
2
|
+
import { featuresCommand } from "./features.js";
|
|
2
3
|
import { command, layout, packageVersion, readInstallNote } from "./home.js";
|
|
3
4
|
import { CLIENTS, nodeIsCurrent } from "./machine.js";
|
|
4
5
|
import { mcp } from "./mcp.js";
|
|
5
6
|
import { menu } from "./menu.js";
|
|
7
|
+
import { logsCommand, reportCommand } from "./report.js";
|
|
6
8
|
import { setup } from "./setup.js";
|
|
7
9
|
import { open, start, studio } from "./start.js";
|
|
8
10
|
import { autostart, restart, status, stop } from "./status.js";
|
|
@@ -17,6 +19,8 @@ const HELP = `engenty wizards ${packageVersion()}
|
|
|
17
19
|
${command()} open let this browser into the running studio (--print: only show the link)
|
|
18
20
|
${command()} status what is installed and what runs
|
|
19
21
|
${command()} doctor status, and what is wrong
|
|
22
|
+
${command()} logs the last lines it printed (\`logs 500\` for more)
|
|
23
|
+
${command()} report send status, model settings and the last log lines to your account, for support
|
|
20
24
|
${command()} stop stop it; the data is kept
|
|
21
25
|
${command()} restart stop it and start it again (after an update: the new version)
|
|
22
26
|
${command()} update the newest version
|
|
@@ -77,10 +81,10 @@ function parse(argv) {
|
|
|
77
81
|
else if (arg.startsWith("-")) {
|
|
78
82
|
return `Unknown option ${arg}.`;
|
|
79
83
|
}
|
|
80
|
-
else if (args.command === "autostart" && !args.value) {
|
|
84
|
+
else if ((args.command === "autostart" || args.command === "logs") && !args.value) {
|
|
81
85
|
args.value = arg;
|
|
82
86
|
}
|
|
83
|
-
else if (
|
|
87
|
+
else if (["connect", "disconnect", "features"].includes(args.command)) {
|
|
84
88
|
args.apps.push(arg);
|
|
85
89
|
}
|
|
86
90
|
else if (args.command) {
|
|
@@ -140,6 +144,10 @@ async function main(argv) {
|
|
|
140
144
|
return status(paths, false);
|
|
141
145
|
case "doctor":
|
|
142
146
|
return status(paths, true);
|
|
147
|
+
case "logs":
|
|
148
|
+
return logsCommand(paths, args.value);
|
|
149
|
+
case "report":
|
|
150
|
+
return reportCommand(paths);
|
|
143
151
|
case "stop":
|
|
144
152
|
return stop(paths);
|
|
145
153
|
case "restart":
|
|
@@ -153,6 +161,8 @@ async function main(argv) {
|
|
|
153
161
|
return connectCommand(paths, args.apps, args.command === "disconnect");
|
|
154
162
|
case "mcp":
|
|
155
163
|
return mcp(paths);
|
|
164
|
+
case "features":
|
|
165
|
+
return featuresCommand(args.apps);
|
|
156
166
|
default:
|
|
157
167
|
console.error(`Unknown command ${args.command}.\n\n${HELP}`);
|
|
158
168
|
return 2;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { LOCAL_MCP_HEADER, mintLocalTicket } from "../auth/local-ticket.js";
|
|
4
|
+
import { command } from "./home.js";
|
|
5
|
+
import { runningRuntime, settings } from "./start.js";
|
|
6
|
+
import { bad, dim, ok } from "./ui.js";
|
|
7
|
+
/**
|
|
8
|
+
* `wizards logs` shows the last lines the running runtime printed; `wizards report` sends its
|
|
9
|
+
* status, model settings and those lines to the linked account, where an operator reads them
|
|
10
|
+
* (docs/plans/remote-debug.md). Both talk to the running process, signed with the data folder's
|
|
11
|
+
* secret as `wizards mcp` does; neither sends anything by itself.
|
|
12
|
+
*/
|
|
13
|
+
function readSecret(paths, dataDir) {
|
|
14
|
+
const given = process.env.APP_SECRET?.trim() || settings(paths).fileEnv.APP_SECRET?.trim();
|
|
15
|
+
if (given) {
|
|
16
|
+
return given;
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
return readFileSync(join(dataDir, "secret"), "utf8").trim();
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async function ask(paths, method, path) {
|
|
26
|
+
const { dataDir } = settings(paths);
|
|
27
|
+
const running = await runningRuntime(dataDir);
|
|
28
|
+
if (!running) {
|
|
29
|
+
return `engenty wizards is not running. Start it with \`${command()} start\` first.`;
|
|
30
|
+
}
|
|
31
|
+
const secret = readSecret(paths, dataDir);
|
|
32
|
+
if (!secret) {
|
|
33
|
+
return "The data folder's secret could not be read.";
|
|
34
|
+
}
|
|
35
|
+
const res = await fetch(`http://127.0.0.1:${running.port}/api/local/debug${path}`, {
|
|
36
|
+
method,
|
|
37
|
+
headers: { [LOCAL_MCP_HEADER]: mintLocalTicket(secret, Date.now(), "local-debug") },
|
|
38
|
+
signal: AbortSignal.timeout(30_000),
|
|
39
|
+
});
|
|
40
|
+
return {
|
|
41
|
+
status: res.status,
|
|
42
|
+
body: (await res.json().catch(() => ({}))),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** The last lines of the running runtime; `value` may name how many (default 100). */
|
|
46
|
+
export async function logsCommand(paths, value) {
|
|
47
|
+
const limit = Math.min(Math.max(1, Number(value) || 100), 2000);
|
|
48
|
+
const answer = await ask(paths, "GET", `/logs?limit=${limit}`);
|
|
49
|
+
if (typeof answer === "string") {
|
|
50
|
+
console.error(answer);
|
|
51
|
+
return 1;
|
|
52
|
+
}
|
|
53
|
+
const lines = answer.body.lines ?? [];
|
|
54
|
+
for (const line of lines) {
|
|
55
|
+
const at = new Date(line.at).toISOString().slice(11, 19);
|
|
56
|
+
const level = line.level === "info" ? " " : line.level.padEnd(5);
|
|
57
|
+
console.log(`${dim(at)} ${level} ${line.text}`);
|
|
58
|
+
}
|
|
59
|
+
if (!lines.length) {
|
|
60
|
+
console.log(dim("Nothing printed yet."));
|
|
61
|
+
}
|
|
62
|
+
return 0;
|
|
63
|
+
}
|
|
64
|
+
export async function reportCommand(paths) {
|
|
65
|
+
console.log(dim("Sending status, model settings (no keys) and the last log lines …"));
|
|
66
|
+
const answer = await ask(paths, "POST", "/report");
|
|
67
|
+
if (typeof answer === "string") {
|
|
68
|
+
console.error(answer);
|
|
69
|
+
return 1;
|
|
70
|
+
}
|
|
71
|
+
if (answer.status === 409) {
|
|
72
|
+
console.error(`${bad} This install is linked to no account. Link one in the studio under Settings → Modelle & Konto, then send again.`);
|
|
73
|
+
return 1;
|
|
74
|
+
}
|
|
75
|
+
if (answer.status !== 200) {
|
|
76
|
+
console.error(`${bad} ${answer.body.error ?? `The runtime answered ${answer.status}.`}`);
|
|
77
|
+
return 1;
|
|
78
|
+
}
|
|
79
|
+
console.log(`${ok} Report ${answer.body.id} is at ${answer.body.to}. Name it when you ask for help.`);
|
|
80
|
+
return 0;
|
|
81
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { arch, hostname, platform } from "node:os";
|
|
2
|
+
import { desc, eq } from "drizzle-orm";
|
|
3
|
+
import { accountToken, linkedAccount } from "./auth/account.js";
|
|
4
|
+
import { packageVersion } from "./cli/home.js";
|
|
5
|
+
import { db, knownTenants, schema, withTenant } from "./db/client.js";
|
|
6
|
+
import { env } from "./env.js";
|
|
7
|
+
import { logState, startedAt, tail } from "./log.js";
|
|
8
|
+
import { managed } from "./manage.js";
|
|
9
|
+
import { modelSettings } from "./models.js";
|
|
10
|
+
import { loadedPlugins } from "./plugins/registry.js";
|
|
11
|
+
import { runIndexOf, tenantOfRun } from "./tenants/control.js";
|
|
12
|
+
import { LOCAL_TENANT } from "./tenants/tenant.js";
|
|
13
|
+
/**
|
|
14
|
+
* What an installation tells about itself when someone looks at it from afar
|
|
15
|
+
* (docs/plans/remote-debug.md): its status, the lines it printed, one run, a tenant's models.
|
|
16
|
+
* Nothing here holds a key or a person's answers; the log ring took the secrets out already.
|
|
17
|
+
* The Manage-App asks a runtime of its own over `/api/internal/debug/*`; a runtime that runs
|
|
18
|
+
* alone answers the command line over `/api/local/debug/*` and sends a report to its account.
|
|
19
|
+
*/
|
|
20
|
+
/** This process, as every installation names itself. */
|
|
21
|
+
export function processStatus() {
|
|
22
|
+
const memory = process.memoryUsage();
|
|
23
|
+
return {
|
|
24
|
+
pid: process.pid,
|
|
25
|
+
node: process.version,
|
|
26
|
+
platform: `${platform()} ${arch()}`,
|
|
27
|
+
hostname: hostname(),
|
|
28
|
+
startedAt: new Date(startedAt).toISOString(),
|
|
29
|
+
uptimeSeconds: Math.round((Date.now() - startedAt) / 1000),
|
|
30
|
+
memoryMb: Math.round(memory.rss / 1_048_576),
|
|
31
|
+
log: logState(),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export async function status() {
|
|
35
|
+
const tenants = await knownTenants().catch(() => []);
|
|
36
|
+
return {
|
|
37
|
+
app: "engenty-wizards-runtime",
|
|
38
|
+
version: packageVersion(),
|
|
39
|
+
process: processStatus(),
|
|
40
|
+
managed,
|
|
41
|
+
appUrl: env.appUrl,
|
|
42
|
+
manageUrl: env.manage.url || null,
|
|
43
|
+
gatewayUrl: (managed ? env.manage.gatewayUrl : env.local.gatewayUrl) || null,
|
|
44
|
+
sandbox: env.sandbox,
|
|
45
|
+
browser: env.browserCdpUrl ? "cdp" : "local",
|
|
46
|
+
files: env.s3.bucket ? "s3" : "data folder",
|
|
47
|
+
database: env.databaseUrl ? "libsql" : "file",
|
|
48
|
+
tenantDatabases: env.tenantDbUrlTemplate ? "libsql" : "files",
|
|
49
|
+
tenants: tenants.length,
|
|
50
|
+
plugins: loadedPlugins().map((p) => ({
|
|
51
|
+
id: p.source.id,
|
|
52
|
+
version: p.source.manifest.version ?? null,
|
|
53
|
+
})),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
const from = () => hostname();
|
|
57
|
+
/** The lines this process printed. */
|
|
58
|
+
export function logs(query) {
|
|
59
|
+
const own = from();
|
|
60
|
+
return {
|
|
61
|
+
sources: [{ from: own, ok: true }],
|
|
62
|
+
lines: tail(query).map((line) => ({ ...line, from: own })),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/** One run, whichever tenant it belongs to: its state, its error, what the line knows, its last events. */
|
|
66
|
+
export async function runDetail(runId) {
|
|
67
|
+
if (!/^[A-Za-z0-9_-]{1,64}$/.test(runId)) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
const tenantId = await tenantOfRun(runId);
|
|
71
|
+
if (!tenantId) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
return withTenant(tenantId, async () => {
|
|
75
|
+
const run = await db.query.run.findFirst({
|
|
76
|
+
where: eq(schema.run.id, runId),
|
|
77
|
+
columns: {
|
|
78
|
+
id: true,
|
|
79
|
+
wizardId: true,
|
|
80
|
+
version: true,
|
|
81
|
+
mode: true,
|
|
82
|
+
status: true,
|
|
83
|
+
cursor: true,
|
|
84
|
+
error: true,
|
|
85
|
+
ask: true,
|
|
86
|
+
unattended: true,
|
|
87
|
+
costMicros: true,
|
|
88
|
+
userId: true,
|
|
89
|
+
createdAt: true,
|
|
90
|
+
updatedAt: true,
|
|
91
|
+
expiresAt: true,
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
if (!run) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
const [wizard, events, line] = await Promise.all([
|
|
98
|
+
db.query.wizard.findFirst({
|
|
99
|
+
where: eq(schema.wizard.id, run.wizardId),
|
|
100
|
+
columns: { title: true, publishedVersion: true },
|
|
101
|
+
}),
|
|
102
|
+
db
|
|
103
|
+
.select({
|
|
104
|
+
id: schema.runEvent.id,
|
|
105
|
+
stepId: schema.runEvent.stepId,
|
|
106
|
+
type: schema.runEvent.type,
|
|
107
|
+
message: schema.runEvent.message,
|
|
108
|
+
createdAt: schema.runEvent.createdAt,
|
|
109
|
+
})
|
|
110
|
+
.from(schema.runEvent)
|
|
111
|
+
.where(eq(schema.runEvent.runId, runId))
|
|
112
|
+
.orderBy(desc(schema.runEvent.id))
|
|
113
|
+
.limit(60),
|
|
114
|
+
runIndexOf(runId),
|
|
115
|
+
]);
|
|
116
|
+
return {
|
|
117
|
+
tenantId,
|
|
118
|
+
run: {
|
|
119
|
+
...run,
|
|
120
|
+
ask: run.ask ? { id: run.ask.id, kind: run.ask.kind } : null,
|
|
121
|
+
costCredits: Math.round(run.costMicros / 10_000) / 100,
|
|
122
|
+
},
|
|
123
|
+
wizard: wizard ? { id: run.wizardId, ...wizard } : null,
|
|
124
|
+
line,
|
|
125
|
+
events: events.reverse(),
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
/** Where each class of a tenant runs, as its settings page shows it; never a key. */
|
|
130
|
+
export async function tenantModels(tenantId) {
|
|
131
|
+
if (!/^[A-Za-z0-9_-]{1,64}$/.test(tenantId)) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
const known = await knownTenants();
|
|
135
|
+
if (!known.includes(tenantId)) {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
return withTenant(tenantId, async () => {
|
|
139
|
+
const settings = await modelSettings();
|
|
140
|
+
return {
|
|
141
|
+
tenantId,
|
|
142
|
+
source: settings.source,
|
|
143
|
+
bindings: settings.bindings,
|
|
144
|
+
creditFallback: settings.creditFallback,
|
|
145
|
+
keys: settings.keys,
|
|
146
|
+
ways: settings.ways,
|
|
147
|
+
};
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
export class ReportError extends Error {
|
|
151
|
+
code;
|
|
152
|
+
constructor(code, message) {
|
|
153
|
+
super(message);
|
|
154
|
+
this.code = code;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* A runtime that runs alone sends its account what a look from afar needs: its status, its
|
|
159
|
+
* model settings (never a key) and the last lines it printed. The person asks for it
|
|
160
|
+
* (`wizards report`); nothing goes by itself.
|
|
161
|
+
*/
|
|
162
|
+
export async function sendReport() {
|
|
163
|
+
const account = await linkedAccount();
|
|
164
|
+
const token = account ? await accountToken() : null;
|
|
165
|
+
if (!account || !token) {
|
|
166
|
+
throw new ReportError("no_account", "This install is linked to no account.");
|
|
167
|
+
}
|
|
168
|
+
const [state, models] = await Promise.all([status(), withTenant(LOCAL_TENANT, modelSettings)]);
|
|
169
|
+
const body = {
|
|
170
|
+
kind: "install",
|
|
171
|
+
version: state.version,
|
|
172
|
+
status: state,
|
|
173
|
+
models: {
|
|
174
|
+
source: models.source,
|
|
175
|
+
bindings: models.bindings,
|
|
176
|
+
creditFallback: models.creditFallback,
|
|
177
|
+
keys: models.keys,
|
|
178
|
+
ways: models.ways,
|
|
179
|
+
},
|
|
180
|
+
logs: tail({ limit: 500 }),
|
|
181
|
+
};
|
|
182
|
+
const res = await fetch(`${env.local.accountUrl}/v1/diagnostics`, {
|
|
183
|
+
method: "POST",
|
|
184
|
+
headers: { authorization: `Bearer ${token}`, "content-type": "application/json" },
|
|
185
|
+
body: JSON.stringify(body),
|
|
186
|
+
signal: AbortSignal.timeout(20_000),
|
|
187
|
+
}).catch((err) => {
|
|
188
|
+
throw new ReportError("not_sent", `The account did not answer: ${err.message}`);
|
|
189
|
+
});
|
|
190
|
+
if (!res.ok) {
|
|
191
|
+
const text = await res.text().catch(() => "");
|
|
192
|
+
throw new ReportError("not_sent", `The account answered ${res.status}: ${text.slice(0, 200)}`);
|
|
193
|
+
}
|
|
194
|
+
const { id } = (await res.json());
|
|
195
|
+
return { id, to: env.local.accountUrl };
|
|
196
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { declaredByPlugin, defaultFeatures, FEATURES, parseFeatures, resolveFeatures, } from "@engenty-wizards/shared/features";
|
|
4
|
+
import { accountOverview } from "./auth/account.js";
|
|
5
|
+
import { isCheckout, layout, packageRoot } from "./cli/home.js";
|
|
6
|
+
import { env } from "./env.js";
|
|
7
|
+
import { managed, tenantInfo } from "./manage.js";
|
|
8
|
+
import { loadedPlugins } from "./plugins/registry.js";
|
|
9
|
+
import { currentTenantOrNull } from "./tenants/tenant.js";
|
|
10
|
+
/**
|
|
11
|
+
* Which feature switches are on for this runtime (packages/shared/src/features.ts), laid over
|
|
12
|
+
* each other, the last winning:
|
|
13
|
+
*
|
|
14
|
+
* 1. the defaults the code declares: the core's (packages/shared/src/feature-flags.ts) and the
|
|
15
|
+
* loaded plugins' (their manifests' `features`, under the plugin's id)
|
|
16
|
+
* 2. the `FEATURES` environment variable (`teamPlans=on,site=off`): the cloud image, compose, dev
|
|
17
|
+
* 3. what the Manage-App says — for the team of a cloud runtime, for the linked account of a
|
|
18
|
+
* local one; a plan's switches and what an admin set for the team or for everyone
|
|
19
|
+
* 4. the `.features` file of this install (`~/.engenty/wizards/.features`; a checkout reads
|
|
20
|
+
* the repo root's, a cloud runtime its data folder's) — this machine's word is the last
|
|
21
|
+
*
|
|
22
|
+
* The file is read on every call, so a change counts at once. Nothing here is documented
|
|
23
|
+
* outside the code.
|
|
24
|
+
*/
|
|
25
|
+
/** The `.features` file this runtime reads: the install's, the checkout's root, or the data folder's. */
|
|
26
|
+
export function featuresFile() {
|
|
27
|
+
if (managed) {
|
|
28
|
+
return join(env.dataDir, ".features");
|
|
29
|
+
}
|
|
30
|
+
return isCheckout() ? join(packageRoot, ".features") : join(layout().home, ".features");
|
|
31
|
+
}
|
|
32
|
+
let fileCache = null;
|
|
33
|
+
/** What the `.features` file says, read again when it changed. */
|
|
34
|
+
export function fileFeatures(file = featuresFile()) {
|
|
35
|
+
let mtime = -1;
|
|
36
|
+
try {
|
|
37
|
+
mtime = statSync(file).mtimeMs;
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
fileCache = null;
|
|
41
|
+
return {};
|
|
42
|
+
}
|
|
43
|
+
if (fileCache && fileCache.file === file && fileCache.mtime === mtime) {
|
|
44
|
+
return fileCache.value;
|
|
45
|
+
}
|
|
46
|
+
const value = parseFeatures(readFileSync(file, "utf8"));
|
|
47
|
+
fileCache = { file, mtime, value };
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
const envFeatures = () => parseFeatures(process.env.FEATURES ?? "");
|
|
51
|
+
/** Every switch something declared: the core's, then each plugin's under its id. */
|
|
52
|
+
export function declaredFeatures(sources = loadedPlugins().map((p) => p.source)) {
|
|
53
|
+
const out = { ...FEATURES };
|
|
54
|
+
for (const source of sources) {
|
|
55
|
+
Object.assign(out, declaredByPlugin(source.id, source.manifest.features));
|
|
56
|
+
}
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
/** The switches, with what the Manage-App said for the team or account laid in. */
|
|
60
|
+
export function features(remote) {
|
|
61
|
+
return resolveFeatures(defaultFeatures(declaredFeatures()), envFeatures(), remote, fileFeatures());
|
|
62
|
+
}
|
|
63
|
+
export function feature(name, remote) {
|
|
64
|
+
return features(remote)[name] === true;
|
|
65
|
+
}
|
|
66
|
+
let accountCache = null;
|
|
67
|
+
/**
|
|
68
|
+
* What the Manage-App says for the tenant at hand: the team's switches on a cloud runtime
|
|
69
|
+
* (cached a minute by `tenantInfo`), the linked account's on a local one (cached a minute
|
|
70
|
+
* here). Null when nothing is linked or the Manage-App does not answer.
|
|
71
|
+
*/
|
|
72
|
+
export async function remoteFeatures() {
|
|
73
|
+
if (managed) {
|
|
74
|
+
const id = currentTenantOrNull();
|
|
75
|
+
if (!id) {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
return tenantInfo(id)
|
|
79
|
+
.then((info) => info.features ?? null)
|
|
80
|
+
.catch(() => null);
|
|
81
|
+
}
|
|
82
|
+
if (accountCache && Date.now() - accountCache.at < 60_000) {
|
|
83
|
+
return accountCache.value;
|
|
84
|
+
}
|
|
85
|
+
const overview = await accountOverview().catch(() => null);
|
|
86
|
+
accountCache = { at: Date.now(), value: overview?.tenant.features ?? null };
|
|
87
|
+
return accountCache.value;
|
|
88
|
+
}
|
|
89
|
+
/** The switches for the tenant at hand, with what the Manage-App says fetched. */
|
|
90
|
+
export async function currentFeatures() {
|
|
91
|
+
return features(await remoteFeatures());
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Writes one switch into the `.features` file as `name=on` or `name=off`, on the line that
|
|
95
|
+
* names it when there is one (commented out or not), else at the end; `null` turns the line
|
|
96
|
+
* back into the template's `# name=on|off`. Other lines and comments stay as they are. For the
|
|
97
|
+
* `wizards features` command.
|
|
98
|
+
*/
|
|
99
|
+
export function writeFeature(name, on, file = featuresFile()) {
|
|
100
|
+
let lines = [];
|
|
101
|
+
try {
|
|
102
|
+
lines = readFileSync(file, "utf8").replace(/\n+$/, "").split("\n");
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
lines = [];
|
|
106
|
+
}
|
|
107
|
+
const names = (line) => {
|
|
108
|
+
const m = /^\s*#?\s*([A-Za-z0-9.]+)\s*=\s*\S+\s*(?:#.*)?$/.exec(line);
|
|
109
|
+
return m?.[1] === name;
|
|
110
|
+
};
|
|
111
|
+
const written = on === null ? `# ${name}=on|off` : `${name}=${on ? "on" : "off"}`;
|
|
112
|
+
const at = lines.findIndex(names);
|
|
113
|
+
if (at === -1) {
|
|
114
|
+
lines.push(written);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
lines[at] = written;
|
|
118
|
+
}
|
|
119
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
120
|
+
writeFileSync(file, `${lines.join("\n")}\n`);
|
|
121
|
+
fileCache = null;
|
|
122
|
+
return fileFeatures(file);
|
|
123
|
+
}
|
|
@@ -13,6 +13,8 @@ export const exhausted = (h) => h.exhausted ??
|
|
|
13
13
|
`Das Konto, mit dem ${h.name} angemeldet ist, hat kein Guthaben oder Kontingent mehr.`;
|
|
14
14
|
/** The failure a client reports, as the person should read it. */
|
|
15
15
|
export function failureOf(h, detail) {
|
|
16
|
+
// The client's own words stay in the log: the sentence below sums them up for the person.
|
|
17
|
+
console.warn(`[${h.id}] ${detail.replace(/\s+/g, " ").trim().slice(0, 1500)}`);
|
|
16
18
|
if (/credit balance|billing|too low|usage limit|quota|insufficient|rate.?limit|\b429\b/i.test(detail)) {
|
|
17
19
|
return new ModelUnavailableError(exhausted(h));
|
|
18
20
|
}
|
|
@@ -4,6 +4,7 @@ import { adoptLegacyData } from "./db/adopt.js";
|
|
|
4
4
|
import { knownTenants, migrateAllTenants, migrateControlDb, openTenantDb, withTenant, } from "./db/client.js";
|
|
5
5
|
import { resumeInterruptedRuns } from "./engine/runner.js";
|
|
6
6
|
import { basePath, env } from "./env.js";
|
|
7
|
+
import { hookConsole } from "./log.js";
|
|
7
8
|
import { managed } from "./manage.js";
|
|
8
9
|
import { checkModelsAtStart, loadCatalog, loadLocalModels } from "./models.js";
|
|
9
10
|
import { startJobs } from "./plugins/jobs.js";
|
|
@@ -16,6 +17,8 @@ import { purgeExpiredRuns } from "./services/shares.js";
|
|
|
16
17
|
import { purgeUnusedStores } from "./store/index.js";
|
|
17
18
|
import { LOCAL_TENANT } from "./tenants/tenant.js";
|
|
18
19
|
import { onRestart, RESTART_EXIT } from "./update.js";
|
|
20
|
+
// Before anything prints: the last lines stay in memory for a look from afar (debug.ts).
|
|
21
|
+
hookConsole();
|
|
19
22
|
await migrateControlDb();
|
|
20
23
|
// What the linked account's plan confirmed last: the Pro modules it includes load with the rest.
|
|
21
24
|
await loadEntitlement();
|