@acrylblends/blends-frame 0.1.0 → 0.1.2
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/lib/app-plugins.d.ts +32 -5
- package/lib/app-reload.d.ts +5 -4
- package/lib/applied-live.d.ts +43 -0
- package/lib/client.js +31886 -367
- package/lib/client.js.map +1 -1
- package/lib/contract.d.ts +116 -0
- package/lib/github-connect.d.ts +44 -0
- package/lib/graph-contract.d.ts +85 -0
- package/lib/graph-inspect.d.ts +16 -0
- package/lib/index.d.ts +20 -3
- package/lib/index.js +1188 -51
- package/lib/index.js.map +1 -1
- package/lib/plain.d.ts +26 -0
- package/lib/plugin-graph.d.ts +38 -0
- package/lib/registry.d.ts +71 -0
- package/lib/types/client/AppSection.d.ts +10 -0
- package/lib/types/client/BlendsFrame.d.ts +7 -1
- package/lib/types/client/GraphView.d.ts +22 -0
- package/lib/types/client/NameYourApp.d.ts +10 -0
- package/lib/types/client/PluginsSection.d.ts +11 -1
- package/lib/types/client/RegistryView.d.ts +11 -0
- package/lib/types/client/SettingsExpand.d.ts +6 -0
- package/lib/types/client/ShareChooser.d.ts +21 -0
- package/lib/types/client/desktop-app.d.ts +65 -0
- package/lib/types/client/graph-api.d.ts +11 -0
- package/lib/types/client/graph-canvas.d.ts +63 -0
- package/lib/types/client/graph-link.d.ts +21 -0
- package/lib/types/client/graph-model.d.ts +23 -0
- package/lib/types/client/graph-styles.d.ts +3 -0
- package/lib/types/client/index.d.ts +1 -1
- package/lib/types/client/plain-failure.d.ts +10 -0
- package/lib/types/client/plugins-navigator.d.ts +37 -7
- package/lib/types/client/registry-api.d.ts +21 -0
- package/lib/types/client/registry-list.d.ts +14 -0
- package/lib/types/client/settings-fullscreen.d.ts +13 -0
- package/lib/types/client/theme-switch.d.ts +27 -0
- package/lib/types/contract.d.ts +116 -0
- package/lib/types/graph-contract.d.ts +85 -0
- package/lib/types/graph-inspect.d.ts +16 -0
- package/package.json +13 -9
package/lib/index.js
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import { appGeneration } from "./app-generation.js";
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
|
-
import { execFile, execFileSync } from "node:child_process";
|
|
4
|
-
import { cpSync, existsSync, mkdirSync, realpathSync, rmSync, watch, writeFileSync } from "node:fs";
|
|
3
|
+
import { execFile, execFileSync, spawn, spawnSync } from "node:child_process";
|
|
4
|
+
import { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmSync, statSync, watch, writeFileSync } from "node:fs";
|
|
5
5
|
import { mkdir, readFile, readdir, rename, writeFile } from "node:fs/promises";
|
|
6
|
-
import { basename, dirname, isAbsolute, join, resolve } from "node:path";
|
|
6
|
+
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
7
7
|
import { Acryl } from "acrylblends";
|
|
8
8
|
import { error } from "@acrylblends/loopback-http";
|
|
9
9
|
import { INVALID_JSON, jsonBody, ownPage, privateApi, readJson } from "@acrylblends/loopback-http/router";
|
|
10
|
+
import { PluginStore, StoreConflictError, bumpVersion, gitCli, githubCli, parseVersion, savePlugin, sharePlugin, storeRootOf } from "@webboxes/plugin-hub";
|
|
10
11
|
import { GitRunner, Versions } from "@acrylblends/versions";
|
|
11
12
|
import * as workspaceHost from "@acrylblends/workspace";
|
|
12
13
|
import { promisify } from "node:util";
|
|
13
|
-
import { PluginStore, bumpVersion, gitCli, githubCli, parseVersion, sharePlugin, storeRootOf } from "@webboxes/plugin-hub";
|
|
14
14
|
import { pathToFileURL } from "node:url";
|
|
15
|
+
import { digestFolder } from "@webboxes/blends-core";
|
|
16
|
+
import { parseDocument } from "yaml";
|
|
15
17
|
import { boundContextSummary, createUserMessage } from "@deepseek-ai/dsh-llm";
|
|
16
18
|
import { randomBytes } from "node:crypto";
|
|
17
19
|
//#region src/contract.ts
|
|
@@ -41,7 +43,7 @@ const FRAME_GIT_PULL_PATH = `${FRAME_API_PREFIX}/git/pull`;
|
|
|
41
43
|
const FRAME_SNAPSHOT_CAPTURE_PATH = `${FRAME_API_PREFIX}/snapshot/capture`;
|
|
42
44
|
/** @throws when the value is not a push plan. */
|
|
43
45
|
function parsePushPlan(value) {
|
|
44
|
-
if (!isRecord$
|
|
46
|
+
if (!isRecord$2(value)) throw new Error("push plan: not a plan");
|
|
45
47
|
const { branch, remote, target, address, versions, createsBranch, head } = value;
|
|
46
48
|
if (!isName(branch) || !isName(remote) || !isName(target) || !isName(address)) throw new Error("push plan: names");
|
|
47
49
|
if (!isCount(versions) || typeof createsBranch !== "boolean" || typeof head !== "string" || !/^[0-9a-f]{40,64}$/u.test(head)) throw new Error("push plan: versions");
|
|
@@ -71,12 +73,12 @@ function branchNameProblem(name) {
|
|
|
71
73
|
if (!/^[A-Za-z0-9][A-Za-z0-9._/-]*$/u.test(name)) return "Use letters, digits, - _ . and /, starting with a letter or digit.";
|
|
72
74
|
if (name.includes("..") || name.includes("//") || name.endsWith("/") || name.endsWith(".") || name.endsWith(".lock")) return "That is not a name git accepts.";
|
|
73
75
|
}
|
|
74
|
-
function isRecord$
|
|
76
|
+
function isRecord$2(value) {
|
|
75
77
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
76
78
|
}
|
|
77
79
|
/** @throws an Error naming the field when the answer is not a frame state. */
|
|
78
80
|
function parseFrameState(value) {
|
|
79
|
-
if (!isRecord$
|
|
81
|
+
if (!isRecord$2(value)) throw new Error("frame state: not an object");
|
|
80
82
|
const { appName, appRoot, appPage, branches, head, unsaved, upstreamHead } = value;
|
|
81
83
|
if (typeof appName !== "string" || appName === "") throw new Error("frame state: appName");
|
|
82
84
|
if (typeof appRoot !== "string" || appRoot === "") throw new Error("frame state: appRoot");
|
|
@@ -86,7 +88,7 @@ function parseFrameState(value) {
|
|
|
86
88
|
if (unsaved !== void 0 && (typeof unsaved !== "number" || !Number.isInteger(unsaved) || unsaved < 0)) throw new Error("frame state: unsaved");
|
|
87
89
|
if (upstreamHead !== void 0 && (typeof upstreamHead !== "string" || upstreamHead === "")) throw new Error("frame state: upstreamHead");
|
|
88
90
|
const parsed = branches.map((branch) => {
|
|
89
|
-
if (!isRecord$
|
|
91
|
+
if (!isRecord$2(branch) || typeof branch.name !== "string" || typeof branch.current !== "boolean") throw new Error("frame state: branch");
|
|
90
92
|
return {
|
|
91
93
|
name: branch.name,
|
|
92
94
|
current: branch.current
|
|
@@ -108,26 +110,41 @@ const FRAME_PLUGINS_SWITCH_PATH = `${FRAME_API_PREFIX}/plugins/switch`;
|
|
|
108
110
|
const FRAME_PLUGINS_SHARE_PLAN_PATH = `${FRAME_API_PREFIX}/plugins/share-plan`;
|
|
109
111
|
/** POST `{ name, confirm }`: publish it, only when `confirm` is the plan the person said yes to and it still holds. */
|
|
110
112
|
const FRAME_PLUGINS_SHARE_PATH = `${FRAME_API_PREFIX}/plugins/share`;
|
|
113
|
+
/** POST `{ name }`: keep a packed copy of this local plugin on this machine (the app's plugin store); nothing leaves the machine. */
|
|
114
|
+
const FRAME_PLUGINS_SAVE_PATH = `${FRAME_API_PREFIX}/plugins/save`;
|
|
115
|
+
/** GET: the public plugin registry (cordisplugins.github.io), each entry marked when this app already has it; read fresh by the Host, never by the page. */
|
|
116
|
+
const FRAME_PLUGINS_REGISTRY_PATH = `${FRAME_API_PREFIX}/plugins/registry`;
|
|
117
|
+
/** GET: whether this computer can share to GitHub (the helper program is installed and signed in), read before the person is asked anything. */
|
|
118
|
+
const FRAME_PLUGINS_GITHUB_PATH = `${FRAME_API_PREFIX}/plugins/github`;
|
|
119
|
+
/** POST: start GitHub's browser sign-in (a one-time code for github.com/login/device); GET: where it is; DELETE: stop one still waiting. */
|
|
120
|
+
const FRAME_PLUGINS_GITHUB_CONNECT_PATH = `${FRAME_API_PREFIX}/plugins/github/connect`;
|
|
121
|
+
/** POST `{ source }`: pull one released plugin (`github:<owner>/<repo>@<version>`) into the running app, through the app's plugin-share checks. */
|
|
122
|
+
const FRAME_PLUGINS_PULL_PATH = `${FRAME_API_PREFIX}/plugins/pull`;
|
|
111
123
|
const GITHUB_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]*$/u;
|
|
112
124
|
/** @throws when the value is not a share plan. */
|
|
113
125
|
function parseSharePlan(value) {
|
|
114
|
-
if (!isRecord$
|
|
115
|
-
const { owner, repo, version, visibility, createsRepository, released } = value;
|
|
126
|
+
if (!isRecord$2(value)) throw new Error("share plan: not a plan");
|
|
127
|
+
const { owner, repo, version, visibility, createsRepository, released, license, registry } = value;
|
|
116
128
|
if (typeof owner !== "string" || !GITHUB_NAME.test(owner) || typeof repo !== "string" || !GITHUB_NAME.test(repo)) throw new Error("share plan: repository");
|
|
117
129
|
if (typeof version !== "string" || version === "" || released !== void 0 && (typeof released !== "string" || released === "")) throw new Error("share plan: version");
|
|
118
130
|
if (visibility !== "private" && visibility !== "public" || typeof createsRepository !== "boolean") throw new Error("share plan: visibility");
|
|
131
|
+
if (license !== void 0 && (typeof license !== "string" || !/^[A-Za-z0-9.+-]+$/u.test(license))) throw new Error("share plan: license");
|
|
132
|
+
if (registry !== void 0 && typeof registry !== "boolean") throw new Error("share plan: registry");
|
|
133
|
+
if (registry === true && visibility !== "public") throw new Error("share plan: only a public share can be listed in the registry");
|
|
119
134
|
return {
|
|
120
135
|
owner,
|
|
121
136
|
repo,
|
|
122
137
|
version,
|
|
123
138
|
visibility,
|
|
124
139
|
createsRepository,
|
|
125
|
-
...released === void 0 ? {} : { released }
|
|
140
|
+
...released === void 0 ? {} : { released },
|
|
141
|
+
...license === void 0 ? {} : { license },
|
|
142
|
+
...registry === true ? { registry } : {}
|
|
126
143
|
};
|
|
127
144
|
}
|
|
128
145
|
/** The same plan: what the person confirmed is what would be published now. */
|
|
129
146
|
function samePlan(a, b) {
|
|
130
|
-
return a.owner === b.owner && a.repo === b.repo && a.version === b.version && a.visibility === b.visibility && a.createsRepository === b.createsRepository && a.released === b.released;
|
|
147
|
+
return a.owner === b.owner && a.repo === b.repo && a.version === b.version && a.visibility === b.visibility && a.createsRepository === b.createsRepository && a.released === b.released && a.license === b.license && a.registry === true === (b.registry === true);
|
|
131
148
|
}
|
|
132
149
|
const FRAME_FEEDBACK_PATH = `${FRAME_API_PREFIX}/feedback`;
|
|
133
150
|
const FRAME_FEEDBACK_SEEN_PATH = `${FRAME_API_PREFIX}/feedback/seen`;
|
|
@@ -137,15 +154,15 @@ const FRAME_FEEDBACK_SHOT_PATH = `${FRAME_API_PREFIX}/feedback/shot`;
|
|
|
137
154
|
function parseFeedbackNotes(value) {
|
|
138
155
|
if (!Array.isArray(value)) throw new Error("feedback: not a list");
|
|
139
156
|
return value.map((entry) => {
|
|
140
|
-
if (!isRecord$
|
|
157
|
+
if (!isRecord$2(entry) || typeof entry.id !== "string" || typeof entry.at !== "string" || typeof entry.text !== "string" || typeof entry.page !== "string" || typeof entry.seen !== "boolean") throw new Error("feedback: note");
|
|
141
158
|
const element = entry.element;
|
|
142
|
-
const pinned = isRecord$
|
|
159
|
+
const pinned = isRecord$2(element) && typeof element.selector === "string" && typeof element.label === "string" ? { element: {
|
|
143
160
|
selector: element.selector,
|
|
144
161
|
label: element.label
|
|
145
162
|
} } : {};
|
|
146
163
|
const shot = entry.screenshot;
|
|
147
164
|
let screenshot = {};
|
|
148
|
-
if (isRecord$
|
|
165
|
+
if (isRecord$2(shot) && (shot["mediaType"] === "image/webp" || shot["mediaType"] === "image/png") && typeof shot["width"] === "number" && typeof shot["height"] === "number") screenshot = { screenshot: {
|
|
149
166
|
mediaType: shot["mediaType"],
|
|
150
167
|
width: shot["width"],
|
|
151
168
|
height: shot["height"]
|
|
@@ -359,11 +376,68 @@ async function checkedShares(records, check) {
|
|
|
359
376
|
const entries = await Promise.all(Object.entries(records).map(async ([name, shared]) => [name, await check(shared.url) === false ? {
|
|
360
377
|
url: shared.url,
|
|
361
378
|
version: shared.version,
|
|
362
|
-
gone: true
|
|
379
|
+
gone: true,
|
|
380
|
+
...shared.registry === true ? { registry: true } : {}
|
|
363
381
|
} : shared]));
|
|
364
382
|
return Object.fromEntries(entries);
|
|
365
383
|
}
|
|
366
|
-
|
|
384
|
+
/** A path inside `root` as a relative path with forward slashes, or undefined when it is outside. */
|
|
385
|
+
function insideApp(root, file) {
|
|
386
|
+
const rel = relative(root, file);
|
|
387
|
+
if (rel === "" || rel.startsWith("..") || isAbsolute(rel)) return void 0;
|
|
388
|
+
return rel.split("\\").join("/");
|
|
389
|
+
}
|
|
390
|
+
/** The files a plugin's code starts in, first match wins: the entry beside its package.json, then `src/`, then what package.json names, then the README. */
|
|
391
|
+
function mainFileOf(folder) {
|
|
392
|
+
const entries = [...ENTRIES, ...ENTRIES.map((file) => join("src", file))];
|
|
393
|
+
const named = [];
|
|
394
|
+
try {
|
|
395
|
+
const manifest = JSON.parse(readFileSync(join(folder, "package.json"), "utf8"));
|
|
396
|
+
if (typeof manifest.main === "string") named.push(manifest.main);
|
|
397
|
+
const dot = typeof manifest.exports === "object" && manifest.exports !== null ? manifest.exports["."] : manifest.exports;
|
|
398
|
+
if (typeof dot === "string") named.push(dot);
|
|
399
|
+
} catch {}
|
|
400
|
+
return [
|
|
401
|
+
...entries,
|
|
402
|
+
...named,
|
|
403
|
+
"README.md"
|
|
404
|
+
].find((file) => existsSync(join(folder, file)));
|
|
405
|
+
}
|
|
406
|
+
/** The plugin's own folder in `plugins/` with the file its code starts in, else undefined. */
|
|
407
|
+
function ownLocation(root, name) {
|
|
408
|
+
const folder = join(root, "plugins", name);
|
|
409
|
+
if (!existsSync(folder)) return void 0;
|
|
410
|
+
const entry = mainFileOf(folder);
|
|
411
|
+
return {
|
|
412
|
+
folder,
|
|
413
|
+
...entry === void 0 ? {} : { main: `plugins/${name}/${entry.replace(/^\.\//u, "").split("\\").join("/")}` }
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
/** Where a plugin of `blend.yaml` or the frame has its code: a folder of the app's `plugins/`, else the package the app installed (read-only). */
|
|
417
|
+
async function pluginLocation(root, name) {
|
|
418
|
+
const own = ownLocation(root, name);
|
|
419
|
+
if (own !== void 0) return own;
|
|
420
|
+
const found = await modulePackage(root, name);
|
|
421
|
+
if (found?.dir === void 0) return void 0;
|
|
422
|
+
const main = found.main === void 0 ? void 0 : insideApp(root, found.main);
|
|
423
|
+
return {
|
|
424
|
+
folder: found.dir,
|
|
425
|
+
...main === void 0 ? {} : { main }
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
/** The newest packed copy of each plugin kept in the app's plugin store, by name. An unreadable store lists nothing. */
|
|
429
|
+
function savedCopies(storeRoot) {
|
|
430
|
+
const copies = /* @__PURE__ */ new Map();
|
|
431
|
+
if (storeRoot === void 0) return copies;
|
|
432
|
+
try {
|
|
433
|
+
for (const stored of new PluginStore(storeRoot).list()) if (!copies.has(stored.name)) copies.set(stored.name, {
|
|
434
|
+
version: stored.version,
|
|
435
|
+
path: stored.path
|
|
436
|
+
});
|
|
437
|
+
} catch {}
|
|
438
|
+
return copies;
|
|
439
|
+
}
|
|
440
|
+
async function readAppPlugins(root, git, sharedFile, check, storeRoot) {
|
|
367
441
|
const text = await readFile(join(root, "blend.yaml"), "utf8");
|
|
368
442
|
const off = new Set(extensionsOff(text));
|
|
369
443
|
const versions = await libraryVersions(root);
|
|
@@ -371,9 +445,12 @@ async function readAppPlugins(root, git, sharedFile, check) {
|
|
|
371
445
|
const shareRecords = check === void 0 ? recorded : await checkedShares(recorded, check);
|
|
372
446
|
const blendRows = parseBlendRows(text);
|
|
373
447
|
const own = await madePlugins(root);
|
|
448
|
+
const copies = savedCopies(storeRoot);
|
|
374
449
|
const made = await Promise.all(own.map(async (name) => {
|
|
375
450
|
const [description, first] = await Promise.all([packageDescription(join(root, "plugins", name)), firstCommit(git, root, `plugins/${name}`)]);
|
|
376
451
|
const shared = shareRecords[name];
|
|
452
|
+
const saved = copies.get(name);
|
|
453
|
+
const where = ownLocation(root, name);
|
|
377
454
|
const row = blendRows.find((entry) => entry.name === name);
|
|
378
455
|
return {
|
|
379
456
|
name,
|
|
@@ -383,6 +460,11 @@ async function readAppPlugins(root, git, sharedFile, check) {
|
|
|
383
460
|
at: first.at
|
|
384
461
|
},
|
|
385
462
|
...shared === void 0 ? {} : { shared },
|
|
463
|
+
...saved === void 0 ? {} : { saved },
|
|
464
|
+
...where === void 0 ? {} : {
|
|
465
|
+
folder: where.folder,
|
|
466
|
+
...where.main === void 0 ? {} : { main: where.main }
|
|
467
|
+
},
|
|
386
468
|
description,
|
|
387
469
|
on: row === void 0 ? !off.has(name) : !row.disabled
|
|
388
470
|
};
|
|
@@ -390,10 +472,15 @@ async function readAppPlugins(root, git, sharedFile, check) {
|
|
|
390
472
|
const rows = await Promise.all(blendRows.filter((row) => !own.includes(row.name)).map(async (row) => {
|
|
391
473
|
const description = row.comment !== "" ? firstSentence(row.comment) : await packageDescription(join(root, "plugins", row.name));
|
|
392
474
|
const version = versions.get(row.name);
|
|
475
|
+
const where = await pluginLocation(root, row.name);
|
|
393
476
|
return {
|
|
394
477
|
name: row.name,
|
|
395
478
|
kind: "row",
|
|
396
479
|
...version === void 0 ? {} : { version },
|
|
480
|
+
...where === void 0 ? {} : {
|
|
481
|
+
folder: where.folder,
|
|
482
|
+
...where.main === void 0 ? {} : { main: where.main }
|
|
483
|
+
},
|
|
397
484
|
description,
|
|
398
485
|
on: !row.disabled
|
|
399
486
|
};
|
|
@@ -442,6 +529,8 @@ async function modulePackage(root, name) {
|
|
|
442
529
|
const parsed = JSON.parse(await readFile(join(dir, "package.json"), "utf8"));
|
|
443
530
|
if (parsed.name !== name) continue;
|
|
444
531
|
return {
|
|
532
|
+
dir,
|
|
533
|
+
main: file,
|
|
445
534
|
...typeof parsed.version === "string" ? { version: parsed.version } : {},
|
|
446
535
|
...typeof parsed.description === "string" ? { description: parsed.description } : {}
|
|
447
536
|
};
|
|
@@ -451,22 +540,26 @@ async function modulePackage(root, name) {
|
|
|
451
540
|
async function readFramePlugins(root, rows) {
|
|
452
541
|
return Promise.all(rows.filter((row) => isFramePlugin(row.name, row.id)).map(async (row) => {
|
|
453
542
|
const manifest = await modulePackage(root, row.name);
|
|
543
|
+
const main = manifest?.main === void 0 ? void 0 : insideApp(root, manifest.main);
|
|
454
544
|
return {
|
|
455
545
|
id: row.id,
|
|
456
546
|
name: row.name,
|
|
457
547
|
...manifest?.version === void 0 ? {} : { version: manifest.version },
|
|
458
548
|
description: manifest?.description ?? "",
|
|
459
|
-
state: frameRowState(row)
|
|
549
|
+
state: frameRowState(row),
|
|
550
|
+
...manifest?.dir === void 0 ? {} : { folder: manifest.dir },
|
|
551
|
+
...main === void 0 ? {} : { main }
|
|
460
552
|
};
|
|
461
553
|
}));
|
|
462
554
|
}
|
|
463
555
|
/** The package.json a local plugin is shared with: the fields the builder's package_plugin writes (plugins/builder/index.ts), plus the Loader
|
|
464
556
|
* patch the plugin-hub's share requires so the package can also be installed as a row. */
|
|
465
|
-
function pluginPackageManifest(name, description, version = "0.1.0") {
|
|
557
|
+
function pluginPackageManifest(name, description, version = "0.1.0", license) {
|
|
466
558
|
return {
|
|
467
559
|
name,
|
|
468
560
|
version,
|
|
469
561
|
description,
|
|
562
|
+
...license === void 0 ? {} : { license },
|
|
470
563
|
type: "module",
|
|
471
564
|
exports: {
|
|
472
565
|
".": "./index.ts",
|
|
@@ -500,10 +593,11 @@ async function readSharedRecords(file) {
|
|
|
500
593
|
const records = {};
|
|
501
594
|
for (const [name, value] of Object.entries(parsed)) {
|
|
502
595
|
if (value === null || typeof value !== "object") continue;
|
|
503
|
-
const { url, version } = value;
|
|
596
|
+
const { url, version, registry } = value;
|
|
504
597
|
if (typeof url === "string" && typeof version === "string") records[name] = {
|
|
505
598
|
url,
|
|
506
|
-
version
|
|
599
|
+
version,
|
|
600
|
+
...registry === true ? { registry: true } : {}
|
|
507
601
|
};
|
|
508
602
|
}
|
|
509
603
|
return records;
|
|
@@ -523,7 +617,7 @@ async function recordShare(file, name, shared) {
|
|
|
523
617
|
/** Where the frame packages the plugins it shares: under the app's home, never in the app's folder, so a share leaves no file to save. */
|
|
524
618
|
const pluginPackagesOf = (home) => join(home, "plugin-packages");
|
|
525
619
|
/** Copy one local plugin into its package folder under `packages`, the way the builder's package_plugin does; answers the folder. */
|
|
526
|
-
async function packageAppPlugin(root, name, packages) {
|
|
620
|
+
async function packageAppPlugin(root, name, packages, license) {
|
|
527
621
|
if (!(await madePlugins(root)).includes(name)) throw new Error(`plugins/ has no local plugin "${name}"`);
|
|
528
622
|
const source = join(root, "plugins", name);
|
|
529
623
|
const target = join(packages, name);
|
|
@@ -542,7 +636,7 @@ async function packageAppPlugin(root, name, packages) {
|
|
|
542
636
|
filter: (path) => !/(^|[\\/])(node_modules|\.git)([\\/]|$)/u.test(path.slice(source.length))
|
|
543
637
|
});
|
|
544
638
|
const description = await packageDescription(source);
|
|
545
|
-
writeFileSync(join(target, "package.json"), `${JSON.stringify(pluginPackageManifest(name, description, version), null, 2)}\n`);
|
|
639
|
+
writeFileSync(join(target, "package.json"), `${JSON.stringify(pluginPackageManifest(name, description, version, license), null, 2)}\n`);
|
|
546
640
|
writeFileSync(join(target, "cordis.patch.yml"), `- insert:\n - id: ${name}\n name: ./index.ts\n`);
|
|
547
641
|
if (!existsSync(join(target, "README.md"))) writeFileSync(join(target, "README.md"), `# ${name}\n\n${description}\n\nA Cordis plugin. In an ACRYLBLENDS app: \`acrylblends add <this folder>\`.\n`);
|
|
548
642
|
return target;
|
|
@@ -557,23 +651,67 @@ async function bumpPackageVersion(dir) {
|
|
|
557
651
|
await writeFile(join(dir, "package.json"), `${JSON.stringify(manifest, null, 2)}\n`);
|
|
558
652
|
return next;
|
|
559
653
|
}
|
|
654
|
+
/**
|
|
655
|
+
* Keep a packed copy of one local plugin on this machine: the plugin is packaged under the app's home as a share does, then put in the app's plugin
|
|
656
|
+
* store with its checksum. A version is stored once: when this version is already saved with other content, the version is raised (patch) and saved
|
|
657
|
+
* again, so a changed plugin never overwrites an earlier copy. Nothing leaves the machine.
|
|
658
|
+
*/
|
|
659
|
+
async function saveAppPlugin(root, home, name) {
|
|
660
|
+
const dir = await packageAppPlugin(root, name, pluginPackagesOf(home));
|
|
661
|
+
const store = new PluginStore(storeRootOf(home));
|
|
662
|
+
try {
|
|
663
|
+
const { stored, added } = savePlugin(dir, store);
|
|
664
|
+
return {
|
|
665
|
+
version: stored.version,
|
|
666
|
+
path: stored.path,
|
|
667
|
+
added,
|
|
668
|
+
bumped: false
|
|
669
|
+
};
|
|
670
|
+
} catch (cause) {
|
|
671
|
+
if (!(cause instanceof StoreConflictError)) throw cause;
|
|
672
|
+
}
|
|
673
|
+
await bumpPackageVersion(dir);
|
|
674
|
+
const { stored, added } = savePlugin(dir, store);
|
|
675
|
+
return {
|
|
676
|
+
version: stored.version,
|
|
677
|
+
path: stored.path,
|
|
678
|
+
added,
|
|
679
|
+
bumped: true
|
|
680
|
+
};
|
|
681
|
+
}
|
|
560
682
|
const githubSharePorts = () => ({
|
|
561
683
|
host: githubCli(),
|
|
562
684
|
git: gitCli()
|
|
563
685
|
});
|
|
564
686
|
/**
|
|
687
|
+
* The hub labels every release with the topics catalogs find a plugin by, and `cordis-plugin` is the one the registry's sync lists a public repository
|
|
688
|
+
* by. A share that was not asked to go to the registry must not put the repository there, so its host takes no topics; a registry share passes them on.
|
|
689
|
+
*/
|
|
690
|
+
function listedOnlyWhenAsked(host, listed) {
|
|
691
|
+
return {
|
|
692
|
+
...host,
|
|
693
|
+
addTopics: (owner, repo, topics) => {
|
|
694
|
+
if (listed) host.addTopics(owner, repo, topics);
|
|
695
|
+
}
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
565
699
|
* What sharing one local plugin would do, read without sending anything: it packages the plugin under the app's home and asks the host who is
|
|
566
700
|
* signed in, whether the repository exists and whether this version is released there (then the plan releases the next patch).
|
|
567
|
-
* @param options - the repository owner (the signed-in account when absent)
|
|
701
|
+
* @param options - the repository owner (the signed-in account when absent), the visibility the person asked for (private when absent: a share is
|
|
702
|
+
* public only when the person chose it, because anyone can then read and pull it), the license of a public share (MIT when absent; the plan names it)
|
|
703
|
+
* and whether the public registry lists it (`registry`: the share sets the `cordis-plugin` topic, which the registry's hourly sync finds).
|
|
568
704
|
*/
|
|
569
705
|
async function planAppPluginShare(root, home, name, options, ports) {
|
|
570
|
-
const
|
|
706
|
+
const registry = options.registry === true;
|
|
707
|
+
const visibility = registry ? "public" : options.visibility ?? "private";
|
|
708
|
+
const license = visibility === "public" ? options.license ?? "MIT" : void 0;
|
|
709
|
+
const dir = await packageAppPlugin(root, name, pluginPackagesOf(home), license);
|
|
571
710
|
const manifest = JSON.parse(await readFile(join(dir, "package.json"), "utf8"));
|
|
572
711
|
const current = typeof manifest.version === "string" && parseVersion(manifest.version) !== void 0 ? manifest.version : void 0;
|
|
573
712
|
if (current === void 0) throw new Error(`${name}: its package has no semantic version`);
|
|
574
713
|
const owner = options.owner ?? ports.host.login();
|
|
575
714
|
const repo = name;
|
|
576
|
-
const visibility = manifest.acryl?.visibility === "public" ? "public" : "private";
|
|
577
715
|
const existing = ports.host.repository(owner, repo);
|
|
578
716
|
const released = existing !== void 0 && ports.host.releaseExists(owner, repo, `v${current}`);
|
|
579
717
|
return {
|
|
@@ -582,7 +720,9 @@ async function planAppPluginShare(root, home, name, options, ports) {
|
|
|
582
720
|
version: released ? bumpVersion(current, "patch") : current,
|
|
583
721
|
visibility,
|
|
584
722
|
createsRepository: existing === void 0,
|
|
585
|
-
...released ? { released: current } : {}
|
|
723
|
+
...released ? { released: current } : {},
|
|
724
|
+
...license === void 0 ? {} : { license },
|
|
725
|
+
...registry ? { registry } : {}
|
|
586
726
|
};
|
|
587
727
|
}
|
|
588
728
|
/**
|
|
@@ -594,7 +734,12 @@ async function planAppPluginShare(root, home, name, options, ports) {
|
|
|
594
734
|
* @param options - the repository owner the frame is configured with (the signed-in account when absent).
|
|
595
735
|
*/
|
|
596
736
|
async function shareAppPlugin(root, home, name, confirmed, options, ports) {
|
|
597
|
-
const plan = await planAppPluginShare(root, home, name,
|
|
737
|
+
const plan = await planAppPluginShare(root, home, name, {
|
|
738
|
+
...options,
|
|
739
|
+
visibility: confirmed.visibility,
|
|
740
|
+
...confirmed.license === void 0 ? {} : { license: confirmed.license },
|
|
741
|
+
...confirmed.registry === true ? { registry: true } : {}
|
|
742
|
+
}, ports);
|
|
598
743
|
if (!samePlan(plan, confirmed)) throw new Error(`The share changed since you confirmed it (${plan.owner}/${plan.repo} v${plan.version}); nothing was sent. Look at it again.`);
|
|
599
744
|
const dir = join(pluginPackagesOf(home), name);
|
|
600
745
|
if (plan.released !== void 0 && await bumpPackageVersion(dir) !== plan.version) throw new Error(`${name}: the next version is not v${plan.version}; nothing was sent`);
|
|
@@ -604,19 +749,22 @@ async function shareAppPlugin(root, home, name, confirmed, options, ports) {
|
|
|
604
749
|
visibility: plan.visibility
|
|
605
750
|
}, {
|
|
606
751
|
...ports,
|
|
752
|
+
host: listedOnlyWhenAsked(ports.host, plan.registry === true),
|
|
607
753
|
store: new PluginStore(storeRootOf(home))
|
|
608
754
|
});
|
|
609
755
|
if (result.status === "refused") throw new Error(result.reason);
|
|
610
756
|
await recordShare(join(home, "shared-plugins.json"), name, {
|
|
611
757
|
url: result.url,
|
|
612
|
-
version: result.version
|
|
758
|
+
version: result.version,
|
|
759
|
+
...plan.registry === true ? { registry: true } : {}
|
|
613
760
|
});
|
|
614
761
|
return {
|
|
615
762
|
url: result.url,
|
|
616
763
|
version: result.version,
|
|
617
764
|
owner: result.owner,
|
|
618
765
|
repo: result.repo,
|
|
619
|
-
tag: result.tag
|
|
766
|
+
tag: result.tag,
|
|
767
|
+
...plan.registry === true ? { registry: true } : {}
|
|
620
768
|
};
|
|
621
769
|
}
|
|
622
770
|
//#endregion
|
|
@@ -665,6 +813,581 @@ function appFallbackTarget(appBase, url, headers) {
|
|
|
665
813
|
return fromAppPage(appBase, url, headers) ? appUrlUnderBase(appBase, url) : void 0;
|
|
666
814
|
}
|
|
667
815
|
//#endregion
|
|
816
|
+
//#region src/graph-contract.ts
|
|
817
|
+
/**
|
|
818
|
+
* The plugin graph's wire types: what the frame's Host answers and what the browser draws (Settings > Plugins > Graph). Ported from cordis-plugin-graph
|
|
819
|
+
* (MIT, github.com/cordisplugins/cordis-plugin-graph, commit e9bf6b5, `src/inspect.ts`), with two additions the original did not have: a node says where its
|
|
820
|
+
* code is (`source`), and the Host keeps a numbered list of fiber lifecycle events (`GraphEvent`) so the browser hears a change instead of asking again.
|
|
821
|
+
* No Node or browser import: both sides read this file.
|
|
822
|
+
*/
|
|
823
|
+
/** The whole graph of the Host's Cordis context, now. */
|
|
824
|
+
const FRAME_GRAPH_PATH = `${FRAME_API_PREFIX}/graph`;
|
|
825
|
+
/** The fiber events after `?after=<seq>`: answered at once when there are some, else when the next one happens (or after about 20 s with none). */
|
|
826
|
+
const FRAME_GRAPH_EVENTS_PATH = `${FRAME_API_PREFIX}/graph/events`;
|
|
827
|
+
//#endregion
|
|
828
|
+
//#region src/plain.ts
|
|
829
|
+
/** A failure whose message is already for the person; a route answers it as it is. */
|
|
830
|
+
var PlainError = class extends Error {
|
|
831
|
+
plain;
|
|
832
|
+
constructor(plain) {
|
|
833
|
+
super(plain.say);
|
|
834
|
+
this.plain = plain;
|
|
835
|
+
this.name = "PlainError";
|
|
836
|
+
}
|
|
837
|
+
};
|
|
838
|
+
const KEEP_HERE = "Keeping the plugin on this computer still works.";
|
|
839
|
+
const GITHUB_MISSING = {
|
|
840
|
+
status: "missing",
|
|
841
|
+
say: `Sharing needs a free helper program from GitHub, and it is not installed on this computer. Install it from cli.github.com, then sign in once. ${KEEP_HERE}`,
|
|
842
|
+
link: "https://cli.github.com"
|
|
843
|
+
};
|
|
844
|
+
const GITHUB_SIGNED_OUT = {
|
|
845
|
+
status: "signed-out",
|
|
846
|
+
say: `To share, connect your GitHub account once. ${KEEP_HERE}`,
|
|
847
|
+
command: "gh auth login"
|
|
848
|
+
};
|
|
849
|
+
/** What a failed tool call says about the person's setup: the helper is not installed, or it is not signed in. */
|
|
850
|
+
function githubProblemOf(message) {
|
|
851
|
+
if (/is not available here/iu.test(message)) return "missing";
|
|
852
|
+
if (/auth login|not logged in|credential|HTTP 401|authentication/iu.test(message)) return "signed-out";
|
|
853
|
+
}
|
|
854
|
+
/** Read whether sharing to GitHub can work: ask who is signed in. `login` is the host's own check (it throws a tool message when it cannot). */
|
|
855
|
+
function githubReadiness(login) {
|
|
856
|
+
try {
|
|
857
|
+
const account = login().trim();
|
|
858
|
+
return account === "" ? GITHUB_SIGNED_OUT : {
|
|
859
|
+
status: "ready",
|
|
860
|
+
account
|
|
861
|
+
};
|
|
862
|
+
} catch (cause) {
|
|
863
|
+
const problem = githubProblemOf(cause instanceof Error ? cause.message : String(cause));
|
|
864
|
+
if (problem === "missing") return GITHUB_MISSING;
|
|
865
|
+
if (problem === "signed-out") return GITHUB_SIGNED_OUT;
|
|
866
|
+
return {
|
|
867
|
+
status: "unavailable",
|
|
868
|
+
say: "GitHub did not answer. Check your internet connection and try again."
|
|
869
|
+
};
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
/** A share that could not be planned or sent, in words a person can act on. */
|
|
873
|
+
function plainShareProblem(cause) {
|
|
874
|
+
const message = cause instanceof Error ? cause.message : String(cause);
|
|
875
|
+
const setup = githubProblemOf(message);
|
|
876
|
+
if (setup === "missing") return /\bgit is not available/iu.test(message) ? {
|
|
877
|
+
say: `Sharing needs a free program called git, and it is not installed on this computer. Install it from git-scm.com and try again. ${KEEP_HERE}`,
|
|
878
|
+
details: message
|
|
879
|
+
} : {
|
|
880
|
+
say: GITHUB_MISSING.status === "missing" ? GITHUB_MISSING.say : "",
|
|
881
|
+
details: message
|
|
882
|
+
};
|
|
883
|
+
if (setup === "signed-out") return {
|
|
884
|
+
say: "GitHub is not connected. Press Connect GitHub, then try again. Nothing was sent.",
|
|
885
|
+
details: message
|
|
886
|
+
};
|
|
887
|
+
if (/changed since you confirmed/iu.test(message)) return {
|
|
888
|
+
say: "Something changed while you were deciding, so nothing was sent. Please try again.",
|
|
889
|
+
details: message
|
|
890
|
+
};
|
|
891
|
+
if (/secret|password|token|private key|credential/iu.test(message)) return {
|
|
892
|
+
say: "This plugin contains something that looks like a password or a key, so it was not shared. Remove it from the plugin and try again.",
|
|
893
|
+
details: message
|
|
894
|
+
};
|
|
895
|
+
if (/already (released|exists)|released once/iu.test(message)) return {
|
|
896
|
+
say: "This version is already shared. Change the plugin, then share it again to send the next version.",
|
|
897
|
+
details: message
|
|
898
|
+
};
|
|
899
|
+
if (/could not resolve host|network|timed out|ENOTFOUND|ECONN/iu.test(message)) return {
|
|
900
|
+
say: "GitHub could not be reached. Check your internet connection and try again. Nothing was sent.",
|
|
901
|
+
details: message
|
|
902
|
+
};
|
|
903
|
+
return {
|
|
904
|
+
say: "The plugin could not be shared. Nothing was sent.",
|
|
905
|
+
details: message
|
|
906
|
+
};
|
|
907
|
+
}
|
|
908
|
+
/** Why a plugin could not be added to the app, by the kind of refusal the app's pull gives (`plugins/plugin-share/pull-check.ts`). */
|
|
909
|
+
function plainPullRefusal(refusal, reason, next) {
|
|
910
|
+
const details = [reason, next].filter((part) => part !== "").join(" - ");
|
|
911
|
+
return {
|
|
912
|
+
say: (() => {
|
|
913
|
+
switch (refusal) {
|
|
914
|
+
case "source": return "That address is not one this app understands.";
|
|
915
|
+
case "fetch": return "The plugin could not be downloaded. Check your internet connection and that it is still public, then try again.";
|
|
916
|
+
case "name": return "This plugin's name does not fit in this app, so it was not added.";
|
|
917
|
+
case "digest": return "The downloaded file is not the one its author published, so nothing was added.";
|
|
918
|
+
case "portability": return "This plugin only works on some kinds of computer, so it was not added.";
|
|
919
|
+
case "dependencies": return "This plugin needs extra software installed first, so it was not added.";
|
|
920
|
+
case "entry": return "This plugin has no starting file this app can use, so it was not added.";
|
|
921
|
+
case "inject":
|
|
922
|
+
case "missing-service": return "This plugin needs parts that this app does not have, so it cannot work here. Nothing was added.";
|
|
923
|
+
case "folder-exists": return "You already have a different plugin with this name. Remove it first if you want this one. Nothing was changed.";
|
|
924
|
+
case "install": return "The plugin did not start, so nothing was changed.";
|
|
925
|
+
default: return "The plugin could not be added. Nothing was changed.";
|
|
926
|
+
}
|
|
927
|
+
})(),
|
|
928
|
+
...details === "" ? {} : { details }
|
|
929
|
+
};
|
|
930
|
+
}
|
|
931
|
+
//#endregion
|
|
932
|
+
//#region src/graph-inspect.ts
|
|
933
|
+
const MAX_FIBERS = 4096;
|
|
934
|
+
const MAX_LABEL = 400;
|
|
935
|
+
const PHASE = {
|
|
936
|
+
0: "pending",
|
|
937
|
+
1: "loading",
|
|
938
|
+
2: "active",
|
|
939
|
+
3: "failed",
|
|
940
|
+
4: "disposed",
|
|
941
|
+
5: "unloading"
|
|
942
|
+
};
|
|
943
|
+
const clamp = (value) => value.length > MAX_LABEL ? `${value.slice(0, MAX_LABEL)}…` : value;
|
|
944
|
+
const phaseOf = (fiber) => PHASE[fiber.state] ?? "pending";
|
|
945
|
+
/** Every live fiber reachable from the context root, root first, uid-ascending. */
|
|
946
|
+
function collectFibers(ctx) {
|
|
947
|
+
const found = /* @__PURE__ */ new Set([ctx.root.fiber]);
|
|
948
|
+
for (const runtime of ctx.root.registry.values()) for (const fiber of runtime.fibers) if (fiber.uid !== null) found.add(fiber);
|
|
949
|
+
const result = [...found].sort((a, b) => (a.uid ?? 0) - (b.uid ?? 0));
|
|
950
|
+
if (result.length > MAX_FIBERS) throw new Error("the context has more fibers than the graph shows");
|
|
951
|
+
return result;
|
|
952
|
+
}
|
|
953
|
+
/** Live service implementations in the root reflect store, keyed off `impl.fiber.uid`, so a wrapped fiber never mis-attributes a provider. */
|
|
954
|
+
function liveServices(ctx) {
|
|
955
|
+
const store = ctx.root.reflect.store;
|
|
956
|
+
return Reflect.ownKeys(store).map((key) => store[key]).filter((impl) => impl != null && typeof impl.name === "string" && impl.fiber != null && impl.fiber.uid != null);
|
|
957
|
+
}
|
|
958
|
+
/** The current Cordis graph of `ctx`: every fiber, the disabled Loader rows, and the three kinds of relation. */
|
|
959
|
+
function inspectCordisContext(ctx, plane, sourceOf = () => void 0) {
|
|
960
|
+
const fibers = collectFibers(ctx);
|
|
961
|
+
const providesByUid = /* @__PURE__ */ new Map();
|
|
962
|
+
for (const impl of liveServices(ctx)) {
|
|
963
|
+
const uid = impl.fiber.uid ?? 0;
|
|
964
|
+
const list = providesByUid.get(uid) ?? [];
|
|
965
|
+
list.push(impl.name);
|
|
966
|
+
providesByUid.set(uid, list);
|
|
967
|
+
}
|
|
968
|
+
const loader = ctx.get("loader");
|
|
969
|
+
const uidByEntryId = /* @__PURE__ */ new Map();
|
|
970
|
+
const nodes = [];
|
|
971
|
+
const edges = [];
|
|
972
|
+
const known = /* @__PURE__ */ new Set();
|
|
973
|
+
for (const fiber of fibers) {
|
|
974
|
+
const uid = fiber.uid ?? 0;
|
|
975
|
+
known.add(uid);
|
|
976
|
+
const parent = fiber.parent.fiber;
|
|
977
|
+
const parentUid = parent === fiber ? null : parent.uid ?? null;
|
|
978
|
+
const ownsEntry = fiber.entry !== void 0 && (parent === fiber || parent.entry !== fiber.entry);
|
|
979
|
+
const entryId = ownsEntry ? loader?.locate?.(fiber) ?? null : null;
|
|
980
|
+
if (entryId !== null) uidByEntryId.set(entryId, uid);
|
|
981
|
+
const moduleName = ownsEntry ? fiber.entry?.options.name ?? null : null;
|
|
982
|
+
const source = uid === 0 ? void 0 : sourceOf(moduleName, fiber.name);
|
|
983
|
+
nodes.push({
|
|
984
|
+
uid,
|
|
985
|
+
name: clamp(fiber.name),
|
|
986
|
+
phase: phaseOf(fiber),
|
|
987
|
+
enabled: true,
|
|
988
|
+
moduleName,
|
|
989
|
+
loaderEntryId: entryId,
|
|
990
|
+
provides: [...providesByUid.get(uid) ?? []].sort(),
|
|
991
|
+
effects: fiber.getEffects().map((meta) => clamp(meta.label)),
|
|
992
|
+
...source === void 0 ? {} : { source }
|
|
993
|
+
});
|
|
994
|
+
if (parentUid !== null && known.has(parentUid)) edges.push({
|
|
995
|
+
kind: "nesting",
|
|
996
|
+
from: uid,
|
|
997
|
+
to: parentUid
|
|
998
|
+
});
|
|
999
|
+
for (const service of Object.keys(fiber.inject).sort()) {
|
|
1000
|
+
const providerUid = (fiber.store?.[service])?.fiber?.uid ?? null;
|
|
1001
|
+
const resolved = providerUid !== null && known.has(providerUid);
|
|
1002
|
+
edges.push({
|
|
1003
|
+
kind: "inject",
|
|
1004
|
+
from: uid,
|
|
1005
|
+
to: resolved ? providerUid : uid,
|
|
1006
|
+
service,
|
|
1007
|
+
resolved
|
|
1008
|
+
});
|
|
1009
|
+
if (resolved && providerUid !== uid) edges.push({
|
|
1010
|
+
kind: "provider",
|
|
1011
|
+
from: providerUid,
|
|
1012
|
+
to: uid,
|
|
1013
|
+
service
|
|
1014
|
+
});
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
let synthetic = -1;
|
|
1018
|
+
if (typeof loader?.entries === "function") for (const entry of loader.entries()) {
|
|
1019
|
+
if (entry?.options?.group) continue;
|
|
1020
|
+
const id = typeof entry.id === "string" ? entry.id : null;
|
|
1021
|
+
if (id !== null && uidByEntryId.has(id)) continue;
|
|
1022
|
+
const fiberUid = entry.fiber?.uid ?? null;
|
|
1023
|
+
if (fiberUid !== null && known.has(fiberUid)) continue;
|
|
1024
|
+
const disabled = entry.disabled === true;
|
|
1025
|
+
const moduleName = entry.options?.name ?? null;
|
|
1026
|
+
const source = moduleName === null ? void 0 : sourceOf(moduleName, moduleName);
|
|
1027
|
+
nodes.push({
|
|
1028
|
+
uid: fiberUid ?? synthetic--,
|
|
1029
|
+
name: clamp(moduleName ?? id ?? "unknown"),
|
|
1030
|
+
phase: disabled ? "disabled" : entry.fiber ? phaseOf(entry.fiber) : "pending",
|
|
1031
|
+
enabled: !disabled,
|
|
1032
|
+
moduleName,
|
|
1033
|
+
loaderEntryId: id,
|
|
1034
|
+
provides: [],
|
|
1035
|
+
effects: [],
|
|
1036
|
+
...source === void 0 ? {} : { source }
|
|
1037
|
+
});
|
|
1038
|
+
}
|
|
1039
|
+
return {
|
|
1040
|
+
capturedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1041
|
+
plane,
|
|
1042
|
+
nodes,
|
|
1043
|
+
edges
|
|
1044
|
+
};
|
|
1045
|
+
}
|
|
1046
|
+
//#endregion
|
|
1047
|
+
//#region src/plugin-graph.ts
|
|
1048
|
+
/**
|
|
1049
|
+
* The plugin graph, Host side: the numbered list of fiber changes the browser listens to, where each plugin's code is, and the port the frame's routes use.
|
|
1050
|
+
* The reading of the context itself is `./graph-inspect.ts` (ported from cordis-plugin-graph, MIT); the recorder and the source finder are new: that plugin
|
|
1051
|
+
* kept a ring of tool calls and had the browser ask for the whole graph every two seconds, while here a fiber's own `internal/status` and `internal/plugin`
|
|
1052
|
+
* events are recorded, and the browser's request waits until one happens.
|
|
1053
|
+
*/
|
|
1054
|
+
const MAX_EVENTS = 200;
|
|
1055
|
+
/** How long a request for events waits for one before it answers with none; the browser asks again at once. */
|
|
1056
|
+
const EVENTS_WAIT_MS = 2e4;
|
|
1057
|
+
/** The recent fiber changes, numbered; a request for those after a number waits for the next change when there are none. */
|
|
1058
|
+
var GraphRecorder = class {
|
|
1059
|
+
seq = 0;
|
|
1060
|
+
events = [];
|
|
1061
|
+
waiting = /* @__PURE__ */ new Set();
|
|
1062
|
+
/** The newest event number; 0 before the first. */
|
|
1063
|
+
get head() {
|
|
1064
|
+
return this.seq;
|
|
1065
|
+
}
|
|
1066
|
+
record(event, at = Date.now()) {
|
|
1067
|
+
this.seq += 1;
|
|
1068
|
+
this.events.push({
|
|
1069
|
+
...event,
|
|
1070
|
+
seq: this.seq,
|
|
1071
|
+
at
|
|
1072
|
+
});
|
|
1073
|
+
if (this.events.length > MAX_EVENTS) this.events.shift();
|
|
1074
|
+
for (const wake of [...this.waiting]) wake();
|
|
1075
|
+
}
|
|
1076
|
+
/** Every recorded event after `after`, oldest first. */
|
|
1077
|
+
since(after) {
|
|
1078
|
+
return {
|
|
1079
|
+
seq: this.seq,
|
|
1080
|
+
events: this.events.filter((event) => event.seq > after)
|
|
1081
|
+
};
|
|
1082
|
+
}
|
|
1083
|
+
/** The events after `after`, as soon as there are some; none when `ms` passes first or `signal` aborts (the browser left). */
|
|
1084
|
+
async wait(after, ms, signal) {
|
|
1085
|
+
if (this.seq > after || ms <= 0 || signal?.aborted === true) return this.since(after);
|
|
1086
|
+
await new Promise((done) => {
|
|
1087
|
+
const finish = () => {
|
|
1088
|
+
clearTimeout(timer);
|
|
1089
|
+
this.waiting.delete(finish);
|
|
1090
|
+
signal?.removeEventListener("abort", finish);
|
|
1091
|
+
done();
|
|
1092
|
+
};
|
|
1093
|
+
const timer = setTimeout(finish, ms);
|
|
1094
|
+
this.waiting.add(finish);
|
|
1095
|
+
signal?.addEventListener("abort", finish, { once: true });
|
|
1096
|
+
});
|
|
1097
|
+
return this.since(after);
|
|
1098
|
+
}
|
|
1099
|
+
/** Wake every waiting request now (the Host is stopping); each answers with what it has. */
|
|
1100
|
+
release() {
|
|
1101
|
+
for (const wake of [...this.waiting]) wake();
|
|
1102
|
+
}
|
|
1103
|
+
};
|
|
1104
|
+
/** Record every fiber of `ctx`'s tree changing: created, removed, or moved to another phase. Returns the stop. */
|
|
1105
|
+
function recordFibers(ctx, recorder) {
|
|
1106
|
+
const root = ctx.root;
|
|
1107
|
+
const offPlugin = root.on("internal/plugin", (fiber) => {
|
|
1108
|
+
try {
|
|
1109
|
+
recorder.record({
|
|
1110
|
+
uid: fiber.uid ?? 0,
|
|
1111
|
+
name: clamp(fiber.name),
|
|
1112
|
+
kind: fiber.uid === null ? "removed" : "created"
|
|
1113
|
+
});
|
|
1114
|
+
} catch {}
|
|
1115
|
+
});
|
|
1116
|
+
const offStatus = root.on("internal/status", (fiber, before) => {
|
|
1117
|
+
try {
|
|
1118
|
+
recorder.record({
|
|
1119
|
+
uid: fiber.uid ?? 0,
|
|
1120
|
+
name: clamp(fiber.name),
|
|
1121
|
+
kind: "status",
|
|
1122
|
+
from: PHASE[before] ?? "pending",
|
|
1123
|
+
to: PHASE[fiber.state] ?? "pending"
|
|
1124
|
+
});
|
|
1125
|
+
} catch {}
|
|
1126
|
+
});
|
|
1127
|
+
return () => {
|
|
1128
|
+
offPlugin();
|
|
1129
|
+
offStatus();
|
|
1130
|
+
};
|
|
1131
|
+
}
|
|
1132
|
+
const MAIN_FILES = [
|
|
1133
|
+
"index.ts",
|
|
1134
|
+
"index.mts",
|
|
1135
|
+
"index.js",
|
|
1136
|
+
"index.mjs",
|
|
1137
|
+
"src/index.ts",
|
|
1138
|
+
"src/index.js"
|
|
1139
|
+
];
|
|
1140
|
+
const FILE_ENDINGS = [
|
|
1141
|
+
"",
|
|
1142
|
+
".ts",
|
|
1143
|
+
".mts",
|
|
1144
|
+
".tsx",
|
|
1145
|
+
".js",
|
|
1146
|
+
".mjs",
|
|
1147
|
+
".cjs"
|
|
1148
|
+
];
|
|
1149
|
+
const isFile = (path) => {
|
|
1150
|
+
try {
|
|
1151
|
+
return statSync(path).isFile();
|
|
1152
|
+
} catch {
|
|
1153
|
+
return false;
|
|
1154
|
+
}
|
|
1155
|
+
};
|
|
1156
|
+
const isDirectory = (path) => {
|
|
1157
|
+
try {
|
|
1158
|
+
return statSync(path).isDirectory();
|
|
1159
|
+
} catch {
|
|
1160
|
+
return false;
|
|
1161
|
+
}
|
|
1162
|
+
};
|
|
1163
|
+
/** The main file of a plugin folder: the manifest's `main` when it names one, else the first index file. */
|
|
1164
|
+
function mainOfFolder(folder) {
|
|
1165
|
+
try {
|
|
1166
|
+
const manifest = JSON.parse(readFileSync(join(folder, "plugin.json"), "utf8"));
|
|
1167
|
+
if (typeof manifest.main === "string" && isFile(join(folder, manifest.main))) return join(folder, manifest.main);
|
|
1168
|
+
} catch {}
|
|
1169
|
+
return MAIN_FILES.map((name) => join(folder, name)).find(isFile);
|
|
1170
|
+
}
|
|
1171
|
+
/** The file a path names: the file itself, the file with an ending, or the main file of the folder. */
|
|
1172
|
+
function fileOf(path) {
|
|
1173
|
+
if (isDirectory(path)) return mainOfFolder(path);
|
|
1174
|
+
return FILE_ENDINGS.map((ending) => path + ending).find(isFile);
|
|
1175
|
+
}
|
|
1176
|
+
/** The folder of the package a file belongs to: the nearest parent with a package.json, else the file's own folder. */
|
|
1177
|
+
function packageFolderOf(file) {
|
|
1178
|
+
let dir = dirname(file);
|
|
1179
|
+
for (;;) {
|
|
1180
|
+
if (existsSync(join(dir, "package.json"))) return dir;
|
|
1181
|
+
const parent = dirname(dir);
|
|
1182
|
+
if (parent === dir) return dirname(file);
|
|
1183
|
+
dir = parent;
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
/**
|
|
1187
|
+
* Find where a Loader entry's code is. In the order the app itself finds it: a folder of the app's `plugins/` named like the row, then a path from the app's
|
|
1188
|
+
* folder (`./src/frame.ts`), then a package the app or the frame (for the DSH packages, which the frame's own install carries) can resolve.
|
|
1189
|
+
*/
|
|
1190
|
+
function sourceFinder(appRoot) {
|
|
1191
|
+
const root = (() => {
|
|
1192
|
+
try {
|
|
1193
|
+
return realpathSync(appRoot);
|
|
1194
|
+
} catch {
|
|
1195
|
+
return resolve(appRoot);
|
|
1196
|
+
}
|
|
1197
|
+
})();
|
|
1198
|
+
const fromApp = createRequire(join(root, "package.json"));
|
|
1199
|
+
const fromFrame = createRequire(import.meta.url);
|
|
1200
|
+
const cache = /* @__PURE__ */ new Map();
|
|
1201
|
+
const describe = (file, folder) => {
|
|
1202
|
+
const real = (() => {
|
|
1203
|
+
try {
|
|
1204
|
+
return realpathSync(file);
|
|
1205
|
+
} catch {
|
|
1206
|
+
return file;
|
|
1207
|
+
}
|
|
1208
|
+
})();
|
|
1209
|
+
const realFolder = (() => {
|
|
1210
|
+
try {
|
|
1211
|
+
return realpathSync(folder);
|
|
1212
|
+
} catch {
|
|
1213
|
+
return folder;
|
|
1214
|
+
}
|
|
1215
|
+
})();
|
|
1216
|
+
const inside = relative(root, real);
|
|
1217
|
+
return {
|
|
1218
|
+
file: real,
|
|
1219
|
+
folder: realFolder,
|
|
1220
|
+
...inside !== "" && !inside.startsWith("..") && !isAbsolute(inside) && !inside.split(sep).includes("node_modules") ? { inApp: inside.split(sep).join("/") } : {}
|
|
1221
|
+
};
|
|
1222
|
+
};
|
|
1223
|
+
const find = (moduleName) => {
|
|
1224
|
+
if (moduleName.startsWith("file:")) moduleName = moduleName.slice(5);
|
|
1225
|
+
if (isAbsolute(moduleName) || moduleName.startsWith("./") || moduleName.startsWith("../")) {
|
|
1226
|
+
const file = fileOf(isAbsolute(moduleName) ? moduleName : resolve(root, moduleName));
|
|
1227
|
+
if (file === void 0) return void 0;
|
|
1228
|
+
const base = isAbsolute(moduleName) ? moduleName : resolve(root, moduleName);
|
|
1229
|
+
return describe(file, isDirectory(base) ? base : dirname(file));
|
|
1230
|
+
}
|
|
1231
|
+
const local = join(root, "plugins", moduleName);
|
|
1232
|
+
if (isDirectory(local)) {
|
|
1233
|
+
const file = mainOfFolder(local);
|
|
1234
|
+
if (file !== void 0) return describe(file, local);
|
|
1235
|
+
}
|
|
1236
|
+
for (const requireFrom of [fromApp, fromFrame]) try {
|
|
1237
|
+
const file = requireFrom.resolve(moduleName);
|
|
1238
|
+
return describe(file, packageFolderOf(file));
|
|
1239
|
+
} catch {}
|
|
1240
|
+
};
|
|
1241
|
+
return (moduleName, name) => {
|
|
1242
|
+
for (const candidate of [moduleName, name]) {
|
|
1243
|
+
if (candidate === null || candidate === "" || candidate === "root") continue;
|
|
1244
|
+
if (!cache.has(candidate)) cache.set(candidate, find(candidate));
|
|
1245
|
+
const found = cache.get(candidate);
|
|
1246
|
+
if (found !== void 0) return found;
|
|
1247
|
+
}
|
|
1248
|
+
};
|
|
1249
|
+
}
|
|
1250
|
+
/** The graph of the Host's context, with its recorder started. `stop` ends the recording and answers every waiting request. */
|
|
1251
|
+
function createGraphPort(ctx, appRoot) {
|
|
1252
|
+
const recorder = new GraphRecorder();
|
|
1253
|
+
const stopRecording = recordFibers(ctx, recorder);
|
|
1254
|
+
const sourceOf = sourceFinder(appRoot);
|
|
1255
|
+
return {
|
|
1256
|
+
port: {
|
|
1257
|
+
read: () => {
|
|
1258
|
+
const seq = recorder.head;
|
|
1259
|
+
return {
|
|
1260
|
+
snapshot: inspectCordisContext(ctx, "host", sourceOf),
|
|
1261
|
+
seq
|
|
1262
|
+
};
|
|
1263
|
+
},
|
|
1264
|
+
events: (after, signal) => recorder.wait(after, EVENTS_WAIT_MS, signal)
|
|
1265
|
+
},
|
|
1266
|
+
stop: () => {
|
|
1267
|
+
stopRecording();
|
|
1268
|
+
recorder.release();
|
|
1269
|
+
}
|
|
1270
|
+
};
|
|
1271
|
+
}
|
|
1272
|
+
//#endregion
|
|
1273
|
+
//#region src/registry.ts
|
|
1274
|
+
const REGISTRY_INDEX_URL = "https://cordisplugins.github.io/registry/index.json";
|
|
1275
|
+
/** What the person is told when the registry cannot be read. */
|
|
1276
|
+
const OFFLINE = "The registry could not be reached. Check your internet connection and try again.";
|
|
1277
|
+
const BUSY = "The registry is busy right now. Try again in a few minutes.";
|
|
1278
|
+
const CHANGED = "The registry answered in a way this app does not understand. Try again later.";
|
|
1279
|
+
const SOURCE = /\bgithub:([A-Za-z0-9][A-Za-z0-9-]*)\/([A-Za-z0-9][A-Za-z0-9._-]*)@([0-9][0-9A-Za-z.+-]*)/u;
|
|
1280
|
+
/** A plugin's folder name: the name without its scope, lower case (`@owner/notes` is `notes`). */
|
|
1281
|
+
const bareName = (name) => (name.includes("/") ? name.slice(name.lastIndexOf("/") + 1) : name).toLowerCase();
|
|
1282
|
+
/** `owner/repo` of a `github:` source, lower case; undefined for any other source. */
|
|
1283
|
+
function repoOfSource(source) {
|
|
1284
|
+
const match = SOURCE.exec(source);
|
|
1285
|
+
return match === null ? void 0 : `${match[1] ?? ""}/${match[2] ?? ""}`.toLowerCase();
|
|
1286
|
+
}
|
|
1287
|
+
function isRecord$1(value) {
|
|
1288
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
1289
|
+
}
|
|
1290
|
+
/**
|
|
1291
|
+
* The entries a Blends app can pull: the ones the index gives a version and a pull line (a plugin whose code is only a folder of another
|
|
1292
|
+
* repository has none). @throws when the index is not formatVersion 1.
|
|
1293
|
+
*/
|
|
1294
|
+
function registryEntries(index, marks) {
|
|
1295
|
+
if (!isRecord$1(index) || index.formatVersion !== 1 || !Array.isArray(index.entries)) throw new Error("registry: not a version 1 index");
|
|
1296
|
+
const entries = [];
|
|
1297
|
+
for (const entry of index.entries) {
|
|
1298
|
+
if (!isRecord$1(entry)) continue;
|
|
1299
|
+
const { name, description, repo, version, published, compatibility, acrylPull } = entry;
|
|
1300
|
+
if (typeof name !== "string" || typeof repo !== "string" || typeof version !== "string" || typeof acrylPull !== "string") continue;
|
|
1301
|
+
const source = SOURCE.exec(acrylPull)?.[0];
|
|
1302
|
+
const where = /^https:\/\/github\.com\/([^/]+)\/([^/#?]+)/u.exec(repo);
|
|
1303
|
+
if (source === void 0 || where === null) continue;
|
|
1304
|
+
const owner = where[1] ?? "";
|
|
1305
|
+
const repository = repoOfSource(source);
|
|
1306
|
+
entries.push({
|
|
1307
|
+
name,
|
|
1308
|
+
description: typeof description === "string" ? description : "",
|
|
1309
|
+
owner,
|
|
1310
|
+
url: `https://github.com/${owner}/${where[2] ?? ""}`,
|
|
1311
|
+
version,
|
|
1312
|
+
...typeof published === "string" && /^\d{4}-\d{2}-\d{2}$/u.test(published) ? { published } : {},
|
|
1313
|
+
compatibility: typeof compatibility === "string" ? compatibility : "DSH stock",
|
|
1314
|
+
source,
|
|
1315
|
+
installed: marks.names.has(bareName(name)) || repository !== void 0 && marks.repos.has(repository),
|
|
1316
|
+
mine: marks.accounts.has(owner.toLowerCase())
|
|
1317
|
+
});
|
|
1318
|
+
}
|
|
1319
|
+
return entries.sort((a, b) => Number(b.mine) - Number(a.mine) || (b.published ?? "").localeCompare(a.published ?? "") || a.name.localeCompare(b.name));
|
|
1320
|
+
}
|
|
1321
|
+
/** The raw index, read once in a while: a fresh copy within `ttlMs`, else a new read, else the copy read before with the time it was read. */
|
|
1322
|
+
function createRegistryReader(fetcher, options = {}) {
|
|
1323
|
+
const ttl = options.ttlMs ?? 3e5;
|
|
1324
|
+
const timeout = options.timeoutMs ?? 1e4;
|
|
1325
|
+
const now = options.now ?? Date.now;
|
|
1326
|
+
let kept;
|
|
1327
|
+
return async () => {
|
|
1328
|
+
if (kept !== void 0 && now() - kept.at < ttl) return {
|
|
1329
|
+
index: kept.index,
|
|
1330
|
+
readAt: new Date(kept.at).toISOString()
|
|
1331
|
+
};
|
|
1332
|
+
const abort = new AbortController();
|
|
1333
|
+
const timer = setTimeout(() => {
|
|
1334
|
+
abort.abort();
|
|
1335
|
+
}, timeout);
|
|
1336
|
+
let failure = OFFLINE;
|
|
1337
|
+
try {
|
|
1338
|
+
const response = await fetcher(REGISTRY_INDEX_URL, {
|
|
1339
|
+
signal: abort.signal,
|
|
1340
|
+
headers: { Accept: "application/json" }
|
|
1341
|
+
});
|
|
1342
|
+
if (response.ok) {
|
|
1343
|
+
const index = await response.json();
|
|
1344
|
+
kept = {
|
|
1345
|
+
index,
|
|
1346
|
+
at: now()
|
|
1347
|
+
};
|
|
1348
|
+
return {
|
|
1349
|
+
index,
|
|
1350
|
+
readAt: new Date(kept.at).toISOString()
|
|
1351
|
+
};
|
|
1352
|
+
}
|
|
1353
|
+
failure = response.status === 403 || response.status === 429 ? BUSY : response.status >= 500 ? OFFLINE : CHANGED;
|
|
1354
|
+
} catch (cause) {
|
|
1355
|
+
failure = cause instanceof SyntaxError ? CHANGED : OFFLINE;
|
|
1356
|
+
} finally {
|
|
1357
|
+
clearTimeout(timer);
|
|
1358
|
+
}
|
|
1359
|
+
return kept === void 0 ? { message: failure } : {
|
|
1360
|
+
index: kept.index,
|
|
1361
|
+
readAt: new Date(kept.at).toISOString()
|
|
1362
|
+
};
|
|
1363
|
+
};
|
|
1364
|
+
}
|
|
1365
|
+
/** The registry as the frame shows it, from one read of the index. */
|
|
1366
|
+
async function readRegistryView(read, marks) {
|
|
1367
|
+
const got = await read();
|
|
1368
|
+
if ("message" in got) return {
|
|
1369
|
+
status: "unavailable",
|
|
1370
|
+
message: got.message
|
|
1371
|
+
};
|
|
1372
|
+
try {
|
|
1373
|
+
return {
|
|
1374
|
+
status: "ready",
|
|
1375
|
+
entries: registryEntries(got.index, await marks()),
|
|
1376
|
+
readAt: got.readAt
|
|
1377
|
+
};
|
|
1378
|
+
} catch {
|
|
1379
|
+
return {
|
|
1380
|
+
status: "unavailable",
|
|
1381
|
+
message: CHANGED
|
|
1382
|
+
};
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
/** The running app's `pluginShare`, read at each call (the app's rows come and go with a reload); undefined when the app has none. */
|
|
1386
|
+
function pluginShareOf(service) {
|
|
1387
|
+
const share = service();
|
|
1388
|
+
return typeof share?.pull === "function" && typeof share.list === "function" ? share : void 0;
|
|
1389
|
+
}
|
|
1390
|
+
//#endregion
|
|
668
1391
|
//#region src/auto-save.ts
|
|
669
1392
|
/**
|
|
670
1393
|
* The frame's auto-save: when the chat's agent finishes a turn that changed the app's files, the Host makes a save point (a version) by itself and
|
|
@@ -996,21 +1719,28 @@ function isNoise(filename) {
|
|
|
996
1719
|
return filename.split(/[\\/]/u).some((part) => part.startsWith(".")) || /(~|\.sw[a-p]|\.tmp)$/u.test(filename);
|
|
997
1720
|
}
|
|
998
1721
|
/**
|
|
999
|
-
* Calls `onChange` once, `debounceMs` after the last change of a file under `dir` (recursively, or only the `only` files at its top), whoever made it
|
|
1000
|
-
*
|
|
1722
|
+
* Calls `onChange` once, `debounceMs` after the last change of a file under `dir` (recursively, or only the `only` files at its top), whoever made it,
|
|
1723
|
+
* with the paths that changed in the burst (`dir` itself when the system did not say which file). Returns the function that stops watching; a folder
|
|
1724
|
+
* that does not exist is not watched (an app without `src/` has no code to load again).
|
|
1001
1725
|
*/
|
|
1002
1726
|
function watchFolder(dir, onChange, options = {}) {
|
|
1003
1727
|
if (!existsSync(dir)) return () => {};
|
|
1004
1728
|
const debounceMs = options.debounceMs ?? 300;
|
|
1005
1729
|
let timer;
|
|
1730
|
+
const changed = /* @__PURE__ */ new Set();
|
|
1006
1731
|
const startedAt = Date.now();
|
|
1007
1732
|
const only = options.only;
|
|
1008
1733
|
const watcher = watch(dir, { recursive: only === void 0 }, (_event, filename) => {
|
|
1009
1734
|
if (Date.now() - startedAt < REPLAY_WINDOW_MS) return;
|
|
1010
1735
|
if (only !== void 0 && (typeof filename !== "string" || !only.includes(filename))) return;
|
|
1011
1736
|
if (typeof filename === "string" && isNoise(filename)) return;
|
|
1737
|
+
changed.add(typeof filename === "string" ? join(dir, filename) : dir);
|
|
1012
1738
|
clearTimeout(timer);
|
|
1013
|
-
timer = setTimeout(
|
|
1739
|
+
timer = setTimeout(() => {
|
|
1740
|
+
const burst = [...changed];
|
|
1741
|
+
changed.clear();
|
|
1742
|
+
onChange(burst);
|
|
1743
|
+
}, debounceMs);
|
|
1014
1744
|
});
|
|
1015
1745
|
watcher.on("error", (cause) => {
|
|
1016
1746
|
options.report?.(cause);
|
|
@@ -1028,6 +1758,143 @@ function watchFolders(dirs, onChange, options = {}) {
|
|
|
1028
1758
|
};
|
|
1029
1759
|
}
|
|
1030
1760
|
//#endregion
|
|
1761
|
+
//#region src/applied-live.ts
|
|
1762
|
+
/**
|
|
1763
|
+
* Which changes of an app's files the running app has already applied itself, so the frame does not load its code again for them (spec 049 T022).
|
|
1764
|
+
*
|
|
1765
|
+
* A pull (`plugin pull`, the door's or the frame's) installs a plugin live: it writes `plugins/<name>/`, remembers the release in `blend.yaml` (the
|
|
1766
|
+
* `plugin-share` row's `pulled` config) and the plugin runs at once. Loading the whole app again for those writes restarts every app row for seconds,
|
|
1767
|
+
* and a second pull made meanwhile is cut off. The files themselves say which writes those were, so this needs no timer and no event:
|
|
1768
|
+
*
|
|
1769
|
+
* - `plugins/<name>/` that digests to the digest of its pull is the released plugin as pulled. An edit changes the digest, so an agent editing a pulled
|
|
1770
|
+
* plugin still has the app loaded again. The digest is the one `blend.yaml` remembers or, for a pull `blend.yaml` does not remember (from a local
|
|
1771
|
+
* file), the one the running `pluginShare` installed.
|
|
1772
|
+
* - `plugins/<name>/` that is gone and was not there when the app's code was last loaded came and went live (a refused pull, a plugin removed live).
|
|
1773
|
+
* - `blend.yaml` that differs only in the pulled records changes no row.
|
|
1774
|
+
*
|
|
1775
|
+
* Anything else, every change of `src/` among it, has the app loaded again, as before.
|
|
1776
|
+
*/
|
|
1777
|
+
/** The row whose config remembers the pulled plugins (acrylblends' `plugin-share`). */
|
|
1778
|
+
const SHARE_ROW = "plugin-share";
|
|
1779
|
+
/** `blend.yaml`'s text as an {@link AppDefinition}. A text that does not parse is its own definition, so any change to it loads the app again. */
|
|
1780
|
+
function readDefinition(text) {
|
|
1781
|
+
const document = parseDocument(text);
|
|
1782
|
+
if (document.errors.length > 0) return {
|
|
1783
|
+
app: text,
|
|
1784
|
+
pulled: {}
|
|
1785
|
+
};
|
|
1786
|
+
const value = document.toJS();
|
|
1787
|
+
const rows = value?.spec?.rows;
|
|
1788
|
+
const pulled = {};
|
|
1789
|
+
if (Array.isArray(rows)) for (const row of rows) {
|
|
1790
|
+
if (row?.name !== SHARE_ROW || typeof row.config !== "object" || row.config === null) continue;
|
|
1791
|
+
const records = row.config.pulled;
|
|
1792
|
+
if (typeof records === "object" && records !== null) {
|
|
1793
|
+
for (const [name, record] of Object.entries(records)) if (typeof record?.digest === "string") pulled[name] = record.digest;
|
|
1794
|
+
}
|
|
1795
|
+
delete row.config.pulled;
|
|
1796
|
+
if (Object.keys(row.config).length === 0) delete row.config;
|
|
1797
|
+
}
|
|
1798
|
+
return {
|
|
1799
|
+
app: JSON.stringify(value),
|
|
1800
|
+
pulled
|
|
1801
|
+
};
|
|
1802
|
+
}
|
|
1803
|
+
/** The folders of `plugins/` now. */
|
|
1804
|
+
function pluginFolders(root) {
|
|
1805
|
+
const dir = join(root, "plugins");
|
|
1806
|
+
if (!existsSync(dir)) return /* @__PURE__ */ new Set();
|
|
1807
|
+
return new Set(readdirSync(dir, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => entry.name));
|
|
1808
|
+
}
|
|
1809
|
+
/** The app's files as its code was last loaded, and the decision for each burst of changes after it. */
|
|
1810
|
+
var AppliedLive = class {
|
|
1811
|
+
root;
|
|
1812
|
+
pulledDigest;
|
|
1813
|
+
definition;
|
|
1814
|
+
folders;
|
|
1815
|
+
/** `root` is the app's folder; the state is read now, as the app's code was loaded at start. `pulledDigest` answers for a pull `blend.yaml` does not remember. */
|
|
1816
|
+
constructor(root, pulledDigest = () => void 0) {
|
|
1817
|
+
this.root = root;
|
|
1818
|
+
this.pulledDigest = pulledDigest;
|
|
1819
|
+
this.definition = this.read().app;
|
|
1820
|
+
this.folders = pluginFolders(root);
|
|
1821
|
+
}
|
|
1822
|
+
/** The app's code is being loaded again from the files as they are now: they are the new reference. */
|
|
1823
|
+
loaded() {
|
|
1824
|
+
this.definition = this.read().app;
|
|
1825
|
+
this.folders = pluginFolders(this.root);
|
|
1826
|
+
}
|
|
1827
|
+
/**
|
|
1828
|
+
* Whether the app's code must be loaded again for these changed paths (absolute, as the watcher reports them). `false` when the running app has
|
|
1829
|
+
* applied every one of them itself; the pulled-records-only `blend.yaml` then becomes the reference.
|
|
1830
|
+
*/
|
|
1831
|
+
needsReload(changed) {
|
|
1832
|
+
const now = this.read();
|
|
1833
|
+
for (const path of changed) {
|
|
1834
|
+
const parts = relative(this.root, path).split(sep);
|
|
1835
|
+
if (parts.length === 1 && parts[0] === "blend.yaml") {
|
|
1836
|
+
if (now.app !== this.definition) return true;
|
|
1837
|
+
continue;
|
|
1838
|
+
}
|
|
1839
|
+
const name = parts[0] === "plugins" ? parts[1] : void 0;
|
|
1840
|
+
if (name === void 0 || name === "") return true;
|
|
1841
|
+
const dir = join(this.root, "plugins", name);
|
|
1842
|
+
if (existsSync(dir) && statSync(dir).isDirectory()) {
|
|
1843
|
+
const remembered = now.pulled[name] ?? this.pulledDigest(name);
|
|
1844
|
+
if (remembered === void 0 || digestFolder(dir) !== remembered) return true;
|
|
1845
|
+
} else if (this.folders.has(name)) return true;
|
|
1846
|
+
}
|
|
1847
|
+
this.definition = now.app;
|
|
1848
|
+
return false;
|
|
1849
|
+
}
|
|
1850
|
+
read() {
|
|
1851
|
+
const file = join(this.root, "blend.yaml");
|
|
1852
|
+
return readDefinition(existsSync(file) ? readFileSync(file, "utf8") : "");
|
|
1853
|
+
}
|
|
1854
|
+
};
|
|
1855
|
+
/**
|
|
1856
|
+
* Watches the app's files - `src/`, `plugins/` and the `blend.yaml` at its root - and calls `reload` for a burst of changes the running app has not
|
|
1857
|
+
* applied itself. A decision that cannot read a file (it went away meanwhile) is reported and loads the app again, as without the decision.
|
|
1858
|
+
* @returns the function that stops watching.
|
|
1859
|
+
*/
|
|
1860
|
+
function watchAppFiles(root, reload, options = {}) {
|
|
1861
|
+
const applied = new AppliedLive(root, options.pulledDigest);
|
|
1862
|
+
const onChange = (changed) => {
|
|
1863
|
+
try {
|
|
1864
|
+
if (!applied.needsReload(changed)) return;
|
|
1865
|
+
applied.loaded();
|
|
1866
|
+
} catch (cause) {
|
|
1867
|
+
options.report?.(cause);
|
|
1868
|
+
}
|
|
1869
|
+
reload();
|
|
1870
|
+
};
|
|
1871
|
+
const watching = {
|
|
1872
|
+
...options.debounceMs === void 0 ? {} : { debounceMs: options.debounceMs },
|
|
1873
|
+
...options.report === void 0 ? {} : { report: options.report }
|
|
1874
|
+
};
|
|
1875
|
+
const stopCode = watchFolders([join(root, "src"), join(root, "plugins")], onChange, watching);
|
|
1876
|
+
const stopBlend = watchFolder(root, onChange, {
|
|
1877
|
+
...watching,
|
|
1878
|
+
only: ["blend.yaml"]
|
|
1879
|
+
});
|
|
1880
|
+
return () => {
|
|
1881
|
+
stopCode();
|
|
1882
|
+
stopBlend();
|
|
1883
|
+
};
|
|
1884
|
+
}
|
|
1885
|
+
/**
|
|
1886
|
+
* The running app's `pluginShare` as the frame reads it: its `pulledDigest`, when the service is there and has one. Read at each call, as the app's rows
|
|
1887
|
+
* come and go with a reload; anything else answers undefined, so the decision falls back to `blend.yaml` alone.
|
|
1888
|
+
*/
|
|
1889
|
+
function pulledDigestOf(service) {
|
|
1890
|
+
return (name) => {
|
|
1891
|
+
const share = service();
|
|
1892
|
+
if (typeof share?.pulledDigest !== "function") return void 0;
|
|
1893
|
+
const digest = share.pulledDigest.call(share, name);
|
|
1894
|
+
return typeof digest === "string" ? digest : void 0;
|
|
1895
|
+
};
|
|
1896
|
+
}
|
|
1897
|
+
//#endregion
|
|
1031
1898
|
//#region src/outward-guard.ts
|
|
1032
1899
|
/**
|
|
1033
1900
|
* The agent never sends the person's work out of the machine without their yes.
|
|
@@ -2133,6 +3000,157 @@ async function upstreamHead(git, root) {
|
|
|
2133
3000
|
}
|
|
2134
3001
|
}
|
|
2135
3002
|
//#endregion
|
|
3003
|
+
//#region src/github-connect.ts
|
|
3004
|
+
/**
|
|
3005
|
+
* Connect GitHub from the app, for a person who has never opened a terminal: GitHub's own browser sign-in (`gh auth login --web`), started by a button.
|
|
3006
|
+
*
|
|
3007
|
+
* gh prints a one-time code and the address github.com/login/device, then waits until the person enters the code there and says yes. This module runs
|
|
3008
|
+
* that one gh process, reads the code from what it prints, and reports where the sign-in is. No password or token passes through the app: the person
|
|
3009
|
+
* signs in on github.com, and gh keeps its credential where it always does. When the sign-in succeeded, `gh auth setup-git` lets git push over HTTPS
|
|
3010
|
+
* with the same account, which a share needs.
|
|
3011
|
+
*
|
|
3012
|
+
* One sign-in at a time. The owner (the frame's Host) disposes it with the plugin, which ends a sign-in still waiting.
|
|
3013
|
+
*/
|
|
3014
|
+
const DEVICE_URL = "https://github.com/login/device";
|
|
3015
|
+
const CODE = /\b([A-Z0-9]{4}-[A-Z0-9]{4})\b/u;
|
|
3016
|
+
const URL_IN_TEXT = /https:\/\/github\.com\/login\/device\b/u;
|
|
3017
|
+
/** How long a code stays usable on GitHub's side (15 minutes); after that this gives up and the person starts again. */
|
|
3018
|
+
const GIVE_UP_MS = 9e5;
|
|
3019
|
+
const GITHUB_CONNECT_MISSING = "Connecting needs a free helper program from GitHub, and it is not installed on this computer. Install it from cli.github.com, then try again.";
|
|
3020
|
+
/** The gh this computer has, without a terminal: no prompts, no browser opened by gh itself (the page offers the link). */
|
|
3021
|
+
function githubConnectPorts() {
|
|
3022
|
+
const env = {
|
|
3023
|
+
...process.env,
|
|
3024
|
+
GH_BROWSER: "true",
|
|
3025
|
+
GH_PROMPT_DISABLED: "1",
|
|
3026
|
+
GIT_TERMINAL_PROMPT: "0"
|
|
3027
|
+
};
|
|
3028
|
+
return {
|
|
3029
|
+
startLogin: () => {
|
|
3030
|
+
if (spawnSync("gh", ["--version"], {
|
|
3031
|
+
env,
|
|
3032
|
+
encoding: "utf8"
|
|
3033
|
+
}).error !== void 0) return void 0;
|
|
3034
|
+
return spawn("gh", [
|
|
3035
|
+
"auth",
|
|
3036
|
+
"login",
|
|
3037
|
+
"--hostname",
|
|
3038
|
+
"github.com",
|
|
3039
|
+
"--git-protocol",
|
|
3040
|
+
"https",
|
|
3041
|
+
"--web",
|
|
3042
|
+
"--skip-ssh-key"
|
|
3043
|
+
], {
|
|
3044
|
+
env,
|
|
3045
|
+
stdio: [
|
|
3046
|
+
"pipe",
|
|
3047
|
+
"pipe",
|
|
3048
|
+
"pipe"
|
|
3049
|
+
]
|
|
3050
|
+
});
|
|
3051
|
+
},
|
|
3052
|
+
run: (args) => {
|
|
3053
|
+
const result = spawnSync("gh", [...args], {
|
|
3054
|
+
env,
|
|
3055
|
+
encoding: "utf8"
|
|
3056
|
+
});
|
|
3057
|
+
return result.error !== void 0 ? 1 : result.status ?? 1;
|
|
3058
|
+
}
|
|
3059
|
+
};
|
|
3060
|
+
}
|
|
3061
|
+
var GithubConnect = class {
|
|
3062
|
+
ports;
|
|
3063
|
+
giveUpMs;
|
|
3064
|
+
current = { status: "idle" };
|
|
3065
|
+
child;
|
|
3066
|
+
timer;
|
|
3067
|
+
disposed = false;
|
|
3068
|
+
constructor(ports, giveUpMs = GIVE_UP_MS) {
|
|
3069
|
+
this.ports = ports;
|
|
3070
|
+
this.giveUpMs = giveUpMs;
|
|
3071
|
+
}
|
|
3072
|
+
state() {
|
|
3073
|
+
return this.current;
|
|
3074
|
+
}
|
|
3075
|
+
/**
|
|
3076
|
+
* Start a sign-in, or return the one already waiting. Resolves when the code is known (or the start failed), which is well before the person
|
|
3077
|
+
* finishes on github.com; ask `state()` for the rest.
|
|
3078
|
+
*/
|
|
3079
|
+
async start() {
|
|
3080
|
+
if (this.disposed) return this.current;
|
|
3081
|
+
if (this.child !== void 0) return this.waitForCode();
|
|
3082
|
+
const child = this.ports.startLogin();
|
|
3083
|
+
if (child === void 0) {
|
|
3084
|
+
this.current = {
|
|
3085
|
+
status: "failed",
|
|
3086
|
+
say: GITHUB_CONNECT_MISSING
|
|
3087
|
+
};
|
|
3088
|
+
return this.current;
|
|
3089
|
+
}
|
|
3090
|
+
this.child = child;
|
|
3091
|
+
this.current = { status: "starting" };
|
|
3092
|
+
let said = "";
|
|
3093
|
+
const read = (chunk) => {
|
|
3094
|
+
said += String(chunk);
|
|
3095
|
+
if (this.current.status !== "starting") return;
|
|
3096
|
+
const code = CODE.exec(said)?.[1];
|
|
3097
|
+
if (code !== void 0) this.current = {
|
|
3098
|
+
status: "code",
|
|
3099
|
+
code,
|
|
3100
|
+
url: URL_IN_TEXT.exec(said)?.[0] ?? DEVICE_URL
|
|
3101
|
+
};
|
|
3102
|
+
};
|
|
3103
|
+
child.stdout?.on("data", read);
|
|
3104
|
+
child.stderr?.on("data", read);
|
|
3105
|
+
child.stdin?.on("error", () => {});
|
|
3106
|
+
child.stdin?.write("\n");
|
|
3107
|
+
child.on("error", (cause) => this.finish({
|
|
3108
|
+
status: "failed",
|
|
3109
|
+
say: GITHUB_CONNECT_MISSING,
|
|
3110
|
+
details: cause.message
|
|
3111
|
+
}));
|
|
3112
|
+
child.on("exit", (status) => {
|
|
3113
|
+
if (status === 0) {
|
|
3114
|
+
this.ports.run(["auth", "setup-git"]);
|
|
3115
|
+
this.finish({ status: "connected" });
|
|
3116
|
+
} else if (this.current.status !== "failed") this.finish({
|
|
3117
|
+
status: "failed",
|
|
3118
|
+
say: "GitHub did not finish connecting. Press Connect GitHub to try again.",
|
|
3119
|
+
details: said.trim().split("\n").at(-1) ?? ""
|
|
3120
|
+
});
|
|
3121
|
+
});
|
|
3122
|
+
this.timer = setTimeout(() => {
|
|
3123
|
+
this.finish({
|
|
3124
|
+
status: "failed",
|
|
3125
|
+
say: "The code ran out before GitHub was connected. Press Connect GitHub to get a new one."
|
|
3126
|
+
});
|
|
3127
|
+
}, this.giveUpMs);
|
|
3128
|
+
return this.waitForCode();
|
|
3129
|
+
}
|
|
3130
|
+
/** End a waiting sign-in (the person closed the chooser, or the plugin unloads). Nothing on GitHub changes. */
|
|
3131
|
+
cancel() {
|
|
3132
|
+
if (this.child === void 0) return;
|
|
3133
|
+
this.finish({ status: "idle" });
|
|
3134
|
+
}
|
|
3135
|
+
dispose() {
|
|
3136
|
+
this.disposed = true;
|
|
3137
|
+
this.cancel();
|
|
3138
|
+
}
|
|
3139
|
+
finish(next) {
|
|
3140
|
+
if (this.timer !== void 0) clearTimeout(this.timer);
|
|
3141
|
+
this.timer = void 0;
|
|
3142
|
+
const child = this.child;
|
|
3143
|
+
this.child = void 0;
|
|
3144
|
+
if (child !== void 0 && child.exitCode === null && child.signalCode === null) child.kill();
|
|
3145
|
+
this.current = next;
|
|
3146
|
+
}
|
|
3147
|
+
/** The code usually arrives within a second; give up waiting for it after a few, and let the page ask again. */
|
|
3148
|
+
async waitForCode() {
|
|
3149
|
+
for (let waited = 0; waited < 5e3 && this.current.status === "starting"; waited += 50) await new Promise((resolve) => setTimeout(resolve, 50));
|
|
3150
|
+
return this.current;
|
|
3151
|
+
}
|
|
3152
|
+
};
|
|
3153
|
+
//#endregion
|
|
2136
3154
|
//#region src/app-snapshot.ts
|
|
2137
3155
|
/**
|
|
2138
3156
|
* The app's snapshot verbs as the frame's service `appSnapshot`: every version the frame saves (Save point, the auto-save) is captured first, so
|
|
@@ -2310,6 +3328,15 @@ async function readAppName(root) {
|
|
|
2310
3328
|
} catch {}
|
|
2311
3329
|
return basename(root);
|
|
2312
3330
|
}
|
|
3331
|
+
/** The only address a page may ask to add: a released plugin of a GitHub account, by version (never a local file or another URL). */
|
|
3332
|
+
const REGISTRY_SOURCE = /^github:[A-Za-z0-9][A-Za-z0-9-]*\/[A-Za-z0-9][A-Za-z0-9._-]*@[0-9][0-9A-Za-z.+-]*$/u;
|
|
3333
|
+
/** What a failed route answers: the plain sentence, and for a developer the tool's own text (the page shows it behind "Details"). */
|
|
3334
|
+
function plainBody(plain) {
|
|
3335
|
+
return {
|
|
3336
|
+
...error(plain.say),
|
|
3337
|
+
...plain.details === void 0 ? {} : { details: plain.details }
|
|
3338
|
+
};
|
|
3339
|
+
}
|
|
2313
3340
|
function parseSwitch(body) {
|
|
2314
3341
|
if (body === null || typeof body !== "object") return void 0;
|
|
2315
3342
|
const { kind, name, on } = body;
|
|
@@ -2372,7 +3399,7 @@ function parseBranchName(body) {
|
|
|
2372
3399
|
const problem = branchNameProblem(name);
|
|
2373
3400
|
return problem === void 0 ? { name } : { problem };
|
|
2374
3401
|
}
|
|
2375
|
-
function frameRoutes({ state, report = () => {}, plugins, feedback, branches, branchChats, chatSaves, shared, realChats, gitSync, snapshot, appReload, selection }) {
|
|
3402
|
+
function frameRoutes({ state, report = () => {}, plugins, graph, feedback, branches, branchChats, chatSaves, shared, realChats, gitSync, snapshot, appReload, selection }) {
|
|
2376
3403
|
const app = new Acryl();
|
|
2377
3404
|
if (appReload !== void 0) app.get(FRAME_APP_PATH, ownPage, async (c) => {
|
|
2378
3405
|
const after = Number(c.req.query("after"));
|
|
@@ -2533,6 +3560,21 @@ function frameRoutes({ state, report = () => {}, plugins, feedback, branches, br
|
|
|
2533
3560
|
}
|
|
2534
3561
|
});
|
|
2535
3562
|
}
|
|
3563
|
+
if (graph !== void 0) {
|
|
3564
|
+
app.get(FRAME_GRAPH_PATH, ownPage, async (c) => {
|
|
3565
|
+
try {
|
|
3566
|
+
return c.json(graph.read());
|
|
3567
|
+
} catch (cause) {
|
|
3568
|
+
report(cause);
|
|
3569
|
+
return c.json(error("the plugin graph could not be read"), 500);
|
|
3570
|
+
}
|
|
3571
|
+
});
|
|
3572
|
+
app.get(FRAME_GRAPH_EVENTS_PATH, ownPage, async (c) => {
|
|
3573
|
+
const after = Number(c.req.query("after") ?? "0");
|
|
3574
|
+
if (!Number.isInteger(after) || after < 0) return c.json(error("name the last event number you have"), 400);
|
|
3575
|
+
return c.json(await graph.events(after, c.req.raw.signal));
|
|
3576
|
+
});
|
|
3577
|
+
}
|
|
2536
3578
|
if (plugins !== void 0) {
|
|
2537
3579
|
app.get(FRAME_PLUGINS_PATH, ownPage, async (c) => {
|
|
2538
3580
|
try {
|
|
@@ -2559,10 +3601,26 @@ function frameRoutes({ state, report = () => {}, plugins, feedback, branches, br
|
|
|
2559
3601
|
const name = pluginName(body === INVALID_JSON || body === null || typeof body !== "object" ? void 0 : body);
|
|
2560
3602
|
if (name === void 0) return c.json(error("name one local plugin"), 400);
|
|
2561
3603
|
try {
|
|
2562
|
-
|
|
3604
|
+
const asked = body.visibility;
|
|
3605
|
+
const listed = body.registry === true;
|
|
3606
|
+
return c.json(await plugins.planShare(name, listed ? "public" : asked === "public" || asked === "private" ? asked : void 0, listed));
|
|
2563
3607
|
} catch (cause) {
|
|
2564
3608
|
report(cause);
|
|
2565
|
-
return c.json(
|
|
3609
|
+
return c.json(plainBody(plainShareProblem(cause)), 409);
|
|
3610
|
+
}
|
|
3611
|
+
});
|
|
3612
|
+
app.post(FRAME_PLUGINS_SAVE_PATH, ownPage, jsonBody(), async (c) => {
|
|
3613
|
+
const body = await readJson(c);
|
|
3614
|
+
const name = pluginName(body === INVALID_JSON || body === null || typeof body !== "object" ? void 0 : body);
|
|
3615
|
+
if (name === void 0) return c.json(error("name one local plugin"), 400);
|
|
3616
|
+
try {
|
|
3617
|
+
return c.json(await plugins.save(name));
|
|
3618
|
+
} catch (cause) {
|
|
3619
|
+
report(cause);
|
|
3620
|
+
return c.json(plainBody({
|
|
3621
|
+
say: "The plugin could not be kept on this computer. Nothing was changed.",
|
|
3622
|
+
details: cause instanceof Error ? cause.message : String(cause)
|
|
3623
|
+
}), 409);
|
|
2566
3624
|
}
|
|
2567
3625
|
});
|
|
2568
3626
|
app.post(FRAME_PLUGINS_SHARE_PATH, ownPage, jsonBody(), async (c) => {
|
|
@@ -2580,7 +3638,44 @@ function frameRoutes({ state, report = () => {}, plugins, feedback, branches, br
|
|
|
2580
3638
|
return c.json(await plugins.share(name, confirmed));
|
|
2581
3639
|
} catch (cause) {
|
|
2582
3640
|
report(cause);
|
|
2583
|
-
return c.json(
|
|
3641
|
+
return c.json(plainBody(plainShareProblem(cause)), 409);
|
|
3642
|
+
}
|
|
3643
|
+
});
|
|
3644
|
+
app.get(FRAME_PLUGINS_GITHUB_PATH, ownPage, async (c) => {
|
|
3645
|
+
try {
|
|
3646
|
+
return c.json(await plugins.github());
|
|
3647
|
+
} catch (cause) {
|
|
3648
|
+
report(cause);
|
|
3649
|
+
return c.json(error("GitHub could not be checked"), 500);
|
|
3650
|
+
}
|
|
3651
|
+
});
|
|
3652
|
+
app.post(FRAME_PLUGINS_GITHUB_CONNECT_PATH, ownPage, async (c) => c.json(await plugins.connect.start()));
|
|
3653
|
+
app.get(FRAME_PLUGINS_GITHUB_CONNECT_PATH, ownPage, (c) => c.json(plugins.connect.state()));
|
|
3654
|
+
app.delete(FRAME_PLUGINS_GITHUB_CONNECT_PATH, ownPage, (c) => {
|
|
3655
|
+
plugins.connect.cancel();
|
|
3656
|
+
return c.json(plugins.connect.state());
|
|
3657
|
+
});
|
|
3658
|
+
app.get(FRAME_PLUGINS_REGISTRY_PATH, ownPage, async (c) => {
|
|
3659
|
+
try {
|
|
3660
|
+
return c.json(await plugins.registry());
|
|
3661
|
+
} catch (cause) {
|
|
3662
|
+
report(cause);
|
|
3663
|
+
return c.json(error("The registry could not be read."), 500);
|
|
3664
|
+
}
|
|
3665
|
+
});
|
|
3666
|
+
app.post(FRAME_PLUGINS_PULL_PATH, ownPage, jsonBody(), async (c) => {
|
|
3667
|
+
const body = await readJson(c);
|
|
3668
|
+
const source = body === INVALID_JSON || body === null || typeof body !== "object" ? void 0 : body.source;
|
|
3669
|
+
if (typeof source !== "string" || !REGISTRY_SOURCE.test(source)) return c.json(error("That address is not one this app can add a plugin from."), 400);
|
|
3670
|
+
try {
|
|
3671
|
+
return c.json(await plugins.pull(source));
|
|
3672
|
+
} catch (cause) {
|
|
3673
|
+
if (cause instanceof PlainError) return c.json(plainBody(cause.plain), 409);
|
|
3674
|
+
report(cause);
|
|
3675
|
+
return c.json(plainBody({
|
|
3676
|
+
say: "The plugin could not be added. Nothing was changed.",
|
|
3677
|
+
details: cause instanceof Error ? cause.message : String(cause)
|
|
3678
|
+
}), 409);
|
|
2584
3679
|
}
|
|
2585
3680
|
});
|
|
2586
3681
|
}
|
|
@@ -2702,7 +3797,8 @@ function hostIdentity(instance) {
|
|
|
2702
3797
|
}
|
|
2703
3798
|
function apply(ctx, config = {}) {
|
|
2704
3799
|
const appPage = typeof config.appPage === "string" && config.appPage.startsWith("/") ? config.appPage : void 0;
|
|
2705
|
-
const
|
|
3800
|
+
const wantedOwner = config.shareOwner ?? process.env.ACRYLBLENDS_SHARE_OWNER;
|
|
3801
|
+
const shareOwner = typeof wantedOwner === "string" && /^[A-Za-z0-9-]+$/u.test(wantedOwner) ? wantedOwner : void 0;
|
|
2706
3802
|
const shareTo = shareOwner === void 0 ? {} : { owner: shareOwner };
|
|
2707
3803
|
const root = ctx.appInstance.root;
|
|
2708
3804
|
if (typeof root !== "string") throw new Error("acryl-blends-frame: the app instance has no app folder (root)");
|
|
@@ -2769,6 +3865,7 @@ function apply(ctx, config = {}) {
|
|
|
2769
3865
|
...upstream === void 0 ? {} : { upstreamHead: upstream }
|
|
2770
3866
|
};
|
|
2771
3867
|
};
|
|
3868
|
+
const graph = createGraphPort(ctx, root);
|
|
2772
3869
|
const frameRows = () => {
|
|
2773
3870
|
const loader = ctx.get("loader");
|
|
2774
3871
|
if (loader === void 0) return [];
|
|
@@ -2779,9 +3876,13 @@ function apply(ctx, config = {}) {
|
|
|
2779
3876
|
fiberState: entry.fiber?.state
|
|
2780
3877
|
}));
|
|
2781
3878
|
};
|
|
3879
|
+
const githubConnect = new GithubConnect(githubConnectPorts());
|
|
3880
|
+
ctx.effect(() => () => {
|
|
3881
|
+
githubConnect.dispose();
|
|
3882
|
+
}, "acryl-blends-frame: connecting GitHub");
|
|
2782
3883
|
const plugins = {
|
|
2783
3884
|
read: async () => {
|
|
2784
|
-
const [app, frame] = await Promise.all([readAppPlugins(root, git, join(ctx.appInstance.home, "shared-plugins.json"), ghRepositoryCheck), readFramePlugins(root, frameRows())]);
|
|
3885
|
+
const [app, frame] = await Promise.all([readAppPlugins(root, git, join(ctx.appInstance.home, "shared-plugins.json"), ghRepositoryCheck, storeRootOf(ctx.appInstance.home)), readFramePlugins(root, frameRows())]);
|
|
2785
3886
|
return {
|
|
2786
3887
|
plugins: app,
|
|
2787
3888
|
frame
|
|
@@ -2793,8 +3894,47 @@ function apply(ctx, config = {}) {
|
|
|
2793
3894
|
const entry = loader === void 0 ? void 0 : [...loader.entries()].find((row) => row.options.id === APP_PLUGINS_ROW);
|
|
2794
3895
|
if (entry?.fiber !== void 0) await entry.fiber.restart();
|
|
2795
3896
|
},
|
|
2796
|
-
planShare: async (name) => planAppPluginShare(root, ctx.appInstance.home, name,
|
|
2797
|
-
|
|
3897
|
+
planShare: async (name, visibility, registry) => planAppPluginShare(root, ctx.appInstance.home, name, {
|
|
3898
|
+
...shareTo,
|
|
3899
|
+
...visibility === void 0 ? {} : { visibility },
|
|
3900
|
+
...registry === true ? { registry } : {}
|
|
3901
|
+
}, githubSharePorts()),
|
|
3902
|
+
share: async (name, confirmed) => shareAppPlugin(root, ctx.appInstance.home, name, confirmed, shareTo, githubSharePorts()),
|
|
3903
|
+
save: async (name) => saveAppPlugin(root, ctx.appInstance.home, name),
|
|
3904
|
+
github: async () => githubReadiness(() => githubSharePorts().host.login()),
|
|
3905
|
+
connect: githubConnect,
|
|
3906
|
+
registry: async () => readRegistryView(registryRead, async () => registryMarks()),
|
|
3907
|
+
pull: async (source) => {
|
|
3908
|
+
const share = pluginShareOf(() => ctx.get("pluginShare"));
|
|
3909
|
+
if (share === void 0) throw new PlainError({ say: "This app cannot add plugins: its plugin sharing part is switched off. Switch it on in Settings > Plugins, then try again." });
|
|
3910
|
+
const answer = await share.pull(source);
|
|
3911
|
+
if (!answer.ok) throw new PlainError(plainPullRefusal(answer.refusal, answer.reason, answer.next));
|
|
3912
|
+
return {
|
|
3913
|
+
name: answer.name,
|
|
3914
|
+
version: answer.version,
|
|
3915
|
+
action: answer.action,
|
|
3916
|
+
state: answer.state
|
|
3917
|
+
};
|
|
3918
|
+
}
|
|
3919
|
+
};
|
|
3920
|
+
const registryRead = createRegistryReader((input, init) => fetch(input, init));
|
|
3921
|
+
let signedIn;
|
|
3922
|
+
const registryMarks = async () => {
|
|
3923
|
+
const listed = pluginShareOf(() => ctx.get("pluginShare"))?.list() ?? [];
|
|
3924
|
+
const names = /* @__PURE__ */ new Set([...frameRows().map((row) => bareName(row.name)), ...listed.map((info) => bareName(info.name))]);
|
|
3925
|
+
const repos = new Set(listed.flatMap((info) => info.pulledFrom === void 0 ? [] : [repoOfSource(info.pulledFrom) ?? ""]).filter((repo) => repo !== ""));
|
|
3926
|
+
if (signedIn === void 0 || Date.now() - signedIn.at > 6e4) {
|
|
3927
|
+
const ready = githubReadiness(() => githubSharePorts().host.login());
|
|
3928
|
+
signedIn = {
|
|
3929
|
+
at: Date.now(),
|
|
3930
|
+
account: ready.status === "ready" ? ready.account : void 0
|
|
3931
|
+
};
|
|
3932
|
+
}
|
|
3933
|
+
return {
|
|
3934
|
+
names,
|
|
3935
|
+
repos,
|
|
3936
|
+
accounts: /* @__PURE__ */ new Set([...shareOwner === void 0 ? [] : [shareOwner.toLowerCase()], ...signedIn.account === void 0 ? [] : [signedIn.account.toLowerCase()]])
|
|
3937
|
+
};
|
|
2798
3938
|
};
|
|
2799
3939
|
const store = new SaveStore(join(ctx.appInstance.dshHome, "chat-saves.json"), report);
|
|
2800
3940
|
const versions = new Versions(git);
|
|
@@ -2831,18 +3971,13 @@ function apply(ctx, config = {}) {
|
|
|
2831
3971
|
appRoot: realpathSync(root),
|
|
2832
3972
|
report
|
|
2833
3973
|
});
|
|
2834
|
-
const
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
const stopWatchingBlend = appReload === void 0 ? () => {} : watchFolder(root, reloadApp, {
|
|
3974
|
+
const pulledDigest = pulledDigestOf(() => ctx.get("pluginShare"));
|
|
3975
|
+
const stopWatching = appReload === void 0 ? () => {} : watchAppFiles(root, () => {
|
|
3976
|
+
appReload.reload();
|
|
3977
|
+
}, {
|
|
2839
3978
|
report,
|
|
2840
|
-
|
|
3979
|
+
pulledDigest
|
|
2841
3980
|
});
|
|
2842
|
-
const stopWatching = () => {
|
|
2843
|
-
stopWatchingCode();
|
|
2844
|
-
stopWatchingBlend();
|
|
2845
|
-
};
|
|
2846
3981
|
/** git's own line as the message a person reads: the runner keeps it as the failure's detail. */
|
|
2847
3982
|
const gitReason = (cause, fallback) => {
|
|
2848
3983
|
const detail = cause.detail;
|
|
@@ -2859,6 +3994,7 @@ function apply(ctx, config = {}) {
|
|
|
2859
3994
|
ctx.logger.error(`acryl-blends-frame: request failed: ${cause instanceof Error ? cause.message : String(cause)}`);
|
|
2860
3995
|
},
|
|
2861
3996
|
plugins,
|
|
3997
|
+
graph: graph.port,
|
|
2862
3998
|
feedback: feedbackPort(() => {
|
|
2863
3999
|
return (ctx.get("loader")?.entries().find((row) => row.options.id === APP_PLUGINS_ROW))?.fiber?.ctx?.get("feedback") ?? ctx.get("feedback");
|
|
2864
4000
|
}),
|
|
@@ -2908,6 +4044,7 @@ function apply(ctx, config = {}) {
|
|
|
2908
4044
|
ctx.routerHttp.mount(FRAME_API_PREFIX);
|
|
2909
4045
|
const stopSnapshot = ctx.reflect.provide("appSnapshot", snapshot);
|
|
2910
4046
|
return () => {
|
|
4047
|
+
graph.stop();
|
|
2911
4048
|
stopSnapshot();
|
|
2912
4049
|
stopListening();
|
|
2913
4050
|
stopGuard();
|