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.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
canvas: text nodes write by default, script → film through a text model, engine-locked palette
Claude7782828255ef
diff --git a/src/lib/catalog/canvas.ts b/src/lib/catalog/canvas.tsindex ad22ff15..628f97fe 100644--- a/src/lib/catalog/canvas.ts+++ b/src/lib/catalog/canvas.ts@@ -149,6 +149,9 @@ export interface CanvasSummary {export const EMPTY_GRAPH: CanvasGraph = { nodes: [], edges: [] };+/** Longest text a prompt, script or note may hold (longer pastes are cut, never refused). */+export const CANVAS_TEXT_MAX = 10_000;+// ---------------------------------------------------------------------------// Node registry@@ -194,6 +197,8 @@ export interface PaletteItem {icon: string;data?: CanvasNodeData;keywords?: string;+ /** Why it can't be added on this install (no model for it on the only engine allowed). */+ disabled?: string;}export const PALETTE: PaletteItem[] = [@@ -221,9 +226,21 @@ function pick(type: CanvasNodeType): Omit<PaletteItem, "id"> {return { type, label: s.label, group: s.group, description: s.description, icon: s.icon };}-/** Palette entries whose models exist in this catalog (no text models until an LLM model is installed). */-export function paletteItems(): PaletteItem[] {- return PALETTE.filter((p) => p.id !== "model-text" || MODELS.some((m) => m.kind === "text" && !m.hidden));+const paletteCache = new Map<string, PaletteItem[]>();++/**+ * Palette entries whose models exist in this catalog (no text models until an LLM model is+ * installed). On an install locked to one engine, entries with no model there are disabled, with why.+ */+export function paletteItems(mode: "both" | "open" | "cloud" = "both"): PaletteItem[] {+ const hit = paletteCache.get(mode);+ if (hit) return hit;+ const items = PALETTE.filter((p) => p.id !== "model-text" || MODELS.some((m) => m.kind === "text" && !m.hidden)).map((p): PaletteItem => {+ if (mode === "both" || !isGenerator(p) || defaultModelForNode({ type: p.type, data: p.data ?? {} }, mode)) return p;+ return { ...p, disabled: `No ${mode === "open" ? "open-source" : "cloud"} model for ${p.label.toLowerCase()} yet` };+ });+ paletteCache.set(mode, items);+ return items;}export function nodeLabel(n: Pick<CanvasNode, "type" | "data">): string {@@ -639,6 +656,34 @@ function sourceOf(s: CanvasNode): Source | { error: string } {return { error: `${nodeLabel(s)} has no output` };}+/**+ * A text model (LLM) on Auto writes, unless a language to translate into is set. (The catalog+ * lists translate first, so leaving it to inferOperation would translate a "Write a slogan…" prompt.)+ */+function textOp(model: ModelDef, params: GenParams | undefined): Operation | undefined {+ if (model.kind !== "text") return undefined;+ if (params?.targetLanguage && supportsOp(model, "translate")) return "translate";+ return supportsOp(model, "write") ? "write" : undefined;+}++/** What a generator node's own text box asks for, by the op it runs. */+export function promptPlaceholder(op: Operation | undefined): string {+ switch (op) {+ case "write":+ return "What should it write? Connected text joins in, or use {text}";+ case "translate":+ return "Text to translate, or connect a text";+ case "tts":+ return "Text to speak, or connect a text";+ case "sfx":+ return "Describe the sound, or connect a text";+ case "music":+ return "Describe the music: style, mood, instruments";+ default:+ return "Describe it — connected text joins in, or use {text}";+ }+}+/** The operation a node runs: its own choice, the utility's op, or inferred from what's connected. */export function effectiveOp(graph: CanvasGraph, n: CanvasNode): Operation | undefined {const model = n.data.modelId ? getModel(n.data.modelId) : undefined;@@ -646,6 +691,8 @@ export function effectiveOp(graph: CanvasGraph, n: CanvasNode): Operation | undeif (n.data.op && ops.includes(n.data.op)) return n.data.op;if (n.type !== "model") return ops[0];if (!model) return undefined;+ const text = textOp(model, n.data.params);+ if (text) return text;const built = buildRequest(graph, n, model);if ("error" in built) return undefined;let op = inferOperation(model, withPlaceholders(built.request));@@ -664,12 +711,18 @@ function mentionLine(handles: string[]): string {return `With ${list.length > 1 ? `${list.slice(0, -1).join(", ")} and ${list[list.length - 1]}` : list[0]}.`;}+/** Handles already @mentioned in a text, lowercased (the same pattern as elements.ts: letters, digits, _ and -). */+function mentionedIn(text: string): Set<string> {+ return new Set([...text.matchAll(/@([\p{L}\p{N}_-]+)/gu)].map((m) => m[1].toLowerCase()));+}+function buildRequest(graph: CanvasGraph, n: CanvasNode, model: ModelDef): NodeRequest | { error: string } {const nodes = nodeMap(graph);const ports = inputPorts(n);const ops = nodeOps(n, model);- const op = n.data.op && ops.includes(n.data.op) ? n.data.op : n.type !== "model" ? ops[0] : undefined;+ const op = n.data.op && ops.includes(n.data.op) ? n.data.op : n.type !== "model" ? ops[0] : textOp(model, n.data.params);if (n.type !== "model" && op && !supportsOp(model, op)) return { error: `${model.name} can't do ${OPERATION_LABELS[op].toLowerCase()} — pick another model` };+ if (op === "translate" && !n.data.params?.targetLanguage) return { error: "Pick a language to translate into" };const inputs: NonNullable<GenerationRequest["inputs"]> = {};const chain: ChainSlot[] = [];@@ -739,7 +792,8 @@ function buildRequest(graph: CanvasGraph, n: CanvasNode, model: ModelDef): NodeRconst own = (n.data.prompt ?? "").trim();const slot = own.includes("{text}");const parts = generatedText || !slot ? [own, ...texts].filter(Boolean) : [own.replaceAll("{text}", texts.join("\n\n")).trim()].filter(Boolean);- const mentions = mentionLine(handles.filter((h) => !new RegExp(`@${h}(?![\\p{L}\\p{N}_-])`, "iu").test(parts.join(" "))));+ const already = mentionedIn(parts.join(" "));+ const mentions = mentionLine(handles.filter((h) => !already.has(h.toLowerCase())));if (mentions) parts.push(mentions);if (generatedText && !slot) parts.push("{text}");const prompt = parts.join("\n\n");@@ -1145,50 +1199,55 @@ export const CANVAS_TEMPLATES: CanvasTemplate[] = [{id: "script-film",name: "Script → storyboard → film",- description: "A short script becomes three frames, three shots and one film",+ description: "A text model breaks a short script into three shots: three frames, three clips, one film",icon: "GalleryHorizontal",hue: 200,build: tpl([{ id: "script", type: "prompt", x: 0, y: ROW, data: { label: "Script", text: "A paper boat sails down a rain-soaked city gutter, rides a little waterfall into a storm drain and comes out on a sunny canal." } },+ // The script reaches every shot through a text model: edit the script and all three shots follow....[0, 1, 2].map((i) => ({- id: `frame${i + 1}`,+ id: `beat${i + 1}`,type: "model" as const,x: COL,y: i * ROW,data: {- mediaKind: "image" as const,- label: `Frame ${i + 1}`,- prompt: [- "Storyboard frame 1 of 3 — the opening moment of this story. Cinematic still, consistent look:",- "Storyboard frame 2 of 3 — the turning point of this story. Cinematic still, consistent look:",- "Storyboard frame 3 of 3 — the ending of this story. Cinematic still, consistent look:",- ][i],- params: { aspectRatio: "16:9" },+ mediaKind: "text" as const,+ label: `Shot ${i + 1} text`,+ op: "write" as const,+ prompt: `Describe shot ${i + 1} of 3 of this script (${["the opening moment", "the turning point", "the ending"][i]}) as one cinematic sentence. Reply with the sentence only.\n\n{text}`,},+ models: { cloud: "text-llm", open: "oss-text-llm" },+ })),+ ...[0, 1, 2].map((i) => ({+ id: `frame${i + 1}`,+ type: "model" as const,+ x: COL * 2,+ y: i * ROW,+ data: { mediaKind: "image" as const, label: `Frame ${i + 1}`, prompt: "Cinematic storyboard still, one consistent look across the film: {text}", params: { aspectRatio: "16:9" } },models: { cloud: "nano-banana-pro", open: "oss-z-image-turbo" },})),...[0, 1, 2].map((i) => ({id: `shot${i + 1}`,type: "model" as const,- x: COL * 2,+ x: COL * 3,y: i * ROW,- data: {- mediaKind: "video" as const,- label: `Shot ${i + 1}`,- op: "i2v" as const,- prompt: ["The boat drifts forward, rain ripples the water, gentle tracking shot", "The boat tips over the edge and drops, the camera follows it down", "The boat glides into sunlight on the canal, the camera pulls back"][i],- params: { aspectRatio: "16:9", duration: 5 },- },+ data: { mediaKind: "video" as const, label: `Shot ${i + 1}`, op: "i2v" as const, prompt: "Animate this moment, smooth cinematic camera move: {text}", params: { aspectRatio: "16:9", duration: 5 } },models: { cloud: "kling-2-5-turbo", open: "oss-wan22-14b-fast" },})),- { id: "film", type: "join", x: COL * 3, y: ROW, data: { label: "Film" }, models: { cloud: "join-clips", open: "oss-join-clips" } },- { id: "out", type: "output", x: COL * 4, y: ROW, data: { label: "Film" } },+ { id: "film", type: "join", x: COL * 4, y: ROW / 2, data: { label: "Film" }, models: { cloud: "join-clips", open: "oss-join-clips" } },+ { id: "out", type: "output", x: COL * 4, y: ROW * 1.5, data: { label: "Film" } },],[- ["script", "frame1", "prompt"],- ["script", "frame2", "prompt"],- ["script", "frame3", "prompt"],+ ["script", "beat1", "prompt"],+ ["script", "beat2", "prompt"],+ ["script", "beat3", "prompt"],+ ["beat1", "frame1", "prompt"],+ ["beat2", "frame2", "prompt"],+ ["beat3", "frame3", "prompt"],+ ["beat1", "shot1", "prompt"],+ ["beat2", "shot2", "prompt"],+ ["beat3", "shot3", "prompt"],["frame1", "shot1", "images"],["frame2", "shot2", "images"],["frame3", "shot3", "images"],diff --git a/tests/canvas.test.ts b/tests/canvas.test.tsindex e081cc38..cf45c84a 100644--- a/tests/canvas.test.ts+++ b/tests/canvas.test.ts@@ -5,6 +5,7 @@ import {cleanGraph,connectionError,convertEngine,+ defaultModelForNode,describeNode,effectiveOp,graphEngine,@@ -14,6 +15,7 @@ import {outputType,paletteItems,planCanvas,+ promptPlaceholder,singleInput,topoOrder,withPlaceholders,@@ -157,6 +159,41 @@ describe("canvas graph → plan", () => {expect(step.deps).toEqual(["w"]);});+ it("a new Text model node writes; picking a language makes it translate", () => {+ const item = paletteItems().find((p) => p.id === "model-text")!;+ const data = { ...item.data, modelId: defaultModelForNode({ type: item.type, data: item.data ?? {} }, "cloud")!.id, prompt: "Write a slogan for a lemonade stand" };+ expect(data.modelId).toBe("text-llm");+ const w = node("w", "model", 0, 0, data);+ const g: CanvasGraph = { nodes: [w], edges: [] };+ expect(effectiveOp(g, w)).toBe("write");+ // The request says so (left out, the server would infer the catalog's first op: translate).+ expect(planCanvas(g).steps[0].request).toMatchObject({ modelId: "text-llm", op: "write", prompt: "Write a slogan for a lemonade stand" });+ expect(promptPlaceholder("write")).toMatch(/^What should it write/);+ expect(promptPlaceholder("translate")).toMatch(/^Text to translate/);+ expect(promptPlaceholder("tts")).toMatch(/^Text to speak/);++ w.data.params = { targetLanguage: "French" };+ expect(effectiveOp(g, w)).toBe("translate");+ expect(planCanvas(g).steps[0].request).toMatchObject({ op: "translate", params: { targetLanguage: "French" } });+ // Translate chosen by hand needs a language.+ w.data.params = undefined;+ w.data.op = "translate";+ expect(describeNode(g, w).error).toBe("Pick a language to translate into");+ });++ it("joins elements with any handle into the prompt without building a regex from it", () => {+ const g: CanvasGraph = {+ nodes: [node("el", "element", 0, 0, { handle: "a(b", elementId: "x" }), node("img", "model", 300, 0, { modelId: "nano-banana-pro", prompt: "A portrait" })],+ edges: [edge("el", "img", "elements")],+ };+ const d = describeNode(g, g.nodes[1]);+ expect(d.request?.prompt).toBe("A portrait\n\nWith @a(b.");+ // A handle already mentioned isn't added twice.+ g.nodes[1].data.prompt = "A portrait of @Maya";+ g.nodes[0].data.handle = "maya";+ expect(describeNode(g, g.nodes[1]).request?.prompt).toBe("A portrait of @Maya");+ });+it("reports validation errors on the node and blocks what depends on it", () => {const g = threeNodes();g.nodes[1].data.modelId = undefined;@@ -311,14 +348,48 @@ describe("canvas templates", () => {it("switches a whole board to open-source models", () => {const g = CANVAS_TEMPLATES.find((t) => t.id === "script-film")!.build("cloud");const { graph, changed, missing } = convertEngine(g, "open");- expect(changed).toBe(7);+ expect(changed).toBe(10);expect(missing).toEqual([]);expect(graphEngine(graph)).toBe("open");expect(graph.nodes.find((n) => n.id === "film")!.data.modelId).toBe("oss-join-clips");+ expect(graph.nodes.find((n) => n.id === "beat1")!.data).toMatchObject({ modelId: "oss-text-llm", op: "write" });expect(graph.nodes.find((n) => n.id === "shot1")!.data).toMatchObject({ modelId: "oss-wan22-14b-fast", op: "i2v" });expect(planCanvas(graph, { engineMode: "open" }).errors).toEqual([]);});+ it.each(["cloud", "open"] as const)("script → film (%s): the script reaches every frame and shot through a text model", (engine) => {+ const g = CANVAS_TEMPLATES.find((t) => t.id === "script-film")!.build(engine);+ const llm = engine === "open" ? "oss-text-llm" : "text-llm";+ const plan = planCanvas(g, { engineMode: engine });+ expect(plan.errors).toEqual([]);+ const step = (id: string) => plan.steps.find((s) => s.nodeId === id)!;+ for (const i of [1, 2, 3]) {+ // The shot text is written from the script…+ expect(step(`beat${i}`).request).toMatchObject({ modelId: llm, op: "write" });+ expect(step(`beat${i}`).request.prompt).toContain(`shot ${i} of 3`);+ expect(step(`beat${i}`).request.prompt).toContain("A paper boat sails down");+ // …and becomes the frame's prompt and the shot's prompt (with the frame as its start).+ expect(step(`frame${i}`)).toMatchObject({ deps: [`beat${i}`], request: { chain: [{ slot: "prompt" }] } });+ expect(step(`frame${i}`).request.prompt).toContain("{text}");+ expect(step(`shot${i}`).deps).toEqual([`beat${i}`, `frame${i}`]);+ expect(step(`shot${i}`).request.chain).toEqual([{ slot: "prompt" }, { slot: "images", position: "first" }]);+ }+ // A new script changes what every shot is written from: no paper boat left anywhere.+ g.nodes.find((n) => n.id === "script")!.data.text = "An astronaut plants a flower on Mars";+ const again = planCanvas(g, { engineMode: engine });+ const prompts = again.steps.map((s) => s.request.prompt ?? "").join("\n");+ expect(prompts).toContain("An astronaut plants a flower on Mars");+ expect(prompts).not.toMatch(/boat/i);+ });++ it("disables palette entries with no model on an install's only engine", () => {+ expect(paletteItems("both").filter((p) => p.disabled)).toEqual([]);+ const open = paletteItems("open");+ expect(open.find((p) => p.id === "frame")?.disabled).toMatch(/^No open-source model for extract frame yet/);+ expect(open.find((p) => p.id === "model-image")?.disabled).toBeUndefined();+ expect(open.find((p) => p.id === "join")?.disabled).toBeUndefined();+ });+it("offers palette entries for every node type the catalog can run", () => {const ids = paletteItems().map((p) => p.id);expect(ids).toEqual(expect.arrayContaining(["prompt", "image-input", "video-input", "audio-input", "element", "model-image", "model-video", "join", "frame", "upscale", "bg-remove", "tts", "output"]));