diff --git a/packages/core/src/opendirector/application/creation_canvas.py b/packages/core/src/opendirector/application/creation_canvas.py index 1d6e9d19..99bfcbfc 100644 --- a/packages/core/src/opendirector/application/creation_canvas.py +++ b/packages/core/src/opendirector/application/creation_canvas.py @@ -554,6 +554,8 @@ def _mutate(self, data, kind, payload): elif kind == 'canvas.update': item = self._editable(data, payload.itemId, unlocking=value.get('locked') is False) item.update({key: val for key, val in value.items() if key != 'itemId' and (val is not None or key == 'parentId')}) + if item.get('color') == '': + item.pop('color') elif kind == 'canvas.hide': self._editable(data, payload.itemId)['visible'] = False elif kind == 'canvas.materialize': diff --git a/packages/core/src/opendirector/domain/creation/base.py b/packages/core/src/opendirector/domain/creation/base.py index 6e71c689..42c5108c 100644 --- a/packages/core/src/opendirector/domain/creation/base.py +++ b/packages/core/src/opendirector/domain/creation/base.py @@ -12,6 +12,8 @@ Revision = Annotated[int, Field(strict=True, ge=1)] PositiveInt = Annotated[int, Field(strict=True, ge=1)] Identifier = Annotated[str, Field(min_length=1, max_length=100, pattern=r'\S')] +# Canvas presentation color: #RRGGBB, or '' in an update to restore the default. +CanvasColor = Annotated[str, Field(pattern=r'^(#[0-9a-fA-F]{6})?$')] Fingerprint = Annotated[str, Field(pattern=r'^sha256:[0-9a-f]{64}$')] Digest = Annotated[str, Field(pattern=r'^[0-9a-f]{64}$')] MediaKind = Literal['text', 'image', 'video', 'audio', 'file'] diff --git a/packages/core/src/opendirector/domain/creation/canvas.py b/packages/core/src/opendirector/domain/creation/canvas.py index 74cac41a..181a31f0 100644 --- a/packages/core/src/opendirector/domain/creation/canvas.py +++ b/packages/core/src/opendirector/domain/creation/canvas.py @@ -7,7 +7,7 @@ from opendirector.domain.projects import ProjectError from .assets import AssetRef -from .base import CanvasItemVersion, Contract, Identifier, Point, Positive, ProjectEntity, unique +from .base import CanvasColor, CanvasItemVersion, Contract, Identifier, Point, Positive, ProjectEntity, unique from .generation import FollowingSource, GenerationSpec, ResolvedReference, validate_generation from .models import ModelConfigPublic @@ -103,6 +103,7 @@ class CanvasItemBase(ProjectEntity): height: Positive = Field(default=198, description='Height in pixels.') zIndex: int = Field(default=0, strict=True, description='Canvas stacking order.') locked: bool = Field(default=False, description='Whether edits to this object are blocked.') + color: CanvasColor | None = Field(default=None, exclude_if=lambda value: not value, description='Group background color in #RRGGBB format.') visible: bool = Field(default=True, description='Whether this object is displayed.') parentId: UUID | None = Field(default=None, description='Parent canvas group ID, or null for the root.') tags: tuple[Identifier, ...] = Field(default=(), description='User-defined search tags.') diff --git a/packages/core/src/opendirector/domain/creation/commands.py b/packages/core/src/opendirector/domain/creation/commands.py index 556b6d6c..e79902c4 100644 --- a/packages/core/src/opendirector/domain/creation/commands.py +++ b/packages/core/src/opendirector/domain/creation/commands.py @@ -5,7 +5,7 @@ from pydantic import ConfigDict, Field, TypeAdapter, create_model, model_validator from .assets import AssetRef, MediaMetadata -from .base import Contract, Digest, GenerationKind, MediaKind, ObjectVersion, Point, Positive, Revision, digest, unique +from .base import CanvasColor, Contract, Digest, GenerationKind, MediaKind, ObjectVersion, Point, Positive, Revision, digest, unique from .canvas import CanvasItem from .editors import EditorDocument from opendirector.domain.rule_facts import PROMPT_MAX_LENGTH @@ -109,6 +109,8 @@ class CanvasPresentationUpdate(Contract): height: Positive | None = Field(default=None, description='Height in pixels.') zIndex: int | None = Field(default=None, strict=True, description='Canvas stacking order.') locked: bool | None = Field(default=None, description='Whether edits to this object are blocked.') + # Omitted when unset so commands recorded before this field keep their fingerprint. + color: CanvasColor | None = Field(default=None, exclude_if=lambda value: value is None, description="Group background color in #RRGGBB format, or '' for the default.") parentId: UUID | None = Field(default=None, description='Parent canvas group ID, or null for the root.') # Presence of parentId distinguishes retaining a group from ungrouping. diff --git a/src/backend/creationProjection.ts b/src/backend/creationProjection.ts index 06b43fd0..24c4bdd7 100644 --- a/src/backend/creationProjection.ts +++ b/src/backend/creationProjection.ts @@ -74,6 +74,7 @@ const presentationFields = [ "height", "zIndex", "locked", + "color", "parentId", ] as const; const extension: Record = { @@ -602,6 +603,7 @@ export class CreationProjection { width: item.width, height: item.height, locked: item.locked, + ...(item.color ? { color: item.color } : {}), zIndex: item.zIndex, parentId: item.parentId ?? undefined, }; @@ -1594,6 +1596,7 @@ export class CreationProjection { height: node.height, zIndex: node.zIndex ?? 0, locked: node.locked ?? false, + ...(node.color ? { color: node.color } : {}), parentId: node.parentId ?? null, binding, } as CanvasItem, @@ -1613,6 +1616,7 @@ export class CreationProjection { height: node.height, zIndex: node.zIndex ?? 0, locked: node.locked ?? false, + color: node.color ?? "", parentId: node.parentId ?? null, }; const oldPresentation = { @@ -1621,6 +1625,7 @@ export class CreationProjection { height: old?.height, zIndex: old?.zIndex ?? 0, locked: old?.locked ?? false, + color: old?.color ?? "", parentId: old?.parentId ?? null, }; const changedFields = presentationFields.filter( diff --git a/src/canvas/NodeSelectionToolbar.tsx b/src/canvas/NodeSelectionToolbar.tsx index cb352bd3..29641e0f 100644 --- a/src/canvas/NodeSelectionToolbar.tsx +++ b/src/canvas/NodeSelectionToolbar.tsx @@ -12,6 +12,7 @@ import type { ToolGlyphName } from "./toolGlyphs"; import { TooltipButton } from "./TooltipButton"; import { GridSplitOptions, SelectionToolbarMenu } from "./SelectionToolbarMenu"; import type { CanvasNode } from "./model"; +import type { GroupLayout } from "./operations"; import type { CanvasToolId } from "./toolCatalog"; import { useT } from "../i18n/I18nProvider"; import type { MessageKey } from "../i18n/messages"; @@ -46,8 +47,26 @@ export interface NodeSelectionToolbarProps { onRename?: () => void; onDuplicate?: () => void; onDelete?: () => void; + /** Group actions: run the members' generation workflow, ungroup, arrange, recolor. */ + onRunWorkflow?: () => void; + workflowRunning?: boolean; + onUngroup?: () => void; + onLayout?: (layout: GroupLayout) => void; + onGroupColor?: (color: string) => void; } +/** Group background presets; "" restores the default style. */ +export const GROUP_COLORS: readonly [string, MessageKey][] = [ + ["", "canvas.selection.group.colorDefault"], + ["#8b8f9a", "canvas.selection.group.colorGray"], + ["#e5484d", "canvas.selection.group.colorRed"], + ["#f76b15", "canvas.selection.group.colorOrange"], + ["#ffc53d", "canvas.selection.group.colorYellow"], + ["#30a46c", "canvas.selection.group.colorGreen"], + ["#3e63dd", "canvas.selection.group.colorBlue"], + ["#8e4ec6", "canvas.selection.group.colorPurple"], +]; + type Action = { id: string; label: string; @@ -68,6 +87,10 @@ type MenuOption = { run?: () => void; grid?: boolean; disabled?: boolean; + /** Color swatch shown instead of the icon; "" draws the default swatch. */ + swatch?: string; + /** Marks the current choice, such as the active background color. */ + checked?: boolean; }; type OpenMenu = { nodeId: string; @@ -122,6 +145,11 @@ export function NodeSelectionToolbar({ onRename, onDuplicate, onDelete, + onRunWorkflow, + workflowRunning = false, + onUngroup, + onLayout, + onGroupColor, }: NodeSelectionToolbarProps) { const t = useT(); const toolbar = useRef(null); @@ -190,7 +218,38 @@ export function NodeSelectionToolbar({ disabled: locked, }); }; - if (node.type === "image") { + if (node.type === "group") { + if (onRunWorkflow) + editing.push({ + id: "run-workflow", + label: workflowRunning ? t("canvas.selection.group.running") : t("canvas.selection.group.run"), + icon: "play", + run: onRunWorkflow, + disabled: workflowRunning || locked, + }); + if (onUngroup) + editing.push({ id: "ungroup", label: t("canvas.selection.group.ungroup"), icon: "group", run: onUngroup, disabled: locked }); + if (onLayout) + addMenu("layout", t("canvas.selection.group.layout"), "grid", [ + { id: "grid", label: t("canvas.selection.group.layoutGrid"), icon: "grid", run: () => onLayout("grid") }, + { id: "row", label: t("canvas.selection.group.layoutRow"), icon: "referenceArrowsHorizontal", run: () => onLayout("row") }, + { id: "column", label: t("canvas.selection.group.layoutColumn"), icon: "referenceArrowsVertical", run: () => onLayout("column") }, + ]); + if (onGroupColor) + addMenu( + "group-color", + t("canvas.selection.group.color"), + "palette", + GROUP_COLORS.map(([color, label]) => ({ + id: `color-${color || "default"}`, + label: t(label), + icon: "palette", + swatch: color, + checked: (node.color ?? "") === color, + run: () => onGroupColor(color), + })), + ); + } else if (node.type === "image") { for (const [tool, labelKey, icon, glyph] of imageTools) addTool(tool, t(labelKey), icon, glyph); addMenu( @@ -371,17 +430,18 @@ export function NodeSelectionToolbar({ run: onPreview, iconOnly: true, }); - if (onDownload && (hasMedia || node.type === "text")) + if (onDownload && (hasMedia || node.type === "text" || node.type === "group")) outputs.push({ id: "download", - label: t("common.download"), + label: node.type === "group" ? t("canvas.selection.group.download") : t("common.download"), icon: "download", run: onDownload, iconOnly: true, }); const organization: Action[] = []; - if (onRename) + // A group's bar holds only its own actions; rename and delete stay in the context menu. + if (onRename && node.type !== "group") organization.push({ id: "rename", label: t("common.rename"), @@ -390,7 +450,7 @@ export function NodeSelectionToolbar({ iconOnly: true, disabled: locked, }); - if (onDuplicate) + if (onDuplicate && node.type !== "group") organization.push({ id: "duplicate", label: t("canvas.selection.duplicate"), @@ -398,7 +458,7 @@ export function NodeSelectionToolbar({ run: onDuplicate, iconOnly: true, }); - if (onDelete) + if (onDelete && node.type !== "group") organization.push({ id: "delete", label: t("canvas.selection.deleteNode"), @@ -495,13 +555,13 @@ export function NodeSelectionToolbar({ } shortcut={action.id === "output" ? (navigator.platform.includes("Mac") ? "⌘ Enter" : "Ctrl Enter") : undefined} label={ - action.disabled + action.disabled && locked ? t("canvas.selection.locked", { label: action.label }) : action.label } tooltipSide="top" // Labelled buttons already show their text; only icons and lock reasons need a tooltip. - showTooltip={Boolean(action.iconOnly || action.disabled)} + showTooltip={Boolean(action.iconOnly || (action.disabled && locked))} className={`${action.iconOnly ? "is-icon-only" : ""}${action.destructive ? " is-destructive" : ""}`} disabled={action.disabled} onClick={(event) => { @@ -590,7 +650,8 @@ export function NodeSelectionToolbar({ - + {option.swatch !== undefined ? ( + )) )} diff --git a/src/canvas/SelectionToolbarMenu.tsx b/src/canvas/SelectionToolbarMenu.tsx index b64146ad..0235021f 100644 --- a/src/canvas/SelectionToolbarMenu.tsx +++ b/src/canvas/SelectionToolbarMenu.tsx @@ -80,7 +80,7 @@ export function SelectionToolbarMenu({ place(); const items = panel.querySelectorAll( role === "menu" - ? '[role="menuitem"]:not(:disabled)' + ? '[role="menuitem"]:not(:disabled),[role="menuitemradio"]:not(:disabled)' : 'input:not(:disabled),button:not(:disabled):not([tabindex="-1"])', ); (items[initialFocus === "last" ? items.length - 1 : 0] ?? panel).focus({ @@ -146,7 +146,7 @@ export function SelectionToolbarMenu({ event.preventDefault(); const items = [ ...(root.current?.querySelectorAll( - '[role="menuitem"]:not(:disabled)', + '[role="menuitem"]:not(:disabled),[role="menuitemradio"]:not(:disabled)', ) ?? []), ]; if (!items.length) return; diff --git a/src/canvas/groupWorkflow.ts b/src/canvas/groupWorkflow.ts new file mode 100644 index 00000000..24b69f54 --- /dev/null +++ b/src/canvas/groupWorkflow.ts @@ -0,0 +1,80 @@ +import type { CanvasEdge } from "./model.ts"; + +export interface WorkflowOutcome { + succeeded: string[]; + failed: string[]; + /** Not run: a referenced member failed, or the step sits on a reference cycle. */ + skipped: string[]; +} + +/** + * Runs workflow steps in reference order. A step starts once every step it + * references, directly or through non-step nodes, has succeeded; steps without pending references run + * concurrently. A failure skips everything downstream of it. + */ +export async function runWorkflowSteps( + stepIds: readonly string[], + edges: readonly Pick[], + runStep: (id: string) => Promise, +): Promise { + const ids = new Set(stepIds); + const sources = new Map(); + for (const edge of edges) + if (edge.intent !== "post_edit_result" && edge.source !== edge.target) + sources.set(edge.target, [...(sources.get(edge.target) ?? []), edge.source]); + // A step depends on the nearest steps upstream of it, also through + // non-step nodes such as a subject or stack that carries a result onward. + const upstream = new Map( + stepIds.map((id) => { + const found = new Set(); + const seen = new Set([id]); + const pending = [...(sources.get(id) ?? [])]; + while (pending.length) { + const node = pending.pop()!; + if (seen.has(node)) continue; + seen.add(node); + if (ids.has(node)) found.add(node); + else pending.push(...(sources.get(node) ?? [])); + } + return [id, found]; + }), + ); + const outcome = new Map(); + const started = new Set(); + let running = 0; + await new Promise((resolve) => { + const advance = () => { + let progressed = true; + while (progressed) { + progressed = false; + for (const id of stepIds) { + if (started.has(id)) continue; + const sources = [...upstream.get(id)!]; + if (sources.some((source) => ["failed", "skipped"].includes(outcome.get(source) ?? ""))) { + started.add(id); + outcome.set(id, "skipped"); + progressed = true; + continue; + } + if (!sources.every((source) => outcome.get(source) === "succeeded")) continue; + started.add(id); + running++; + void runStep(id) + .catch(() => false) + .then((succeeded) => { + running--; + outcome.set(id, succeeded ? "succeeded" : "failed"); + advance(); + }); + } + } + if (running) return; + // Whatever never started is waiting on a reference cycle. + for (const id of stepIds) if (!outcome.has(id)) outcome.set(id, "skipped"); + resolve(); + }; + advance(); + }); + const pick = (state: string) => stepIds.filter((id) => outcome.get(id) === state); + return { succeeded: pick("succeeded"), failed: pick("failed"), skipped: pick("skipped") }; +} diff --git a/src/canvas/history.ts b/src/canvas/history.ts index 75a0629f..80f60880 100644 --- a/src/canvas/history.ts +++ b/src/canvas/history.ts @@ -113,6 +113,7 @@ function sameNode(a: CanvasNode, b: CanvasNode): boolean { a.width === b.width && a.height === b.height && a.locked === b.locked && + (a.color ?? "") === (b.color ?? "") && a.zIndex === b.zIndex && a.parentId === b.parentId && sameNodeData(a.data, b.data) diff --git a/src/canvas/model.ts b/src/canvas/model.ts index 67ae554e..d1035b13 100644 --- a/src/canvas/model.ts +++ b/src/canvas/model.ts @@ -254,6 +254,8 @@ export interface CanvasNode { width: number; height: number; locked?: boolean; + /** Group background color (#rrggbb); absent means the default group style. */ + color?: string; zIndex?: number; /** Child positions are relative to their parent's top-left corner. */ parentId?: string; diff --git a/src/canvas/node-selection-toolbar.css b/src/canvas/node-selection-toolbar.css index ae7eb223..0b7ccece 100644 --- a/src/canvas/node-selection-toolbar.css +++ b/src/canvas/node-selection-toolbar.css @@ -215,3 +215,17 @@ min-height: 44px; } } +.cw-selection-menu-item > .cw-selection-swatch { + display: block; + flex: 0 0 auto; + width: 16px; + height: 16px; + margin: 0 1px; + border-radius: 50%; + box-shadow: inset 0 0 0 1px #ffffff2e; +} +.cw-selection-menu-item > .cw-selection-swatch.is-default { + background: rgba(157, 164, 179, 0.12); + border: 1px dashed #6c6f7b; + box-shadow: none; +} diff --git a/src/canvas/nodes.css b/src/canvas/nodes.css index 4ada7126..5f29b2f8 100644 --- a/src/canvas/nodes.css +++ b/src/canvas/nodes.css @@ -367,8 +367,8 @@ cursor: default; } .cw-node-group { - background: rgba(157, 164, 179, 0.035); - border: 1px dashed #6c6f7b; + background: var(--cw-group-fill, rgba(157, 164, 179, 0.035)); + border: 1px dashed var(--cw-group-border, #6c6f7b); border-radius: var(--cw-node-radius); } .cw-workspace .react-flow__node-group { diff --git a/src/canvas/operations.ts b/src/canvas/operations.ts index 573c5cff..934a79d2 100644 --- a/src/canvas/operations.ts +++ b/src/canvas/operations.ts @@ -170,6 +170,89 @@ export function ungroupSelection( }; } +export type GroupLayout = "grid" | "row" | "column"; + +/** Padding a group keeps around its members; matches {@link groupSelection}. */ +const GROUP_PADDING = { left: 24, top: 40, right: 24, bottom: 24 }; +const GROUP_LAYOUT_GAP = 40; + +/** + * Arranges a group's direct members in a row, a column or a near-square grid, + * in their current reading order, and resizes the group to fit them. Member + * positions are relative to the group, which keeps its top-left corner. + */ +export function layoutGroup( + document: CanvasDocument, + groupId: string, + layout: GroupLayout, +): CanvasDocument { + const group = document.nodes.find( + (node) => node.id === groupId && node.type === "group", + ); + if (!group) return document; + const members = document.nodes + .filter((node) => node.parentId === groupId) + .sort((a, b) => + layout === "row" + ? a.position.x - b.position.x || a.position.y - b.position.y + : a.position.y - b.position.y || a.position.x - b.position.x, + ); + if (!members.length) return document; + const columns = + layout === "row" + ? members.length + : layout === "column" + ? 1 + : Math.ceil(Math.sqrt(members.length)); + const positions = new Map(); + let x = GROUP_PADDING.left, + y = GROUP_PADDING.top, + rowHeight = 0, + right = 0, + bottom = 0; + members.forEach((node, index) => { + if (index && index % columns === 0) { + x = GROUP_PADDING.left; + y += rowHeight + GROUP_LAYOUT_GAP; + rowHeight = 0; + } + positions.set(node.id, { x, y }); + right = Math.max(right, x + node.width); + bottom = Math.max(bottom, y + node.height); + x += node.width + GROUP_LAYOUT_GAP; + rowHeight = Math.max(rowHeight, node.height); + }); + return { + ...document, + nodes: document.nodes.map((node) => + node.id === groupId + ? { + ...node, + width: right + GROUP_PADDING.right, + height: bottom + GROUP_PADDING.bottom, + } + : positions.has(node.id) + ? { ...node, position: positions.get(node.id)! } + : node, + ), + }; +} + +/** Members of a group at any depth, excluding the group itself. */ +export function groupDescendants( + nodes: readonly CanvasNode[], + groupId: string, +): CanvasNode[] { + const inside = new Set([groupId]); + const result: CanvasNode[] = []; + for (const node of orderNodesForCanvas(nodes)) + if (node.parentId && inside.has(node.parentId)) { + inside.add(node.id); + result.push(node); + } + return result; +} + /** Copies selected groups with all descendants and edges internal to the copy. */ export function copySelection( document: CanvasDocument, diff --git a/src/canvas/storage.ts b/src/canvas/storage.ts index 7e3b5681..d9fffe95 100644 --- a/src/canvas/storage.ts +++ b/src/canvas/storage.ts @@ -122,6 +122,8 @@ export function parseDocument(input: unknown): Result { !finite(raw.height) || raw.height <= 0 || (raw.locked !== undefined && typeof raw.locked !== "boolean") || + (raw.color !== undefined && + (typeof raw.color !== "string" || !/^#[0-9a-fA-F]{6}$/.test(raw.color))) || (raw.zIndex !== undefined && !Number.isSafeInteger(raw.zIndex)) || (raw.parentId !== undefined && !nonempty(raw.parentId)) ) @@ -151,6 +153,7 @@ export function parseDocument(input: unknown): Result { height: raw.height, data: parsedData.value, ...(typeof raw.locked === "boolean" ? { locked: raw.locked } : {}), + ...(typeof raw.color === "string" ? { color: raw.color } : {}), ...(typeof raw.zIndex === "number" ? { zIndex: raw.zIndex } : {}), ...(typeof raw.parentId === "string" ? { parentId: raw.parentId } : {}), }); diff --git a/src/canvas/workspace/WorkspaceView.tsx b/src/canvas/workspace/WorkspaceView.tsx index d5fad311..3e1828f0 100644 --- a/src/canvas/workspace/WorkspaceView.tsx +++ b/src/canvas/workspace/WorkspaceView.tsx @@ -78,7 +78,7 @@ import { NodeSelectionToolbar } from "../NodeSelectionToolbar"; import { TextNodeWindow } from "../TextNodeWindow"; import { SelectionBarAnchor } from "../SelectionBarAnchor"; import { NodeTaskPanel } from "../NodeTaskPanel"; -import { unstackSelection } from "../operations"; +import { layoutGroup, orderNodesForCanvas, unstackSelection, type GroupLayout } from "../operations"; import { ProjectSelector } from "../ProjectSelector"; import "../settings.css"; @@ -152,6 +152,8 @@ export function WorkspaceView({ rt }: { rt: WorkspaceRuntime }) { dismiss, doc, downloadNode, + downloadGroup, + runGroupWorkflow, editProjectNameId, editing, editorMode, @@ -394,6 +396,47 @@ export function WorkspaceView({ rt }: { rt: WorkspaceRuntime }) { rt.revealCreatedNodes.current([output], doc.nodes.some((node) => node.id === output.id) ? doc.nodes : [...doc.nodes, output]); }, [outputState?.output.id]); + const [workflowGroups, setWorkflowGroups] = useState>(() => new Set()); + const runWorkflow = (groupId: string) => { + if (workflowGroups.has(groupId)) return; + setWorkflowGroups((current) => new Set(current).add(groupId)); + void (runGroupWorkflow(groupId) as Promise).finally(() => + setWorkflowGroups((current) => { + const next = new Set(current); + next.delete(groupId); + return next; + }), + ); + }; + + // Canvas locking is no longer offered. Locks saved while it was available are + // released the first time this browser opens the project, parents first (a + // locked group blocks its children). Later locks from the Agent or the API + // are kept and can be released from the context menu. + const unlockedOnOpen = useRef(null); + useEffect(() => { + // The startup placeholder and empty projects have nothing to release. + if (unlockedOnOpen.current === doc.id || !doc.nodes.length) return; + unlockedOnOpen.current = doc.id; + const key = `opendirector.legacyLocksReleased:${doc.id}`; + try { + if (localStorage.getItem(key)) return; + localStorage.setItem(key, "1"); + } catch { + return; // Without storage the open-time release could repeat; keep locks. + } + const locked = orderNodesForCanvas(doc.nodes).filter((node) => node.locked); + if (!locked.length) return; + command({ + type: "batch", + commands: locked.map((node) => ({ + type: "update-node", + id: node.id, + patch: { locked: false }, + })), + }); + }, [doc.id, doc.nodes]); + const desktop = !!window.opendirectorDesktop; const [desktopSidebarOpen, setDesktopSidebarOpen] = useState(() => { try { return desktop && localStorage.getItem("opendirector.desktop.sidebar.v1") === "open"; } @@ -1484,7 +1527,33 @@ export function WorkspaceView({ rt }: { rt: WorkspaceRuntime }) { } else openEditor(single, mode); }} onPreview={() => setPreview(single)} - onDownload={() => downloadNode(single)} + onDownload={() => + single.type === "group" ? void downloadGroup(single) : downloadNode(single) + } + onRunWorkflow={single.type === "group" ? () => runWorkflow(single.id) : undefined} + workflowRunning={workflowGroups.has(single.id)} + onUngroup={single.type === "group" ? ungroup : undefined} + onLayout={ + single.type === "group" + ? (layout: GroupLayout) => { + if (doc.nodes.some((node) => node.parentId === single.id && lockedIds.has(node.id))) { + notify(t("canvas.workspace.toast.unlockFirst")); + return; + } + change((d) => layoutGroup(d, single.id, layout)); + } + : undefined + } + onGroupColor={ + single.type === "group" + ? (color: string) => + command({ + type: "update-node", + id: single.id, + patch: { color: color || undefined }, + }) + : undefined + } onAssistant={showAssistant} onExpand={ single.type === "text" @@ -1896,74 +1965,26 @@ export function WorkspaceView({ rt }: { rt: WorkspaceRuntime }) { })), )}
- {menuButton( - single && lockedIds.has(single.id) && !single.locked - ? t("canvas.workspace.menu.groupLocked") - : single?.locked - ? t("canvas.workspace.menu.unlock") - : t("canvas.workspace.menu.lock"), - "lock", - () => { - if (single && lockedIds.has(single.id) && !single.locked) { - notify(t("canvas.workspace.toast.unlockGroupFirst")); - return; - } + {/* Locking is not offered, but a node locked by the Agent, the API or an + undo of the open-time unlock must still be recoverable. */} + {selected.some((id) => lockedIds.has(id)) && + menuButton(t("canvas.workspace.menu.unlock"), "unlock", () => { + const byId = new Map(doc.nodes.map((node) => [node.id, node])); + const targets = new Set(); + for (const id of selected) + for (let node = byId.get(id); node; node = node.parentId ? byId.get(node.parentId) : undefined) + if (node.locked) targets.add(node.id); command({ type: "batch", - commands: selected.map((id) => ({ - type: "update-node", - id, - patch: { - locked: !doc.nodes.find((n) => n.id === id)?.locked, - }, - })), + commands: orderNodesForCanvas(doc.nodes) + .filter((node) => targets.has(node.id)) + .map((node) => ({ type: "update-node" as const, id: node.id, patch: { locked: false } })), }); - }, - )} + })} {single?.type === "stack" && menuButton(t("canvas.workspace.menu.unstack"), "stack", () => change((d) => unstackSelection(d, selected)), )} - {menuButton(t("canvas.workspace.menu.moveToFront"), "arrow", () => - command({ - type: "reorder-nodes", - ids: selected, - direction: "front", - }), - )} - {menuButton( - t("canvas.workspace.menu.moveForward"), - "arrow", - () => - command({ - type: "reorder-nodes", - ids: selected, - direction: "forward", - }), - "⌘ ]", - )} - {menuButton( - t("canvas.workspace.menu.moveBackward"), - "arrow", - () => - command({ - type: "reorder-nodes", - ids: selected, - direction: "backward", - }), - "⌘ [", - )} - {menuButton( - t("canvas.workspace.menu.moveToBack"), - "arrow", - () => - command({ - type: "reorder-nodes", - ids: selected, - direction: "back", - }), - "⌘ ⇧ [", - )} {single && ["image", "video", "audio", "file"].includes(single.type) && !lockedIds.has(single.id) && diff --git a/src/canvas/workspace/useWorkspaceGeneration.ts b/src/canvas/workspace/useWorkspaceGeneration.ts index 337f4257..d5a59677 100644 --- a/src/canvas/workspace/useWorkspaceGeneration.ts +++ b/src/canvas/workspace/useWorkspaceGeneration.ts @@ -15,6 +15,8 @@ import { lockedNodeIds } from "../hierarchy"; import { imageAnnotationPayload } from "../imageActionPayload"; import { appendPendingResult, removePendingResult, validateNodeActionSource, type GenerationResultMode, type LinkedMediaResult, type ResultSource } from "../linkedResults"; import { type CanvasNode } from "../model"; +import { groupDescendants } from "../operations"; +import { runWorkflowSteps } from "../groupWorkflow"; import { getNodeGenerationSettings } from "../NodeComposer"; import { serviceGenerationPlan, textGenerationPlan } from "../workspaceInteraction.ts"; import { generationPrepareBlock } from "./entryPoints.ts"; @@ -115,8 +117,9 @@ export function useWorkspaceGeneration(rt: WorkspaceRuntime) { origin: ResultSource, _title: string, resultMode: GenerationResultMode = "replace", - ) => { + ): Promise => { const id = crypto.randomUUID(); + let jobIds: string[] = []; const run: ServiceRun = { controller: new AbortController(), unified: true, @@ -167,7 +170,7 @@ export function useWorkspaceGeneration(rt: WorkspaceRuntime) { ), }; change(() => configured); - if (!(await persist(configured))) return; + if (!(await persist(configured))) return []; run.controller.signal.throwIfAborted(); run.phase = "submitting"; refreshRunning(); @@ -182,8 +185,10 @@ export function useWorkspaceGeneration(rt: WorkspaceRuntime) { // Never abort delivery and mint a replacement command. for (const jobId of receipt.jobIds) await creationClient(origin.documentId).control(jobId, "cancel"); - } else if (receipt.status === "accepted") + } else if (receipt.status === "accepted") { + jobIds = [...(receipt.jobIds ?? [])]; notify(t("canvas.workspace.toast.generationSubmitted")); + } } catch (error) { if (!(error instanceof DOMException && error.name === "AbortError")) notify(errorMessage(error)); @@ -191,7 +196,7 @@ export function useWorkspaceGeneration(rt: WorkspaceRuntime) { serviceRuns.current.delete(id); refreshRunning(); } - return; + return jobIds; }; /** Submit once; the service ledger owns progress/history in both modes. */ const runToolTask = async (origin: ResultSource, task: CanvasToolTask) => { @@ -542,16 +547,20 @@ export function useWorkspaceGeneration(rt: WorkspaceRuntime) { }); if (plan.status === "blocked") { notify(plan.message ?? t("canvas.workspace.error.modelUnavailable")); - return; + return Promise.resolve([]); } const batchId = plan.count > 1 ? crypto.randomUUID() : undefined; + const runs: Promise[] = []; for (let index = 0; index < plan.count; index++) - void runServiceTask( - { ...plan.request, batchId, executionSlot: index }, - plan.origin, - plan.label, - index === 0 ? "replace" : "additional", + runs.push( + runServiceTask( + { ...plan.request, batchId, executionSlot: index }, + plan.origin, + plan.label, + index === 0 ? "replace" : "additional", + ), ); + return Promise.all(runs).then((ids) => ids.flat()); }; const cancelServiceRuns = (nodeId: string) => { for (const job of jobs) @@ -587,7 +596,9 @@ export function useWorkspaceGeneration(rt: WorkspaceRuntime) { return; } }; - const prepareGeneration = (nodeId: string) => { + /** Starts a node's generation; resolves to the accepted job IDs (none when it did not start). */ + const prepareGeneration = (nodeId: string): Promise => { + const none = Promise.resolve([]); const current = historyRef.current.present; const node = current.nodes.find((node) => node.id === nodeId); if ( @@ -601,8 +612,8 @@ export function useWorkspaceGeneration(rt: WorkspaceRuntime) { ), } ) ) - return; - if (!node) return; + return none; + if (!node) return none; const incoming = incomingNodes(current, nodeId); if (node.type === "text") { const plan = textGenerationPlan({ @@ -614,24 +625,145 @@ export function useWorkspaceGeneration(rt: WorkspaceRuntime) { }); if (plan.status === "blocked") { notify(plan.message ?? t("canvas.workspace.error.agentUnavailable")); - return; + return none; } - void runServiceTask(plan.request, plan.origin, plan.label); - return; + return runServiceTask(plan.request, plan.origin, plan.label); } const settings = getNodeGenerationSettings(node, backendCatalog.models); if (settings.modelConfigId) { try { - runServiceGeneration(node, incoming); + return runServiceGeneration(node, incoming); } catch (error) { notify(errorMessage(error)); } - return; + return none; } notify( t("canvas.workspace.error.noGenerationModel"), ); + return none; + }; + /** Latest render's generation entry point; a workflow outlives the render that started it. */ + const prepareLatest = useRef(prepareGeneration); + prepareLatest.current = prepareGeneration; + /** + * Resolves once every job is terminal (true when one succeeded), or false + * when the canvas has switched to another project. + */ + const awaitJobs = (documentId: string, jobIds: readonly string[]) => + new Promise((resolve) => { + const client = creationClient(documentId); + const terminal = ["succeeded", "failed", "cancelled", "interrupted"]; + let finish = () => {}; + const check = () => { + if (!mounted.current || historyRef.current.present.id !== documentId) { + finish(); + resolve(false); + return; + } + const states = jobIds.map( + (id) => client.snapshot.jobs.find((job) => job.id === id)?.status, + ); + if (!states.every((status) => status && terminal.includes(status))) return; + finish(); + resolve(states.includes("succeeded")); + }; + const stop = client.listen(check); + const poll = setInterval(check, 1000); + finish = () => { + stop(); + clearInterval(poll); + }; + check(); + }); + /** + * Waits until the canvas shows the result the service adopted for the node, + * so a downstream step reads the new content (for example generated text + * used as its prompt). Resolves false when no result was adopted (a + * candidate-only result, or inputs edited while running), after three + * minutes, or when the canvas switched project. + */ + const awaitDisplayed = (documentId: string, nodeId: string, jobIds: readonly string[]) => + new Promise((resolve) => { + const snapshot = creationClient(documentId).snapshot; + const adopted = new Set( + snapshot.jobs.filter((job) => jobIds.includes(job.id) && job.adopted).map((job) => job.id), + ); + const assets = new Set( + snapshot.results + .filter((result) => adopted.has(result.jobId)) + .map((result) => result.asset.assetId), + ); + if (!assets.size) { + resolve(false); + return; + } + const started = Date.now(); + const check = () => { + const present = historyRef.current.present; + const node = present.nodes.find((candidate) => candidate.id === nodeId); + const done = + present.id === documentId && node && assets.has(node.data.backendAssetId ?? "") + ? true + : !mounted.current || present.id !== documentId || !node || Date.now() - started > 180_000 + ? false + : undefined; + if (done === undefined) return; + clearInterval(poll); + resolve(done); + }; + const poll = setInterval(check, 200); + check(); + }); + /** + * Runs a group's generation nodes. A node waits for the members it + * references, directly or through non-generating nodes, and is skipped when + * one of them fails; independent nodes run concurrently. Submissions go one + * at a time, as the project's command queue would serialize them anyway, so + * each starts from the canvas the previous one left behind. + */ + const runGroupWorkflow = async (groupId: string) => { + const document = historyRef.current.present; + const steps = groupDescendants(document.nodes, groupId).filter((node) => + isWorkflowStep( + node, + document.edges.some( + (edge) => edge.target === node.id && edge.intent !== "post_edit_result", + ), + ), + ); + if (!steps.length) { + notify(t("canvas.workspace.group.noRunnableNodes")); + return; + } + notify(t("canvas.workspace.group.workflowStarted", { count: steps.length })); + let submissions: Promise = Promise.resolve(); + const result = await runWorkflowSteps( + steps.map((node) => node.id), + document.edges, + async (id) => { + const submitted = submissions.then(() => + historyRef.current.present.id === document.id + ? prepareLatest.current(id) + : [], + ); + submissions = submitted.catch(() => undefined); + const jobIds = await submitted; + if (!jobIds.length || !(await awaitJobs(document.id, jobIds))) return false; + return awaitDisplayed(document.id, id, jobIds); + }, + ); + if (historyRef.current.present.id !== document.id) return; + notify( + result.failed.length || result.skipped.length + ? t("canvas.workspace.group.workflowIncomplete", { + succeeded: result.succeeded.length, + failed: result.failed.length, + skipped: result.skipped.length, + }) + : t("canvas.workspace.group.workflowDone", { count: steps.length }), + ); }; const importActionResultFiles = async (files: File[]) => { const binding = resultBinding.current; @@ -705,6 +837,18 @@ export function useWorkspaceGeneration(rt: WorkspaceRuntime) { rt.runServiceGeneration = runServiceGeneration; rt.cancelServiceRuns = cancelServiceRuns; rt.prepareGeneration = prepareGeneration; + rt.runGroupWorkflow = runGroupWorkflow; rt.importActionResultFiles = importActionResultFiles; rt.cancelResultImport = cancelResultImport; } + +/** + * Group members a workflow run generates: nodes with a configured model and a + * prompt (media may instead take their prompt from connected references). + */ +function isWorkflowStep(node: CanvasNode, referenced: boolean): boolean { + const prompt = Boolean(node.data.prompt?.trim() || node.data.promptContent?.length); + if (node.type === "text") return Boolean(node.data.agentSettings?.modelConfigId && prompt); + if (!["image", "video", "audio"].includes(node.type)) return false; + return Boolean(node.data.settings?.modelConfigId && (prompt || referenced)); +} diff --git a/src/canvas/workspace/useWorkspaceInteraction.tsx b/src/canvas/workspace/useWorkspaceInteraction.tsx index e026a97f..fb001462 100644 --- a/src/canvas/workspace/useWorkspaceInteraction.tsx +++ b/src/canvas/workspace/useWorkspaceInteraction.tsx @@ -1,5 +1,5 @@ import { type Connection, type Edge, type NodeChange, type OnConnectEnd } from "@xyflow/react"; -import { useCallback, useEffect, useMemo, useRef, type ReactNode } from "react"; +import { useCallback, useEffect, useMemo, useRef, type CSSProperties, type ReactNode } from "react"; import { errorMessage } from "../../backend/api.ts"; import { CanvasContextMenu } from "../CanvasContextMenu"; @@ -646,7 +646,17 @@ export function useWorkspaceInteraction(rt: WorkspaceRuntime) { !lockedIds.has(n.id) && !spacePressed && activeAction?.nodeId !== n.id, - style: { width: n.width, height: n.height }, + style: { + width: n.width, + height: n.height, + // A colored group tints its fill and border (#rrggbb plus alpha). + ...(n.type === "group" && n.color + ? { + "--cw-group-fill": `${n.color}24`, + "--cw-group-border": `${n.color}b3`, + } + : {}), + } as CSSProperties, zIndex: n.type === "group" ? -1 : n.zIndex, ariaLabel: t("canvas.workspace.aria.nodeLabel", { kind: t(nodeKindKeys[n.type]), diff --git a/src/canvas/workspace/useWorkspaceTransfer.ts b/src/canvas/workspace/useWorkspaceTransfer.ts index 7b08523a..bc5f5e19 100644 --- a/src/canvas/workspace/useWorkspaceTransfer.ts +++ b/src/canvas/workspace/useWorkspaceTransfer.ts @@ -29,7 +29,8 @@ import { } from "../library/libraryClient"; import { type SubjectImportChoice } from "../library/SubjectImportDialog"; import { applyCommand, createNode, type CanvasCommand, type CanvasNode, type CanvasResource, type Point } from "../model"; -import { copySelection, cutSelection, deleteSelection, groupSelection, pasteFragment, ungroupSelection, type CanvasFragment } from "../operations"; +import { copySelection, cutSelection, deleteSelection, groupDescendants, groupSelection, pasteFragment, ungroupSelection, type CanvasFragment } from "../operations"; +import { createZip } from "../zipArchive"; import { buildCanvasArchive, readCanvasArchive } from "../canvasArchive.ts"; import { @@ -582,6 +583,65 @@ export function useWorkspaceTransfer(rt: WorkspaceRuntime) { notify(error instanceof Error ? error.message : t("canvas.workspace.toast.downloadFailed")); } }; + /** Downloads every asset and text in a group, as one ZIP when there are several. */ + const downloadGroup = async (group: CanvasNode) => { + const document = historyRef.current.present; + const members = groupDescendants(document.nodes, group.id).filter( + (node) => + (node.data.src && node.data.assetState !== "missing") || + (node.type === "text" && node.data.text), + ); + if (!members.length) { + notify(t("canvas.workspace.toast.noDownloadableAsset")); + return; + } + if (members.length === 1) { + await downloadNode(members[0]!); + return; + } + try { + const used = new Set(); + const entries = await Promise.all( + members.map(async (node) => { + let blob: Blob; + if (node.data.src) { + const source = ["image/heic", "image/heif"].includes(node.data.mimeType ?? "") + ? await creationClient(document.id).originalMedia(node.data.src, node.data.mimeType) + : node.data.src; + if (source instanceof Blob) blob = source; + else { + const response = await fetch(source); + if (!response.ok) throw new Error(t("canvas.workspace.toast.downloadFailed")); + blob = await response.blob(); + } + } else + blob = new Blob([node.data.text ?? ""], { type: "text/plain;charset=utf-8" }); + return { node, blob }; + }), + ); + const zipEntries = await Promise.all( + entries.map(async ({ node, blob }) => { + const name = downloadFilename( + node.data.title, + node.data.src ? node.data.mimeType : "text/plain", + ).replace(/[\\/]/g, "_"); + const dot = name.lastIndexOf("."); + const [stem, extension] = dot > 0 ? [name.slice(0, dot), name.slice(dot)] : [name, ""]; + // Names differing only in case collide when extracted on macOS or Windows. + let path = name; + for (let index = 2; used.has(path.toLowerCase()); index++) path = `${stem} (${index})${extension}`; + used.add(path.toLowerCase()); + return { path, data: new Uint8Array(await blob.arrayBuffer()) }; + }), + ); + downloadFile( + await createZip(zipEntries), + `${(group.data.title || t("canvas.workspace.kind.group")).replace(/[\\/]/g, "_")}.zip`, + ); + } catch (error) { + notify(error instanceof Error ? error.message : t("canvas.workspace.toast.downloadFailed")); + } + }; const exportDocument = async () => { try { const document = historyRef.current.present; @@ -886,6 +946,7 @@ export function useWorkspaceTransfer(rt: WorkspaceRuntime) { rt.group = group; rt.ungroup = ungroup; rt.downloadNode = downloadNode; + rt.downloadGroup = downloadGroup; rt.exportDocument = exportDocument; rt.importDocument = importDocument; rt.importCanvasArchive = importCanvasArchive; diff --git a/src/contracts/creation.schema.json b/src/contracts/creation.schema.json index e7a54411..cd5f0113 100644 --- a/src/contracts/creation.schema.json +++ b/src/contracts/creation.schema.json @@ -1009,6 +1009,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -2443,6 +2457,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -2821,6 +2849,20 @@ "description": "Whether edits to this object are blocked.", "title": "Locked" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format, or '' for the default.", + "title": "Color" + }, "parentId": { "anyOf": [ { @@ -4410,6 +4452,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -4701,6 +4757,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -5199,6 +5269,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -5879,6 +5963,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -6123,6 +6221,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -8018,6 +8130,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -11818,6 +11944,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -12215,6 +12355,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -12482,6 +12636,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -12854,6 +13022,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -13937,6 +14119,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -14470,6 +14666,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -16805,6 +17015,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -18282,6 +18506,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -18675,6 +18913,20 @@ "description": "Whether edits to this object are blocked.", "title": "Locked" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format, or '' for the default.", + "title": "Color" + }, "parentId": { "anyOf": [ { @@ -20294,6 +20546,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -20603,6 +20869,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -21121,6 +21401,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -21833,6 +22127,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -22092,6 +22400,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -24079,6 +24401,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -27983,6 +28319,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -28399,6 +28749,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -28687,6 +29051,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -29083,6 +29461,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -30204,6 +30596,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", @@ -30759,6 +31165,20 @@ "title": "Locked", "type": "boolean" }, + "color": { + "anyOf": [ + { + "pattern": "^(#[0-9a-fA-F]{6})?$", + "type": "string" + }, + { + "type": "null" + } + ], + "default": null, + "description": "Group background color in #RRGGBB format.", + "title": "Color" + }, "visible": { "default": true, "description": "Whether this object is displayed.", diff --git a/src/contracts/generated.ts b/src/contracts/generated.ts index fde278c6..b50ffd4b 100644 --- a/src/contracts/generated.ts +++ b/src/contracts/generated.ts @@ -259,6 +259,7 @@ export interface CanvasItemBaseInput extends ProjectEntityInput { readonly height?: number; readonly zIndex?: number; readonly locked?: boolean; + readonly color?: string | null; readonly visible?: boolean; readonly parentId?: string | null; readonly tags?: ReadonlyArray; @@ -310,6 +311,7 @@ export interface CanvasPresentationUpdateInput extends ContractInput { readonly height?: number | null; readonly zIndex?: number | null; readonly locked?: boolean | null; + readonly color?: string | null; readonly parentId?: string | null; } @@ -1906,6 +1908,7 @@ export interface CanvasItemBase extends ProjectEntity { readonly height: number; readonly zIndex: number; readonly locked: boolean; + readonly color?: string | null; readonly visible: boolean; readonly parentId: string | null; readonly tags: ReadonlyArray; @@ -1957,6 +1960,7 @@ export interface CanvasPresentationUpdate extends Contract { readonly height: number | null; readonly zIndex: number | null; readonly locked: boolean | null; + readonly color?: string | null; readonly parentId: string | null; } diff --git a/src/i18n/messages/en-US/canvasShell.ts b/src/i18n/messages/en-US/canvasShell.ts index 3b86829c..567ca5cf 100644 --- a/src/i18n/messages/en-US/canvasShell.ts +++ b/src/i18n/messages/en-US/canvasShell.ts @@ -141,6 +141,23 @@ export const canvasShell = { "canvas.selection.assistant": "AI assistant", "canvas.selection.previewFile": "Preview file", "canvas.selection.previewFullscreen": "Full-screen preview", + "canvas.selection.group.run": "Run workflow", + "canvas.selection.group.running": "Workflow running", + "canvas.selection.group.ungroup": "Ungroup", + "canvas.selection.group.layout": "Layout", + "canvas.selection.group.layoutGrid": "Grid", + "canvas.selection.group.layoutRow": "Row", + "canvas.selection.group.layoutColumn": "Column", + "canvas.selection.group.color": "Background", + "canvas.selection.group.colorDefault": "Default", + "canvas.selection.group.colorGray": "Gray", + "canvas.selection.group.colorRed": "Red", + "canvas.selection.group.colorOrange": "Orange", + "canvas.selection.group.colorYellow": "Yellow", + "canvas.selection.group.colorGreen": "Green", + "canvas.selection.group.colorBlue": "Blue", + "canvas.selection.group.colorPurple": "Purple", + "canvas.selection.group.download": "Download group assets", "canvas.selection.duplicate": "Duplicate", "canvas.selection.deleteNode": "Delete node", diff --git a/src/i18n/messages/en-US/canvasWorkspace.ts b/src/i18n/messages/en-US/canvasWorkspace.ts index f87cfcb1..2b2bafbc 100644 --- a/src/i18n/messages/en-US/canvasWorkspace.ts +++ b/src/i18n/messages/en-US/canvasWorkspace.ts @@ -122,6 +122,17 @@ export const canvasWorkspace = { "canvas.workspace.selection.duplicate": "Duplicate", "canvas.workspace.selection.deleteSelected": "Delete selection", + "canvas.workspace.group.noRunnableNodes": "No runnable generation nodes in this group. Choose a model and enter a prompt first.", + "canvas.workspace.group.workflowStarted": { + one: "Running the workflow: {count} node.", + other: "Running the workflow: {count} nodes.", + }, + "canvas.workspace.group.workflowDone": { + one: "Workflow finished: {count} node generated.", + other: "Workflow finished: all {count} nodes generated.", + }, + "canvas.workspace.group.workflowIncomplete": "Workflow finished: {succeeded} succeeded, {failed} failed, {skipped} skipped (failed upstream or reference cycle).", + "canvas.workspace.menu.rename": "Rename", "canvas.workspace.menu.addDownstream": "Add downstream node", "canvas.workspace.menu.copy": "Copy", diff --git a/src/i18n/messages/zh-CN/canvasShell.ts b/src/i18n/messages/zh-CN/canvasShell.ts index fb168d0a..dd3220b1 100644 --- a/src/i18n/messages/zh-CN/canvasShell.ts +++ b/src/i18n/messages/zh-CN/canvasShell.ts @@ -137,6 +137,23 @@ export const canvasShell = { "canvas.selection.assistant": "AI 助手", "canvas.selection.previewFile": "预览文件", "canvas.selection.previewFullscreen": "全屏预览", + "canvas.selection.group.run": "运行工作流", + "canvas.selection.group.running": "工作流运行中", + "canvas.selection.group.ungroup": "解除编组", + "canvas.selection.group.layout": "布局", + "canvas.selection.group.layoutGrid": "网格排列", + "canvas.selection.group.layoutRow": "横向排列", + "canvas.selection.group.layoutColumn": "纵向排列", + "canvas.selection.group.color": "背景色", + "canvas.selection.group.colorDefault": "默认", + "canvas.selection.group.colorGray": "灰色", + "canvas.selection.group.colorRed": "红色", + "canvas.selection.group.colorOrange": "橙色", + "canvas.selection.group.colorYellow": "黄色", + "canvas.selection.group.colorGreen": "绿色", + "canvas.selection.group.colorBlue": "蓝色", + "canvas.selection.group.colorPurple": "紫色", + "canvas.selection.group.download": "下载组内素材", "canvas.selection.duplicate": "创建副本", "canvas.selection.deleteNode": "删除节点", diff --git a/src/i18n/messages/zh-CN/canvasWorkspace.ts b/src/i18n/messages/zh-CN/canvasWorkspace.ts index 3a9dbca8..b56bee86 100644 --- a/src/i18n/messages/zh-CN/canvasWorkspace.ts +++ b/src/i18n/messages/zh-CN/canvasWorkspace.ts @@ -121,6 +121,17 @@ export const canvasWorkspace = { "canvas.workspace.selection.duplicate": "复制副本", "canvas.workspace.selection.deleteSelected": "删除所选", + "canvas.workspace.group.noRunnableNodes": "分组内没有可运行的生成节点:需要先选好模型并填写提示词。", + "canvas.workspace.group.workflowStarted": { + one: "开始运行工作流,共 {count} 个节点。", + other: "开始运行工作流,共 {count} 个节点。", + }, + "canvas.workspace.group.workflowDone": { + one: "工作流已完成,{count} 个节点全部生成成功。", + other: "工作流已完成,{count} 个节点全部生成成功。", + }, + "canvas.workspace.group.workflowIncomplete": "工作流已结束:成功 {succeeded} 个,失败 {failed} 个,因上游失败或循环引用跳过 {skipped} 个。", + "canvas.workspace.menu.rename": "重命名", "canvas.workspace.menu.addDownstream": "添加下游节点", "canvas.workspace.menu.copy": "复制", diff --git a/tests/canvasModel.test.ts b/tests/canvasModel.test.ts index b8d73165..9f723093 100644 --- a/tests/canvasModel.test.ts +++ b/tests/canvasModel.test.ts @@ -548,3 +548,21 @@ test("empty media cards use the agreed shapes and adopt the file aspect once kno [audio.width, audio.height], ); }); + +test("a group color change is a business content change", () => { + const group = createNode("group"); + const document = { ...createDocument("Colors"), nodes: [group] }; + const colored = { ...document, nodes: [{ ...group, color: "#3e63dd" }] }; + assert.equal(sameCanvasContent(document, colored), false); + assert.equal( + sameCanvasContent(colored, { ...document, nodes: [{ ...group, color: "#3e63dd" }] }), + true, + ); + // Clearing the color back to the default is also saved. + assert.equal( + sameCanvasContent(colored, { ...document, nodes: [{ ...group, color: undefined }] }), + false, + ); + assert.equal(parseDocument(colored).ok, true); + assert.equal(parseDocument({ ...document, nodes: [{ ...group, color: "blue" }] }).ok, false); +}); diff --git a/tests/canvasOperations.test.ts b/tests/canvasOperations.test.ts index f0fb079b..6a702420 100644 --- a/tests/canvasOperations.test.ts +++ b/tests/canvasOperations.test.ts @@ -8,7 +8,9 @@ import { edgesAttachedTo, deleteSelection, orderNodesForCanvas, + groupDescendants, groupSelection, + layoutGroup, pasteFragment, ungroupSelection, } from "../src/canvas/operations.ts"; @@ -365,3 +367,63 @@ test("deleting nodes lists its attached references while other edges stay", () = assert.deepEqual(deleted.nodes.map((node) => node.id), [other.id, target.id]); assert.deepEqual(deleted.edges.map((edge) => edge.id), ["e-other"]); }); + +test("layoutGroup arranges direct members and fits the group around them", () => { + const group = { ...createNode("group", { x: 100, y: 100 }), width: 900, height: 900 }; + const member = (x: number, y: number, width: number, height: number) => ({ + ...createNode("image", { x, y }), + width, + height, + parentId: group.id, + }); + const a = member(300, 30, 200, 100); + const b = member(24, 400, 100, 150); + const c = member(24, 40, 120, 80); + const outside = createNode("text", { x: 0, y: 0 }); + const document: CanvasDocument = { + ...createDocument(), + nodes: [group, a, b, c, outside], + }; + const at = (doc: CanvasDocument, id: string) => + doc.nodes.find((node) => node.id === id)!; + + const row = layoutGroup(document, group.id, "row"); + // Left-to-right order: c, b (x 24, lower), a. + assert.deepEqual(at(row, c.id).position, { x: 24, y: 40 }); + assert.deepEqual(at(row, b.id).position, { x: 184, y: 40 }); + assert.deepEqual(at(row, a.id).position, { x: 324, y: 40 }); + assert.equal(at(row, group.id).width, 324 + 200 + 24); + assert.equal(at(row, group.id).height, 40 + 150 + 24); + assert.deepEqual(at(row, group.id).position, group.position); + assert.equal(at(row, outside.id), outside); + + const column = layoutGroup(document, group.id, "column"); + // Top-to-bottom order: a (y 30), c, b. + assert.deepEqual( + [a, c, b].map((node) => at(column, node.id).position), + [ + { x: 24, y: 40 }, + { x: 24, y: 180 }, + { x: 24, y: 300 }, + ], + ); + assert.equal(at(column, group.id).width, 24 + 200 + 24); + + const grid = layoutGroup(document, group.id, "grid"); + assert.deepEqual(at(grid, b.id).position, { x: 24, y: 40 + 100 + 40 }); + assert.equal(layoutGroup(document, outside.id, "grid"), document); +}); + +test("groupDescendants returns members at every depth", () => { + const outer = createNode("group", { x: 0, y: 0 }); + const inner = { ...createNode("group", { x: 10, y: 10 }), parentId: outer.id }; + const leaf = { ...createNode("image", { x: 5, y: 5 }), parentId: inner.id }; + const direct = { ...createNode("text", { x: 50, y: 5 }), parentId: outer.id }; + const other = createNode("image", { x: 500, y: 500 }); + const nodes = [leaf, other, direct, inner, outer]; + assert.deepEqual( + new Set(groupDescendants(nodes, outer.id).map((node) => node.id)), + new Set([inner.id, leaf.id, direct.id]), + ); + assert.deepEqual(groupDescendants(nodes, inner.id).map((node) => node.id), [leaf.id]); +}); diff --git a/tests/groupWorkflow.test.ts b/tests/groupWorkflow.test.ts new file mode 100644 index 00000000..325353fd --- /dev/null +++ b/tests/groupWorkflow.test.ts @@ -0,0 +1,86 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { runWorkflowSteps } from "../src/canvas/groupWorkflow.ts"; + +const edge = (source: string, target: string) => ({ source, target, intent: undefined }); + +function harness(fail: readonly string[] = []) { + const events: string[] = []; + const pending = new Map void>(); + let active = 0, + peak = 0; + const runStep = (id: string) => + new Promise((resolve) => { + events.push(`start:${id}`); + active++; + peak = Math.max(peak, active); + pending.set(id, () => { + active--; + events.push(`end:${id}`); + resolve(!fail.includes(id)); + }); + }); + const finish = async (id: string) => { + for (let tries = 0; !pending.has(id) && tries < 50; tries++) + await new Promise((resolve) => setTimeout(resolve, 1)); + pending.get(id)!(); + pending.delete(id); + await new Promise((resolve) => setTimeout(resolve, 1)); + }; + return { events, runStep, finish, peak: () => peak }; +} + +test("independent steps run concurrently and dependents wait for their references", async () => { + const h = harness(); + // a -> c, b -> c, d independent. + const run = runWorkflowSteps(["a", "b", "c", "d"], [edge("a", "c"), edge("b", "c")], h.runStep); + await new Promise((resolve) => setTimeout(resolve, 1)); + assert.deepEqual(h.events, ["start:a", "start:b", "start:d"]); + await h.finish("a"); + assert.ok(!h.events.includes("start:c"), "c waits for b"); + await h.finish("b"); + assert.ok(h.events.includes("start:c")); + await h.finish("c"); + await h.finish("d"); + assert.deepEqual(await run, { succeeded: ["a", "b", "c", "d"], failed: [], skipped: [] }); + assert.equal(h.peak(), 3); +}); + +test("a failure skips its downstream chain but not unrelated steps", async () => { + const h = harness(["a"]); + const run = runWorkflowSteps(["a", "b", "c", "x"], [edge("a", "b"), edge("b", "c")], h.runStep); + await h.finish("a"); + await h.finish("x"); + assert.deepEqual(await run, { succeeded: ["x"], failed: ["a"], skipped: ["b", "c"] }); + assert.ok(!h.events.includes("start:b")); +}); + +test("reference cycles are skipped; inputs-only nodes and edit provenance add no dependency", async () => { + const h = harness(); + const run = runWorkflowSteps( + ["a", "b", "c"], + [edge("a", "b"), edge("b", "a"), edge("material", "c"), { source: "a", target: "c", intent: "post_edit_result" }], + h.runStep, + ); + await h.finish("c"); + assert.deepEqual(await run, { succeeded: ["c"], failed: [], skipped: ["a", "b"] }); +}); + +test("a dependency carried through a non-step node still orders the steps", async () => { + const h = harness(); + // image a -> subject s -> video b: b must wait for a. + const run = runWorkflowSteps(["a", "b"], [edge("a", "s"), edge("s", "b")], h.runStep); + await new Promise((resolve) => setTimeout(resolve, 1)); + assert.deepEqual(h.events, ["start:a"]); + await h.finish("a"); + await h.finish("b"); + assert.deepEqual(await run, { succeeded: ["a", "b"], failed: [], skipped: [] }); +}); + +test("a step that throws counts as failed", async () => { + const result = await runWorkflowSteps(["a", "b"], [edge("a", "b")], async (id) => { + if (id === "a") throw new Error("boom"); + return true; + }); + assert.deepEqual(result, { succeeded: [], failed: ["a"], skipped: ["b"] }); +}); diff --git a/tests/test_creation_canvas_service.py b/tests/test_creation_canvas_service.py index 9b3d573c..ac7f8f6a 100644 --- a/tests/test_creation_canvas_service.py +++ b/tests/test_creation_canvas_service.py @@ -468,3 +468,37 @@ def test_material_show_uses_media_ratio_unless_size_authored(project, size, expl restored = next(item for item in reloaded.snapshot(pid).canvasItems if item.id == item_id) assert (restored.width, restored.height) == (saved.width, saved.height) assert restored.binding.objectRef == asset_ref(asset) + + +def test_group_color_is_set_reset_and_validated(project): + store, pid, session = project + group_id = uuid4() + assert send(store, pid, 'canvas.show', session=session, + item=dict(id=group_id, projectId=pid, kind='group', color='#3e63dd')).status == 'applied' + group = store.snapshot(pid).canvasItems[-1] + assert group.color == '#3e63dd' + assert send(store, pid, 'canvas.update', session=session, versions=[ver('canvas_item', group)], + itemId=group.id, color='#30a46c').status == 'applied' + group = store.snapshot(pid).canvasItems[-1] + assert group.color == '#30a46c' + # An empty color restores the default, which is omitted from the item. + assert send(store, pid, 'canvas.update', session=session, versions=[ver('canvas_item', group)], + itemId=group.id, color='').status == 'applied' + group = store.snapshot(pid).canvasItems[-1] + assert group.color is None and 'color' not in group.model_dump(mode='json') + with pytest.raises(ValueError, match='color'): + send(store, pid, 'canvas.update', session=session, versions=[ver('canvas_item', group)], + itemId=group.id, color='blue') + + +def test_canvas_update_without_color_keeps_its_historical_fingerprint(): + # Commands recorded before the color field existed must hash the same, or + # history validation rejects every project containing a canvas.update. + item_id = str(uuid4()) + base = dict(commandId=str(uuid4()), projectId=str(uuid4()), type='canvas.update', + expectedVersions=[dict(objectType='canvas_item', id=item_id, revision=1)]) + legacy = dict(itemId=item_id, title=None, width=None, height=336.0, zIndex=None, locked=None, parentId=None) + command = parse_command(dict(base, payload=legacy)) + # The fingerprint hashes this dump, so it must equal the pre-color payload exactly. + assert command.model_dump(mode='json')['payload'] == legacy + assert parse_command(dict(base, payload=dict(legacy, color=''))).model_dump(mode='json')['payload']['color'] == ''