Skip to content

Image Pipeline Playground

feat(frontend/store): serialize and load workflow JSON documents

Mohammad Rajaei43cd4332c7d2
diff --git a/frontend/src/store/graphStore.ts b/frontend/src/store/graphStore.ts
index d3013ae..5ff1984 100644
--- a/frontend/src/store/graphStore.ts
+++ b/frontend/src/store/graphStore.ts
@@ -10,6 +10,8 @@ import {
} from '@xyflow/react'
import { create } from 'zustand'
import type { ExecutionPreview, GraphNodeData, NodeMetadata } from '../types'
+import type { WorkflowDocument, WorkflowGraphPayload } from '../workflow/io'
+
type PipelineNode = Node<GraphNodeData>
@@ -64,25 +66,22 @@ interface GraphState {
setSampleCount: (count: number) => void
getInputImages: (nodeId: string) => string[]
getResultImages: (nodeId: string) => string[]
- toGraphPayload: () => {
- nodes: Array<{
- id: string
- type: string
- params: Record<string, unknown>
- position: { x: number; y: number }
- }>
- edges: Array<{
- id: string
- source: string
- source_port: string
- target: string
- target_port: string
- }>
- }
+ toGraphPayload: () => WorkflowGraphPayload
+ toWorkflowDocument: () => WorkflowDocument
+ loadWorkflow: (doc: WorkflowDocument) => { skippedTypes: string[] }
}
let nodeCounter = 1
+function syncNodeCounter(nodeIds: string[]) {
+ let max = 0
+ for (const id of nodeIds) {
+ const match = /-(\d+)$/.exec(id)
+ if (match) max = Math.max(max, Number(match[1]))
+ }
+ nodeCounter = Math.max(nodeCounter, max + 1)
+}
+
function emptyNodeImages(): NodeImageState {
return { result: null, samples: [], ports: {}, portSamples: {} }
}
@@ -461,4 +460,79 @@ export const useGraphStore = create<GraphState>((set, get) => ({
})),
}
},
+
+ toWorkflowDocument: () => {
+ const { seed, sampleCount } = get()
+ return {
+ version: 1 as const,
+ seed,
+ sampleCount,
+ graph: get().toGraphPayload(),
+ }
+ },
+
+ loadWorkflow: (doc) => {
+ const catalogByType = Object.fromEntries(
+ get().nodeCatalog.map((meta) => [meta.type, meta]),
+ )
+ const skipped = new Set<string>()
+ const nodes: PipelineNode[] = []
+ const keptIds = new Set<string>()
+
+ for (const raw of doc.graph.nodes) {
+ const meta = catalogByType[raw.type]
+ if (!meta) {
+ skipped.add(raw.type)
+ continue
+ }
+ keptIds.add(raw.id)
+ nodes.push({
+ id: raw.id,
+ type: 'pipeline',
+ position: raw.position,
+ data: {
+ type: meta.type,
+ label: meta.label,
+ category: meta.category,
+ params: { ...raw.params },
+ ports: meta.ports,
+ localPreviewUrls: [],
+ active: false,
+ },
+ })
+ }
+
+ const edges: Edge[] = doc.graph.edges
+ .filter((edge) => keptIds.has(edge.source) && keptIds.has(edge.target))
+ .map((edge) => ({
+ id: edge.id,
+ source: edge.source,
+ target: edge.target,
+ sourceHandle: edge.source_port,
+ targetHandle: edge.target_port,
+ type: 'editable',
+ zIndex: 1000,
+ reconnectable: true,
+ selectable: true,
+ data: { waypoints: [] },
+ }))
+
+ syncNodeCounter(nodes.map((node) => node.id))
+
+ set({
+ nodes,
+ edges,
+ selectedNodeId: null,
+ activeNodeId: null,
+ previews: [],
+ nodeImages: {},
+ logs: [],
+ isExecuting: false,
+ generatedCode: '# Run codegen to export a Python script\n',
+ seed: doc.seed,
+ sampleCount: Math.max(1, doc.sampleCount),
+ })
+
+ return { skippedTypes: [...skipped].sort() }
+ },
}))
diff --git a/frontend/src/workflow/io.ts b/frontend/src/workflow/io.ts
new file mode 100644
index 0000000..a3569fc
--- /dev/null
+++ b/frontend/src/workflow/io.ts
@@ -0,0 +1,82 @@
+/** Versioned workflow JSON for export / load. */
+
+export interface WorkflowGraphPayload {
+ nodes: Array<{
+ id: string
+ type: string
+ params: Record<string, unknown>
+ position: { x: number; y: number }
+ }>
+ edges: Array<{
+ id: string
+ source: string
+ source_port: string
+ target: string
+ target_port: string
+ }>
+}
+
+export interface WorkflowDocument {
+ version: 1
+ seed: number
+ sampleCount: number
+ graph: WorkflowGraphPayload
+}
+
+export function isWorkflowDocument(value: unknown): value is WorkflowDocument {
+ if (!value || typeof value !== 'object') return false
+ const doc = value as Record<string, unknown>
+ if (doc.version !== 1) return false
+ if (!doc.graph || typeof doc.graph !== 'object') return false
+ const graph = doc.graph as Record<string, unknown>
+ return Array.isArray(graph.nodes) && Array.isArray(graph.edges)
+}
+
+export function parseWorkflowJson(text: string): WorkflowDocument {
+ let parsed: unknown
+ try {
+ parsed = JSON.parse(text) as unknown
+ } catch {
+ throw new Error('Invalid JSON file')
+ }
+ if (!isWorkflowDocument(parsed)) {
+ throw new Error('Not a valid Image Pipes workflow (expected version 1)')
+ }
+ return {
+ version: 1,
+ seed: typeof parsed.seed === 'number' ? parsed.seed : 0,
+ sampleCount:
+ typeof parsed.sampleCount === 'number' ? Math.max(1, parsed.sampleCount) : 1,
+ graph: {
+ nodes: parsed.graph.nodes.map((node) => ({
+ id: String(node.id),
+ type: String(node.type),
+ params:
+ node.params && typeof node.params === 'object'
+ ? { ...(node.params as Record<string, unknown>) }
+ : {},
+ position: {
+ x: Number(node.position?.x ?? 0),
+ y: Number(node.position?.y ?? 0),
+ },
+ })),
+ edges: parsed.graph.edges.map((edge) => ({
+ id: String(edge.id),
+ source: String(edge.source),
+ source_port: String(edge.source_port ?? 'image'),
+ target: String(edge.target),
+ target_port: String(edge.target_port ?? 'image'),
+ })),
+ },
+ }
+}
+
+export function downloadWorkflowJson(doc: WorkflowDocument, filename = 'workflow.json') {
+ const blob = new Blob([JSON.stringify(doc, null, 2)], { type: 'application/json' })
+ const url = URL.createObjectURL(blob)
+ const anchor = document.createElement('a')
+ anchor.href = url
+ anchor.download = filename
+ anchor.click()
+ URL.revokeObjectURL(url)
+}