@bendyline/gezel-cli 1.0.5 → 1.0.7
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 +3 -3
- package/dist/app-author-command.js +464 -0
- package/dist/app-command.js +685 -0
- package/dist/bin/gezel-main.js +732 -25
- package/dist/handboek-export.js +729 -35
- package/dist/knowledge-command.js +351 -0
- package/dist/tui/index.js +692 -77
- package/package.json +10 -9
|
@@ -0,0 +1,685 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/app-command.ts
|
|
4
|
+
import { readFile as readFile2 } from "fs/promises";
|
|
5
|
+
import { isAbsolute, join as join2, resolve as resolve2 } from "path";
|
|
6
|
+
import { compareSemver } from "@bendyline/gezel";
|
|
7
|
+
|
|
8
|
+
// src/connection.ts
|
|
9
|
+
import { createHash, randomUUID } from "crypto";
|
|
10
|
+
import { chmod, mkdir, readFile, rm, writeFile } from "fs/promises";
|
|
11
|
+
import { homedir } from "os";
|
|
12
|
+
import { basename, dirname, join, resolve } from "path";
|
|
13
|
+
import { pickRandomNameWithGender } from "@bendyline/gezel";
|
|
14
|
+
import {
|
|
15
|
+
GezelSdkError,
|
|
16
|
+
authorizeLocal,
|
|
17
|
+
authorizeLocalOwner
|
|
18
|
+
} from "@bendyline/gezel-app-sdk";
|
|
19
|
+
import {
|
|
20
|
+
GezelApiError,
|
|
21
|
+
GezelClient,
|
|
22
|
+
createTrustingFetch,
|
|
23
|
+
discoverOrSpawn,
|
|
24
|
+
electronNativeBinCandidates,
|
|
25
|
+
isProcessAlive,
|
|
26
|
+
readRuntime,
|
|
27
|
+
readSystemServiceEndpoint,
|
|
28
|
+
resolveDaemonEntry,
|
|
29
|
+
stopOwnedDaemon,
|
|
30
|
+
systemSharedAssetsDir
|
|
31
|
+
} from "@bendyline/gezel-client/node";
|
|
32
|
+
import { gezelPaths } from "@bendyline/gezel/paths";
|
|
33
|
+
var CliError = class extends Error {
|
|
34
|
+
constructor(message) {
|
|
35
|
+
super(message);
|
|
36
|
+
this.name = "CliError";
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
async function ensureProjectForFolder(client, folderPath) {
|
|
40
|
+
const wd = resolve(folderPath);
|
|
41
|
+
const eq = (a, b) => !!a && (process.platform === "win32" ? a.toLowerCase() === b.toLowerCase() : a === b);
|
|
42
|
+
const { projects } = await client.listProjects();
|
|
43
|
+
const exact = projects.find((p) => eq(p.workingDir, wd));
|
|
44
|
+
if (exact) return exact.id;
|
|
45
|
+
const name = basename(wd) || "workspace";
|
|
46
|
+
const orphan = projects.find((p) => !p.workingDir && p.name === name);
|
|
47
|
+
if (orphan) {
|
|
48
|
+
await client.setProjectWorkingDir(orphan.id, wd);
|
|
49
|
+
return orphan.id;
|
|
50
|
+
}
|
|
51
|
+
const created = await client.createProject({
|
|
52
|
+
name,
|
|
53
|
+
description: `CLI workspace at ${wd}`,
|
|
54
|
+
about: `${name} \u2014 working directory ${wd}. Fill in who this project is for, what's in scope, and what's explicitly out of scope.`,
|
|
55
|
+
missionObjectives: `${name} \u2014 fill in concrete success criteria for this project.`,
|
|
56
|
+
mode: "solo",
|
|
57
|
+
workingDir: wd
|
|
58
|
+
});
|
|
59
|
+
return created.id;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// src/app-command.ts
|
|
63
|
+
function resolveInputPath(file) {
|
|
64
|
+
return isAbsolute(file) ? file : join2(process.cwd(), file);
|
|
65
|
+
}
|
|
66
|
+
function semverDelta(a, b) {
|
|
67
|
+
try {
|
|
68
|
+
return compareSemver(a, b);
|
|
69
|
+
} catch {
|
|
70
|
+
return a.localeCompare(b);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function apiMessage(err) {
|
|
74
|
+
if (err && typeof err === "object") {
|
|
75
|
+
const details = err.details;
|
|
76
|
+
if (details && typeof details === "object" && "error" in details) {
|
|
77
|
+
const message = details.error;
|
|
78
|
+
if (typeof message === "string" && message.length > 0) return message;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return err instanceof Error ? err.message : String(err);
|
|
82
|
+
}
|
|
83
|
+
async function confirm(prompt, refusal, yes) {
|
|
84
|
+
if (yes) return;
|
|
85
|
+
if (!process.stdin.isTTY) throw new CliError(refusal);
|
|
86
|
+
const { createInterface } = await import("readline/promises");
|
|
87
|
+
const rl = createInterface({ input: process.stdin, output: process.stderr });
|
|
88
|
+
try {
|
|
89
|
+
const answer = (await rl.question(prompt)).trim().toLowerCase();
|
|
90
|
+
if (answer !== "y" && answer !== "yes") throw new CliError("Cancelled.");
|
|
91
|
+
} finally {
|
|
92
|
+
rl.close();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function formatDependency(dependency) {
|
|
96
|
+
const need = dependency.required ? "required" : "optional";
|
|
97
|
+
return `${dependency.kind} ${dependency.id}@${dependency.version} (${need})`;
|
|
98
|
+
}
|
|
99
|
+
function printReview(result) {
|
|
100
|
+
const m = result.manifest;
|
|
101
|
+
const publisher = m.publisher.url ? `${m.publisher.name} <${m.publisher.url}>` : m.publisher.name;
|
|
102
|
+
console.log(`${m.name} ${m.entry.version} \u2014 ${publisher} (${m.signature.status})`);
|
|
103
|
+
if (m.description) console.log(` ${m.description}`);
|
|
104
|
+
if (result.previous) {
|
|
105
|
+
const delta = semverDelta(m.entry.version, result.previous.version);
|
|
106
|
+
if (delta > 0) console.log(` replaces installed ${result.previous.version}`);
|
|
107
|
+
else if (delta < 0) console.log(` DOWNGRADE from installed ${result.previous.version}`);
|
|
108
|
+
else console.log(` already installed at ${result.previous.version}`);
|
|
109
|
+
if (!result.previous.enabled) {
|
|
110
|
+
console.log(" note: currently disabled \u2014 installing re-enables it");
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
console.log(
|
|
114
|
+
` items: ${result.items.map((item) => `${item.kind} ${item.id}@${item.version}`).join(", ")}`
|
|
115
|
+
);
|
|
116
|
+
if (result.dependencies.length > 0) {
|
|
117
|
+
console.log(` dependency locks: ${result.dependencies.map(formatDependency).join(", ")}`);
|
|
118
|
+
}
|
|
119
|
+
const requiredMissing = result.missingDependencies.filter((dependency) => dependency.required);
|
|
120
|
+
if (result.missingDependencies.length > 0) {
|
|
121
|
+
console.log(
|
|
122
|
+
` missing dependencies: ${result.missingDependencies.map(formatDependency).join(", ")}`
|
|
123
|
+
);
|
|
124
|
+
} else if (result.dependencies.length > 0) {
|
|
125
|
+
console.log(" missing dependencies: none");
|
|
126
|
+
}
|
|
127
|
+
if (requiredMissing.length > 0) {
|
|
128
|
+
console.log(" WARNING: required dependencies are unavailable \u2014 install will be refused.");
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function printInstallOutcome(result) {
|
|
132
|
+
const installed = result.installed;
|
|
133
|
+
if (!installed) return;
|
|
134
|
+
if (installed.alreadyPresent) {
|
|
135
|
+
console.log(`${installed.appId}@${installed.version} is already installed \u2014 no changes.`);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const upgrade = result.previous ? ` (${result.previous.version} kept on disk for rollback)` : "";
|
|
139
|
+
console.log(`Installed ${installed.appId}@${installed.version}${upgrade}.`);
|
|
140
|
+
}
|
|
141
|
+
async function readGezappFile(file) {
|
|
142
|
+
const path = resolveInputPath(file);
|
|
143
|
+
try {
|
|
144
|
+
return await readFile2(path);
|
|
145
|
+
} catch {
|
|
146
|
+
throw new CliError(`cannot read .gezapp file: ${path}`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
async function previewImport(client, bytes) {
|
|
150
|
+
try {
|
|
151
|
+
return await client.importAiAppPackage(bytes);
|
|
152
|
+
} catch (err) {
|
|
153
|
+
throw new CliError(`.gezapp rejected: ${apiMessage(err)}`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async function confirmImport(client, bytes) {
|
|
157
|
+
try {
|
|
158
|
+
return await client.importAiAppPackage(bytes, { confirm: true });
|
|
159
|
+
} catch (err) {
|
|
160
|
+
throw new CliError(apiMessage(err));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
async function runAppAdd(client, file, opts) {
|
|
164
|
+
const bytes = await readGezappFile(file);
|
|
165
|
+
const preview = await previewImport(client, bytes);
|
|
166
|
+
if (!opts.json) printReview(preview);
|
|
167
|
+
if (preview.previous && semverDelta(preview.manifest.entry.version, preview.previous.version) < 0) {
|
|
168
|
+
if (!opts.force) {
|
|
169
|
+
throw new CliError(
|
|
170
|
+
`refusing to downgrade ${preview.manifest.entry.projectType} from ${preview.previous.version} to ${preview.manifest.entry.version} \u2014 pass --force to allow it`
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
if (!opts.json) console.log(" --force: downgrading anyway.");
|
|
174
|
+
}
|
|
175
|
+
await confirm(
|
|
176
|
+
"Install? [y/N] ",
|
|
177
|
+
"Refusing to install without confirmation. Pass --yes.",
|
|
178
|
+
opts.yes || opts.json
|
|
179
|
+
);
|
|
180
|
+
const result = await confirmImport(client, bytes);
|
|
181
|
+
if (opts.json) {
|
|
182
|
+
console.log(JSON.stringify(result, null, 2));
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
printInstallOutcome(result);
|
|
186
|
+
}
|
|
187
|
+
async function runAppUpdate(client, file, opts) {
|
|
188
|
+
const bytes = await readGezappFile(file);
|
|
189
|
+
const preview = await previewImport(client, bytes);
|
|
190
|
+
const appId = preview.manifest.entry.projectType;
|
|
191
|
+
if (!preview.previous) {
|
|
192
|
+
throw new CliError(`${appId} is not installed \u2014 use \`gezel app add\` first`);
|
|
193
|
+
}
|
|
194
|
+
const delta = semverDelta(preview.manifest.entry.version, preview.previous.version);
|
|
195
|
+
if (delta < 0 && !opts.force) {
|
|
196
|
+
throw new CliError(
|
|
197
|
+
`refusing to downgrade ${appId} from ${preview.previous.version} to ${preview.manifest.entry.version} \u2014 pass --force to allow it`
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
if (!opts.json) printReview(preview);
|
|
201
|
+
const result = await confirmImport(client, bytes);
|
|
202
|
+
if (opts.json) {
|
|
203
|
+
console.log(JSON.stringify(result, null, 2));
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (result.installed?.alreadyPresent && delta === 0) {
|
|
207
|
+
console.log(`${appId} is already up to date (${preview.manifest.entry.version}).`);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
printInstallOutcome(result);
|
|
211
|
+
}
|
|
212
|
+
async function runAppList(client, opts) {
|
|
213
|
+
const { apps } = await client.listAiApps();
|
|
214
|
+
if (opts.json) {
|
|
215
|
+
console.log(JSON.stringify(apps, null, 2));
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (apps.length === 0) {
|
|
219
|
+
console.log("No AI Apps installed. Add one with: gezel app add <file.gezapp>");
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
console.log(`${"APP".padEnd(28)} ${"VERSION".padEnd(10)} ${"INSTALLED".padEnd(12)} STATE`);
|
|
223
|
+
for (const app of apps) {
|
|
224
|
+
const installed = app.installedAt.slice(0, 10);
|
|
225
|
+
const state = app.enabled ? "enabled" : "disabled";
|
|
226
|
+
const name = app.name && app.name !== app.appId ? ` ${app.name}` : "";
|
|
227
|
+
console.log(
|
|
228
|
+
`${app.appId.padEnd(28)} ${app.version.padEnd(10)} ${installed.padEnd(12)} ${state}${name}`
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
function printDetail(detail) {
|
|
233
|
+
console.log(`${detail.name ?? detail.appId} ${detail.version}`);
|
|
234
|
+
if (detail.description) console.log(` ${detail.description}`);
|
|
235
|
+
if (detail.publisher) {
|
|
236
|
+
const publisher = detail.publisher.url ? `${detail.publisher.name} <${detail.publisher.url}>` : detail.publisher.name;
|
|
237
|
+
console.log(` publisher: ${publisher}`);
|
|
238
|
+
}
|
|
239
|
+
console.log(` app id: ${detail.appId}`);
|
|
240
|
+
console.log(` state: ${detail.enabled ? "enabled" : "disabled"}`);
|
|
241
|
+
console.log(` installed: ${detail.installedAt}`);
|
|
242
|
+
console.log(` package sha256: ${detail.packageSha256.slice(0, 16)}\u2026`);
|
|
243
|
+
console.log(` items: ${detail.itemCount}, dependency locks: ${detail.dependencyCount}`);
|
|
244
|
+
if (detail.missingDependencies.length > 0) {
|
|
245
|
+
console.log(
|
|
246
|
+
` missing dependencies: ${detail.missingDependencies.map(formatDependency).join(", ")}`
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
if (detail.versionsOnDisk.length > 0) {
|
|
250
|
+
console.log(` versions on disk: ${detail.versionsOnDisk.join(", ")}`);
|
|
251
|
+
}
|
|
252
|
+
if (detail.appliedProjects.length > 0) {
|
|
253
|
+
console.log(" applied to:");
|
|
254
|
+
for (const project of detail.appliedProjects) {
|
|
255
|
+
const lag = project.version && semverDelta(detail.version, project.version) > 0 ? ` (outfitted with ${project.version} \u2014 update available)` : "";
|
|
256
|
+
console.log(` ${project.name} [${project.id}]${lag}`);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
if (!detail.manifest) {
|
|
260
|
+
console.log(" WARNING: install receipt is unreadable \u2014 reinstall to repair.");
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
async function runAppShow(client, appId, opts) {
|
|
264
|
+
let detail;
|
|
265
|
+
try {
|
|
266
|
+
detail = await client.getAiApp(appId);
|
|
267
|
+
} catch (err) {
|
|
268
|
+
throw new CliError(apiMessage(err));
|
|
269
|
+
}
|
|
270
|
+
if (opts.json) {
|
|
271
|
+
console.log(JSON.stringify(detail, null, 2));
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
printDetail(detail);
|
|
275
|
+
}
|
|
276
|
+
async function runAppRemove(client, appId, opts) {
|
|
277
|
+
const { apps } = await client.listAiApps();
|
|
278
|
+
const current = apps.find((app) => app.appId === appId);
|
|
279
|
+
if (!current) throw new CliError(`AI App not found: ${appId}`);
|
|
280
|
+
await confirm(
|
|
281
|
+
`Remove ${current.name ?? appId} ${current.version}${opts.keepFiles ? " (keeping files)" : ""}? [y/N] `,
|
|
282
|
+
"Refusing to remove without confirmation. Pass --yes.",
|
|
283
|
+
opts.yes
|
|
284
|
+
);
|
|
285
|
+
let removed;
|
|
286
|
+
try {
|
|
287
|
+
removed = await client.removeAiApp(appId, { keepFiles: opts.keepFiles ?? false });
|
|
288
|
+
} catch (err) {
|
|
289
|
+
throw new CliError(apiMessage(err));
|
|
290
|
+
}
|
|
291
|
+
const versions = removed.removedVersions.length > 0 ? `removed ${removed.removedVersions.join(", ")}` : "no version dirs removed";
|
|
292
|
+
const kept = removed.keptVersions.length > 0 ? `; kept on disk: ${removed.keptVersions.join(", ")}` : "";
|
|
293
|
+
console.log(`Uninstalled ${appId} (${versions}${kept}).`);
|
|
294
|
+
if (removed.appliedProjects.length > 0) {
|
|
295
|
+
console.log(
|
|
296
|
+
`${removed.appliedProjects.length} project(s) were outfitted by this app \u2014 they keep their copied gezels, scripts, and files, but the app's pages and updates are gone.`
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
async function runAppSetEnabled(client, appId, enabled) {
|
|
301
|
+
try {
|
|
302
|
+
const { entry } = await client.setAiAppEnabled(appId, enabled);
|
|
303
|
+
console.log(`${enabled ? "Enabled" : "Disabled"} ${entry.appId}@${entry.version}.`);
|
|
304
|
+
} catch (err) {
|
|
305
|
+
throw new CliError(apiMessage(err));
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
function findProjectForFolder(projects, folder) {
|
|
309
|
+
const wd = resolve2(folder);
|
|
310
|
+
return projects.find(
|
|
311
|
+
(project) => !!project.workingDir && (process.platform === "win32" ? project.workingDir.toLowerCase() === wd.toLowerCase() : project.workingDir === wd)
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
function parseParamFlags(flags) {
|
|
315
|
+
const params = {};
|
|
316
|
+
for (const flag of flags) {
|
|
317
|
+
const eq = flag.indexOf("=");
|
|
318
|
+
if (eq <= 0) throw new CliError(`invalid --param "${flag}" \u2014 expected key=value`);
|
|
319
|
+
params[flag.slice(0, eq)] = flag.slice(eq + 1);
|
|
320
|
+
}
|
|
321
|
+
return params;
|
|
322
|
+
}
|
|
323
|
+
function printPlan(plan) {
|
|
324
|
+
const roles = plan.gezels.map(
|
|
325
|
+
(gezel) => `${gezel.templateId}${gezel.voorman ? " (voorman" : " ("}${gezel.reuse ? ", reused)" : gezel.voorman ? ", new)" : "new)"}`
|
|
326
|
+
);
|
|
327
|
+
if (roles.length > 0) console.log(` roles: ${roles.join(", ")}`);
|
|
328
|
+
if (plan.scripts.length > 0) console.log(` scripts: ${plan.scripts.join(", ")}`);
|
|
329
|
+
if (plan.seeds.length > 0) {
|
|
330
|
+
const seeds = plan.seeds.map(
|
|
331
|
+
(seed) => seed.state === "keep-modified" ? `${seed.path} (kept \u2014 you modified it)` : `${seed.path} (${seed.state})`
|
|
332
|
+
);
|
|
333
|
+
console.log(` seeds: ${seeds.join(", ")}`);
|
|
334
|
+
}
|
|
335
|
+
if (plan.toolsets.installNow.length > 0) {
|
|
336
|
+
console.log(` toolsets: ${plan.toolsets.installNow.join(", ")}`);
|
|
337
|
+
}
|
|
338
|
+
if (plan.toolsets.deferred.length > 0) {
|
|
339
|
+
console.log(
|
|
340
|
+
` deferred: ${plan.toolsets.deferred.join(", ")} (install explicitly from the catalog)`
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
if (plan.craftbooks.length > 0) console.log(` craftbooks: ${plan.craftbooks.join(", ")}`);
|
|
344
|
+
for (const schedule of plan.schedules) {
|
|
345
|
+
const cadence = schedule.cron ? `cron ${schedule.cron}` : "night shift";
|
|
346
|
+
const consent = schedule.consent === "auto" ? "armed on apply" : "created paused; enable when asked";
|
|
347
|
+
console.log(` schedule: ${schedule.craftbook} (${cadence} \u2014 ${consent})`);
|
|
348
|
+
}
|
|
349
|
+
if (plan.pages) console.log(" pages: Output page included");
|
|
350
|
+
}
|
|
351
|
+
function printApplied(applied, projectName) {
|
|
352
|
+
const parts = [];
|
|
353
|
+
if (applied.gezelsCreated.length > 0) {
|
|
354
|
+
parts.push(`${applied.gezelsCreated.length} gezel(s)`);
|
|
355
|
+
}
|
|
356
|
+
if (applied.scriptsInstalled.length > 0) {
|
|
357
|
+
parts.push(`${applied.scriptsInstalled.length} script(s)`);
|
|
358
|
+
}
|
|
359
|
+
if (applied.workspaceSeeded.length > 0) {
|
|
360
|
+
parts.push(`${applied.workspaceSeeded.length} seeded file(s)`);
|
|
361
|
+
}
|
|
362
|
+
if (applied.toolsetsInstalled.length > 0) {
|
|
363
|
+
parts.push(`${applied.toolsetsInstalled.length} toolset(s)`);
|
|
364
|
+
}
|
|
365
|
+
if (applied.craftbooksInstalled.length > 0) {
|
|
366
|
+
parts.push(`${applied.craftbooksInstalled.length} craftbook(s)`);
|
|
367
|
+
}
|
|
368
|
+
if (applied.schedulesCreated.length > 0) {
|
|
369
|
+
parts.push(`${applied.schedulesCreated.length} schedule(s)`);
|
|
370
|
+
}
|
|
371
|
+
console.log(
|
|
372
|
+
`Applied ${applied.typeId}@${applied.version} to "${projectName}"${parts.length > 0 ? `: ${parts.join(", ")}` : ""}.`
|
|
373
|
+
);
|
|
374
|
+
if (applied.seedsSkipped.length > 0) {
|
|
375
|
+
console.log(`Kept your modified file(s): ${applied.seedsSkipped.join(", ")}.`);
|
|
376
|
+
}
|
|
377
|
+
if (applied.deferred.toolsets.length > 0) {
|
|
378
|
+
console.log(
|
|
379
|
+
`Deferred toolset(s): ${applied.deferred.toolsets.join(", ")} \u2014 install them from the catalog when needed.`
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
if (applied.schedulesCreated.some((schedule) => schedule.status === "paused")) {
|
|
383
|
+
console.log("Schedules were created paused \u2014 enable them when Gezel asks.");
|
|
384
|
+
}
|
|
385
|
+
console.log("Run `gezel` in this folder to start.");
|
|
386
|
+
}
|
|
387
|
+
async function runAppApply(client, appId, folder, opts) {
|
|
388
|
+
const { apps } = await client.listAiApps();
|
|
389
|
+
const app = apps.find((candidate) => candidate.appId === appId);
|
|
390
|
+
if (!app) {
|
|
391
|
+
throw new CliError(`AI App not installed: ${appId} \u2014 install it with \`gezel app add\``);
|
|
392
|
+
}
|
|
393
|
+
if (!app.enabled) {
|
|
394
|
+
throw new CliError(
|
|
395
|
+
`AI App ${appId} is disabled \u2014 enable it with \`gezel app enable ${appId}\``
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
const params = parseParamFlags(opts.param ?? []);
|
|
399
|
+
const { projects } = await client.listProjects();
|
|
400
|
+
const existing = findProjectForFolder(projects, folder);
|
|
401
|
+
const projectId = existing?.id ?? await ensureProjectForFolder(client, folder);
|
|
402
|
+
const project = await client.getProject(projectId);
|
|
403
|
+
if (!opts.json) {
|
|
404
|
+
console.log(
|
|
405
|
+
`Folder: ${resolve2(folder)} \u2192 project "${project.name}"${existing ? "" : " (new)"}`
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
const provenance = project.projectType;
|
|
409
|
+
if (provenance && provenance.id !== appId && !opts.force) {
|
|
410
|
+
throw new CliError(
|
|
411
|
+
`this folder's project already has "${provenance.id}" applied \u2014 pass --force to apply ${appId} over it`
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
const targetVersion = opts.version ?? app.version;
|
|
415
|
+
if (provenance?.id === appId && provenance.version === targetVersion && !opts.refresh) {
|
|
416
|
+
console.log(`${appId}@${targetVersion} is already applied here \u2014 pass --refresh to re-apply.`);
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
const body = {
|
|
420
|
+
typeId: appId,
|
|
421
|
+
...opts.version ? { version: opts.version } : {},
|
|
422
|
+
...Object.keys(params).length > 0 ? { params } : {},
|
|
423
|
+
seedPolicy: "preserve",
|
|
424
|
+
reuseRosterGezels: true
|
|
425
|
+
};
|
|
426
|
+
let plan;
|
|
427
|
+
try {
|
|
428
|
+
plan = await client.preflightProjectType(projectId, body);
|
|
429
|
+
} catch (err) {
|
|
430
|
+
throw new CliError(`cannot apply ${appId}: ${apiMessage(err)}`);
|
|
431
|
+
}
|
|
432
|
+
if (!opts.json) {
|
|
433
|
+
console.log(`Applying ${plan.typeId} ${plan.version}:`);
|
|
434
|
+
printPlan(plan);
|
|
435
|
+
}
|
|
436
|
+
await confirm(
|
|
437
|
+
"Apply? [y/N] ",
|
|
438
|
+
"Refusing to apply without confirmation. Pass --yes.",
|
|
439
|
+
opts.yes || opts.json
|
|
440
|
+
);
|
|
441
|
+
let applied;
|
|
442
|
+
try {
|
|
443
|
+
applied = await client.applyProjectType(projectId, body);
|
|
444
|
+
} catch (err) {
|
|
445
|
+
throw new CliError(apiMessage(err));
|
|
446
|
+
}
|
|
447
|
+
if (opts.json) {
|
|
448
|
+
console.log(JSON.stringify(applied, null, 2));
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
printApplied(applied, project.name);
|
|
452
|
+
}
|
|
453
|
+
async function runAppStatus(client, folder, opts) {
|
|
454
|
+
const { projects } = await client.listProjects();
|
|
455
|
+
const project = findProjectForFolder(projects, folder);
|
|
456
|
+
if (!project) {
|
|
457
|
+
console.log(`No gezel project is linked to ${resolve2(folder)}.`);
|
|
458
|
+
console.log("Apply an app here with: gezel app apply <appId>");
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
const status = await client.projectTypeStatus(project.id);
|
|
462
|
+
if (opts.json) {
|
|
463
|
+
console.log(JSON.stringify(status, null, 2));
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
console.log(`Folder: ${resolve2(folder)} \u2192 project "${project.name}"`);
|
|
467
|
+
if (!status.provenance) {
|
|
468
|
+
console.log("No AI App is applied to this project.");
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
const applied = `${status.provenance.id} ${status.provenance.version} (applied ${status.provenance.appliedAt.slice(0, 10)})`;
|
|
472
|
+
if (status.installedApp) {
|
|
473
|
+
const state = status.installedApp.enabled ? "enabled" : "disabled";
|
|
474
|
+
console.log(`App: ${applied} \u2014 installed ${status.installedApp.version}, ${state}`);
|
|
475
|
+
if (status.updateAvailable) {
|
|
476
|
+
console.log(`Update available: gezel app apply ${status.provenance.id}`);
|
|
477
|
+
}
|
|
478
|
+
} else {
|
|
479
|
+
console.log(`App: ${applied} \u2014 no longer installed (project keeps its copies)`);
|
|
480
|
+
}
|
|
481
|
+
if (status.seeds.length > 0) {
|
|
482
|
+
const counts = /* @__PURE__ */ new Map();
|
|
483
|
+
for (const seed of status.seeds) counts.set(seed.state, (counts.get(seed.state) ?? 0) + 1);
|
|
484
|
+
const summary = ["ok", "modified", "missing", "untracked"].filter((state) => counts.has(state)).map((state) => `${counts.get(state)} ${state}`).join(", ");
|
|
485
|
+
console.log(`Seeds: ${summary}`);
|
|
486
|
+
for (const seed of status.seeds) {
|
|
487
|
+
if (seed.state !== "ok") console.log(` ${seed.path} \u2014 ${seed.state}`);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
async function openUrlInBrowser(url) {
|
|
492
|
+
const { spawn } = await import("child_process");
|
|
493
|
+
try {
|
|
494
|
+
if (process.platform === "win32") {
|
|
495
|
+
spawn("cmd", ["/c", "start", "", url], {
|
|
496
|
+
detached: true,
|
|
497
|
+
stdio: "ignore",
|
|
498
|
+
windowsHide: true
|
|
499
|
+
}).unref();
|
|
500
|
+
} else if (process.platform === "darwin") {
|
|
501
|
+
spawn("open", [url], { detached: true, stdio: "ignore" }).unref();
|
|
502
|
+
} else {
|
|
503
|
+
spawn("xdg-open", [url], { detached: true, stdio: "ignore" }).unref();
|
|
504
|
+
}
|
|
505
|
+
} catch {
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
async function resolveServeProject(client, appId, folder) {
|
|
509
|
+
const { projects } = await client.listProjects();
|
|
510
|
+
if (!appId) {
|
|
511
|
+
const existing = findProjectForFolder(projects, folder);
|
|
512
|
+
const projectId = existing?.id ?? await ensureProjectForFolder(client, folder);
|
|
513
|
+
const project = await client.getProject(projectId);
|
|
514
|
+
if (!project.projectType) {
|
|
515
|
+
throw new CliError(
|
|
516
|
+
`no AI App is applied to ${resolve2(folder)} \u2014 run \`gezel app apply <appId>\` first`
|
|
517
|
+
);
|
|
518
|
+
}
|
|
519
|
+
return projectId;
|
|
520
|
+
}
|
|
521
|
+
const matches = projects.filter((project) => project.projectType?.id === appId);
|
|
522
|
+
if (matches.length === 0) {
|
|
523
|
+
throw new CliError(`no project has ${appId} applied \u2014 run \`gezel app apply ${appId}\` first`);
|
|
524
|
+
}
|
|
525
|
+
if (matches.length > 1) {
|
|
526
|
+
const names = matches.map((project) => `"${project.name}"`).join(", ");
|
|
527
|
+
throw new CliError(
|
|
528
|
+
`${appId} is applied to several projects (${names}) \u2014 run from the folder you want, or pass --project <folder>`
|
|
529
|
+
);
|
|
530
|
+
}
|
|
531
|
+
const only = matches[0];
|
|
532
|
+
if (!only) throw new CliError(`no project has ${appId} applied`);
|
|
533
|
+
return only.id;
|
|
534
|
+
}
|
|
535
|
+
async function runAppServeStart(client, appId, folder, opts) {
|
|
536
|
+
const projectId = await resolveServeProject(client, appId, folder);
|
|
537
|
+
let started;
|
|
538
|
+
try {
|
|
539
|
+
started = await client.startAppServe({
|
|
540
|
+
projectId,
|
|
541
|
+
...opts.port !== void 0 ? { port: opts.port } : {},
|
|
542
|
+
...opts.host ? { host: opts.host } : {},
|
|
543
|
+
...opts.allowHost.length > 0 ? { allowedHosts: opts.allowHost } : {},
|
|
544
|
+
...opts.chat ? { chat: true } : {},
|
|
545
|
+
...opts.public ? { public: true } : {},
|
|
546
|
+
...opts.key ? { siteKey: opts.key } : {}
|
|
547
|
+
});
|
|
548
|
+
} catch (err) {
|
|
549
|
+
throw new CliError(apiMessage(err));
|
|
550
|
+
}
|
|
551
|
+
if (opts.json) {
|
|
552
|
+
console.log(JSON.stringify(started, null, 2));
|
|
553
|
+
if (opts.detach) return;
|
|
554
|
+
} else {
|
|
555
|
+
console.log("");
|
|
556
|
+
console.log(` Serving "${started.typeName}" (${started.typeId}@${started.typeVersion})`);
|
|
557
|
+
console.log(` project: ${started.projectName} - site: ${started.siteId}`);
|
|
558
|
+
console.log("");
|
|
559
|
+
console.log(` Share link: ${started.shareUrl}`);
|
|
560
|
+
if (!started.public) {
|
|
561
|
+
console.log(" (the link carries the site key - anyone with it can visit)");
|
|
562
|
+
}
|
|
563
|
+
console.log("");
|
|
564
|
+
if (started.chat) {
|
|
565
|
+
console.log(
|
|
566
|
+
" Chat: on - visitors get their own conversation with the project lead (no tools)."
|
|
567
|
+
);
|
|
568
|
+
}
|
|
569
|
+
console.log(" To reach the internet, put a tunnel in front, e.g.:");
|
|
570
|
+
console.log(` ngrok http ${started.port}`);
|
|
571
|
+
console.log(` cloudflared tunnel --url http://127.0.0.1:${started.port}`);
|
|
572
|
+
console.log(" (for a proxy hostname, restart with --allow-host your.domain)");
|
|
573
|
+
console.log("");
|
|
574
|
+
}
|
|
575
|
+
if (opts.open) await openUrlInBrowser(started.shareUrl);
|
|
576
|
+
if (opts.detach) {
|
|
577
|
+
console.log("Serving continues in the daemon - `gezel app serve stop` to end it.");
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
await new Promise((resolvePromise) => {
|
|
581
|
+
let stopping = false;
|
|
582
|
+
const clearPoll = () => {
|
|
583
|
+
clearInterval(timer);
|
|
584
|
+
};
|
|
585
|
+
const finish = async () => {
|
|
586
|
+
if (stopping) return;
|
|
587
|
+
stopping = true;
|
|
588
|
+
clearPoll();
|
|
589
|
+
process.off("SIGINT", onSignal);
|
|
590
|
+
process.off("SIGTERM", onSignal);
|
|
591
|
+
const visitors = await client.getAppServeSite(started.siteId).then((site) => site.visitors).catch(() => 0);
|
|
592
|
+
await client.stopAppServeSite(started.siteId).catch(() => {
|
|
593
|
+
});
|
|
594
|
+
if (process.stderr.isTTY) process.stderr.write("\n");
|
|
595
|
+
console.log(
|
|
596
|
+
`Stopped.${visitors > 0 ? ` ${visitors} visitor(s) were connected; their chat sessions were archived.` : ""}`
|
|
597
|
+
);
|
|
598
|
+
resolvePromise();
|
|
599
|
+
};
|
|
600
|
+
const onSignal = () => {
|
|
601
|
+
void finish();
|
|
602
|
+
};
|
|
603
|
+
process.on("SIGINT", onSignal);
|
|
604
|
+
process.on("SIGTERM", onSignal);
|
|
605
|
+
const poll = async () => {
|
|
606
|
+
const site = await client.getAppServeSite(started.siteId).catch(() => null);
|
|
607
|
+
if (!site) {
|
|
608
|
+
if (!stopping && process.stderr.isTTY) process.stderr.write("\n");
|
|
609
|
+
if (!stopping) console.log("The site was stopped from elsewhere.");
|
|
610
|
+
stopping = true;
|
|
611
|
+
clearPoll();
|
|
612
|
+
process.off("SIGINT", onSignal);
|
|
613
|
+
process.off("SIGTERM", onSignal);
|
|
614
|
+
resolvePromise();
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
if (process.stderr.isTTY) {
|
|
618
|
+
process.stderr.write(
|
|
619
|
+
`\r visitors ${site.visitors} - views ${site.counters.pageViews} - invokes ${site.counters.invokes} - chat ${site.counters.chatMessages} Ctrl+C stops serving `
|
|
620
|
+
);
|
|
621
|
+
}
|
|
622
|
+
};
|
|
623
|
+
const timer = setInterval(() => void poll(), 5e3);
|
|
624
|
+
void poll();
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
async function runAppServeStatus(client, opts) {
|
|
628
|
+
const { sites } = await client.listAppServeSites();
|
|
629
|
+
if (opts.json) {
|
|
630
|
+
console.log(JSON.stringify(sites, null, 2));
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
if (sites.length === 0) {
|
|
634
|
+
console.log("No app sites are being served. Start one with: gezel app serve");
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
637
|
+
for (const site of sites) {
|
|
638
|
+
const chat = site.chat ? ", chat on" : "";
|
|
639
|
+
console.log(
|
|
640
|
+
`${site.siteId} ${site.typeId}@${site.typeVersion} ${site.url} (project "${site.projectName}", ${site.visitors} visitor(s)${chat})`
|
|
641
|
+
);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
async function runAppServeStop(client, target, opts) {
|
|
645
|
+
const { sites } = await client.listAppServeSites();
|
|
646
|
+
if (sites.length === 0) {
|
|
647
|
+
console.log("No app sites are being served.");
|
|
648
|
+
return;
|
|
649
|
+
}
|
|
650
|
+
let victims = sites;
|
|
651
|
+
if (!opts.all) {
|
|
652
|
+
if (target) {
|
|
653
|
+
victims = sites.filter((site) => site.siteId === target || site.typeId === target);
|
|
654
|
+
if (victims.length === 0) throw new CliError(`no served site matches "${target}"`);
|
|
655
|
+
} else if (sites.length > 1) {
|
|
656
|
+
throw new CliError(
|
|
657
|
+
`several sites are serving (${sites.map((site) => site.siteId).join(", ")}) \u2014 name one or pass --all`
|
|
658
|
+
);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
for (const site of victims) {
|
|
662
|
+
await client.stopAppServeSite(site.siteId).catch((err) => {
|
|
663
|
+
throw new CliError(apiMessage(err));
|
|
664
|
+
});
|
|
665
|
+
console.log(`Stopped ${site.siteId} (${site.typeId}).`);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
async function runAppServe(client, args, folder, opts) {
|
|
669
|
+
const [first, second] = args;
|
|
670
|
+
if (first === "status") return runAppServeStatus(client, opts);
|
|
671
|
+
if (first === "stop") return runAppServeStop(client, second, opts);
|
|
672
|
+
return runAppServeStart(client, first, folder, opts);
|
|
673
|
+
}
|
|
674
|
+
export {
|
|
675
|
+
parseParamFlags,
|
|
676
|
+
runAppAdd,
|
|
677
|
+
runAppApply,
|
|
678
|
+
runAppList,
|
|
679
|
+
runAppRemove,
|
|
680
|
+
runAppServe,
|
|
681
|
+
runAppSetEnabled,
|
|
682
|
+
runAppShow,
|
|
683
|
+
runAppStatus,
|
|
684
|
+
runAppUpdate
|
|
685
|
+
};
|