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: element nodes follow a later rename of their element

Claude8e435c3e7c48
diff --git a/src/components/canvas/canvas-editor.tsx b/src/components/canvas/canvas-editor.tsx
index ddbeb1b7..48b70c5f 100644
--- a/src/components/canvas/canvas-editor.tsx
+++ b/src/components/canvas/canvas-editor.tsx
@@ -40,7 +40,7 @@ import { EngineSwitch } from "@/components/studio/engine-switch";
import { fileToSlot } from "@/components/studio/media-slot";
import { Viewer } from "@/components/studio/viewer";
import { useIsMobile } from "@/hooks/use-mobile";
-import { useEngine, useStatus, type EngineMode } from "@/lib/api";
+import { useElements, useEngine, useStatus, type EngineMode } from "@/lib/api";
import { getModel } from "@/lib/catalog";
import {
acceptsType,
@@ -50,6 +50,7 @@ import {
counterpart,
defaultModelForNode,
describeNode,
+ elementUpdates,
EMPTY_GRAPH,
graphEngine,
inputPorts,
@@ -389,6 +390,16 @@ function Editor({ id, initial, view }: { id: string; initial: Canvas; view: Canv
[touch],
);
+ // An element renamed (or given a new photo) while the board is open: its nodes follow, and the board saves.
+ const elements = useElements().data;
+ useEffect(() => {
+ if (!elements) return;
+ const updates = elementUpdates(toGraph(latest.current.nodes, latest.current.edges), elements);
+ if (!updates.size) return;
+ setNodes((ns) => ns.map((n) => (updates.has(n.id) ? { ...n, data: { ...n.data, ...updates.get(n.id) } } : n)));
+ touch();
+ }, [elements, touch]);
+
/** Deleted nodes come back with their edges from the toast's Undo. */
const offerUndo = useCallback(
(count: number, before: { nodes: FlowNode[]; edges: Edge[] }) =>
diff --git a/src/lib/catalog/canvas.ts b/src/lib/catalog/canvas.ts
index 9931e5bb..85545eb3 100644
--- a/src/lib/catalog/canvas.ts
+++ b/src/lib/catalog/canvas.ts
@@ -1062,6 +1062,37 @@ export function graphEngine(graph: CanvasGraph): Engine | null {
return cloud + open === 0 ? null : open > cloud ? "open" : "cloud";
}
+/** What an element node shows and mentions, from the Elements list. */
+export type ElementInfo = { id: string; handle: string; name: string; references: string[] };
+
+/**
+ * Element nodes copy the element's name, @handle and first photo when it's picked. Renaming an
+ * element gives it a new handle too, so a node is brought up to date by its element's id: the patch
+ * for each element node whose element changed (keyed by node id). Elements that are gone, and
+ * nodes without one, are left as they are.
+ */
+export function elementUpdates(graph: CanvasGraph, elements: readonly ElementInfo[]): Map<string, Partial<CanvasNodeData>> {
+ const byId = new Map(elements.map((e) => [e.id, e]));
+ const out = new Map<string, Partial<CanvasNodeData>>();
+ for (const n of graph.nodes) {
+ const e = n.type === "element" && n.data.elementId ? byId.get(n.data.elementId) : undefined;
+ if (!e) continue;
+ const patch: Partial<CanvasNodeData> = {};
+ if (n.data.handle !== e.handle) patch.handle = e.handle;
+ if (n.data.name !== e.name) patch.name = e.name;
+ if ((n.data.preview ?? undefined) !== (e.references[0] ?? undefined)) patch.preview = e.references[0];
+ if (Object.keys(patch).length) out.set(n.id, patch);
+ }
+ return out;
+}
+
+/** The graph with every element node up to date (the same graph when nothing changed). */
+export function withCurrentElements(graph: CanvasGraph, elements: readonly ElementInfo[]): CanvasGraph {
+ const updates = elementUpdates(graph, elements);
+ if (!updates.size) return graph;
+ return { ...graph, nodes: graph.nodes.map((n) => (updates.has(n.id) ? { ...n, data: { ...n.data, ...updates.get(n.id) } } : n)) };
+}
+
// ---------------------------------------------------------------------------
// Graph hygiene
diff --git a/src/server/canvas.ts b/src/server/canvas.ts
index 67f1a13f..7a942f7d 100644
--- a/src/server/canvas.ts
+++ b/src/server/canvas.ts
@@ -12,6 +12,7 @@ import {
nodeSignature,
outline,
planCanvas,
+ withCurrentElements,
type Canvas,
type CanvasGraph,
type CanvasNodeType,
@@ -21,6 +22,7 @@ import {
} from "@/lib/catalog/canvas";
import type { Engine, Generation, MediaKind } from "@/lib/catalog/types";
import { db } from "./db";
+import { listElements } from "./elements";
import { createGenerations, getGeneration } from "./generations";
import { engineMode } from "./settings";
@@ -278,8 +280,10 @@ export function deleteCanvas(id: string): boolean {
/** The canvas with the generations its nodes made (for status, progress and outputs). */
export function canvasView(id: string): { canvas: Canvas; generations: Generation[] } | null {
- const canvas = getCanvas(id);
- if (!canvas) return null;
+ const stored = getCanvas(id);
+ if (!stored) return null;
+ // Element nodes show (and mention) their element as it's called now, after a rename.
+ const canvas = { ...stored, graph: withCurrentElements(stored.graph, listElements()) };
const ids = [...new Set(Object.values(canvas.runs).flatMap((r) => r.ids))];
// getGeneration also makes sure the runner is polling after a restart.
const generations = ids.map((g) => getGeneration(g)).filter((g): g is Generation => !!g);
@@ -318,7 +322,8 @@ export function runCanvas(id: string, opts: RunOptions = {}): RunResult | null {
let canvas = getCanvas(id);
if (!canvas) return null;
if (opts.graph) canvas = updateCanvas(id, { graph: opts.graph })!;
- const graph = canvas.graph;
+ // An element renamed since the board was saved: its node mentions the new @handle.
+ const graph = withCurrentElements(canvas.graph, listElements());
const plan = planCanvas(graph, { targets: opts.targets, force: opts.force, runs: canvas.runs, status: statusOf, engineMode: engineMode() });
const nodes = nodeMap(graph);
const batchId = nanoid(10);
diff --git a/tests/canvas-api.test.ts b/tests/canvas-api.test.ts
index e71a4011..4ec24c02 100644
--- a/tests/canvas-api.test.ts
+++ b/tests/canvas-api.test.ts
@@ -249,4 +249,26 @@ describe("canvas API", () => {
res = await one.GET(req("GET"), ctx(id));
expect(((await res.json()) as { generations: Generation[] }).generations.length).toBeGreaterThan(0);
});
+
+ it("follows an element renamed after it was put on the board", async () => {
+ const elements = await import("@/server/elements");
+ const el = elements.createElement({ name: "Maya", kind: "character", references: [] });
+ const graph: CanvasGraph = {
+ nodes: [
+ { id: "el", type: "element", position: { x: 0, y: 0 }, data: { elementId: el.id, handle: el.handle, name: el.name } },
+ { id: "img", type: "model", position: { x: 320, y: 0 }, data: { modelId: "nano-banana-pro", prompt: "A portrait" } },
+ ],
+ edges: [{ id: "e1", source: "el", sourceHandle: "out", target: "img", targetHandle: "elements" }],
+ };
+ const created = (await (await list.POST(req("POST", { name: "Cast", graph }))).json()) as Canvas;
+ const renamed = elements.updateElement(el.id, { name: "Maya Lee" })!;
+ expect(renamed.handle).not.toBe(el.handle);
+ // The board shows the element's name now, and a run mentions its new @handle.
+ const view = (await (await one.GET(req("GET"), ctx(created.id))).json()) as { canvas: Canvas };
+ expect(view.canvas.graph.nodes[0].data).toMatchObject({ handle: renamed.handle, name: "Maya Lee" });
+ const body = (await (await run.POST(req("POST", { graph }), ctx(created.id))).json()) as { items: Generation[] };
+ expect(body.items[0].request.prompt).toContain(`@${renamed.handle}`);
+ // …which the server resolves to the element (it has no photos: its name goes into the prompt).
+ expect(body.items[0].prompt).toContain("Maya Lee");
+ });
});
diff --git a/tests/canvas.test.ts b/tests/canvas.test.ts
index 5a8e102c..0d639e6a 100644
--- a/tests/canvas.test.ts
+++ b/tests/canvas.test.ts
@@ -8,6 +8,7 @@ import {
defaultModelForNode,
describeNode,
effectiveOp,
+ elementUpdates,
graphEngine,
inputPorts,
isGenerator,
@@ -18,6 +19,7 @@ import {
promptPlaceholder,
singleInput,
topoOrder,
+ withCurrentElements,
withPlaceholders,
type CanvasEdge,
type CanvasGraph,
@@ -194,6 +196,27 @@ describe("canvas graph → plan", () => {
expect(describeNode(g, g.nodes[1]).request?.prompt).toBe("A portrait of @Maya");
});
+ it("follows a renamed element: the node shows its new name and mentions its new @handle", () => {
+ const g: CanvasGraph = {
+ nodes: [
+ node("el", "element", 0, 0, { elementId: "e1", handle: "Maya", name: "Maya", preview: "/api/files/maya.png" }),
+ node("gone", "element", 0, 200, { elementId: "deleted", handle: "Old", name: "Old" }),
+ node("img", "model", 300, 0, { modelId: "nano-banana-pro", prompt: "A portrait" }),
+ ],
+ edges: [edge("el", "img", "elements")],
+ };
+ const now = [{ id: "e1", handle: "Maya_Lee", name: "Maya Lee", references: ["/api/files/maya.png"] }];
+ expect([...elementUpdates(g, now)]).toEqual([["el", { handle: "Maya_Lee", name: "Maya Lee" }]]);
+ const fresh = withCurrentElements(g, now);
+ expect(fresh.nodes[0].data).toMatchObject({ elementId: "e1", handle: "Maya_Lee", name: "Maya Lee" });
+ // An element that's gone keeps what the node had; the rest of the board is the same object.
+ expect(fresh.nodes[1]).toBe(g.nodes[1]);
+ expect(describeNode(fresh, fresh.nodes[2]).request?.prompt).toBe("A portrait\n\nWith @Maya_Lee.");
+ // A new first photo follows too; nothing changed → the same graph.
+ expect(elementUpdates(fresh, [{ ...now[0], references: ["/api/files/new.png"] }]).get("el")).toEqual({ preview: "/api/files/new.png" });
+ expect(withCurrentElements(fresh, now)).toBe(fresh);
+ });
+
it("reports validation errors on the node and blocks what depends on it", () => {
const g = threeNodes();
g.nodes[1].data.modelId = undefined;