@neta-art/cohub-cli 10.18.0 → 10.20.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/dist/board-export.d.ts +9 -2
- package/dist/board-export.js +83 -45
- package/dist/board-gpu/daemon.d.ts +11 -0
- package/dist/board-gpu/daemon.js +302 -0
- package/dist/board-gpu/index.d.ts +3 -2
- package/dist/board-gpu/index.js +1 -0
- package/dist/board-gpu/page.js +1202 -396
- package/dist/board-gpu/protocol.d.ts +27 -1
- package/dist/board-kernel.d.ts +1 -1
- package/dist/board-kernel.js +2075 -461
- package/dist/board-media-export.d.ts +16 -0
- package/dist/board-media-export.js +147 -0
- package/dist/board-media.d.ts +2 -1
- package/dist/board-media.js +23 -0
- package/dist/board-profile.d.ts +19 -0
- package/dist/board-profile.js +53 -0
- package/dist/board-simulate.d.ts +3 -0
- package/dist/board-simulate.js +27 -7
- package/dist/commands/boards/authoring.js +45 -3
- package/dist/commands/boards.js +83 -5
- package/dist/commands/space-displays.d.ts +1 -1
- package/dist/commands/space-displays.js +70 -18
- package/dist/output.d.ts +1 -0
- package/dist/output.js +3 -0
- package/dist/safe-remote-image.d.ts +4 -0
- package/dist/safe-remote-image.js +8 -0
- package/package.json +2 -2
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type BoardMediaSpan, type BoardMediaState } from "@neta-art/cohub/board";
|
|
2
|
+
export declare function createBoardMediaFiles(spaceId: string, dir: string, signal?: AbortSignal): {
|
|
3
|
+
path(src: string, kind: "video" | "audio"): Promise<string>;
|
|
4
|
+
audible(src: string, kind: "video" | "audio"): Promise<boolean>;
|
|
5
|
+
};
|
|
6
|
+
export type BoardMediaFiles = ReturnType<typeof createBoardMediaFiles>;
|
|
7
|
+
export declare function createBoardVideoFrames(files: BoardMediaFiles, dir: string, options: {
|
|
8
|
+
fps: number;
|
|
9
|
+
signal?: AbortSignal;
|
|
10
|
+
}): {
|
|
11
|
+
frame(state: BoardMediaState, rate: number, edge: number): Promise<Uint8Array | null>;
|
|
12
|
+
};
|
|
13
|
+
export declare function boardAudioMix(spans: readonly BoardMediaSpan[], files: BoardMediaFiles, firstInput: number): Promise<{
|
|
14
|
+
inputs: string[];
|
|
15
|
+
filter: string;
|
|
16
|
+
} | null>;
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { execFile as execFileCallback } from "node:child_process";
|
|
2
|
+
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
import { parseBoardMediaSource } from "@neta-art/cohub/board";
|
|
6
|
+
import { createClient } from "./client.js";
|
|
7
|
+
import { downloadPublicAudio, downloadPublicVideo } from "./safe-remote-image.js";
|
|
8
|
+
const execFile = promisify(execFileCallback);
|
|
9
|
+
const CHUNK_SECONDS = 4;
|
|
10
|
+
const MAX_DECODE_FPS = 60;
|
|
11
|
+
async function ffmpeg(args, signal) {
|
|
12
|
+
try {
|
|
13
|
+
return await execFile("ffmpeg", ["-v", "error", ...args], { encoding: "utf8", maxBuffer: 2 * 1024 * 1024, signal });
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
const cause = error;
|
|
17
|
+
if (cause.code === "ENOENT")
|
|
18
|
+
throw new Error("Exporting video and audio needs ffmpeg. Install ffmpeg and try again.");
|
|
19
|
+
throw new Error(`ffmpeg failed: ${(cause.stderr ?? cause.message ?? "unknown error").trim().split("\n").slice(-4).join("\n")}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export function createBoardMediaFiles(spaceId, dir, signal) {
|
|
23
|
+
const files = new Map();
|
|
24
|
+
const audible = new Map();
|
|
25
|
+
const client = createClient();
|
|
26
|
+
return {
|
|
27
|
+
path(src, kind) {
|
|
28
|
+
let pending = files.get(src);
|
|
29
|
+
if (!pending) {
|
|
30
|
+
const path = join(dir, `media-${files.size}`);
|
|
31
|
+
pending = (async () => {
|
|
32
|
+
const source = parseBoardMediaSource(src);
|
|
33
|
+
if (!source)
|
|
34
|
+
throw new Error(`Invalid media source ${src}`);
|
|
35
|
+
const { bytes } = source.kind === "file"
|
|
36
|
+
? { bytes: new Uint8Array(await (await client.space(spaceId).files.download(source.value)).blob.arrayBuffer()) }
|
|
37
|
+
: await (kind === "video" ? downloadPublicVideo : downloadPublicAudio)(source.value);
|
|
38
|
+
await writeFile(path, bytes);
|
|
39
|
+
return path;
|
|
40
|
+
})();
|
|
41
|
+
files.set(src, pending);
|
|
42
|
+
}
|
|
43
|
+
return pending;
|
|
44
|
+
},
|
|
45
|
+
async audible(src, kind) {
|
|
46
|
+
let pending = audible.get(src);
|
|
47
|
+
if (!pending) {
|
|
48
|
+
pending = this.path(src, kind).then(async (path) => {
|
|
49
|
+
const { stdout } = await execFile("ffprobe", ["-v", "error", "-select_streams", "a", "-show_entries", "stream=index", "-of", "csv=p=0", path], { signal });
|
|
50
|
+
return stdout.trim().length > 0;
|
|
51
|
+
});
|
|
52
|
+
audible.set(src, pending);
|
|
53
|
+
}
|
|
54
|
+
return pending;
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export function createBoardVideoFrames(files, dir, options) {
|
|
59
|
+
const chunks = new Map();
|
|
60
|
+
let decoded = 0;
|
|
61
|
+
function lookup(chunk, time, edge) {
|
|
62
|
+
if (!chunk || edge > chunk.edge)
|
|
63
|
+
return null;
|
|
64
|
+
const index = chunk.count === 1 ? (Math.abs(time - chunk.start) < 1 ? 0 : -1) : Math.round(((time - chunk.start) * chunk.fps) / 1000);
|
|
65
|
+
return index >= 0 && index < chunk.count ? join(chunk.dir, `${String(index + 1).padStart(5, "0")}.jpg`) : null;
|
|
66
|
+
}
|
|
67
|
+
async function decode(state, rate, edge) {
|
|
68
|
+
const path = await files.path(state.src, "video");
|
|
69
|
+
const previous = chunks.get(state.id);
|
|
70
|
+
if (previous)
|
|
71
|
+
await rm(previous.dir, { recursive: true, force: true });
|
|
72
|
+
const chunkDir = join(dir, `frames-${decoded++}`);
|
|
73
|
+
await mkdir(chunkDir);
|
|
74
|
+
const fps = rate > 0 ? Math.min(MAX_DECODE_FPS, Math.max(1, Math.ceil(options.fps * rate))) : 0;
|
|
75
|
+
const scale = `scale=w=${edge}:h=${edge}:force_original_aspect_ratio=decrease`;
|
|
76
|
+
await ffmpeg([
|
|
77
|
+
"-ss", (state.time / 1000).toFixed(3), "-i", path,
|
|
78
|
+
...(fps ? ["-t", String(CHUNK_SECONDS), "-vf", `fps=${fps},${scale}`] : ["-frames:v", "1", "-vf", scale]),
|
|
79
|
+
"-q:v", "3", join(chunkDir, "%05d.jpg"),
|
|
80
|
+
], options.signal);
|
|
81
|
+
const count = fps ? Math.max(1, Math.round(CHUNK_SECONDS * fps)) : 1;
|
|
82
|
+
const chunk = { src: state.src, start: state.time, fps: fps || 1, count, edge, dir: chunkDir };
|
|
83
|
+
chunks.set(state.id, chunk);
|
|
84
|
+
return chunk;
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
async frame(state, rate, edge) {
|
|
88
|
+
const current = chunks.get(state.id);
|
|
89
|
+
let file = current?.src === state.src ? lookup(current, state.time, edge) : null;
|
|
90
|
+
if (!file)
|
|
91
|
+
file = lookup(await decode(state, rate, edge), state.time, edge);
|
|
92
|
+
return file ? new Uint8Array(await readFile(file).catch(() => Buffer.alloc(0))) : null;
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function volumeExpression(span) {
|
|
97
|
+
const points = span.volume.map((point) => ({ t: (point.at - span.start) / 1000, v: point.value }));
|
|
98
|
+
if (points.every((point) => point.v === points[0]?.v))
|
|
99
|
+
return String(points[0]?.v ?? 1);
|
|
100
|
+
let expression = String(points.at(-1)?.v ?? 1);
|
|
101
|
+
for (let index = points.length - 2; index >= 0; index -= 1) {
|
|
102
|
+
const a = points[index];
|
|
103
|
+
const b = points[index + 1];
|
|
104
|
+
const slope = b.t > a.t ? (b.v - a.v) / (b.t - a.t) : 0;
|
|
105
|
+
expression = `if(lt(t,${b.t.toFixed(4)}),${a.v.toFixed(4)}+${slope.toFixed(6)}*(t-${a.t.toFixed(4)}),${expression})`;
|
|
106
|
+
}
|
|
107
|
+
return `'${expression}'`;
|
|
108
|
+
}
|
|
109
|
+
function tempo(rate) {
|
|
110
|
+
const stages = [];
|
|
111
|
+
let rest = rate;
|
|
112
|
+
while (rest > 2) {
|
|
113
|
+
stages.push("atempo=2");
|
|
114
|
+
rest /= 2;
|
|
115
|
+
}
|
|
116
|
+
while (rest < 0.5) {
|
|
117
|
+
stages.push("atempo=0.5");
|
|
118
|
+
rest /= 0.5;
|
|
119
|
+
}
|
|
120
|
+
if (Math.abs(rest - 1) > 1e-4)
|
|
121
|
+
stages.push(`atempo=${rest.toFixed(6)}`);
|
|
122
|
+
return stages;
|
|
123
|
+
}
|
|
124
|
+
export async function boardAudioMix(spans, files, firstInput) {
|
|
125
|
+
const inputs = [];
|
|
126
|
+
const chains = [];
|
|
127
|
+
for (const span of spans) {
|
|
128
|
+
if (!(await files.audible(span.src, span.kind)))
|
|
129
|
+
continue;
|
|
130
|
+
const index = firstInput + chains.length;
|
|
131
|
+
inputs.push("-i", await files.path(span.src, span.kind));
|
|
132
|
+
const length = ((span.end - span.start) * span.rate) / 1000;
|
|
133
|
+
const delay = Math.max(0, Math.round(span.start));
|
|
134
|
+
const filters = [
|
|
135
|
+
`atrim=start=${(span.from / 1000).toFixed(4)}:duration=${length.toFixed(4)}`,
|
|
136
|
+
"asetpts=PTS-STARTPTS",
|
|
137
|
+
...tempo(span.rate),
|
|
138
|
+
`volume=${volumeExpression(span)}:eval=frame`,
|
|
139
|
+
`adelay=${delay}:all=1`,
|
|
140
|
+
];
|
|
141
|
+
chains.push(`[${index}:a]${filters.join(",")}[a${chains.length}]`);
|
|
142
|
+
}
|
|
143
|
+
if (!chains.length)
|
|
144
|
+
return null;
|
|
145
|
+
const mixed = chains.map((_, index) => `[a${index}]`).join("");
|
|
146
|
+
return { inputs, filter: `${chains.join(";")};${mixed}amix=inputs=${chains.length}:duration=longest:dropout_transition=0:normalize=0[aout]` };
|
|
147
|
+
}
|
package/dist/board-media.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { type BoardDocument, type BoardMediaFileResolver } from "@neta-art/cohub/board";
|
|
1
|
+
import { type BoardDocument, type BoardItem, type BoardMediaFileResolver } from "@neta-art/cohub/board";
|
|
2
2
|
export declare function boardMediaFileResolver(spaceId: string): BoardMediaFileResolver;
|
|
3
3
|
export declare function settleBoardDocument(spaceId: string, document: BoardDocument): Promise<BoardDocument>;
|
|
4
4
|
export declare function settleBoardPatch<T extends Record<string, unknown>>(spaceId: string, patch: T, current: (ids: string[]) => Promise<Record<string, unknown> | null>): Promise<T>;
|
|
5
|
+
export declare function settleBoardMediaDurations(spaceId: string, document: BoardDocument, ids: readonly string[]): Promise<Record<string, BoardItem>>;
|
package/dist/board-media.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
1
2
|
import { resolve } from "node:path";
|
|
3
|
+
import { promisify } from "node:util";
|
|
2
4
|
import { diffMergePatch, mergePatch, parseBoardItem, settleBoardMediaSizes, } from "@neta-art/cohub/board";
|
|
3
5
|
import { createClient } from "./client.js";
|
|
4
6
|
import { localMediaFacts } from "./local-media.js";
|
|
@@ -41,3 +43,24 @@ export async function settleBoardPatch(spaceId, patch, current) {
|
|
|
41
43
|
const adopted = Object.entries(settled).map(([id, item]) => [id, mergePatch(items[id], diffMergePatch(created[id], item))]);
|
|
42
44
|
return { ...patch, items: { ...items, ...Object.fromEntries(adopted) } };
|
|
43
45
|
}
|
|
46
|
+
const run = promisify(execFile);
|
|
47
|
+
export async function settleBoardMediaDurations(spaceId, document, ids) {
|
|
48
|
+
const local = process.env.COHUB_SPACE_ID === spaceId;
|
|
49
|
+
const settled = {};
|
|
50
|
+
for (const id of ids) {
|
|
51
|
+
const item = document.items[id];
|
|
52
|
+
if ((item?.type !== "video" && item?.type !== "audio") || item.props.snapshot?.durationMs)
|
|
53
|
+
continue;
|
|
54
|
+
const file = item.props.src.startsWith("http") ? null : resolve(WORKSPACE_ROOT, item.props.src);
|
|
55
|
+
const input = file && local && file.startsWith(`${WORKSPACE_ROOT}/`)
|
|
56
|
+
? file
|
|
57
|
+
: file ? (await createClient().space(spaceId).files.resolve(item.props.src, { purpose: "playback" }).catch(() => null))?.url : null;
|
|
58
|
+
const seconds = input
|
|
59
|
+
? Number((await run("ffprobe", ["-v", "error", "-show_entries", "format=duration", "-of", "csv=p=0", input], { timeout: 15_000 }).catch(() => ({ stdout: "" }))).stdout)
|
|
60
|
+
: Number.NaN;
|
|
61
|
+
if (!(seconds > 0))
|
|
62
|
+
throw new Error(`The length of ${id} is unknown; pass --duration.`);
|
|
63
|
+
settled[id] = { ...item, props: { ...item.props, snapshot: { ...item.props.snapshot, durationMs: Math.round(seconds * 1000) } } };
|
|
64
|
+
}
|
|
65
|
+
return settled;
|
|
66
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { BoardDocument, BoardExportRegion } from "@neta-art/cohub/board";
|
|
2
|
+
import { type BoardGpuProfile } from "./board-gpu/index.js";
|
|
3
|
+
export type BoardProfile = BoardGpuProfile & {
|
|
4
|
+
hints: string[];
|
|
5
|
+
};
|
|
6
|
+
export declare function profileBoard(options: {
|
|
7
|
+
spaceId: string;
|
|
8
|
+
target: string;
|
|
9
|
+
time: number;
|
|
10
|
+
animation?: string;
|
|
11
|
+
region: BoardExportRegion;
|
|
12
|
+
size: {
|
|
13
|
+
width: number;
|
|
14
|
+
height: number;
|
|
15
|
+
};
|
|
16
|
+
frames: number;
|
|
17
|
+
withImages: boolean;
|
|
18
|
+
}): Promise<BoardProfile>;
|
|
19
|
+
export declare function boardProfileHints(document: BoardDocument, profile: BoardGpuProfile): string[];
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { gpuResources, loadBoardDocument } from "./board-export.js";
|
|
2
|
+
import { openBoardGpu } from "./board-gpu/daemon.js";
|
|
3
|
+
import { findBoardGpuBrowser } from "./board-gpu/index.js";
|
|
4
|
+
import { BOARD_EXPORT_IMAGE_MEMORY_MIB } from "./board-export-resources.js";
|
|
5
|
+
import { buildBoardScene, planBoardExport } from "./board-kernel.js";
|
|
6
|
+
import { settleBoardDocument } from "./board-media.js";
|
|
7
|
+
const LARGE_OWNER = 4000;
|
|
8
|
+
const LARGE_STATE = 512 * 512;
|
|
9
|
+
const MANY_EFFECTS = 16;
|
|
10
|
+
export async function profileBoard(options) {
|
|
11
|
+
const browser = findBoardGpuBrowser();
|
|
12
|
+
if (!browser)
|
|
13
|
+
throw new Error("Profiling needs Chrome, Chromium or Edge. Install one, or set COHUB_CHROME to its path.");
|
|
14
|
+
const document = await settleBoardDocument(options.spaceId, (await loadBoardDocument(options.spaceId, options.target)).document);
|
|
15
|
+
if (options.animation && !document.animations[options.animation])
|
|
16
|
+
throw new Error(`Animation ${options.animation} does not exist.`);
|
|
17
|
+
const plan = planBoardExport({ scene: buildBoardScene(document), region: options.region, scale: 1, padding: 0 });
|
|
18
|
+
if (!plan)
|
|
19
|
+
throw new Error("Nothing to profile: the region contains no items.");
|
|
20
|
+
const scale = Math.min(options.size.width / plan.world.width, options.size.height / plan.world.height);
|
|
21
|
+
const gpu = await openBoardGpu(gpuResources(options.spaceId, document), { browser });
|
|
22
|
+
try {
|
|
23
|
+
await gpu.init({ document, animation: options.animation ?? null, theme: "dark" });
|
|
24
|
+
const profile = await gpu.profile({
|
|
25
|
+
time: options.time,
|
|
26
|
+
region: options.region,
|
|
27
|
+
scale,
|
|
28
|
+
frames: options.frames,
|
|
29
|
+
withImages: options.withImages,
|
|
30
|
+
imageMemory: BOARD_EXPORT_IMAGE_MEMORY_MIB * 1024 ** 2,
|
|
31
|
+
});
|
|
32
|
+
return { ...profile, hints: boardProfileHints(document, profile) };
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
await gpu.close();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export function boardProfileHints(document, profile) {
|
|
39
|
+
const hints = [];
|
|
40
|
+
const share = (ms) => (profile.frame.p50 > 0 ? ms / profile.frame.p50 : 0);
|
|
41
|
+
for (const owner of profile.owners) {
|
|
42
|
+
if (owner.filter === "always" && share(owner.ms) > 0.1)
|
|
43
|
+
hints.push(`${owner.id} redraws everything under it through a filter every frame; name its filter passes and turn them on with ctx.passes() only while they show.`);
|
|
44
|
+
const item = document.items[owner.id];
|
|
45
|
+
if (item && "size" in item && Math.max(item.size.width, item.size.height) > LARGE_OWNER)
|
|
46
|
+
hints.push(`${owner.id} is larger than any view, so its shaders never leave the screen; a Board script, or shaders on the smaller items, draw only what shows.`);
|
|
47
|
+
if (owner.state && owner.state.width * owner.state.height > LARGE_STATE)
|
|
48
|
+
hints.push(`${owner.id} steps a ${owner.state.width}×${owner.state.height} state 60 times a second and replays it from the start in exports; keep it as small as reads well.`);
|
|
49
|
+
}
|
|
50
|
+
if (profile.effects > MANY_EFFECTS)
|
|
51
|
+
hints.push(`${profile.effects} items draw with shadows, blurs or color filters, each in its own offscreen pass; present them only while they change, and drop neutral values.`);
|
|
52
|
+
return hints;
|
|
53
|
+
}
|
package/dist/board-simulate.d.ts
CHANGED
package/dist/board-simulate.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { diffMergePatch } from "@neta-art/cohub/board";
|
|
2
2
|
import { gpuResources, loadBoardDocument, readSpaceFileBytes, resolveBundledFonts, runBoardExport } from "./board-export.js";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { openBoardGpu } from "./board-gpu/daemon.js";
|
|
4
|
+
import { findBoardGpuBrowser } from "./board-gpu/index.js";
|
|
5
|
+
import { boardScriptAttachments, boardShaderParameters, buildBoardScene, checkBoardShader, createBoardScriptHarness, createNodeBoardRenderer, itemBounds, planBoardExport, rectsIntersect, } from "./board-kernel.js";
|
|
5
6
|
import { settleBoardDocument } from "./board-media.js";
|
|
6
7
|
import { parseBoardTime } from "./board-time.js";
|
|
7
8
|
import { createClient } from "./client.js";
|
|
@@ -35,7 +36,7 @@ export function parseBoardSimulationSteps(value) {
|
|
|
35
36
|
if (value.length > MAX_STEPS)
|
|
36
37
|
throw new Error(`steps exceed the limit of ${MAX_STEPS}`);
|
|
37
38
|
const steps = value.map(parseStep);
|
|
38
|
-
const elapsed = steps.reduce((total, step) => total + ("wait" in step ? step.wait : "tap" in step ? 2 * FRAME_MS : "drag" in step ? (step.drag.length + 1) * FRAME_MS : "pointer" in step ? FRAME_MS : 0), 0);
|
|
39
|
+
const elapsed = steps.reduce((total, step) => total + ("wait" in step ? step.wait : "tap" in step ? 2 * FRAME_MS : "drag" in step ? (step.drag.length + 1) * FRAME_MS : "move" in step ? step.move.length * FRAME_MS : "pointer" in step ? FRAME_MS : 0), 0);
|
|
39
40
|
if (elapsed > MAX_TIME_MS)
|
|
40
41
|
throw new Error(`steps exceed ${MAX_TIME_MS / 60_000} minutes of simulated time`);
|
|
41
42
|
return steps;
|
|
@@ -63,6 +64,11 @@ function parseStep(entry, index) {
|
|
|
63
64
|
throw new Error(`${name}.drag must list at least two points`);
|
|
64
65
|
return { drag: step.drag.map((entry, at) => point(entry, `${name}.drag[${at}]`)), input: pointerInput(step, name) };
|
|
65
66
|
}
|
|
67
|
+
if ("move" in step) {
|
|
68
|
+
if (!Array.isArray(step.move) || !step.move.length)
|
|
69
|
+
throw new Error(`${name}.move must list points`);
|
|
70
|
+
return { move: step.move.map((entry, at) => point(entry, `${name}.move[${at}]`)), input: pointerInput(step, name) };
|
|
71
|
+
}
|
|
66
72
|
if ("pointer" in step) {
|
|
67
73
|
if (step.pointer !== "down" && step.pointer !== "move" && step.pointer !== "up")
|
|
68
74
|
throw new Error(`${name}.pointer must be down, move or up`);
|
|
@@ -78,7 +84,7 @@ function parseStep(entry, index) {
|
|
|
78
84
|
throw new Error(`${name}.select must list item ids`);
|
|
79
85
|
return { select: step.select };
|
|
80
86
|
}
|
|
81
|
-
throw new Error(`${name} must be one of wait, tool, tap, drag, pointer, key or select`);
|
|
87
|
+
throw new Error(`${name} must be one of wait, tool, tap, drag, move, pointer, key or select`);
|
|
82
88
|
}
|
|
83
89
|
async function run(harness, steps) {
|
|
84
90
|
for (const step of steps) {
|
|
@@ -90,6 +96,8 @@ async function run(harness, steps) {
|
|
|
90
96
|
await harness.tap(step.tap, step.input);
|
|
91
97
|
else if ("drag" in step)
|
|
92
98
|
await harness.drag(step.drag, step.input);
|
|
99
|
+
else if ("move" in step)
|
|
100
|
+
await harness.move(step.move, step.input);
|
|
93
101
|
else if ("pointer" in step)
|
|
94
102
|
await harness.pointer(step.pointer, step.at, step.input);
|
|
95
103
|
else if ("key" in step) {
|
|
@@ -102,7 +110,7 @@ async function run(harness, steps) {
|
|
|
102
110
|
}
|
|
103
111
|
async function checkShaders(shaders, spaceId, document) {
|
|
104
112
|
const browser = findBoardGpuBrowser();
|
|
105
|
-
const exporter = browser ? await
|
|
113
|
+
const exporter = browser ? await openBoardGpu(gpuResources(spaceId, document), { browser }).catch(() => null) : null;
|
|
106
114
|
if (exporter) {
|
|
107
115
|
try {
|
|
108
116
|
return { errors: await exporter.compile(shaders), compiled: true };
|
|
@@ -156,11 +164,13 @@ export async function runBoardSimulation(options) {
|
|
|
156
164
|
...(checked ? { shaders: { count: shaders.length, compiled: checked.compiled } } : {}),
|
|
157
165
|
};
|
|
158
166
|
if (options.image && shown) {
|
|
167
|
+
const gpu = checked?.compiled && !Object.keys(checked.errors).length && shaderShown(harness.document, shaders, options.image.region);
|
|
159
168
|
const exported = await runBoardExport({
|
|
160
169
|
spaceId: options.spaceId,
|
|
161
170
|
target: options.target,
|
|
162
|
-
|
|
163
|
-
|
|
171
|
+
...(gpu
|
|
172
|
+
? { document: harness.document, snapshots: await harness.snapshot(), renderer: "gpu" }
|
|
173
|
+
: { document: shown.document, ...(shown.scripts ? { scripts: shown.scripts } : {}) }),
|
|
164
174
|
region: options.image.region,
|
|
165
175
|
scale: options.image.scale,
|
|
166
176
|
theme: options.image.theme,
|
|
@@ -179,3 +189,13 @@ export async function runBoardSimulation(options) {
|
|
|
179
189
|
renderer.destroy();
|
|
180
190
|
}
|
|
181
191
|
}
|
|
192
|
+
function shaderShown(document, shaders, region) {
|
|
193
|
+
const scene = buildBoardScene(document);
|
|
194
|
+
const plan = planBoardExport({ scene, region, scale: 1 });
|
|
195
|
+
if (!plan)
|
|
196
|
+
return false;
|
|
197
|
+
return shaders.some(({ id }) => {
|
|
198
|
+
const item = scene.get(id);
|
|
199
|
+
return !item || rectsIntersect(itemBounds(item.frame), plan.world);
|
|
200
|
+
});
|
|
201
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { readBoardJsonObject } from "../../board-command-support.js";
|
|
2
2
|
import { loadBoardDocument, resolveBundledFonts } from "../../board-export.js";
|
|
3
|
-
import { settleBoardDocument } from "../../board-media.js";
|
|
4
|
-
import { boardFrameRegions, boardPathTrack, boardRegionStep, boardSequencePatch, buildBoardScene, createBoardAnimationSampler, createNodeBoardRenderer, inspectBoardItems, } from "../../board-kernel.js";
|
|
5
|
-
import { parseBoardTimes } from "../../board-time.js";
|
|
3
|
+
import { settleBoardDocument, settleBoardMediaDurations } from "../../board-media.js";
|
|
4
|
+
import { boardFrameRegions, boardPathTrack, boardPlacePatch, boardRegionStep, boardSequencePatch, buildBoardScene, createBoardAnimationSampler, createNodeBoardRenderer, inspectBoardItems, } from "../../board-kernel.js";
|
|
5
|
+
import { parseBoardTime, parseBoardTimes } from "../../board-time.js";
|
|
6
6
|
import { handleHttp, json as outJson, jsonRequested, table } from "../../output.js";
|
|
7
7
|
import { resolveSpace } from "../../space.js";
|
|
8
8
|
const DIRECTIONS = ["left", "right", "up", "down"];
|
|
@@ -97,6 +97,48 @@ Viewers step with N, the arrow keys and swipes; #focus=<frame>&present presents.
|
|
|
97
97
|
handleHttp(cause);
|
|
98
98
|
}
|
|
99
99
|
});
|
|
100
|
+
boards
|
|
101
|
+
.command("place <board>")
|
|
102
|
+
.description("Put items or animations on a timeline as clips; prints a patch for apply, does not write")
|
|
103
|
+
.requiredOption("--animation <id>", "Animation to place them in; created when missing")
|
|
104
|
+
.requiredOption("--targets <ids>", "Comma-separated item or animation ids, laid end to end")
|
|
105
|
+
.option("--at <time>", "Where the first one starts", "0")
|
|
106
|
+
.option("--duration <time>", "Length of each; defaults to what is left of the media or animation, else 3s")
|
|
107
|
+
.option("--from <time>", "Where media or an animation starts playing in its own time", "0")
|
|
108
|
+
.option("--rate <number>", "Playback speed of media and animations", "1")
|
|
109
|
+
.option("--together", "Start them all at --at instead of one after another")
|
|
110
|
+
.addHelpText("after", `
|
|
111
|
+
Each target is shown only during its clip (a visible track); video, audio and
|
|
112
|
+
animations also play (a props.time or time track), and sound in exports while
|
|
113
|
+
their time moves forward. Placing a target that is already on the timeline moves it.
|
|
114
|
+
cohub boards place cut.board --animation cut --targets intro,talk,outro | cohub boards apply cut.board -i -
|
|
115
|
+
cohub boards place cut.board --animation cut --targets music --at 0 --duration 30s --from 12s`)
|
|
116
|
+
.action(async (target, options) => {
|
|
117
|
+
try {
|
|
118
|
+
const targets = options.targets.split(",").map((id) => id.trim()).filter(Boolean);
|
|
119
|
+
if (!targets.length)
|
|
120
|
+
throw new Error("--targets needs at least one id.");
|
|
121
|
+
const rate = Number(options.rate);
|
|
122
|
+
if (!Number.isFinite(rate) || rate <= 0)
|
|
123
|
+
throw new Error("--rate must be a positive number.");
|
|
124
|
+
const spaceId = await resolveSpace(boards);
|
|
125
|
+
const { document: loaded } = await loadBoardDocument(spaceId, target);
|
|
126
|
+
const durations = options.duration ? {} : await settleBoardMediaDurations(spaceId, loaded, targets);
|
|
127
|
+
const document = { ...loaded, items: { ...loaded.items, ...durations } };
|
|
128
|
+
const patch = boardPlacePatch(document, options.animation, targets, {
|
|
129
|
+
at: parseBoardTime(options.at, "--at"),
|
|
130
|
+
from: parseBoardTime(options.from, "--from"),
|
|
131
|
+
rate,
|
|
132
|
+
...(options.duration ? { duration: parseBoardTime(options.duration, "--duration") } : {}),
|
|
133
|
+
together: options.together,
|
|
134
|
+
});
|
|
135
|
+
const items = Object.fromEntries(Object.entries(durations).map(([id, item]) => [id, { props: { snapshot: { durationMs: item.props.snapshot?.durationMs } } }]));
|
|
136
|
+
outJson(Object.keys(items).length ? { ...patch, items } : patch);
|
|
137
|
+
}
|
|
138
|
+
catch (cause) {
|
|
139
|
+
handleHttp(cause);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
100
142
|
boards
|
|
101
143
|
.command("path <target>")
|
|
102
144
|
.description("Sample a polyline into a position track; does not write")
|
package/dist/commands/boards.js
CHANGED
|
@@ -97,6 +97,26 @@ function showPlayback(playback) {
|
|
|
97
97
|
{ key: "timeScale", label: "Speed" },
|
|
98
98
|
]);
|
|
99
99
|
}
|
|
100
|
+
const shellQuote = (value) => `'${value.replaceAll("'", "'\\''")}'`;
|
|
101
|
+
async function runExportTask(boards, options) {
|
|
102
|
+
try {
|
|
103
|
+
const command = ["cohub", ...process.argv.slice(2).filter((arg) => arg !== "--async")].map(shellQuote).join(" ");
|
|
104
|
+
const { taskRunId } = await createClient().space(await resolveSpace(boards)).runCommand({ command });
|
|
105
|
+
if (jsonRequested(options))
|
|
106
|
+
return outJson({ taskRunId, command });
|
|
107
|
+
ok(`Exporting as Task ${taskRunId}`);
|
|
108
|
+
console.log(` Follow it with: cohub tasks get ${taskRunId}`);
|
|
109
|
+
}
|
|
110
|
+
catch (cause) {
|
|
111
|
+
handleHttp(cause);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function parseSize(value) {
|
|
115
|
+
const size = /^(\d+)x(\d+)$/.exec(value);
|
|
116
|
+
if (!size?.[1] || !size[2])
|
|
117
|
+
throw new Error("--size must be widthxheight, e.g. 1600x900.");
|
|
118
|
+
return { width: parseNumber(size[1], "width", { min: 1, max: 8192 }), height: parseNumber(size[2], "height", { min: 1, max: 8192 }) };
|
|
119
|
+
}
|
|
100
120
|
/** One region flag at most: being told to pick beats guessing which one wins. */
|
|
101
121
|
function parseExportRegion(options) {
|
|
102
122
|
const chosen = [options.camera && "--camera", options.frame && "--frame", options.items && "--items", options.rect && "--rect"].filter(Boolean);
|
|
@@ -167,14 +187,21 @@ function registerExportCommand(boards) {
|
|
|
167
187
|
.option("--image-memory <mib>", "Decoded image memory budget in MiB", String(BOARD_EXPORT_IMAGE_MEMORY_MIB))
|
|
168
188
|
.option("--no-images", "Draw placeholders instead of downloading images")
|
|
169
189
|
.option("--renderer <renderer>", "auto draws in Node and uses local Chrome for shaders; canvas or gpu forces one", "auto")
|
|
170
|
-
.option("--force", "Replace existing output files")
|
|
190
|
+
.option("--force", "Replace existing output files")
|
|
191
|
+
.option("--async", "Run as a Task in the Space's sandbox and return at once; paths are Space paths"))
|
|
171
192
|
.addHelpText("after", `
|
|
193
|
+
Shaders draw in a shared headless Chrome that stays up a few minutes between calls;
|
|
194
|
+
Boards without shaders never start it. Set COHUB_BOARD_GPU_DAEMON=0 to start one per call.
|
|
195
|
+
|
|
172
196
|
Examples:
|
|
173
197
|
cohub boards export plan.board -o plan.png
|
|
198
|
+
cohub boards export plan.board --animation intro --at 0:20s:40ms -o intro.mp4 --async
|
|
174
199
|
cohub boards export plan.board --frame s2 -o slide-2.png
|
|
175
200
|
cohub boards export plan.board --animation lecture --at 4.5s -o moment.png
|
|
176
201
|
cohub boards export plan.board --animation lecture --at 0:6s:40ms -o frames/%04d.png`)
|
|
177
202
|
.action(async (target, options) => {
|
|
203
|
+
if (options.async)
|
|
204
|
+
return runExportTask(boards, options);
|
|
178
205
|
const controller = new AbortController();
|
|
179
206
|
const cancel = () => controller.abort(new Error("Export cancelled."));
|
|
180
207
|
process.once("SIGINT", cancel);
|
|
@@ -185,10 +212,7 @@ Examples:
|
|
|
185
212
|
if (options.camera) {
|
|
186
213
|
if (!options.animation || !times)
|
|
187
214
|
throw new Error("--camera needs --animation and --at.");
|
|
188
|
-
|
|
189
|
-
if (!size?.[1] || !size[2])
|
|
190
|
-
throw new Error("--size must be widthxheight, e.g. 1600x900.");
|
|
191
|
-
camera = { width: parseNumber(size[1], "width", { min: 1, max: 8192 }), height: parseNumber(size[2], "height", { min: 1, max: 8192 }) };
|
|
215
|
+
camera = parseSize(options.size ?? "1600x900");
|
|
192
216
|
}
|
|
193
217
|
const progress = (completed, total) => {
|
|
194
218
|
if (!jsonRequested(options) && (completed === total || completed % 30 === 0))
|
|
@@ -373,6 +397,59 @@ Examples:
|
|
|
373
397
|
}
|
|
374
398
|
});
|
|
375
399
|
}
|
|
400
|
+
function registerProfileCommand(boards) {
|
|
401
|
+
withJson(boards.command("profile <board>")
|
|
402
|
+
.description("Measure what a Board costs to draw, shader by shader, and say what to change")
|
|
403
|
+
.option("--at <time>", "A moment of --animation, or a script time without one", "2s")
|
|
404
|
+
.option("--animation <id>", "Animation --at refers to")
|
|
405
|
+
.option("--frame <item-id>", "Profile one frame")
|
|
406
|
+
.option("--items <ids>", "Profile comma-separated item ids")
|
|
407
|
+
.option("--rect <rect>", "Profile a world rect as x,y,width,height")
|
|
408
|
+
.option("--size <widthxheight>", "Fit the region into this many pixels, like a screen", "1600x900")
|
|
409
|
+
.option("--frames <count>", "Frames to time per measurement", "5")
|
|
410
|
+
.option("--no-images", "Draw placeholders instead of downloading images"))
|
|
411
|
+
.addHelpText("after", `
|
|
412
|
+
Draws in a headless Chrome; without a GPU it uses software WebGL, so compare the
|
|
413
|
+
numbers with each other rather than with a real display.
|
|
414
|
+
|
|
415
|
+
Examples:
|
|
416
|
+
cohub boards profile demo.board
|
|
417
|
+
cohub boards profile demo.board --rect 0,0,1600,900 --at 8s --json`)
|
|
418
|
+
.action(async (target, options) => {
|
|
419
|
+
try {
|
|
420
|
+
const { profileBoard } = await import("../board-profile.js");
|
|
421
|
+
const profile = await profileBoard({
|
|
422
|
+
spaceId: await resolveSpace(boards),
|
|
423
|
+
target,
|
|
424
|
+
time: parseBoardTime(options.at ?? "2s", "--at"),
|
|
425
|
+
...(options.animation ? { animation: options.animation } : {}),
|
|
426
|
+
region: parseExportRegion(options),
|
|
427
|
+
size: parseSize(options.size ?? "1600x900"),
|
|
428
|
+
frames: parseNumber(options.frames ?? "5", "--frames", { min: 3, max: 240, integer: true }),
|
|
429
|
+
withImages: options.images !== false,
|
|
430
|
+
});
|
|
431
|
+
if (jsonRequested(options))
|
|
432
|
+
return outJson(profile);
|
|
433
|
+
const ms = (value) => `${value.toFixed(1)} ms`;
|
|
434
|
+
ok(`${profile.width}×${profile.height} in ${profile.renderer}: ${ms(profile.frame.p50)} a frame (p95 ${ms(profile.frame.p95)}), ${profile.items} items`);
|
|
435
|
+
for (const owner of profile.owners) {
|
|
436
|
+
const traits = [owner.filter !== "none" && `filter ${owner.filter}`, owner.sprites && `${owner.sprites} sprites`, owner.state && `state ${owner.state.width}×${owner.state.height}`].filter(Boolean);
|
|
437
|
+
console.log(` ${owner.id.padEnd(24)} ${ms(owner.ms).padStart(9)}${traits.length ? ` ${traits.join(", ")}` : ""}`);
|
|
438
|
+
}
|
|
439
|
+
for (const hint of profile.hints)
|
|
440
|
+
console.log(` ! ${hint}`);
|
|
441
|
+
}
|
|
442
|
+
catch (cause) {
|
|
443
|
+
handleHttp(cause);
|
|
444
|
+
}
|
|
445
|
+
});
|
|
446
|
+
boards.command("gpu-daemon", { hidden: true })
|
|
447
|
+
.requiredOption("--browser <path>")
|
|
448
|
+
.action(async (options) => {
|
|
449
|
+
const { serveBoardGpu } = await import("../board-gpu/daemon.js");
|
|
450
|
+
await serveBoardGpu(options.browser);
|
|
451
|
+
});
|
|
452
|
+
}
|
|
376
453
|
// ─── Commands ────────────────────────────────────────────────────────────────
|
|
377
454
|
export function registerBoards(program) {
|
|
378
455
|
const boards = program
|
|
@@ -611,6 +688,7 @@ Presets: ${BOARD_PRESET_NAMES.join(", ")}
|
|
|
611
688
|
registerBoardExampleCommands(boards);
|
|
612
689
|
registerExportCommand(boards);
|
|
613
690
|
registerSimulateCommand(boards);
|
|
691
|
+
registerProfileCommand(boards);
|
|
614
692
|
registerBoardAuthoringCommands(boards);
|
|
615
693
|
registerPlaybackCommands(boards);
|
|
616
694
|
withJson(boards.command("watch <board>")
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type Command } from "commander";
|
|
2
2
|
export declare function registerSpaceDisplays(spacesCmd: Command): void;
|