topotopo 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +10 -10
- package/README.md +460 -117
- package/dist/cli.js +323 -146
- package/dist/config.d.ts +48 -0
- package/dist/config.js +170 -0
- package/dist/core/bake.d.ts +14 -3
- package/dist/core/bake.js +153 -4
- package/dist/core/bevel.d.ts +3 -1
- package/dist/core/bevel.js +76 -3
- package/dist/core/decimate.d.ts +65 -0
- package/dist/core/decimate.js +409 -3
- package/dist/core/erosion.d.ts +37 -0
- package/dist/core/erosion.js +41 -1
- package/dist/core/geometry.d.ts +5 -1
- package/dist/core/geometry.js +203 -3
- package/dist/core/heightfield.d.ts +27 -4
- package/dist/core/heightfield.js +54 -16
- package/dist/core/intrinsics.d.ts +7 -0
- package/dist/core/intrinsics.js +94 -0
- package/dist/core/manifold.d.ts +12 -1
- package/dist/core/manifold.js +78 -6
- package/dist/core/materials.d.ts +7 -0
- package/dist/core/materials.js +45 -0
- package/dist/core/noise.d.ts +9 -0
- package/dist/core/noise.js +10 -1
- package/dist/core/polymerge.d.ts +21 -0
- package/dist/core/polymerge.js +327 -0
- package/dist/core/repair.d.ts +2 -0
- package/dist/core/repair.js +2 -0
- package/dist/core/report.d.ts +70 -0
- package/dist/core/report.js +275 -0
- package/dist/core/scene.js +2 -1
- package/dist/core/sha256.d.ts +4 -0
- package/dist/core/sha256.js +69 -0
- package/dist/core/shapes.d.ts +3 -0
- package/dist/core/shapes.js +34 -0
- package/dist/core/snippet-lib.d.ts +14 -1
- package/dist/core/snippet-lib.js +39 -13
- package/dist/core/tangents.d.ts +2 -0
- package/dist/core/tangents.js +5 -1
- package/dist/core/texnoise.d.ts +6 -1
- package/dist/core/texnoise.js +6 -2
- package/dist/core/texsdf.d.ts +131 -0
- package/dist/core/texsdf.js +637 -0
- package/dist/core/texture.d.ts +163 -9
- package/dist/core/texture.js +844 -20
- package/dist/core/topology.d.ts +10 -0
- package/dist/core/topology.js +168 -0
- package/dist/core/unwrap.d.ts +72 -0
- package/dist/core/unwrap.js +800 -0
- package/dist/core/usercode.d.ts +27 -0
- package/dist/core/usercode.js +87 -0
- package/dist/core/uv.d.ts +4 -18
- package/dist/core/uv.js +35 -188
- package/dist/core/uvislands.d.ts +62 -0
- package/dist/core/uvislands.js +574 -0
- package/dist/core/uvoverlap.d.ts +58 -0
- package/dist/core/uvoverlap.js +220 -0
- package/dist/core/uvpack.d.ts +64 -0
- package/dist/core/uvpack.js +628 -0
- package/dist/core/uvreport.d.ts +61 -0
- package/dist/core/uvreport.js +207 -143
- package/dist/core/wrangle.d.ts +6 -1
- package/dist/core/wrangle.js +27 -16
- package/dist/core/xatlas.d.ts +16 -1
- package/dist/core/xatlas.js +66 -62
- package/dist/engines.d.ts +63 -0
- package/dist/engines.js +180 -0
- package/dist/explain.d.ts +4 -3
- package/dist/explain.js +129 -12
- package/dist/graph/engine.d.ts +92 -29
- package/dist/graph/engine.js +298 -148
- package/dist/graph/expr.d.ts +6 -3
- package/dist/graph/expr.js +18 -8
- package/dist/graph/outputs.d.ts +23 -0
- package/dist/graph/outputs.js +41 -0
- package/dist/graph/packages.d.ts +72 -0
- package/dist/graph/packages.js +261 -0
- package/dist/graph/parallel.d.ts +3 -1
- package/dist/graph/parallel.js +4 -1
- package/dist/graph/params.d.ts +6 -0
- package/dist/graph/params.js +34 -7
- package/dist/graph/scope.d.ts +55 -0
- package/dist/graph/scope.js +14 -0
- package/dist/graph/types.d.ts +75 -12
- package/dist/graph/types.js +4 -2
- package/dist/graph/vars.d.ts +16 -0
- package/dist/graph/vars.js +80 -0
- package/dist/host/artifacts.d.ts +10 -1
- package/dist/host/artifacts.js +37 -6
- package/dist/host/cook.d.ts +241 -0
- package/dist/host/cook.js +844 -0
- package/dist/host/export-targets.d.ts +42 -5
- package/dist/host/export-targets.js +72 -13
- package/dist/host/godot-commands.d.ts +4 -0
- package/dist/host/godot-commands.js +21 -0
- package/dist/host/godot.d.ts +183 -0
- package/dist/host/godot.js +916 -0
- package/dist/host/node.d.ts +12 -7
- package/dist/host/node.js +27 -9
- package/dist/host/pool.js +51 -2
- package/dist/host/sidecars.d.ts +8 -0
- package/dist/host/sidecars.js +50 -0
- package/dist/host/types.d.ts +19 -6
- package/dist/host/version.d.ts +1 -0
- package/dist/host/version.js +3 -0
- package/dist/host/worker.js +5 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/inspect.d.ts +80 -2
- package/dist/inspect.js +265 -30
- package/dist/io/common.d.ts +25 -5
- package/dist/io/export.js +2 -1
- package/dist/io/files.d.ts +23 -4
- package/dist/io/files.js +60 -8
- package/dist/io/gltf.js +134 -47
- package/dist/io/heightmap.d.ts +3 -0
- package/dist/io/heightmap.js +1 -0
- package/dist/io/import.js +10 -0
- package/dist/io/inspect-scene.d.ts +55 -0
- package/dist/io/inspect-scene.js +155 -0
- package/dist/io/kinds.d.ts +81 -0
- package/dist/io/kinds.js +163 -0
- package/dist/io/points.d.ts +2 -1
- package/dist/io/points.js +3 -1
- package/dist/io/sidecars.d.ts +24 -0
- package/dist/io/sidecars.js +57 -0
- package/dist/io/source.d.ts +65 -0
- package/dist/io/source.js +112 -0
- package/dist/io/targets.d.ts +47 -3
- package/dist/io/targets.js +148 -13
- package/dist/io/textures.js +21 -1
- package/dist/io/unreal.js +5 -1
- package/dist/mcp/docs.d.ts +10 -1
- package/dist/mcp/docs.js +786 -143
- package/dist/mcp/inputs.d.ts +12 -5
- package/dist/mcp/inputs.js +50 -13
- package/dist/mcp/server.js +56 -27
- package/dist/mcp/tools.d.ts +14 -7
- package/dist/mcp/tools.js +196 -103
- package/dist/mcp/verify.d.ts +2 -2
- package/dist/mcp/verify.js +1 -1
- package/dist/mcp/workspace.d.ts +15 -0
- package/dist/mcp/workspace.js +349 -0
- package/dist/nodes/attributes.js +198 -1
- package/dist/nodes/bake.js +151 -47
- package/dist/nodes/beam.d.ts +1 -0
- package/dist/nodes/beam.js +201 -0
- package/dist/nodes/bevel.js +51 -3
- package/dist/nodes/cloth.js +2 -0
- package/dist/nodes/code.js +21 -9
- package/dist/nodes/curves.js +433 -93
- package/dist/nodes/flow.js +53 -33
- package/dist/nodes/generate.js +12 -38
- package/dist/nodes/heightfield.js +248 -77
- package/dist/nodes/imageops.js +16 -19
- package/dist/nodes/index.d.ts +1 -0
- package/dist/nodes/index.js +1 -0
- package/dist/nodes/material.js +89 -24
- package/dist/nodes/measure.js +43 -17
- package/dist/nodes/particles.js +2 -0
- package/dist/nodes/pcg.js +6 -5
- package/dist/nodes/procedural.js +4 -3
- package/dist/nodes/remesh.js +71 -15
- package/dist/nodes/scene.js +4 -2
- package/dist/nodes/sdf.js +10 -5
- package/dist/nodes/solid.js +20 -5
- package/dist/nodes/splines.d.ts +15 -1
- package/dist/nodes/splines.js +112 -105
- package/dist/nodes/texture.d.ts +21 -6
- package/dist/nodes/texture.js +1318 -185
- package/dist/nodes/topology.d.ts +7 -0
- package/dist/nodes/topology.js +133 -6
- package/dist/nodes/transform.js +6 -2
- package/dist/nodes/uv.js +192 -174
- package/dist/nodes/wasmcall.js +5 -1
- package/dist/open.d.ts +19 -0
- package/dist/open.js +12 -0
- package/dist/package.d.ts +288 -0
- package/dist/package.js +772 -0
- package/dist/packages/check.d.ts +32 -0
- package/dist/packages/check.js +169 -0
- package/dist/packages/commands.d.ts +57 -0
- package/dist/packages/commands.js +537 -0
- package/dist/packages/registry.d.ts +796 -0
- package/dist/packages/registry.js +2998 -0
- package/dist/packages/update.d.ts +124 -0
- package/dist/packages/update.js +476 -0
- package/dist/render/options.d.ts +2 -0
- package/dist/render/options.js +44 -5
- package/dist/render/preview.d.ts +15 -4
- package/dist/render/preview.js +6 -4
- package/dist/render/raster.d.ts +66 -5
- package/dist/render/raster.js +506 -138
- package/dist/render/texview.d.ts +6 -0
- package/dist/render/texview.js +27 -0
- package/dist/scan.d.ts +61 -0
- package/dist/scan.js +228 -0
- package/dist/session.d.ts +311 -0
- package/dist/session.js +849 -0
- package/dist/viewer/app/api.js +48 -6
- package/dist/viewer/app/cooking.js +78 -0
- package/dist/viewer/app/embed.js +27 -7
- package/dist/viewer/app/export-popover.js +118 -16
- package/dist/viewer/app/grid.js +5 -1
- package/dist/viewer/app/guides.js +122 -0
- package/dist/viewer/app/keymap.js +6 -3
- package/dist/viewer/app/layers.js +4 -4
- package/dist/viewer/app/main.js +185 -52
- package/dist/viewer/app/markers.js +60 -0
- package/dist/viewer/app/outliner.js +72 -27
- package/dist/viewer/app/output-info.js +45 -0
- package/dist/viewer/app/outputs.css +147 -8
- package/dist/viewer/app/outputs.js +168 -54
- package/dist/viewer/app/packages.js +614 -0
- package/dist/viewer/app/props.js +176 -52
- package/dist/viewer/app/scene.js +30 -5
- package/dist/viewer/app/select.js +4 -3
- package/dist/viewer/app/status.js +12 -15
- package/dist/viewer/app/style.css +65 -7
- package/dist/viewer/app/vars.js +47 -0
- package/dist/viewer/app/welcome.js +72 -0
- package/dist/viewer/graph-file.d.ts +62 -0
- package/dist/viewer/graph-file.js +175 -0
- package/dist/viewer/index.html +23 -10
- package/dist/viewer/json-edit.d.ts +13 -0
- package/dist/viewer/json-edit.js +274 -0
- package/dist/viewer/outputs.d.ts +49 -5
- package/dist/viewer/outputs.js +264 -56
- package/dist/viewer/packages.d.ts +121 -0
- package/dist/viewer/packages.js +584 -0
- package/dist/viewer/server.d.ts +14 -9
- package/dist/viewer/server.js +266 -28
- package/dist/web/app/web.css +419 -0
- package/dist/web/app/web.js +188 -0
- package/dist/{gallery → web}/bake.d.ts +4 -4
- package/dist/{gallery → web}/bake.js +24 -15
- package/dist/web/folder-page.d.ts +101 -0
- package/dist/web/folder-page.js +146 -0
- package/dist/web/folders.d.ts +280 -0
- package/dist/web/folders.js +970 -0
- package/dist/web/library.d.ts +91 -0
- package/dist/web/library.js +228 -0
- package/dist/web/markdown.d.ts +45 -0
- package/dist/web/markdown.js +677 -0
- package/dist/web/pages.d.ts +258 -0
- package/dist/web/pages.js +301 -0
- package/dist/web/server.d.ts +24 -0
- package/dist/web/server.js +1068 -0
- package/dist/workspace.d.ts +88 -118
- package/dist/workspace.js +358 -460
- package/integrations/godot/addons/topotopo/LICENSE +202 -0
- package/integrations/godot/addons/topotopo/README.md +57 -0
- package/integrations/godot/addons/topotopo/context_menu.gd +43 -0
- package/integrations/godot/addons/topotopo/dock.gd +266 -0
- package/integrations/godot/addons/topotopo/dock.tscn +72 -0
- package/integrations/godot/addons/topotopo/export_filter.gd +72 -0
- package/integrations/godot/addons/topotopo/gltf_instancing.gd +131 -0
- package/integrations/godot/addons/topotopo/icons/topotopo.svg +1 -0
- package/integrations/godot/addons/topotopo/inspector.gd +38 -0
- package/integrations/godot/addons/topotopo/instances_post_import.gd +111 -0
- package/integrations/godot/addons/topotopo/package.gd +23 -0
- package/integrations/godot/addons/topotopo/package_importer.gd +187 -0
- package/integrations/godot/addons/topotopo/plugin.cfg +7 -0
- package/integrations/godot/addons/topotopo/plugin.gd +970 -0
- package/integrations/godot/addons/topotopo/record.gd +435 -0
- package/integrations/godot/addons/topotopo/runner.gd +458 -0
- package/integrations/godot/addons/topotopo/tools/check.gd +42 -0
- package/integrations/godot/addons/topotopo/watcher.gd +199 -0
- package/kernels/pkg/README.md +2 -1
- package/kernels/pkg-simd/README.md +2 -1
- package/library/architecture/versions/1.0/architecture.topo +12 -0
- package/{shelf → library/architecture/versions/1.0/graphs}/arch.json +3 -3
- package/{shelf → library/architecture/versions/1.0/graphs}/beam_truss.json +6 -6
- package/{shelf → library/architecture/versions/1.0/graphs}/brick_wall.json +8 -8
- package/{shelf → library/architecture/versions/1.0/graphs}/door_frame.json +3 -3
- package/{shelf → library/architecture/versions/1.0/graphs}/hex_grid_floor.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/house.json +8 -8
- package/library/architecture/versions/1.0/graphs/lineup.json +42 -0
- package/{shelf → library/architecture/versions/1.0/graphs}/pillar.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/railing.json +5 -5
- package/{shelf → library/architecture/versions/1.0/graphs}/roof_gable.json +2 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/roof_hip.json +2 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/roof_skeleton.json +2 -1
- package/{shelf → library/architecture/versions/1.0/graphs}/scaffold.json +4 -4
- package/{shelf → library/architecture/versions/1.0/graphs}/spiral_stairs.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/stairs.json +6 -6
- package/{shelf → library/architecture/versions/1.0/graphs}/tile_floor.json +3 -2
- package/{shelf → library/architecture/versions/1.0/graphs}/wall_segment.json +10 -10
- package/{shelf → library/architecture/versions/1.0/graphs}/window_frame.json +2 -2
- package/library/architecture/versions/1.0/version.json +1119 -0
- package/{shelf → library/nature/versions/1.0/graphs}/bush.json +2 -2
- package/{shelf → library/nature/versions/1.0/graphs}/cliff_block.json +4 -3
- package/{shelf → library/nature/versions/1.0/graphs}/forest_patch.json +5 -5
- package/{shelf → library/nature/versions/1.0/graphs}/grass_clumps.json +2 -2
- package/library/nature/versions/1.0/graphs/lineup.json +24 -0
- package/{shelf → library/nature/versions/1.0/graphs}/log.json +6 -6
- package/{shelf → library/nature/versions/1.0/graphs}/rock.json +5 -4
- package/{shelf → library/nature/versions/1.0/graphs}/scatter_rocks.json +5 -5
- package/{shelf → library/nature/versions/1.0/graphs}/tree_lowpoly.json +3 -2
- package/library/nature/versions/1.0/nature.topo +12 -0
- package/library/nature/versions/1.0/version.json +534 -0
- package/{shelf → library/props/versions/1.0/graphs}/barrel.json +5 -4
- package/{shelf → library/props/versions/1.0/graphs}/crate.json +3 -3
- package/library/props/versions/1.0/graphs/lineup.json +18 -0
- package/{shelf → library/props/versions/1.0/graphs}/pallet.json +4 -3
- package/{shelf → library/props/versions/1.0/graphs}/pipe_run.json +3 -3
- package/{shelf → library/props/versions/1.0/graphs}/sandbag_wall.json +5 -5
- package/library/props/versions/1.0/props.topo +12 -0
- package/library/props/versions/1.0/version.json +336 -0
- package/{shelf → library/street/versions/1.0/graphs}/bench.json +5 -5
- package/{shelf → library/street/versions/1.0/graphs}/bollard.json +2 -1
- package/{shelf → library/street/versions/1.0/graphs}/cable.json +4 -4
- package/{shelf → library/street/versions/1.0/graphs}/fence.json +7 -7
- package/{shelf → library/street/versions/1.0/graphs}/lamp_post.json +2 -2
- package/library/street/versions/1.0/graphs/lineup.json +22 -0
- package/{shelf → library/street/versions/1.0/graphs}/road_strip.json +2 -2
- package/{shelf → library/street/versions/1.0/graphs}/sign_post.json +1 -1
- package/library/street/versions/1.0/street.topo +12 -0
- package/library/street/versions/1.0/version.json +421 -0
- package/package.json +17 -8
- package/dist/gallery/app/gallery.css +0 -100
- package/dist/gallery/app/gallery.js +0 -80
- package/dist/gallery/cli.d.ts +0 -4
- package/dist/gallery/cli.js +0 -157
- package/dist/gallery/library.d.ts +0 -124
- package/dist/gallery/library.js +0 -366
- package/dist/gallery/pages.d.ts +0 -94
- package/dist/gallery/pages.js +0 -148
- package/dist/gallery/server.d.ts +0 -21
- package/dist/gallery/server.js +0 -391
- package/dist/graph/migrate.d.ts +0 -23
- package/dist/graph/migrate.js +0 -58
- package/dist/mcp/library.d.ts +0 -12
- package/dist/mcp/library.js +0 -159
- package/shelf/index.json +0 -190
package/dist/gallery/server.js
DELETED
|
@@ -1,391 +0,0 @@
|
|
|
1
|
-
// The gallery web server: every library's projects as a grid, a page per project with its 3D
|
|
2
|
-
// preview, and stable URLs a board pastes (link, thumbnail, iframe embed). It reads files and serves baked previews;
|
|
3
|
-
// it never cooks: bakes run in child processes (BakeQueue) and "Open in editor" starts a `topotopo edit` child.
|
|
4
|
-
// Bound to 127.0.0.1 on a fixed port (stable links); requests must name this server as their Host, and writes must
|
|
5
|
-
// come from its own pages.
|
|
6
|
-
import http from 'node:http';
|
|
7
|
-
import fs from 'node:fs';
|
|
8
|
-
import path from 'node:path';
|
|
9
|
-
import zlib from 'node:zlib';
|
|
10
|
-
import { spawn } from 'node:child_process';
|
|
11
|
-
import { fileURLToPath } from 'node:url';
|
|
12
|
-
import { encodePNG } from '../render/png.js';
|
|
13
|
-
import { MIME, appDir, threeDir } from '../viewer/server.js';
|
|
14
|
-
import { galleryDir, graphNames, isStale, listFiles, loadConfig, readBakeState, readGraph, scanLibrary } from './library.js';
|
|
15
|
-
import { BakeQueue } from './bake.js';
|
|
16
|
-
import { embedPage, galleryPage, projectPage } from './pages.js';
|
|
17
|
-
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
18
|
-
const assetDir = path.join(here, 'app');
|
|
19
|
-
const cli = path.resolve(here, '..', import.meta.url.endsWith('.ts') ? 'cli.ts' : 'cli.js');
|
|
20
|
-
const RESCAN_MS = 5 * 60_000;
|
|
21
|
-
const EDITOR_START_MS = 60_000;
|
|
22
|
-
const FILE_TYPES = {
|
|
23
|
-
'.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.webp': 'image/webp', '.gif': 'image/gif', '.avif': 'image/avif',
|
|
24
|
-
'.pdf': 'application/pdf', '.txt': 'text/plain; charset=utf-8', '.md': 'text/plain; charset=utf-8', '.json': 'application/json',
|
|
25
|
-
'.glb': 'model/gltf-binary', '.csv': 'text/csv; charset=utf-8',
|
|
26
|
-
};
|
|
27
|
-
/** shown inline; everything else (SVG and HTML among them: they run scripts) is a download */
|
|
28
|
-
const INLINE = new Set(['.png', '.jpg', '.jpeg', '.webp', '.gif', '.avif', '.pdf', '.txt', '.md', '.json', '.csv']);
|
|
29
|
-
class HttpError extends Error {
|
|
30
|
-
status;
|
|
31
|
-
constructor(status, msg) { super(msg); this.status = status; }
|
|
32
|
-
}
|
|
33
|
-
/** A gallery answers here already: its URL, or null. */
|
|
34
|
-
export async function runningGallery(port) {
|
|
35
|
-
try {
|
|
36
|
-
const res = await fetch(`http://127.0.0.1:${port}/api/health`, { signal: AbortSignal.timeout(1500) });
|
|
37
|
-
const j = await res.json();
|
|
38
|
-
return j?.gallery ? `http://127.0.0.1:${port}/` : null;
|
|
39
|
-
}
|
|
40
|
-
catch {
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
export async function startGallery(o = {}) {
|
|
45
|
-
const cfg = o.config ?? loadConfig();
|
|
46
|
-
const log = o.log ?? (() => { });
|
|
47
|
-
const bakeOn = o.bake ?? true;
|
|
48
|
-
let base = '';
|
|
49
|
-
// ---------------------------------------------------------------- libraries
|
|
50
|
-
let scans = [];
|
|
51
|
-
const projects = new Map();
|
|
52
|
-
const keyOf = (p) => `${p.lib}/${p.id}`;
|
|
53
|
-
const rescan = () => {
|
|
54
|
-
scans = cfg.libraries.map(scanLibrary);
|
|
55
|
-
projects.clear();
|
|
56
|
-
for (const s of scans)
|
|
57
|
-
for (const p of s.projects)
|
|
58
|
-
projects.set(keyOf(p), p);
|
|
59
|
-
};
|
|
60
|
-
const find = (lib, id) => {
|
|
61
|
-
let p = projects.get(`${lib}/${id}`);
|
|
62
|
-
if (!p) {
|
|
63
|
-
rescan();
|
|
64
|
-
p = projects.get(`${lib}/${id}`);
|
|
65
|
-
} // made a moment ago, before the watch noticed
|
|
66
|
-
if (!p)
|
|
67
|
-
throw new HttpError(404, `no project "${id}" in library "${lib}"`);
|
|
68
|
-
return p;
|
|
69
|
-
};
|
|
70
|
-
// ---------------------------------------------------------------- events (SSE) and bakes
|
|
71
|
-
const clients = new Set();
|
|
72
|
-
const broadcast = (ev) => { const m = `data: ${JSON.stringify(ev)}\n\n`; for (const c of clients)
|
|
73
|
-
c.write(m); };
|
|
74
|
-
const queue = new BakeQueue({
|
|
75
|
-
log,
|
|
76
|
-
onDone: (dir, state) => {
|
|
77
|
-
const p = [...projects.values()].find((x) => x.dir === dir);
|
|
78
|
-
if (p)
|
|
79
|
-
broadcast({ project: keyOf(p), ok: state?.ok ?? false, version: state?.at ?? 0 });
|
|
80
|
-
},
|
|
81
|
-
});
|
|
82
|
-
const bakeIfStale = (p) => {
|
|
83
|
-
if (!bakeOn || queue.pending(p.dir))
|
|
84
|
-
return;
|
|
85
|
-
try {
|
|
86
|
-
if (isStale(p) && queue.add(p.dir))
|
|
87
|
-
broadcast({ project: keyOf(p), baking: true });
|
|
88
|
-
}
|
|
89
|
-
catch { /* removed meanwhile */ }
|
|
90
|
-
};
|
|
91
|
-
rescan();
|
|
92
|
-
for (const p of projects.values())
|
|
93
|
-
bakeIfStale(p);
|
|
94
|
-
// A change below a library: project.json, graphs/, assets/ and inputs/ may need a bake; anything else may be a
|
|
95
|
-
// project made, moved or removed, or attachments: read the libraries again and tell the pages.
|
|
96
|
-
const touched = new Set();
|
|
97
|
-
let changeTimer;
|
|
98
|
-
const onChange = () => {
|
|
99
|
-
clearTimeout(changeTimer);
|
|
100
|
-
changeTimer = setTimeout(() => {
|
|
101
|
-
const before = [...projects.keys()].join('\n');
|
|
102
|
-
rescan();
|
|
103
|
-
for (const k of touched) {
|
|
104
|
-
const p = projects.get(k);
|
|
105
|
-
if (p)
|
|
106
|
-
bakeIfStale(p);
|
|
107
|
-
}
|
|
108
|
-
for (const p of projects.values())
|
|
109
|
-
if (!readBakeState(p.dir))
|
|
110
|
-
bakeIfStale(p);
|
|
111
|
-
broadcast({ changed: [...touched], list: before !== [...projects.keys()].join('\n') });
|
|
112
|
-
touched.clear();
|
|
113
|
-
}, 500);
|
|
114
|
-
};
|
|
115
|
-
const watchers = [];
|
|
116
|
-
for (const s of scans) {
|
|
117
|
-
try {
|
|
118
|
-
const w = fs.watch(s.root, { recursive: true }, (_ev, f) => {
|
|
119
|
-
const rel = f ? String(f).split(path.sep).join('/') : '';
|
|
120
|
-
if (path.isAbsolute(rel))
|
|
121
|
-
return; // the root itself went away: the periodic rescan notices
|
|
122
|
-
const parts = rel.split('/');
|
|
123
|
-
if (parts.some((x) => x.startsWith('.') || x === 'node_modules'))
|
|
124
|
-
return; // .gallery/, .cache/, .part files
|
|
125
|
-
const p = scans.find((x) => x.root === s.root)?.projects.find((x) => rel === x.rel || rel.startsWith(x.rel + '/'));
|
|
126
|
-
if (p)
|
|
127
|
-
touched.add(keyOf(p));
|
|
128
|
-
onChange();
|
|
129
|
-
});
|
|
130
|
-
w.on('error', () => w.close());
|
|
131
|
-
w.unref();
|
|
132
|
-
watchers.push(w);
|
|
133
|
-
}
|
|
134
|
-
catch { /* no folder (a problem on the page) or no recursive watch: the periodic rescan covers it */ }
|
|
135
|
-
}
|
|
136
|
-
const periodic = setInterval(() => { rescan(); for (const p of projects.values())
|
|
137
|
-
bakeIfStale(p); }, RESCAN_MS);
|
|
138
|
-
periodic.unref();
|
|
139
|
-
// ---------------------------------------------------------------- editors
|
|
140
|
-
const editors = new Map();
|
|
141
|
-
const editorFor = (p) => {
|
|
142
|
-
const have = editors.get(p.dir);
|
|
143
|
-
if (have && have.child.exitCode === null)
|
|
144
|
-
return have.url;
|
|
145
|
-
const child = spawn(process.execPath, [cli, 'edit', p.dir, '--json'], { stdio: ['ignore', 'pipe', 'pipe'], windowsHide: true });
|
|
146
|
-
const url = new Promise((resolve, reject) => {
|
|
147
|
-
let out = '', err = '';
|
|
148
|
-
const t = setTimeout(() => { child.kill(); reject(new Error('the editor did not start within a minute')); }, EDITOR_START_MS);
|
|
149
|
-
child.stdout.on('data', (d) => {
|
|
150
|
-
out += d;
|
|
151
|
-
const line = out.split('\n').find((l) => l.startsWith('{'));
|
|
152
|
-
if (line) {
|
|
153
|
-
clearTimeout(t);
|
|
154
|
-
try {
|
|
155
|
-
resolve(JSON.parse(line).url);
|
|
156
|
-
}
|
|
157
|
-
catch (e) {
|
|
158
|
-
reject(e);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
child.stderr.on('data', (d) => { if (err.length < 4000)
|
|
163
|
-
err += d; });
|
|
164
|
-
child.on('exit', (code) => { clearTimeout(t); editors.delete(p.dir); reject(new Error(`the editor stopped (exit ${code}): ${err.trim().split('\n').slice(-3).join(' ')}`)); });
|
|
165
|
-
});
|
|
166
|
-
url.catch(() => { });
|
|
167
|
-
editors.set(p.dir, { child, url });
|
|
168
|
-
return url;
|
|
169
|
-
};
|
|
170
|
-
const reveal = (dir) => {
|
|
171
|
-
const [cmd, args] = process.platform === 'win32' ? ['explorer.exe', [dir]] : process.platform === 'darwin' ? ['open', [dir]] : ['xdg-open', [dir]];
|
|
172
|
-
const c = spawn(cmd, args, { detached: true, stdio: 'ignore', windowsHide: false });
|
|
173
|
-
c.on('error', () => { });
|
|
174
|
-
c.unref();
|
|
175
|
-
};
|
|
176
|
-
// ---------------------------------------------------------------- page data
|
|
177
|
-
const enc = encodeURIComponent;
|
|
178
|
-
const urls = {
|
|
179
|
-
home: '/',
|
|
180
|
-
project: (lib, id) => `/p/${enc(lib)}/${enc(id)}`,
|
|
181
|
-
thumb: (lib, id, v) => `/p/${enc(lib)}/${enc(id)}/thumb.png${v ? `?v=${enc(String(v))}` : ''}`,
|
|
182
|
-
glb: (lib, id, v) => `/p/${enc(lib)}/${enc(id)}/preview.glb${v ? `?v=${enc(String(v))}` : ''}`,
|
|
183
|
-
embed: (lib, id) => `/embed/${enc(lib)}/${enc(id)}`,
|
|
184
|
-
file: (lib, id, rel) => `/p/${enc(lib)}/${enc(id)}/files/${rel.split('/').map(enc).join('/')}`,
|
|
185
|
-
edit: (lib, id) => `/p/${enc(lib)}/${enc(id)}/edit`,
|
|
186
|
-
asset: (name) => `/static/${name}`,
|
|
187
|
-
app: (name) => `/app/${name}`,
|
|
188
|
-
three: '/vendor/three/',
|
|
189
|
-
};
|
|
190
|
-
const status = (p, s) => (queue.pending(p.dir) ? 'baking' : !s ? 'new' : s.ok ? 'ok' : 'failed');
|
|
191
|
-
const cardOf = (p) => {
|
|
192
|
-
const s = readBakeState(p.dir);
|
|
193
|
-
const m = p.manifest;
|
|
194
|
-
return { lib: p.lib, id: p.id, title: m.title, collection: p.collection, tags: m.tags, description: m.description, updated: p.updated, status: status(p, s), version: String(s?.at ?? 0) };
|
|
195
|
-
};
|
|
196
|
-
const projectData = (p) => {
|
|
197
|
-
const s = readBakeState(p.dir);
|
|
198
|
-
const graphs = graphNames(p.dir).map((name) => {
|
|
199
|
-
const g = readGraph(p.dir, name) ?? {};
|
|
200
|
-
const meta = g.params ?? {};
|
|
201
|
-
return {
|
|
202
|
-
name, nodes: Object.keys(g.nodes ?? {}).length,
|
|
203
|
-
vars: Object.entries(g.vars ?? {}).map(([k, v]) => ({ name: k, value: v, desc: meta[k]?.desc ?? meta[k]?.label, min: meta[k]?.min, max: meta[k]?.max })),
|
|
204
|
-
};
|
|
205
|
-
});
|
|
206
|
-
const abs = (u) => base + u;
|
|
207
|
-
return {
|
|
208
|
-
...cardOf(p), rel: p.rel, links: p.manifest.links, graph: p.manifest.graph, graphs, created: p.manifest.created,
|
|
209
|
-
attachments: listFiles(path.join(p.dir, 'attachments')), exports: listFiles(path.join(p.dir, 'exports')),
|
|
210
|
-
glb: fs.existsSync(path.join(galleryDir(p.dir), 'preview.glb')), bake: s,
|
|
211
|
-
share: { link: abs(urls.project(p.lib, p.id)), thumb: abs(urls.thumb(p.lib, p.id)), embed: abs(urls.embed(p.lib, p.id)) },
|
|
212
|
-
};
|
|
213
|
-
};
|
|
214
|
-
const listJSON = () => [...projects.values()].map((p) => {
|
|
215
|
-
const c = cardOf(p);
|
|
216
|
-
return {
|
|
217
|
-
lib: p.lib, id: p.id, title: c.title, description: c.description, tags: c.tags, collection: c.collection, updated: p.updated, status: c.status,
|
|
218
|
-
url: base + urls.project(p.lib, p.id), thumb: base + urls.thumb(p.lib, p.id, c.version), embed: base + urls.embed(p.lib, p.id),
|
|
219
|
-
};
|
|
220
|
-
});
|
|
221
|
-
let placeholder = null;
|
|
222
|
-
const placeholderPNG = () => {
|
|
223
|
-
if (!placeholder) {
|
|
224
|
-
const n = 64, rgba = new Uint8Array(n * n * 4);
|
|
225
|
-
for (let i = 0; i < n * n; i++)
|
|
226
|
-
rgba.set([0x2b, 0x2d, 0x31, 0xff], i * 4);
|
|
227
|
-
placeholder = encodePNG(rgba, n, n, (d) => zlib.deflateSync(d));
|
|
228
|
-
}
|
|
229
|
-
return placeholder;
|
|
230
|
-
};
|
|
231
|
-
/** `files/<rest>` of a project: below attachments/ or exports/, never outside them (links included). */
|
|
232
|
-
const projectFile = (p, rest) => {
|
|
233
|
-
const parts = rest.split('/').map((x) => decodeURIComponent(x));
|
|
234
|
-
// decoded segments: "%2F" and "%5C" must not smuggle separators (and so "..") past the split
|
|
235
|
-
if (!['attachments', 'exports'].includes(parts[0]) || parts.length < 2 || parts.some((x) => !x || x === '.' || x === '..' || /[\\/\0]/.test(x)))
|
|
236
|
-
throw new HttpError(400, 'files/ serves attachments/<path> and exports/<path>');
|
|
237
|
-
const root = path.join(p.dir, parts[0]);
|
|
238
|
-
const abs = path.join(root, ...parts.slice(1));
|
|
239
|
-
if (!abs.startsWith(root + path.sep))
|
|
240
|
-
throw new HttpError(400, 'the path leaves the project');
|
|
241
|
-
if (!fs.statSync(abs, { throwIfNoEntry: false })?.isFile())
|
|
242
|
-
throw new HttpError(404, `no file ${parts.join('/')}`);
|
|
243
|
-
const real = path.relative(fs.realpathSync(root), fs.realpathSync(abs));
|
|
244
|
-
if (real.startsWith('..') || path.isAbsolute(real))
|
|
245
|
-
throw new HttpError(400, 'the path leaves the project');
|
|
246
|
-
return abs;
|
|
247
|
-
};
|
|
248
|
-
const readStatic = (dir, rel) => {
|
|
249
|
-
const p = path.resolve(dir, decodeURIComponent(rel));
|
|
250
|
-
if (!p.startsWith(dir + path.sep) || !fs.statSync(p, { throwIfNoEntry: false })?.isFile())
|
|
251
|
-
return null;
|
|
252
|
-
return p;
|
|
253
|
-
};
|
|
254
|
-
// ---------------------------------------------------------------- routes
|
|
255
|
-
const server = http.createServer(async (req, res) => {
|
|
256
|
-
const head = (code, type, extra = {}) => res.writeHead(code, { 'content-type': type, 'cache-control': 'no-store', 'x-content-type-options': 'nosniff', ...extra });
|
|
257
|
-
const send = (code, body, type = 'text/plain; charset=utf-8', extra = {}) => { head(code, type, extra); res.end(req.method === 'HEAD' ? undefined : body); };
|
|
258
|
-
// pages may only be framed by the gallery itself; the embed page is made to be framed by a board
|
|
259
|
-
const html = (body, framable = false) => send(200, body, MIME['.html'], framable ? {} : { 'content-security-policy': "frame-ancestors 'self'" });
|
|
260
|
-
const json = (code, body, extra = {}) => send(code, JSON.stringify(body), MIME['.json'], extra);
|
|
261
|
-
try {
|
|
262
|
-
const host = String(req.headers.host ?? '');
|
|
263
|
-
if (!/^(127\.0\.0\.1|localhost|\[::1\])(:\d+)?$/i.test(host))
|
|
264
|
-
return json(403, { error: `unexpected Host "${host}"` });
|
|
265
|
-
const origin = req.headers.origin;
|
|
266
|
-
const write = req.method !== 'GET' && req.method !== 'HEAD';
|
|
267
|
-
if (write && origin !== undefined && origin !== `http://${host}`)
|
|
268
|
-
return json(403, { error: `cross-origin ${req.method} refused (${origin})` });
|
|
269
|
-
const u = new URL(req.url ?? '/', 'http://localhost');
|
|
270
|
-
const seg = u.pathname.split('/').slice(1);
|
|
271
|
-
if (u.pathname === '/' || u.pathname === '/index.html') {
|
|
272
|
-
return html(galleryPage({ libraries: scans.map((s) => ({ name: s.name, cards: s.projects.map(cardOf) })), problems: scans.flatMap((s) => s.problems) }, urls));
|
|
273
|
-
}
|
|
274
|
-
if (seg[0] === 'static') {
|
|
275
|
-
const p = readStatic(assetDir, seg.slice(1).join('/'));
|
|
276
|
-
return p ? send(200, fs.readFileSync(p), MIME[path.extname(p)] ?? 'application/octet-stream') : send(404, 'not found');
|
|
277
|
-
}
|
|
278
|
-
if (seg[0] === 'app') {
|
|
279
|
-
const p = readStatic(appDir, seg.slice(1).join('/'));
|
|
280
|
-
return p ? send(200, fs.readFileSync(p), MIME[path.extname(p)] ?? 'application/octet-stream') : send(404, 'not found');
|
|
281
|
-
}
|
|
282
|
-
if (seg[0] === 'vendor' && seg[1] === 'three') {
|
|
283
|
-
const p = readStatic(threeDir, seg.slice(2).join('/'));
|
|
284
|
-
return p ? send(200, fs.readFileSync(p), MIME[path.extname(p)] ?? 'application/octet-stream', { 'cache-control': 'max-age=3600' }) : send(404, 'not found');
|
|
285
|
-
}
|
|
286
|
-
if (u.pathname === '/api/health')
|
|
287
|
-
return json(200, { gallery: true, libraries: scans.length, projects: projects.size, baking: queue.size });
|
|
288
|
-
if (u.pathname === '/api/events') {
|
|
289
|
-
res.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-store', connection: 'keep-alive' });
|
|
290
|
-
res.write(': hello\n\n');
|
|
291
|
-
clients.add(res);
|
|
292
|
-
req.on('close', () => clients.delete(res));
|
|
293
|
-
return;
|
|
294
|
-
}
|
|
295
|
-
if (u.pathname === '/api/projects') {
|
|
296
|
-
// a board on another origin may read the list when the config allows it (gallery.cors)
|
|
297
|
-
const cors = origin && cfg.gallery.cors.includes(origin) ? { 'access-control-allow-origin': origin, vary: 'Origin' } : { vary: 'Origin' };
|
|
298
|
-
if (req.method === 'OPTIONS')
|
|
299
|
-
return send(204, '', 'text/plain', { ...cors, 'access-control-allow-methods': 'GET' });
|
|
300
|
-
return json(200, { libraries: scans.map((s) => ({ name: s.name, projects: s.projects.length, problems: s.problems })), projects: listJSON() }, cors);
|
|
301
|
-
}
|
|
302
|
-
if (seg[0] === 'embed' && seg.length === 3) {
|
|
303
|
-
const p = find(decodeURIComponent(seg[1]), decodeURIComponent(seg[2]));
|
|
304
|
-
const s = readBakeState(p.dir);
|
|
305
|
-
const glb = fs.existsSync(path.join(galleryDir(p.dir), 'preview.glb'));
|
|
306
|
-
return html(embedPage({ lib: p.lib, id: p.id, title: p.manifest.title, glb, version: String(s?.at ?? 0), projectUrl: urls.project(p.lib, p.id) }, urls), true);
|
|
307
|
-
}
|
|
308
|
-
if (seg[0] === 'p' && seg.length >= 3) {
|
|
309
|
-
const p = find(decodeURIComponent(seg[1]), decodeURIComponent(seg[2]));
|
|
310
|
-
const rest = seg.slice(3);
|
|
311
|
-
if (!rest.length || (rest.length === 1 && !rest[0]))
|
|
312
|
-
return html(projectPage(projectData(p), urls));
|
|
313
|
-
if (rest[0] === 'thumb.png' || rest[0] === 'preview.glb') {
|
|
314
|
-
const s = readBakeState(p.dir);
|
|
315
|
-
const file = path.join(galleryDir(p.dir), rest[0]);
|
|
316
|
-
if (!fs.existsSync(file)) {
|
|
317
|
-
bakeIfStale(p);
|
|
318
|
-
if (rest[0] === 'thumb.png')
|
|
319
|
-
return send(200, placeholderPNG(), 'image/png');
|
|
320
|
-
return json(404, { error: s?.ok === false ? `the bake failed: ${s.error}` : 'not baked yet' });
|
|
321
|
-
}
|
|
322
|
-
const etag = `"${s?.key ?? 'x'}-${s?.at ?? 0}"`;
|
|
323
|
-
if (req.headers['if-none-match'] === etag) {
|
|
324
|
-
res.writeHead(304, { etag });
|
|
325
|
-
return res.end();
|
|
326
|
-
}
|
|
327
|
-
return send(200, fs.readFileSync(file), rest[0] === 'thumb.png' ? 'image/png' : MIME['.glb'], { etag, 'cache-control': 'no-cache' });
|
|
328
|
-
}
|
|
329
|
-
if (rest[0] === 'files' && rest.length > 1) {
|
|
330
|
-
const abs = projectFile(p, rest.slice(1).join('/'));
|
|
331
|
-
const ext = path.extname(abs).toLowerCase();
|
|
332
|
-
const disp = INLINE.has(ext) ? {} : { 'content-disposition': `attachment; filename="${path.basename(abs).replace(/["\\\r\n]/g, '_')}"` };
|
|
333
|
-
return send(200, fs.readFileSync(abs), FILE_TYPES[ext] ?? 'application/octet-stream', { ...disp, 'content-security-policy': 'sandbox' });
|
|
334
|
-
}
|
|
335
|
-
if (rest[0] === 'edit') {
|
|
336
|
-
const url = await editorFor(p);
|
|
337
|
-
res.writeHead(302, { location: url, 'cache-control': 'no-store' });
|
|
338
|
-
return res.end();
|
|
339
|
-
}
|
|
340
|
-
if (rest[0] === 'bake') {
|
|
341
|
-
if (req.method !== 'POST')
|
|
342
|
-
throw new HttpError(405, 'POST to bake again');
|
|
343
|
-
const queued = queue.add(p.dir);
|
|
344
|
-
if (queued)
|
|
345
|
-
broadcast({ project: keyOf(p), baking: true });
|
|
346
|
-
return json(200, { ok: true, queued });
|
|
347
|
-
}
|
|
348
|
-
if (rest[0] === 'reveal') {
|
|
349
|
-
if (req.method !== 'POST')
|
|
350
|
-
throw new HttpError(405, 'POST to show the folder');
|
|
351
|
-
reveal(p.dir);
|
|
352
|
-
return json(200, { ok: true });
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
send(404, 'not found');
|
|
356
|
-
}
|
|
357
|
-
catch (e) {
|
|
358
|
-
if (res.headersSent || res.destroyed)
|
|
359
|
-
return;
|
|
360
|
-
const code = e instanceof HttpError ? e.status : 500;
|
|
361
|
-
json(code, { ok: false, error: String(e?.message ?? e) });
|
|
362
|
-
}
|
|
363
|
-
});
|
|
364
|
-
const port = o.port ?? cfg.gallery.port;
|
|
365
|
-
await new Promise((resolve, reject) => {
|
|
366
|
-
server.once('error', reject);
|
|
367
|
-
server.listen(port, '127.0.0.1', () => resolve());
|
|
368
|
-
});
|
|
369
|
-
const actual = server.address().port;
|
|
370
|
-
base = `http://127.0.0.1:${actual}`;
|
|
371
|
-
const keepAlive = setInterval(() => { for (const c of clients)
|
|
372
|
-
c.write(': ping\n\n'); }, 20000);
|
|
373
|
-
keepAlive.unref();
|
|
374
|
-
return {
|
|
375
|
-
url: base + '/', port: actual, rescan, queue,
|
|
376
|
-
close: () => {
|
|
377
|
-
clearInterval(periodic);
|
|
378
|
-
clearInterval(keepAlive);
|
|
379
|
-
clearTimeout(changeTimer);
|
|
380
|
-
for (const w of watchers)
|
|
381
|
-
w.close();
|
|
382
|
-
queue.close();
|
|
383
|
-
for (const e of editors.values())
|
|
384
|
-
e.child.kill();
|
|
385
|
-
for (const c of clients)
|
|
386
|
-
c.end();
|
|
387
|
-
server.close();
|
|
388
|
-
server.closeAllConnections?.();
|
|
389
|
-
},
|
|
390
|
-
};
|
|
391
|
-
}
|
package/dist/graph/migrate.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { GraphSpec, NodeSpec } from './types.ts';
|
|
2
|
-
export interface Migration {
|
|
3
|
-
/** version this migration upgrades from; it produces from + 1 */
|
|
4
|
-
from: number;
|
|
5
|
-
desc: string;
|
|
6
|
-
/** returns the upgraded graph; must not modify its argument (structuredClone it first when changing things) */
|
|
7
|
-
apply(g: GraphSpec): GraphSpec;
|
|
8
|
-
}
|
|
9
|
-
/** Registered migrations, ordered by `from`. Version 0 = unversioned graphs from before versioning existed. */
|
|
10
|
-
export declare const MIGRATIONS: Migration[];
|
|
11
|
-
/** Visit every node of a graph and of the graphs nested in its params (bodies), depth first. */
|
|
12
|
-
export declare function forEachNode(g: GraphSpec, fn: (spec: NodeSpec, name: string, graph: GraphSpec) => void): void;
|
|
13
|
-
export interface MigrationResult {
|
|
14
|
-
graph: GraphSpec;
|
|
15
|
-
/** descriptions of the migrations that ran */
|
|
16
|
-
applied: string[];
|
|
17
|
-
/** e.g. a document newer than this engine */
|
|
18
|
-
notes: string[];
|
|
19
|
-
}
|
|
20
|
-
/** Upgrade a graph document to GRAPH_VERSION; reports what ran. Returns the input object when nothing changes. */
|
|
21
|
-
export declare function migrateGraphDetailed(spec: GraphSpec): MigrationResult;
|
|
22
|
-
/** Upgrade a graph document to GRAPH_VERSION and stamp the version. */
|
|
23
|
-
export declare function migrateGraph(spec: GraphSpec): GraphSpec;
|
package/dist/graph/migrate.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
// Graph format versioning. Every graph document carries `version`; older documents are upgraded
|
|
2
|
-
// in place by the registered migrations, in order, then stamped with GRAPH_VERSION.
|
|
3
|
-
//
|
|
4
|
-
// Migrations run on whole documents (a workspace graph, an asset graph, a file given to the CLI):
|
|
5
|
-
// Engine.cook at depth 0, Workspace load/save, MCP build. Nested graphs (foreach/solver bodies,
|
|
6
|
-
// node params of type "graph") belong to their parent document, so a migration that changes nodes
|
|
7
|
-
// must walk into them itself (see forEachNode). Nested cooks never migrate again.
|
|
8
|
-
import { GRAPH_VERSION } from './types.js';
|
|
9
|
-
/** Registered migrations, ordered by `from`. Version 0 = unversioned graphs from before versioning existed. */
|
|
10
|
-
export const MIGRATIONS = [
|
|
11
|
-
{ from: 0, desc: 'unversioned graph: no format changes, stamped as version 1', apply: (g) => g },
|
|
12
|
-
];
|
|
13
|
-
/** Visit every node of a graph and of the graphs nested in its params (bodies), depth first. */
|
|
14
|
-
export function forEachNode(g, fn) {
|
|
15
|
-
for (const [name, spec] of Object.entries(g.nodes ?? {})) {
|
|
16
|
-
if (!spec || typeof spec !== 'object')
|
|
17
|
-
continue;
|
|
18
|
-
fn(spec, name, g);
|
|
19
|
-
for (const v of Object.values(spec.params ?? {}))
|
|
20
|
-
if (v && typeof v === 'object' && !Array.isArray(v) && v.nodes && typeof v.nodes === 'object')
|
|
21
|
-
forEachNode(v, fn);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
/** Upgrade a graph document to GRAPH_VERSION; reports what ran. Returns the input object when nothing changes. */
|
|
25
|
-
export function migrateGraphDetailed(spec) {
|
|
26
|
-
const applied = [], notes = [];
|
|
27
|
-
if (!spec || typeof spec !== 'object' || Array.isArray(spec))
|
|
28
|
-
return { graph: spec, applied, notes };
|
|
29
|
-
const raw = spec.version;
|
|
30
|
-
const from = raw === undefined || raw === null ? 0 : Number(raw);
|
|
31
|
-
if (!Number.isInteger(from) || from < 0) {
|
|
32
|
-
notes.push(`graph "version" must be a non-negative integer, got ${JSON.stringify(raw)}; treated as ${GRAPH_VERSION}`);
|
|
33
|
-
return { graph: stamp(spec), applied, notes };
|
|
34
|
-
}
|
|
35
|
-
if (from > GRAPH_VERSION) {
|
|
36
|
-
notes.push(`graph version ${from} is newer than this engine (${GRAPH_VERSION}); cooking it as is`);
|
|
37
|
-
return { graph: spec, applied, notes };
|
|
38
|
-
}
|
|
39
|
-
if (from === GRAPH_VERSION)
|
|
40
|
-
return { graph: spec, applied, notes };
|
|
41
|
-
let g = spec;
|
|
42
|
-
for (let v = from; v < GRAPH_VERSION; v++) {
|
|
43
|
-
const m = MIGRATIONS.find((x) => x.from === v);
|
|
44
|
-
if (!m)
|
|
45
|
-
throw new Error(`no migration registered from graph version ${v}`);
|
|
46
|
-
g = m.apply(g);
|
|
47
|
-
applied.push(`v${v} → v${v + 1}: ${m.desc}`);
|
|
48
|
-
}
|
|
49
|
-
return { graph: stamp(g), applied, notes };
|
|
50
|
-
}
|
|
51
|
-
/** Upgrade a graph document to GRAPH_VERSION and stamp the version. */
|
|
52
|
-
export function migrateGraph(spec) {
|
|
53
|
-
return migrateGraphDetailed(spec).graph;
|
|
54
|
-
}
|
|
55
|
-
function stamp(g) {
|
|
56
|
-
const { version: _v, ...rest } = g;
|
|
57
|
-
return { version: GRAPH_VERSION, ...rest };
|
|
58
|
-
}
|
package/dist/mcp/library.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { Workspace } from '../workspace.ts';
|
|
2
|
-
import type { ToolDef } from './tools.ts';
|
|
3
|
-
export interface LibraryToolHost {
|
|
4
|
-
/** the workspace the other tools use now */
|
|
5
|
-
workspace(): Workspace;
|
|
6
|
-
/** make a folder the workspace of every tool (and of the live viewer) */
|
|
7
|
-
open(dir: string): Promise<{
|
|
8
|
-
graphs: number;
|
|
9
|
-
assets: number;
|
|
10
|
-
}>;
|
|
11
|
-
}
|
|
12
|
-
export declare function libraryTool(host: LibraryToolHost): ToolDef;
|
package/dist/mcp/library.js
DELETED
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
// The `library` tool: the person's libraries of projects (src/gallery/library.ts). An agent
|
|
2
|
-
// lists and searches every library, reads a project, makes new ones, edits their project.json, bakes previews, and
|
|
3
|
-
// opens a project: the server's workspace becomes that project folder, so build/edit/render/export work on it.
|
|
4
|
-
import fs from 'node:fs';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { createProject, graphNames, isStale, listFiles, loadConfig, parseManifest, projectDirOf, readBakeState, readGraph, readManifest, scanAll, writeManifest, } from '../gallery/library.js';
|
|
7
|
-
const text = (t, isError = false) => ({ content: [{ type: 'text', text: t }], ...(isError ? { isError } : {}) });
|
|
8
|
-
export function libraryTool(host) {
|
|
9
|
-
const scan = () => { const cfg = loadConfig(); return { cfg, scans: scanAll(cfg) }; };
|
|
10
|
-
const urlsOf = (cfg, p) => {
|
|
11
|
-
const b = `http://127.0.0.1:${cfg.gallery.port}`;
|
|
12
|
-
return { page: `${b}/p/${p.lib}/${p.id}`, embed: `${b}/embed/${p.lib}/${p.id}`, thumb: `${b}/p/${p.lib}/${p.id}/thumb.png` };
|
|
13
|
-
};
|
|
14
|
-
const currentDir = () => projectDirOf(host.workspace().dir);
|
|
15
|
-
const statusOf = (p) => { const s = readBakeState(p.dir); return !s ? 'not baked' : !s.ok ? 'bake failed' : isStale(p) ? 'stale' : 'baked'; };
|
|
16
|
-
/** {library, id}, {path}, or nothing (the open project) */
|
|
17
|
-
const pick = (a, scans) => {
|
|
18
|
-
const all = scans.flatMap((s) => s.projects);
|
|
19
|
-
if (a.id) {
|
|
20
|
-
const hits = all.filter((p) => p.id === a.id && (!a.library || p.lib === a.library));
|
|
21
|
-
if (hits.length === 1)
|
|
22
|
-
return hits[0];
|
|
23
|
-
if (hits.length > 1)
|
|
24
|
-
throw new Error(`id "${a.id}" is in several libraries (${hits.map((p) => p.lib).join(', ')}): give "library"`);
|
|
25
|
-
throw new Error(`no project "${a.id}"${a.library ? ` in library "${a.library}"` : ''}. library {action:"list"} lists them`);
|
|
26
|
-
}
|
|
27
|
-
const dir = a.path ? projectDirOf(path.resolve(a.path)) : currentDir();
|
|
28
|
-
if (!dir)
|
|
29
|
-
throw new Error(a.path ? `${a.path}: no project.json there or above` : 'give {library, id}: the open workspace is not a library project');
|
|
30
|
-
const p = all.find((x) => x.dir === dir);
|
|
31
|
-
if (p)
|
|
32
|
-
return p;
|
|
33
|
-
// a project folder outside the configured libraries still works
|
|
34
|
-
const m = readManifest(dir);
|
|
35
|
-
return { lib: '', id: m.id, dir, rel: path.basename(dir), collection: '', manifest: m, updated: 0 };
|
|
36
|
-
};
|
|
37
|
-
return {
|
|
38
|
-
name: 'library',
|
|
39
|
-
description: 'The person\'s libraries of projects (the TopoTopo Gallery website shows them; a board links to them). A library is a folder; a project is a folder in it with project.json (title, description, tags, links to concept art/board pages) and is a workspace of its own (graphs/, assets/, inputs/, exports/, attachments/ for concept art and references). ' +
|
|
40
|
-
'list {query?, tag?, library?}: every project with status and gallery URL. get {library, id} (or the open project): manifest, graphs and vars, attachments, exports, bake state, URLs; thumbnail:true adds the baked image. ' +
|
|
41
|
-
'open {library, id} | {path}: make that project the workspace of every tool and the live viewer, then build/edit/render/export work in it. create {library, path:"props/lantern", title?, description?, tags?, links?}: a new project, opened. ' +
|
|
42
|
-
'update {library?, id?, title?, description?, tags?, links?, graph?, bake?}: edit project.json (graph = the graph the gallery shows; bake = {graph?, node?, frame?} what its preview shows). bake {library?, id?}: render thumb.png and preview.glb now (the gallery also bakes on its own after changes). See help topic "library".',
|
|
43
|
-
inputSchema: {
|
|
44
|
-
type: 'object',
|
|
45
|
-
required: ['action'],
|
|
46
|
-
properties: {
|
|
47
|
-
action: { type: 'string', enum: ['list', 'get', 'open', 'create', 'update', 'bake'] },
|
|
48
|
-
library: { type: 'string', description: 'library name (list shows them)' },
|
|
49
|
-
id: { type: 'string', description: 'project id' },
|
|
50
|
-
path: { type: 'string', description: 'create: folder below the library ("props/lantern"); open/get: a project folder on disk instead of {library, id}' },
|
|
51
|
-
query: { type: 'string', description: 'list: words to match in title, id, collection, description, tags' },
|
|
52
|
-
tag: { type: 'string', description: 'list: only projects with this tag' },
|
|
53
|
-
title: { type: 'string' },
|
|
54
|
-
description: { type: 'string' },
|
|
55
|
-
tags: { type: 'array', items: { type: 'string' } },
|
|
56
|
-
links: { type: 'array', items: { type: 'object', properties: { label: { type: 'string' }, url: { type: 'string' } }, required: ['url'] }, description: 'links back to the board, concept pages, tickets' },
|
|
57
|
-
graph: { type: 'string', description: 'update: the graph the gallery shows and bakes (default "main")' },
|
|
58
|
-
bake: { type: 'object', properties: { graph: { type: 'string' }, node: { type: 'string' }, frame: { type: 'number' } }, description: 'update: what the preview shows instead of the graph output' },
|
|
59
|
-
thumbnail: { type: 'boolean', description: 'get: include the baked thumbnail image' },
|
|
60
|
-
open: { type: 'boolean', description: 'create: open the new project (default true)' },
|
|
61
|
-
},
|
|
62
|
-
},
|
|
63
|
-
run: async (a) => {
|
|
64
|
-
const { cfg, scans } = scan();
|
|
65
|
-
switch (a.action) {
|
|
66
|
-
case 'list': {
|
|
67
|
-
if (!cfg.libraries.length)
|
|
68
|
-
return text(`no libraries configured (${cfg.file}). The person adds one with \`topotopo library add <name> <folder>\`.`);
|
|
69
|
-
const cur = currentDir();
|
|
70
|
-
const words = String(a.query ?? '').toLowerCase().split(/\s+/).filter(Boolean);
|
|
71
|
-
const lines = [];
|
|
72
|
-
for (const s of scans) {
|
|
73
|
-
if (a.library && s.name !== a.library)
|
|
74
|
-
continue;
|
|
75
|
-
const ps = s.projects.filter((p) => {
|
|
76
|
-
const m = p.manifest, hay = [m.title, p.id, p.collection, m.description, ...m.tags].join(' ').toLowerCase();
|
|
77
|
-
return words.every((w) => hay.includes(w)) && (!a.tag || m.tags.includes(a.tag));
|
|
78
|
-
});
|
|
79
|
-
lines.push(`library "${s.name}" (${s.root}): ${ps.length}${ps.length !== s.projects.length ? ` of ${s.projects.length}` : ''} projects`);
|
|
80
|
-
for (const p of ps)
|
|
81
|
-
lines.push(` ${p.id}${p.dir === cur ? ' (open)' : ''} ${p.rel} "${p.manifest.title}"${p.manifest.tags.length ? ` [${p.manifest.tags.join(', ')}]` : ''} ${statusOf(p)} ${urlsOf(cfg, p).page}`);
|
|
82
|
-
for (const pr of s.problems)
|
|
83
|
-
lines.push(` ! ${pr}`);
|
|
84
|
-
}
|
|
85
|
-
return text(lines.join('\n') || 'no library matches');
|
|
86
|
-
}
|
|
87
|
-
case 'get': {
|
|
88
|
-
const p = pick(a, scans), m = p.manifest, s = readBakeState(p.dir), u = urlsOf(cfg, p);
|
|
89
|
-
const graphs = graphNames(p.dir).map((n) => {
|
|
90
|
-
const g = readGraph(p.dir, n) ?? {};
|
|
91
|
-
const vars = Object.entries(g.vars ?? {}).map(([k, v]) => `${k}=${JSON.stringify(v)}`).join(', ');
|
|
92
|
-
return ` ${n}${n === m.graph ? ' (shown)' : ''}: ${Object.keys(g.nodes ?? {}).length} nodes${vars ? `; vars ${vars}` : ''}`;
|
|
93
|
-
});
|
|
94
|
-
const att = listFiles(path.join(p.dir, 'attachments')), exp = listFiles(path.join(p.dir, 'exports'));
|
|
95
|
-
const lines = [
|
|
96
|
-
`project ${p.lib ? `${p.lib}/` : ''}${p.id}: "${m.title}"${p.dir === currentDir() ? ' (open)' : ''}`,
|
|
97
|
-
`folder ${p.dir}`,
|
|
98
|
-
...(m.description ? [`description: ${m.description}`] : []),
|
|
99
|
-
...(m.tags.length ? [`tags: ${m.tags.join(', ')}`] : []),
|
|
100
|
-
...(m.links.length ? ['links:', ...m.links.map((l) => ` ${l.label}: ${l.url}`)] : []),
|
|
101
|
-
`graphs (${graphs.length}):`, ...(graphs.length ? graphs : [' none yet']),
|
|
102
|
-
...(att.length ? [`attachments/ (${att.length}): ${att.slice(0, 30).map((f) => f.path).join(', ')}${att.length > 30 ? ', …' : ''}`] : []),
|
|
103
|
-
...(exp.length ? [`exports/ (${exp.length}): ${exp.slice(0, 30).map((f) => f.path).join(', ')}${exp.length > 30 ? ', …' : ''}`] : []),
|
|
104
|
-
`preview: ${statusOf(p)}${s ? `, baked ${new Date(s.at).toISOString().slice(0, 16)}` : ''}${s && !s.ok ? `: ${s.error}` : ''}${Object.keys(m.bake).length ? `; bake ${JSON.stringify(m.bake)}` : ''}`,
|
|
105
|
-
`gallery: ${u.page} embed: ${u.embed} thumbnail: ${u.thumb}`,
|
|
106
|
-
];
|
|
107
|
-
const content = [{ type: 'text', text: lines.join('\n') }];
|
|
108
|
-
const thumb = path.join(p.dir, '.gallery', 'thumb.png');
|
|
109
|
-
if (a.thumbnail && fs.existsSync(thumb))
|
|
110
|
-
content.push({ type: 'image', data: fs.readFileSync(thumb).toString('base64'), mimeType: 'image/png' });
|
|
111
|
-
return { content };
|
|
112
|
-
}
|
|
113
|
-
case 'open': {
|
|
114
|
-
const p = pick(a, scans);
|
|
115
|
-
const r = await host.open(p.dir);
|
|
116
|
-
return text(`opened ${p.lib ? `${p.lib}/` : ''}${p.id} ("${p.manifest.title}"): the workspace of every tool is now ${p.dir} (${r.graphs} graphs, ${r.assets} assets). ` +
|
|
117
|
-
`Build into graph "${p.manifest.graph}" (the one the gallery shows); put reference images in attachments/ and guide files in inputs/. Gallery page: ${urlsOf(cfg, p).page}`);
|
|
118
|
-
}
|
|
119
|
-
case 'create': {
|
|
120
|
-
if (!a.library)
|
|
121
|
-
throw new Error(`"library" is required (${cfg.libraries.map((l) => l.name).join(', ') || 'none configured'})`);
|
|
122
|
-
if (!a.path)
|
|
123
|
-
throw new Error('"path" is required: the project folder below the library, e.g. "props/lantern"');
|
|
124
|
-
const lib = cfg.libraries.find((l) => l.name === a.library);
|
|
125
|
-
if (!lib)
|
|
126
|
-
throw new Error(`no library "${a.library}" (${cfg.libraries.map((l) => l.name).join(', ') || `none: the person adds one with \`topotopo library add <name> <folder>\``})`);
|
|
127
|
-
const p = createProject(lib, a.path, { title: a.title, description: a.description, tags: a.tags, links: a.links });
|
|
128
|
-
let opened = '';
|
|
129
|
-
if (a.open !== false) {
|
|
130
|
-
await host.open(p.dir);
|
|
131
|
-
opened = ' and opened it: build/edit now work in it (graph "main" is the one the gallery shows)';
|
|
132
|
-
}
|
|
133
|
-
return text(`created ${p.lib}/${p.id} at ${p.dir}${opened}. Gallery page: ${urlsOf(cfg, p).page}`);
|
|
134
|
-
}
|
|
135
|
-
case 'update': {
|
|
136
|
-
const p = pick(a, scans);
|
|
137
|
-
const m = { ...p.manifest };
|
|
138
|
-
for (const k of ['title', 'description', 'tags', 'links', 'graph', 'bake'])
|
|
139
|
-
if (a[k] !== undefined)
|
|
140
|
-
m[k] = a[k];
|
|
141
|
-
const checked = parseManifest(m, p.id);
|
|
142
|
-
writeManifest(p.dir, checked);
|
|
143
|
-
return text(`updated ${p.lib ? `${p.lib}/` : ''}${p.id}: ${JSON.stringify(checked)}`);
|
|
144
|
-
}
|
|
145
|
-
case 'bake': {
|
|
146
|
-
const p = pick(a, scans);
|
|
147
|
-
const { bakeProject } = await import('../gallery/bake.js');
|
|
148
|
-
const s = await bakeProject(p.dir);
|
|
149
|
-
if (!s.ok)
|
|
150
|
-
return text(`bake of ${p.id} failed: ${s.error}`, true);
|
|
151
|
-
const content = [{ type: 'text', text: `baked ${p.id} in ${s.ms} ms: ${s.points} points, ${s.prims} prims${s.glb ? '' : ' (no preview.glb: too large)'}${s.warnings?.length ? `; warnings: ${s.warnings.slice(0, 5).join('; ')}` : ''}` }];
|
|
152
|
-
content.push({ type: 'image', data: fs.readFileSync(path.join(p.dir, '.gallery', 'thumb.png')).toString('base64'), mimeType: 'image/png' });
|
|
153
|
-
return { content };
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
return text(`unknown action "${a.action}": list, get, open, create, update or bake`, true);
|
|
157
|
-
},
|
|
158
|
-
};
|
|
159
|
-
}
|