Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Canvas: a skill draws as a board and a board reads back as a skill

Claude8b71d8c75497
diff --git a/src/lib/catalog/canvas.ts b/src/lib/catalog/canvas.ts
index 062796a9..a32da8c5 100644
--- a/src/lib/catalog/canvas.ts
+++ b/src/lib/catalog/canvas.ts
@@ -23,6 +23,8 @@ import { fillLayout, layoutPlaceholders } from "@/lib/layout-surfaces";
import { FLY_MODELS, flyKeys, planFlyAround } from "./fly-around";
import type { CameraKey } from "@/lib/camera-path";
import type { ExactOrbit } from "@/lib/exact-orbit";
+import { paramIdFrom } from "@/lib/recipe-builder";
+import type { GenerateStep, Recipe, RecipeCheck, RecipeParam, RecipeStep, RecipeValues } from "@/lib/recipe";
import {
OPERATION_LABELS,
OPERATION_OUTPUT,
@@ -132,6 +134,10 @@ export interface CanvasNodeData {
keys?: CameraKey[];
/** Fly around: the clip's length in seconds (the cloud; your GPU always films about 5 s). */
duration?: number;
+ /** A node drawn from a skill's step (recipeToCanvas): what the board has no field for, kept for the way back. */
+ skill?: SkillStepMark;
+ /** An input node drawn from a skill's input (or a fixed file of a step): the input as it was. */
+ input?: SkillInputMark;
}
export interface CanvasNode {
@@ -2549,3 +2555,513 @@ CANVAS_TEMPLATES.push(...EFFECT_CHAINS.map(effectChainTemplate));
export function getTemplate(id: string): CanvasTemplate | undefined {
return CANVAS_TEMPLATES.find((t) => t.id === id);
}
+
+// ---------------------------------------------------------------------------
+// Skills as canvases (W5, docs/research/work2skill-7.md §10): a recipe skill drawn as a board, and a board saved as a
+// skill. Inputs are input nodes (text, image, video, audio, element), steps are model nodes, chain slots are edges.
+// What a board has no field for (a step's key and label, its feature, LoRAs, a preset, its check, an input's limits)
+// rides on the nodes (`data.skill`, `data.input`), so a skill opened as a canvas and saved back is the same skill.
+
+/** Why a skill isn't drawn (it still runs from its form). */
+export const CANT_DRAW = "This skill can't be drawn yet";
+/** How a skill that runs one studio tool shows on the Skills tab: one read-only note. */
+export const STUDIO_STEP = "Studio step";
+
+/** What a step drawn from a skill keeps that the board has no field for (dropped by the way back if the model changes). */
+export interface SkillStepMark {
+ key: string;
+ label?: string;
+ feature?: string;
+ /** The model the step was drawn with: `extra` applies only while the node keeps it. */
+ modelId: string;
+ /** Request fields the board doesn't hold (LoRAs, a preset, a negative prompt), and settings it has no field for. */
+ extra?: Record<string, unknown>;
+ check?: RecipeCheck;
+ /** The skill it was drawn from (Save as skill offers to save it back). */
+ from?: string;
+}
+
+/** An input node drawn from a skill: its input as it was (the node's label is the input's), its place in the form; or a fixed file. */
+export interface SkillInputMark {
+ param?: Omit<RecipeParam, "label">;
+ at?: number;
+ /** A file a step always uses (not an input): it stays fixed on the way back. */
+ fixed?: boolean;
+}
+
+export type RecipeDrawing = { ok: true; graph: CanvasGraph } | { ok: false; reason: string; detail: string; studio?: string };
+
+/** The settings a board's node keeps (the canvases route's Params); anything else rides on `data.skill.extra`. */
+const BOARD_PARAMS = new Set(["aspectRatio", "resolution", "duration", "numOutputs", "seed", "generateAudio", "voice", "targetLanguage", "sourceLanguage"]);
+/** Request fields a board's node makes itself (buildRequest). */
+const BOARD_FIELDS = new Set(["feature", "modelId", "op", "prompt", "inputs", "params", "chain", "engine"]);
+/** A request's file fields and the port each is drawn into. */
+const FILE_PORT: Record<string, InputPortId> = { images: "images", endImage: "endImage", video: "video", videos: "videos", audio: "audio", voiceAudio: "voice" };
+/** Chain slots a board draws, and the port each fills (voiceAudio ↔ Voice). */
+const CHAIN_PORT: Partial<Record<ChainSlot["slot"], InputPortId>> = { prompt: "prompt", ...(FILE_PORT as Partial<Record<ChainSlot["slot"], InputPortId>>) };
+const EXACT_PH = /^\{\{\s*([A-Za-z][A-Za-z0-9_]*)\s*\}\}$/;
+const ANY_PH = /\{\{\s*([A-Za-z][A-Za-z0-9_]*)\s*\}\}/g;
+const INPUT_NODE = { image: "image-input", video: "video-input", audio: "audio-input", text: "prompt", element: "element" } as const satisfies Record<string, CanvasNodeType>;
+const SENTINEL_EXT = { image: "png", video: "mp4", audio: "wav" } as const;
+const SENTINEL = /^\/api\/files\/recipe-input\/([A-Za-z][A-Za-z0-9_]*)\.(png|mp4|wav)$/;
+const asList = (a: string | string[] | undefined): string[] => (a === undefined ? [] : Array.isArray(a) ? a : [a]);
+const escapeRe = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
+/** An @handle as a whole word (as recipe-builder matches it). */
+const handleRe = (handle: string) => new RegExp(`(?<![\\p{L}\\p{N}[email protected]])${escapeRe(handle)}(?![\\p{L}\\p{N}_-])`, "giu");
+const words = (list: string[]) => (list.length > 1 ? `${list.slice(0, -1).join(", ")} and ${list[list.length - 1]}` : (list[0] ?? ""));
+
+/** Placeholders in a value, with whether each is the whole string. */
+function placeholdersIn(v: unknown, out: { id: string; exact: boolean }[] = []): { id: string; exact: boolean }[] {
+ if (typeof v === "string") {
+ const exact = EXACT_PH.exec(v);
+ if (exact) out.push({ id: exact[1], exact: true });
+ else for (const m of v.matchAll(ANY_PH)) out.push({ id: m[1], exact: false });
+ } else if (Array.isArray(v)) for (const x of v) placeholdersIn(x, out);
+ else if (v && typeof v === "object") for (const x of Object.values(v)) placeholdersIn(x, out);
+ return out;
+}
+
+/**
+ * The board's own words for a prompt whose text inputs are drawn as text nodes: the inputs must sit together, one
+ * after the other with a blank line between (the way a node joins its connected texts), so they become "{text}" in
+ * the node's words. Null when they can't (two texts apart in one prompt).
+ */
+function ownWords(prompt: string, texts: Set<string>): { own: string; order: string[] } | null {
+ const hits = [...prompt.matchAll(ANY_PH)].filter((m) => texts.has(m[1]));
+ if (!hits.length) return { own: prompt, order: [] };
+ const start = hits[0].index!;
+ const last = hits[hits.length - 1];
+ const end = last.index! + last[0].length;
+ const order = hits.map((m) => m[1]);
+ if (new Set(order).size !== order.length || prompt.slice(start, end) !== order.map((id) => `{{${id}}}`).join("\n\n")) return null;
+ const before = prompt.slice(0, start);
+ const after = prompt.slice(end);
+ if (prompt.includes("{text}")) return null;
+ return { own: before.trim() || after.trim() ? `${before}{text}${after}` : "", order };
+}
+
+/**
+ * A recipe skill drawn as a board: each input an input node, each generate step a model node (on its model, op,
+ * words and settings), each chain slot an edge into the port it fills. Only when every slot maps to a port (prompt,
+ * images, endImage, video, videos, audio, voice ↔ voiceAudio): a recipe with a studio step, a settings input (a number
+ * or a choice), `audio2`, `audios`, `model3d` or `mask`, or a chain slot with a stem, an index or a position isn't
+ * drawn. The drawing is read back (canvasToRecipe) before it's offered: what wouldn't come back the same isn't drawn.
+ */
+export function recipeToCanvas(recipe: Recipe, opts: { elements?: readonly ElementInfo[]; from?: string } = {}): RecipeDrawing {
+ const refuse = (detail: string, studio?: string): RecipeDrawing => ({ ok: false, reason: CANT_DRAW, detail, ...(studio && { studio }) });
+ const tool = recipe.steps.find((s): s is Extract<RecipeStep, { kind: "tool" }> => s.kind === "tool");
+ if (tool) return refuse("It runs one studio step, which shows as a note: open it from Skills", tool.tool);
+ const settings = recipe.params.filter((p) => p.type === "number" || p.type === "choice");
+ if (settings.length) return refuse(`A canvas has no node for ${words(settings.map((p) => p.label))} yet`);
+ const params = new Map(recipe.params.map((p) => [p.id, p]));
+ const steps = recipe.steps as GenerateStep[];
+ const keys = new Set(steps.map((s) => s.key));
+ for (const s of steps) {
+ const r = s.request;
+ const name = s.label ?? s.key;
+ if (!getModel(r.modelId)) return refuse(`“${name}” uses a model that's no longer in the catalog`);
+ const after = asList(s.after);
+ if (after.some((a) => !keys.has(a))) return refuse(`“${name}” waits for a step that isn't there`);
+ for (const c of s.chain ?? []) {
+ if (!CHAIN_PORT[c.slot] || c.stem !== undefined || c.index !== undefined || c.position !== undefined || c.of !== undefined || c.optional) return refuse(`“${name}” takes an earlier step's result in a way a canvas can't wire yet`);
+ }
+ if ((s.chain?.length ?? after.length) !== after.length) return refuse(`“${name}” has a chain slot for each step it waits for only on a canvas`);
+ const inputs = (r.inputs ?? {}) as Record<string, unknown>;
+ if (Object.keys(inputs).some((k) => !FILE_PORT[k])) return refuse(`“${name}” uses an input a canvas has no port for yet`);
+ for (const [k, v] of Object.entries(r as Record<string, unknown>)) {
+ const found = placeholdersIn(v);
+ if (!found.length) continue;
+ if (k === "inputs") {
+ if (found.some((f) => !f.exact || !params.has(f.id) || !["image", "video", "audio"].includes(params.get(f.id)!.type))) return refuse(`“${name}” takes a file input a canvas can't wire yet`);
+ } else if (k === "prompt") {
+ if (found.some((f) => !params.has(f.id) || !["text", "element"].includes(params.get(f.id)!.type))) return refuse(`“${name}” puts an input into its words that a canvas can't draw yet`);
+ } else return refuse(`An input changes “${name}”'s settings, which a canvas can't draw yet`);
+ }
+ }
+ for (const p of recipe.params) if (p.type === "element" && !String(p.example ?? p.default ?? "").replace(/^@/, "")) return refuse(`Pick an element for ${p.label} first: a canvas draws it with one`);
+
+ // Layout: inputs on the left, each step one column right of the last step it waits for.
+ const depth = new Map<string, number>();
+ for (const s of steps) depth.set(s.key, 1 + Math.max(0, ...asList(s.after).map((a) => depth.get(a) ?? 0)));
+ const rowsAt = new Map<number, number>();
+ const COL = 400;
+ const nodes: CanvasNode[] = [];
+ const edges: CanvasEdge[] = [];
+ const stepNode = new Map<string, string>();
+ for (const s of steps) {
+ const d = depth.get(s.key)!;
+ const row = rowsAt.get(d) ?? 0;
+ rowsAt.set(d, row + 1);
+ const r = s.request as GenerationRequest & Record<string, unknown>;
+ const model = getModel(r.modelId)!;
+ const texts = new Set([...((typeof r.prompt === "string" ? r.prompt : "").matchAll(ANY_PH))].map((m) => m[1]).filter((id) => params.get(id)?.type === "text"));
+ let prompt = typeof r.prompt === "string" ? r.prompt : "";
+ const own = ownWords(prompt, texts);
+ if (!own) return refuse(`“${s.label ?? s.key}” puts two inputs apart in its words, which a canvas can't draw yet`);
+ prompt = own.own;
+ // An element input is the element's @handle in the words, its node wired into Elements.
+ for (const id of new Set([...prompt.matchAll(ANY_PH)].map((m) => m[1]))) {
+ const p = params.get(id);
+ if (p?.type === "element") prompt = prompt.replace(new RegExp(`\\{\\{\\s*${id}\\s*\\}\\}`, "g"), `@${String(p.example ?? p.default).replace(/^@/, "")}`);
+ }
+ const boardParams: GenParams = {};
+ const extraParams: Record<string, unknown> = {};
+ for (const [k, v] of Object.entries((r.params ?? {}) as Record<string, unknown>)) {
+ const scalar = k === "extra" && v && typeof v === "object" && Object.values(v).every((x) => ["string", "number", "boolean"].includes(typeof x));
+ if (BOARD_PARAMS.has(k) || scalar) (boardParams as Record<string, unknown>)[k] = v;
+ else extraParams[k] = v;
+ }
+ const extra: Record<string, unknown> = Object.fromEntries(Object.entries(r).filter(([k]) => !BOARD_FIELDS.has(k)));
+ if (r.engine && !dualEngine(model)) extra.engine = r.engine;
+ if (Object.keys(extraParams).length) extra.params = extraParams;
+ const mark: SkillStepMark = {
+ key: s.key,
+ ...(s.label && { label: s.label }),
+ ...(r.feature !== "skill" && { feature: r.feature }),
+ modelId: model.id,
+ ...(Object.keys(extra).length && { extra }),
+ ...(recipe.checks?.[s.key] && { check: recipe.checks[s.key] }),
+ ...(opts.from && { from: opts.from }),
+ };
+ const id = `step-${s.key}`.slice(0, 80);
+ stepNode.set(s.key, id);
+ const kind = model.kind === "image" || model.kind === "video" || model.kind === "audio" || model.kind === "text" ? model.kind : undefined;
+ nodes.push({
+ id,
+ type: "model",
+ position: { x: d * COL, y: row * 300 },
+ data: {
+ ...(s.label && { label: s.label }),
+ modelId: model.id,
+ ...(kind && { mediaKind: kind }),
+ ...(r.op && { op: r.op }),
+ ...(dualEngine(model) && r.engine && { engine: r.engine }),
+ ...(prompt && { prompt }),
+ ...(Object.keys(boardParams).length && { params: boardParams }),
+ skill: mark,
+ },
+ });
+ }
+ // Inputs, in the order the steps first use them (connected texts join a prompt top to bottom).
+ const inputNode = new Map<string, string>();
+ let inputRow = 0;
+ let fixedFiles = 0;
+ const addEdge = (source: string, target: string, port: InputPortId) => {
+ const e = { id: `e-${source}-${target}-${port}`.slice(0, 200), source, target, sourceHandle: OUT, targetHandle: port };
+ if (!edges.some((x) => x.id === e.id)) edges.push(e);
+ };
+ const paramNode = (p: RecipeParam): string => {
+ const known = inputNode.get(p.id);
+ if (known) return known;
+ const id = `input-${p.id}`;
+ inputNode.set(p.id, id);
+ const value = p.example ?? p.default;
+ const rest: Omit<RecipeParam, "label"> & { label?: string } = { ...p };
+ delete rest.label;
+ const mark: SkillInputMark = { param: rest, at: recipe.params.indexOf(p) };
+ const data: CanvasNodeData = { label: p.label, input: mark };
+ if (p.type === "text") data.text = typeof value === "string" ? value : value === undefined ? "" : String(value);
+ else if (p.type === "element") {
+ const handle = String(value).replace(/^@/, "");
+ const el = opts.elements?.find((e) => e.handle.toLowerCase() === handle.toLowerCase());
+ Object.assign(data, { handle: el?.handle ?? handle, ...(el && { elementId: el.id, name: el.name, ...(el.references[0] && { preview: el.references[0] }) }) });
+ } else if (typeof value === "string" && value.startsWith("/api/files/")) data.url = value;
+ nodes.push({ id, type: INPUT_NODE[p.type as keyof typeof INPUT_NODE], position: { x: 0, y: inputRow++ * 220 }, data });
+ return id;
+ };
+ for (const s of steps) {
+ const r = s.request;
+ const target = stepNode.get(s.key)!;
+ const prompt = typeof r.prompt === "string" ? r.prompt : "";
+ for (const m of prompt.matchAll(ANY_PH)) {
+ const p = params.get(m[1])!;
+ addEdge(paramNode(p), target, p.type === "element" ? "elements" : "prompt");
+ }
+ for (const [k, v] of Object.entries((r.inputs ?? {}) as Record<string, unknown>)) {
+ for (const item of Array.isArray(v) ? v : [v]) {
+ if (typeof item !== "string") return refuse(`“${s.label ?? s.key}” has a file a canvas can't hold`);
+ const ph = EXACT_PH.exec(item);
+ if (ph) {
+ addEdge(paramNode(params.get(ph[1])!), target, FILE_PORT[k]);
+ continue;
+ }
+ if (!item.startsWith("/api/files/")) return refuse(`“${s.label ?? s.key}” uses a linked file a canvas can't hold: add it to Fuobe first`);
+ const kind = k === "images" || k === "endImage" ? "image" : k === "video" || k === "videos" ? "video" : "audio";
+ const id = `file-${++fixedFiles}`;
+ nodes.push({ id, type: INPUT_NODE[kind], position: { x: 0, y: inputRow++ * 220 }, data: { url: item, input: { fixed: true } } });
+ addEdge(id, target, FILE_PORT[k]);
+ }
+ }
+ const after = asList(s.after);
+ after.forEach((a, i) => addEdge(stepNode.get(a)!, target, CHAIN_PORT[(s.chain?.[i] ?? { slot: "images" }).slot]!));
+ }
+ const graph: CanvasGraph = { nodes, edges };
+ // Read back: what wouldn't come back as the same skill isn't offered as a canvas.
+ const back = canvasToRecipe(graph);
+ if (!back.recipe) return refuse(back.problems[0]?.message ?? "It doesn't come back the same from a canvas");
+ if (recipeShape(back.recipe) !== recipeShape(recipe)) return refuse("It doesn't come back the same from a canvas");
+ return { ok: true, graph };
+}
+
+/** A recipe's params, steps and checks as one string, `after` as a list and each step's chain spelled out. */
+export function recipeShape(recipe: Pick<Recipe, "params" | "steps" | "checks">): string {
+ const stable = (v: unknown): unknown =>
+ Array.isArray(v)
+ ? v.map(stable)
+ : v && typeof v === "object"
+ ? Object.fromEntries(
+ Object.entries(v)
+ .filter(([, x]) => x !== undefined)
+ .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
+ .map(([k, x]) => [k, stable(x)]),
+ )
+ : v;
+ const steps = recipe.steps.map((s) => {
+ if (s.kind === "tool") return s;
+ const after = asList(s.after);
+ const request = { ...s.request, ...(typeof s.request.prompt === "string" && { prompt: s.request.prompt.trim() }) };
+ return { ...s, request, after, chain: s.chain ?? after.map(() => ({ slot: "images" })) };
+ });
+ return JSON.stringify(stable({ params: recipe.params, steps, checks: recipe.checks ?? {} }));
+}
+
+export interface CanvasRecipe {
+ /** Null when the board can't be a skill (`problems` say why). */
+ recipe: Recipe | null;
+ problems: { nodeId?: string; message: string }[];
+ /** A table's filled rows as the Many tab's rows (its columns are text inputs). */
+ rows?: RecipeValues[];
+ /** Inputs that varied in the skill the board was drawn from, and a table's columns: they start as Varies. */
+ varies: string[];
+ /** The skill the board was drawn from, when every step says the same. */
+ from?: string;
+ /** Each step's node (the review screen's thumbnails). */
+ nodes: Record<string, string>;
+}
+
+const nodeValue = (n: CanvasNode): string =>
+ n.type === "prompt" ? (n.data.text ?? "") : n.type === "element" ? (n.data.handle ? `@${n.data.handle}` : "") : (n.data.url ?? "");
+const INPUT_TYPE: Partial<Record<CanvasNodeType, "text" | "image" | "video" | "audio" | "element">> = { prompt: "text", "image-input": "image", "video-input": "video", "audio-input": "audio", "clone-voice": "audio", element: "element" };
+const stepKeyOf = (label: string) =>
+ label
+ .toLowerCase()
+ .normalize("NFKD")
+ .replace(/[^a-z0-9]+/g, "-")
+ .replace(/^-+|-+$/g, "")
+ .slice(0, 32) || "step";
+
+/**
+ * A board as a recipe (the board is the "demo", W5): each input node that feeds a step becomes an input (a file a step
+ * drawn from a skill always uses stays fixed), each generator node a generate step with its request as describeNode
+ * builds it, and a Video concatenator its cuts and join, as runCanvas runs them. A Table's columns become text inputs and
+ * its filled rows the Many tab's rows (at most TABLE_LIMITS: 50 rows of 8). A node that can't run says why.
+ */
+export function canvasToRecipe(graph: CanvasGraph, page: { canvas?: string } = {}): CanvasRecipe {
+ const nodes = nodeMap(graph);
+ const problems: CanvasRecipe["problems"] = [];
+ const fail = (): CanvasRecipe => ({ recipe: null, problems, varies: [], nodes: {} });
+ const generators = graph.nodes.filter(isGenerator);
+ if (!generators.length) {
+ problems.push({ message: "Add a model to the board first: a skill runs its steps" });
+ return fail();
+ }
+ const feedsAStep = (n: CanvasNode) => [...descendants(graph, [n.id])].some((id) => isGenerator(nodes.get(id)!));
+ const tables = graph.nodes.filter((n) => n.type === "table" && feedsAStep(n));
+ if (tables.length > 1) problems.push({ nodeId: tables[1].id, message: "One table per skill: its rows become the runs" });
+ const feeds = tablesFeeding(graph);
+ for (const n of generators.filter((g) => g.type === "concat")) {
+ if (concatClips(graph, n).some((c) => {
+ const r = feeds(nodes.get(c.nodeId)!);
+ return !!r && !("error" in r);
+ }))
+ problems.push({ nodeId: n.id, message: `“${nodeLabel(n)}” joins a clip per row of a table: a skill makes one film per run, so join the clips without the table` });
+ }
+ if (problems.length) return fail();
+
+ // Inputs: their ids (as drawn from a skill, else from their labels) in the form's order.
+ const inputs = graph.nodes
+ .filter((n) => INPUT_TYPE[n.type] && feedsAStep(n) && !n.data.input?.fixed && !(n.type === "clone-voice" && graph.edges.some((e) => e.target === n.id && e.targetHandle === "audio")))
+ .sort((a, b) => (a.data.input?.at ?? Infinity) - (b.data.input?.at ?? Infinity) || byPosition(a, b));
+ const taken = new Set<string>();
+ const idOf = new Map<string, string>();
+ for (const n of inputs) {
+ const want = n.data.input?.param?.id;
+ const id = want && !taken.has(want) ? want : paramIdFrom(nodeLabel(n), taken);
+ taken.add(id);
+ idOf.set(n.id, id);
+ }
+ const handles = new Map<string, string>();
+ const subNodes = graph.nodes.map((n): CanvasNode => {
+ const id = idOf.get(n.id);
+ if (!id) return n;
+ const type = INPUT_TYPE[n.type]!;
+ if (type === "text") return { ...n, data: { ...n.data, text: `{{${id}}}` } };
+ if (type === "element") {
+ if (n.data.handle) handles.set(n.data.handle.toLowerCase(), id);
+ return n;
+ }
+ return { ...n, data: { ...n.data, url: `/api/files/recipe-input/${id}.${SENTINEL_EXT[type]}` } };
+ });
+ // A table: its columns are inputs, read the way rowRequest reads a row ("{Column}" in a node's words, else the row as text).
+ const table = tables[0];
+ const columns: { id: string; label: string; j: number }[] = [];
+ let rows: RecipeValues[] | undefined;
+ let edges = graph.edges;
+ if (table) {
+ const filled = tableRows(table);
+ if (filled.length > TABLE_LIMITS.rows) problems.push({ nodeId: table.id, message: `“${nodeLabel(table)}” has ${filled.length} rows: a skill runs up to ${TABLE_LIMITS.rows} at once` });
+ (table.data.columns ?? ["Prompt"]).slice(0, TABLE_LIMITS.columns).forEach((c, j) => {
+ const label = c.trim() || `Column ${j + 1}`;
+ const id = paramIdFrom(label, taken);
+ taken.add(id);
+ columns.push({ id, label: label.slice(0, 60), j });
+ });
+ rows = filled.map((cells) => Object.fromEntries(columns.map((c) => [c.id, cells[c.j] ?? ""])));
+ const named = new Set<string>();
+ for (const [i, n] of subNodes.entries()) {
+ if (!graph.edges.some((e) => e.source === table.id && e.target === n.id)) continue;
+ const own = n.data.prompt ?? "";
+ const raw = table.data.columns ?? [];
+ if (!raw.some((c) => c.trim() && own.includes(`{${c.trim()}}`))) continue;
+ named.add(n.id);
+ subNodes[i] = { ...n, data: { ...n.data, prompt: columns.reduce((p, c) => (raw[c.j]?.trim() ? p.replaceAll(`{${raw[c.j].trim()}}`, `{{${c.id}}}`) : p), own) } };
+ }
+ edges = graph.edges.filter((e) => !(e.source === table.id && named.has(e.target)));
+ const at = subNodes.findIndex((n) => n.id === table.id);
+ subNodes[at] = { ...table, type: "prompt", data: { text: columns.map((c) => `{{${c.id}}}`).join(", ") } };
+ }
+ if (problems.length) return fail();
+ const sub: CanvasGraph = { ...graph, nodes: subNodes, edges };
+ const subMap = nodeMap(sub);
+ const { order, cyclic } = topoOrder(sub);
+ for (const id of cyclic) if (isGenerator(subMap.get(id)!)) problems.push({ nodeId: id, message: `“${nodeLabel(subMap.get(id)!)}” is part of a loop: remove one of its connections` });
+
+ const usedKeys = new Set<string>();
+ const keyOf = new Map<string, string>();
+ const freeKey = (want: string) => {
+ let k = want.slice(0, 40);
+ for (let i = 2; usedKeys.has(k); i++) k = `${want.slice(0, 36)}-${i}`;
+ usedKeys.add(k);
+ return k;
+ };
+ const steps: GenerateStep[] = [];
+ const checks: Record<string, RecipeCheck> = {};
+ const stepNodes: Record<string, string> = {};
+ const fromSkills = new Set<string>();
+ /** A chain slot's position only where it matters: before the step's own files in that list ("last" is the default). */
+ const chainOf = (req: GenerationRequest): ChainSlot[] | undefined =>
+ req.chain?.map((c) => {
+ const own = (req.inputs as Record<string, unknown> | undefined)?.[c.slot];
+ if (c.position === "first" && Array.isArray(own) && own.length) return c;
+ const rest = { ...c };
+ delete rest.position;
+ return rest;
+ });
+ const cleanRequest = (req: GenerationRequest, n: CanvasNode, main: boolean): GenerationRequest => {
+ const out: GenerationRequest & Record<string, unknown> = { ...req };
+ delete out.chain;
+ delete out.step;
+ const mark = n.data.skill;
+ if (main) out.feature = mark?.feature ?? "skill";
+ if (out.inputs) {
+ const inputs: Record<string, unknown> = {};
+ for (const [k, v] of Object.entries(out.inputs)) {
+ const swap = (x: unknown) => (typeof x === "string" && SENTINEL.test(x) ? `{{${SENTINEL.exec(x)![1]}}}` : x);
+ inputs[k] = Array.isArray(v) ? v.map(swap) : swap(v);
+ }
+ out.inputs = inputs as GenerationRequest["inputs"];
+ }
+ if (typeof out.prompt === "string") for (const [h, id] of handles) out.prompt = out.prompt.replace(handleRe(`@${h}`), `{{${id}}}`);
+ if (main && mark && mark.modelId === req.modelId && mark.extra) {
+ const { params, ...rest } = mark.extra as { params?: Record<string, unknown> };
+ Object.assign(out, rest);
+ if (params) out.params = { ...out.params, ...params } as GenParams;
+ }
+ return out;
+ };
+ for (const id of order) {
+ const n = subMap.get(id)!;
+ if (!isGenerator(n)) continue;
+ const d = describeNode(sub, n);
+ if (d.error || !d.request || !d.deps) {
+ problems.push({ nodeId: n.id, message: `“${nodeLabel(n)}”: ${d.error ?? "it can't run"}` });
+ continue;
+ }
+ const mark = n.data.skill;
+ if (mark?.from) fromSkills.add(mark.from);
+ const key = freeKey(mark?.key && !usedKeys.has(mark.key) ? mark.key : stepKeyOf(nodeLabel(n)));
+ keyOf.set(n.id, key);
+ stepNodes[key] = n.id;
+ const label = n.data.label?.trim() || mark?.label;
+ if (!d.steps) {
+ const after = d.deps.map((dep) => keyOf.get(dep)!);
+ steps.push({
+ key,
+ kind: "generate",
+ ...(label && { label: label.slice(0, 80) }),
+ ...(after.length && { after }),
+ ...(d.request.chain?.length && { chain: chainOf(d.request) }),
+ request: cleanRequest(d.request, n, true) as GenerateStep["request"],
+ });
+ } else {
+ const sub2 = new Map(d.steps.map((st) => [st.key, st.request === d.request ? key : freeKey(`${key}-${st.key.replace(/[^A-Za-z0-9_-]+/g, "-")}`)]));
+ for (const st of d.steps) {
+ const after = st.from.map((f) => ("node" in f ? keyOf.get(f.node)! : sub2.get(f.step)!));
+ const main = st.request === d.request;
+ const own = main ? label : st.request.step?.label;
+ steps.push({
+ key: sub2.get(st.key)!,
+ kind: "generate",
+ ...(own && { label: own.slice(0, 80) }),
+ ...(after.length && { after }),
+ ...(st.request.chain?.length && { chain: chainOf(st.request) }),
+ request: cleanRequest(st.request, n, main) as GenerateStep["request"],
+ });
+ }
+ }
+ if (mark?.check) checks[key] = mark.check;
+ else if (!mark) {
+ const kind = d.plan ? OPERATION_OUTPUT[d.plan.op] : undefined;
+ if (kind && kind !== "text" && !graph.edges.some((e) => e.source === n.id && isGenerator(nodes.get(e.target) ?? n) && e.target !== n.id)) checks[key] = { kind: kind as RecipeCheck["kind"] };
+ }
+ }
+ if (problems.length) return fail();
+ if (steps.length > 24) {
+ problems.push({ message: `The board makes ${steps.length} steps: a skill has at most 24` });
+ return fail();
+ }
+ // Inputs no step uses (a text joined nowhere, an element whose node isn't wired) are left out.
+ const used = new Set(steps.flatMap((s) => placeholdersIn(s.request).map((p) => p.id)));
+ const params: RecipeParam[] = [];
+ const varies: string[] = [];
+ for (const n of inputs) {
+ const id = idOf.get(n.id)!;
+ if (!used.has(id)) continue;
+ const mark = n.data.input?.param;
+ const value = nodeValue(n);
+ const type = INPUT_TYPE[n.type]!;
+ const label = nodeLabel(n).slice(0, 60);
+ if (mark) {
+ const was = mark.example ?? mark.default;
+ const p: RecipeParam = { ...mark, id, label, type };
+ if (value && value !== was) p.example = value.length > 2000 ? undefined : value;
+ params.push(p);
+ varies.push(id);
+ } else params.push({ id, label, type, required: true, ...(value && value.length <= 2000 && { example: value }) });
+ }
+ for (const c of columns) {
+ if (!used.has(c.id)) continue;
+ const first = rows?.[0]?.[c.id];
+ params.push({ id: c.id, label: c.label, type: "text", required: true, ...(typeof first === "string" && first && { example: first }) });
+ varies.push(c.id);
+ }
+ if (rows) rows = rows.map((r) => Object.fromEntries(Object.entries(r).filter(([k]) => used.has(k))));
+ const recipe: Recipe = {
+ fuobeRecipe: 1,
+ params,
+ steps: steps as RecipeStep[],
+ ...(Object.keys(checks).length && { checks }),
+ source: { from: "canvas", ...(page.canvas && { canvas: page.canvas }) },
+ };
+ return { recipe, problems, ...(rows && rows.length && { rows }), varies, ...(fromSkills.size === 1 && { from: [...fromSkills][0] }), nodes: stepNodes };
+}
diff --git a/src/lib/recipe-builder.ts b/src/lib/recipe-builder.ts
index d4ba1e7c..ef6e870d 100644
--- a/src/lib/recipe-builder.ts
+++ b/src/lib/recipe-builder.ts
@@ -65,6 +65,13 @@ export interface DraftCandidate {
setting?: SettingId;
/** Why it can't vary. */
locked?: string;
+ /**
+ * Words inside the text at each spot (a canvas's text node joined into a step's words, W5): it varies as those words,
+ * not as the whole text.
+ */
+ part?: boolean;
+ /** It starts as Varies (an input of the skill a canvas was drawn from, a Table's column). */
+ varies?: boolean;
}
export type SettingId = "numOutputs" | "aspectRatio" | "duration" | "resolution";
@@ -85,6 +92,10 @@ export interface SkillDraft {
notes: string[];
/** The LLM the optional naming pass would ask (null: none, so there is none). */
llm: "claude" | "ollama" | null;
+ /** A canvas's Table rows (W5), by candidate id: the Run form's Many tab starts with them. */
+ rows?: Record<string, string>[];
+ /** The user skill a canvas was drawn from (W5): saving can put the steps back into it. */
+ replaces?: { name: string; title: string };
}
/** A picked result the builder couldn't use, and why. */
@@ -345,10 +356,10 @@ export function buildRecipe(draft: Pick<SkillDraft, "recipe" | "candidates">, ch
const step = byKey.get(spot.step);
if (!step) continue;
const body = bodyOf(step);
- if (c.kind === "element") {
+ if (c.kind === "element" || c.part) {
const words = getAt(body, spot.path);
if (typeof words !== "string") continue;
- const next = words.replace(handleRe(String(c.example)), `{{${id}}}`);
+ const next = c.kind === "element" ? words.replace(handleRe(String(c.example)), `{{${id}}}`) : words.split(String(c.example)).join(`{{${id}}}`);
if (next !== words) {
setAt(body, spot.path, next);
used++;
@@ -406,6 +417,69 @@ export function buildRecipe(draft: Pick<SkillDraft, "recipe" | "candidates">, ch
return recipe;
}
+// ---------------------------------------------------------------------------
+// A recipe made elsewhere (a canvas, W5) as a draft
+
+/** Every spot of each placeholder in a value: its path, and whether the placeholder is the whole string there. */
+function spotsIn(v: unknown, path: (string | number)[], out: Map<string, { path: (string | number)[]; whole: boolean }[]>): void {
+ if (typeof v === "string") {
+ for (const m of v.matchAll(/\{\{\s*([A-Za-z][A-Za-z0-9_]*)\s*\}\}/g)) {
+ const list = out.get(m[1]) ?? [];
+ if (!list.some((x) => x.path.join("/") === path.join("/"))) list.push({ path, whole: m[0] === v });
+ out.set(m[1], list);
+ }
+ } else if (Array.isArray(v)) v.forEach((x, i) => spotsIn(x, [...path, i], out));
+ else if (v && typeof v === "object") for (const [k, x] of Object.entries(v)) spotsIn(x, [...path, k], out);
+}
+
+/**
+ * A recipe with inputs (a canvas read as a recipe, W5) as the review screen's draft: each input put back to the value it
+ * has (Fixed), and a candidate that puts it back as an input when it varies. `varies`: the inputs that start as Varies.
+ * The words of a text input joined into longer words vary as those words (`part`).
+ */
+export function fixedDraft(recipe: Recipe, varies: string[] = []): Pick<SkillDraft, "recipe" | "candidates"> {
+ const fixed = clone(recipe);
+ const candidates: DraftCandidate[] = [];
+ const spots = new Map<string, { step: string; path: (string | number)[]; whole: boolean }[]>();
+ for (const s of fixed.steps) {
+ const found = new Map<string, { path: (string | number)[]; whole: boolean }[]>();
+ spotsIn(bodyOf(s), [], found);
+ for (const [id, list] of found) spots.set(id, [...(spots.get(id) ?? []), ...list.map((x) => ({ ...x, step: s.key }))]);
+ }
+ const byKey = new Map(fixed.steps.map((s) => [s.key, s]));
+ for (const p of recipe.params) {
+ const at = spots.get(p.id) ?? [];
+ if (!at.length) continue;
+ const value = p.example ?? p.default ?? "";
+ for (const spot of at) {
+ const body = bodyOf(byKey.get(spot.step)!);
+ const cur = getAt(body, spot.path);
+ if (typeof cur !== "string") continue;
+ setAt(body, spot.path, spot.whole && typeof value === "number" ? value : cur.replace(new RegExp(`\\{\\{\\s*${p.id}\\s*\\}\\}`, "g"), String(value)));
+ }
+ const kind: CandidateKind = isMediaType(p.type) ? "media" : p.type === "element" ? "element" : p.type === "text" ? "text" : "setting";
+ const part = kind === "text" && at.some((x) => !x.whole);
+ const text = String(value);
+ const locked = kind === "text" && !text.trim() ? "Write its words on the canvas first" : kind === "text" && text.length > 2000 ? "Too long to change in a form (over 2000 characters)" : undefined;
+ candidates.push({
+ id: p.id,
+ label: p.label,
+ type: p.type,
+ kind,
+ example: value,
+ at: at.map(({ step, path }) => ({ step, path })),
+ ...(part && { part: true }),
+ ...(p.max !== undefined && { max: p.max }),
+ ...(p.min !== undefined && { min: p.min }),
+ ...(p.options && { options: p.options }),
+ ...(locked && { locked }),
+ ...(!locked && varies.includes(p.id) && { varies: true }),
+ });
+ }
+ fixed.params = [];
+ return { recipe: fixed, candidates };
+}
+
// ---------------------------------------------------------------------------
// SKILL.md
diff --git a/tests/canvas-recipe.test.ts b/tests/canvas-recipe.test.ts
new file mode 100644
index 00000000..1f1b8e0d
--- /dev/null
+++ b/tests/canvas-recipe.test.ts
@@ -0,0 +1,231 @@
+import { describe, expect, it } from "vitest";
+import { CANT_DRAW, canvasToRecipe, recipeShape, recipeToCanvas, TABLE_LIMITS, type CanvasGraph } from "@/lib/catalog/canvas";
+import { RecipeSchema, recipeProblems, type Recipe } from "@/lib/recipe";
+import { buildRecipe, fixedDraft } from "@/lib/recipe-builder";
+
+/** Skills as canvases (W5): a recipe drawn as a board and read back, a board as a recipe, and what isn't drawn. */
+
+const recipe = (r: Omit<Recipe, "fuobeRecipe">): Recipe => RecipeSchema.parse({ fuobeRecipe: 1, ...r });
+
+/** Cut-out → scene → turntable: a file and words in, a chain of three steps, a check on the last. */
+const turntable = recipe({
+ params: [
+ { id: "product", label: "Product photo", type: "image", required: true, example: "/api/files/uploads/lamp.png" },
+ { id: "name", label: "Product name", type: "text", required: true, example: "Lumen desk lamp", max: 200 },
+ ],
+ steps: [
+ { key: "cutout", kind: "generate", label: "Cut out", request: { feature: "skill", modelId: "birefnet-cloud", op: "bg-remove", inputs: { images: ["{{product}}"] } } },
+ {
+ key: "scene",
+ kind: "generate",
+ after: ["cutout"],
+ chain: [{ slot: "images" }],
+ request: { feature: "skill", modelId: "nano-banana-pro", op: "edit", prompt: "{{name}} on a travertine plinth, soft window light", params: { aspectRatio: "1:1" } },
+ },
+ {
+ key: "spin",
+ kind: "generate",
+ after: ["scene"],
+ chain: [{ slot: "images" }],
+ request: { feature: "image", modelId: "kling-3", op: "i2v", prompt: "slow turntable of the product", negativePrompt: "blur", params: { duration: 5, enhancePrompt: false } },
+ },
+ ],
+ checks: { spin: { kind: "video", count: 1, aspect: "1:1" } },
+});
+
+const drawn = (r: Recipe): CanvasGraph => {
+ const d = recipeToCanvas(r);
+ if (!d.ok) throw new Error(`${d.reason}: ${d.detail}`);
+ return d.graph;
+};
+
+describe("recipe → canvas → recipe", () => {
+ it("draws inputs as input nodes and steps as model nodes wired by their chain slots", () => {
+ const g = drawn(turntable);
+ expect(g.nodes.filter((n) => n.type === "model")).toHaveLength(3);
+ expect(g.nodes.find((n) => n.id === "input-product")).toMatchObject({ type: "image-input", data: { url: "/api/files/uploads/lamp.png", label: "Product photo" } });
+ expect(g.nodes.find((n) => n.id === "input-name")).toMatchObject({ type: "prompt", data: { text: "Lumen desk lamp" } });
+ // The words are the node's own, the input joins them where it was.
+ expect(g.nodes.find((n) => n.id === "step-scene")!.data.prompt).toBe("{text} on a travertine plinth, soft window light");
+ expect(g.edges.map((e) => `${e.source}>${e.target}:${e.targetHandle}`).sort()).toEqual(
+ ["input-name>step-scene:prompt", "input-product>step-cutout:images", "step-cutout>step-scene:images", "step-scene>step-spin:images"].sort(),
+ );
+ });
+
+ it("comes back as the same recipe (keys, labels, features, extras and checks included)", () => {
+ const back = canvasToRecipe(drawn(turntable));
+ expect(back.problems).toEqual([]);
+ expect(recipeShape(back.recipe!)).toBe(recipeShape(turntable));
+ expect(back.recipe!.steps).toEqual(turntable.steps);
+ expect(back.recipe!.params).toEqual(turntable.params);
+ expect(back.recipe!.checks).toEqual(turntable.checks);
+ expect(back.varies.sort()).toEqual(["name", "product"]);
+ expect(recipeProblems(back.recipe!)).toEqual([]);
+ });
+
+ it("keeps a fixed file fixed, an element as its node, two texts in order, and a model swapped on the board", () => {
+ const r = recipe({
+ params: [
+ { id: "who", label: "Who", type: "element", required: true, example: "@Maya" },
+ { id: "place", label: "Place", type: "text", required: true, example: "a rooftop in Lisbon" },
+ { id: "mood", label: "Mood", type: "text", example: "golden hour" },
+ ],
+ steps: [
+ {
+ key: "still",
+ kind: "generate",
+ request: { feature: "skill", modelId: "nano-banana-pro", op: "edit", prompt: "{{who}} stands on {{place}}\n\n{{mood}}", inputs: { images: ["/api/files/uploads/logo.png"] } },
+ },
+ ],
+ });
+ const g = drawn(r);
+ expect(g.nodes.find((n) => n.type === "element")).toMatchObject({ data: { handle: "Maya" } });
+ expect(g.nodes.find((n) => n.id === "file-1")).toMatchObject({ type: "image-input", data: { input: { fixed: true } } });
+ const back = canvasToRecipe(g);
+ expect(back.recipe!.steps).toEqual(r.steps);
+ expect(back.recipe!.params).toEqual(r.params);
+ // Swapping the model on the board: the request follows the new model.
+ const swapped = { ...g, nodes: g.nodes.map((n) => (n.type === "model" ? { ...n, data: { ...n.data, modelId: "qwen-image-edit-2511-cloud" } } : n)) };
+ expect((canvasToRecipe(swapped).recipe!.steps[0] as { request: { modelId: string } }).request.modelId).toBe("qwen-image-edit-2511-cloud");
+ });
+});
+
+describe("what isn't drawn", () => {
+ const base = turntable.steps[1] as Extract<Recipe["steps"][number], { kind: "generate" }>;
+ const refused = (r: Recipe) => {
+ const d = recipeToCanvas(r);
+ expect(d.ok).toBe(false);
+ if (!d.ok) expect(d.reason).toBe(CANT_DRAW);
+ return d.ok ? "" : d.detail;
+ };
+ const withStep = (patch: object, params = turntable.params) => recipe({ params, steps: [turntable.steps[0], { ...base, ...patch } as Recipe["steps"][number]] });
+
+ it.each([
+ ["audio2", { chain: [{ slot: "audio2" }] }],
+ ["audios", { chain: [{ slot: "audios" }] }],
+ ["model3d", { chain: [{ slot: "model3d" }] }],
+ ["mask", { chain: [{ slot: "mask" }] }],
+ ["a stem", { chain: [{ slot: "audio", stem: "music" }] }],
+ ["an index", { chain: [{ slot: "images", index: 0 }] }],
+ ["a position", { chain: [{ slot: "images", position: "first" }] }],
+ ])("a chain slot with %s", (_, patch) => {
+ expect(refused(withStep(patch))).toMatch(/can't wire yet/);
+ });
+
+ it("an input a canvas has no port for", () => {
+ expect(refused(withStep({ request: { ...base.request, inputs: { mask: "/api/files/m.png" } } }))).toMatch(/no port/);
+ });
+
+ it("a settings input (a number or a choice)", () => {
+ const r = recipe({
+ params: [{ id: "takes", label: "Takes", type: "number", default: 1, min: 1, max: 4 }],
+ steps: [{ key: "a", kind: "generate", request: { feature: "skill", modelId: "nano-banana-pro", op: "t2i", prompt: "a lamp", params: { numOutputs: "{{takes}}" } } }],
+ });
+ expect(refused(r)).toMatch(/no node for Takes/);
+ });
+
+ it("a studio step shows as one note, never nodes", () => {
+ const r = recipe({ params: [], steps: [{ key: "dub", kind: "tool", tool: "dub_video", input: { video: "/api/files/a.mp4", language: "es" } }] });
+ const d = recipeToCanvas(r);
+ expect(d).toMatchObject({ ok: false, reason: CANT_DRAW, studio: "dub_video" });
+ });
+
+ it("two text inputs apart in one prompt", () => {
+ const r = recipe({
+ params: [
+ { id: "a", label: "A", type: "text", example: "x" },
+ { id: "b", label: "B", type: "text", example: "y" },
+ ],
+ steps: [{ key: "s", kind: "generate", request: { feature: "skill", modelId: "nano-banana-pro", op: "t2i", prompt: "{{a}} next to {{b}}" } }],
+ });
+ expect(refused(r)).toMatch(/apart/);
+ });
+});
+
+describe("canvas → recipe", () => {
+ const board = (nodes: CanvasGraph["nodes"], edges: [string, string, string][]): CanvasGraph => ({
+ nodes,
+ edges: edges.map(([source, target, targetHandle]) => ({ id: `${source}-${target}-${targetHandle}`, source, target, sourceHandle: "out", targetHandle })),
+ });
+
+ it("makes unconnected input nodes inputs and model nodes steps", () => {
+ const g = board(
+ [
+ { id: "p", type: "prompt", position: { x: 0, y: 0 }, data: { text: "a red fox in snow" } },
+ { id: "img", type: "image-input", position: { x: 0, y: 200 }, data: { url: "/api/files/uploads/fox.png" } },
+ { id: "m", type: "model", position: { x: 400, y: 0 }, data: { modelId: "nano-banana-pro", op: "edit" } },
+ { id: "v", type: "model", position: { x: 800, y: 0 }, data: { modelId: "kling-3", op: "i2v", prompt: "it runs" } },
+ ],
+ [
+ ["p", "m", "prompt"],
+ ["img", "m", "images"],
+ ["m", "v", "images"],
+ ],
+ );
+ const { recipe: r, problems, varies } = canvasToRecipe(g, { canvas: "c1" });
+ expect(problems).toEqual([]);
+ expect(varies).toEqual([]);
+ expect(r!.params.map((p) => [p.id, p.type, p.example])).toEqual([
+ ["text_prompt", "text", "a red fox in snow"],
+ ["image", "image", "/api/files/uploads/fox.png"],
+ ]);
+ expect(r!.steps).toMatchObject([
+ { key: "nano-banana-pro", request: { feature: "skill", modelId: "nano-banana-pro", op: "edit", prompt: "{{text_prompt}}", inputs: { images: ["{{image}}"] } } },
+ { key: "kling-3-0", after: ["nano-banana-pro"], chain: [{ slot: "images" }], request: { modelId: "kling-3", op: "i2v", prompt: "it runs" } },
+ ]);
+ expect(r!.checks).toEqual({ "kling-3-0": { kind: "video" } });
+ expect(r!.source).toEqual({ from: "canvas", canvas: "c1" });
+ expect(recipeProblems(r!)).toEqual([]);
+ // The draft starts with every input Fixed: the recipe as it ran, and the inputs that could vary.
+ const draft = fixedDraft(r!, varies);
+ expect(draft.recipe.params).toEqual([]);
+ expect((draft.recipe.steps[0] as { request: { prompt: string } }).request.prompt).toBe("a red fox in snow");
+ expect(draft.candidates.map((c) => [c.id, c.kind, c.varies ?? false])).toEqual([
+ ["text_prompt", "text", false],
+ ["image", "media", false],
+ ]);
+ expect(buildRecipe(draft, { varies: { image: true, text_prompt: true } }).steps).toEqual(r!.steps);
+ });
+
+ it("turns a Table's columns into text inputs and its rows into the Many tab's rows", () => {
+ const g = board(
+ [
+ { id: "t", type: "table", position: { x: 0, y: 0 }, data: { columns: ["Animal", "Place"], rows: [["fox", "snow"], ["owl", "forest"], ["", ""]] } },
+ { id: "m", type: "model", position: { x: 400, y: 0 }, data: { modelId: "nano-banana-pro", op: "t2i", prompt: "a {Animal} in the {Place}" } },
+ ],
+ [["t", "m", "prompt"]],
+ );
+ const { recipe: r, rows, varies, problems } = canvasToRecipe(g);
+ expect(problems).toEqual([]);
+ expect(r!.params.map((p) => p.id)).toEqual(["animal", "place"]);
+ expect((r!.steps[0] as { request: { prompt: string } }).request.prompt).toBe("a {{animal}} in the {{place}}");
+ expect(rows).toEqual([
+ { animal: "fox", place: "snow" },
+ { animal: "owl", place: "forest" },
+ ]);
+ expect(varies).toEqual(["animal", "place"]);
+ });
+
+ it("reads a table as text when no column is named, and refuses more rows than a run takes", () => {
+ const many = Array.from({ length: TABLE_LIMITS.rows + 1 }, (_, i) => [`row ${i}`]);
+ const g = board(
+ [
+ { id: "t", type: "table", position: { x: 0, y: 0 }, data: { columns: ["Subject"], rows: many } },
+ { id: "m", type: "model", position: { x: 400, y: 0 }, data: { modelId: "nano-banana-pro", op: "t2i" } },
+ ],
+ [["t", "m", "prompt"]],
+ );
+ expect(canvasToRecipe(g).problems[0].message).toMatch(/up to 50/);
+ const ok = canvasToRecipe({ ...g, nodes: g.nodes.map((n) => (n.id === "t" ? { ...n, data: { ...n.data, rows: many.slice(0, 2) } } : n)) });
+ expect((ok.recipe!.steps[0] as { request: { prompt: string } }).request.prompt).toBe("{{subject}}");
+ expect(ok.rows).toEqual([{ subject: "row 0" }, { subject: "row 1" }]);
+ });
+
+ it("names a node that can't run, and a board with no model", () => {
+ expect(canvasToRecipe({ nodes: [], edges: [] }).problems[0].message).toMatch(/Add a model/);
+ const g = board([{ id: "m", type: "model", position: { x: 0, y: 0 }, data: { modelId: "kling-3", op: "i2v" } }], []);
+ const out = canvasToRecipe(g);
+ expect(out.recipe).toBeNull();
+ expect(out.problems[0]).toMatchObject({ nodeId: "m" });
+ });
+});