Skip to content

Image Pipeline Playground

feat(frontend/workflow): embed asset registry in export schema

Mohammad Rajaeibe0375a0cbd9
diff --git a/frontend/src/workflow/io.ts b/frontend/src/workflow/io.ts
index 2e8b74c..9de0e0f 100644
--- a/frontend/src/workflow/io.ts
+++ b/frontend/src/workflow/io.ts
@@ -25,6 +25,22 @@ export interface WorkflowGraphPayload {
}>
}
+export type WorkflowAssetFile = {
+ name: string
+ /** Absolute path when available (same-machine restore). */
+ path?: string
+ /** Portable file bytes for cross-machine import. */
+ content_b64?: string
+}
+
+export type WorkflowAssetBatch = {
+ kind: 'external' | 'staged' | 'folder'
+ root?: string | null
+ files: WorkflowAssetFile[]
+}
+
+export type WorkflowAssets = Record<string, WorkflowAssetBatch>
+
export interface WorkflowDocument {
version: 1
id?: string
@@ -36,6 +52,8 @@ export interface WorkflowDocument {
/** How many times to run the pipeline (stochastic variation). */
iterationCount: number
graph: WorkflowGraphPayload
+ /** Asset registry batches referenced by load_image nodes (export/import). */
+ assets?: WorkflowAssets
}
function isRecord(value: unknown): value is Record<string, unknown> {
@@ -65,6 +83,10 @@ function normalizeGraph(graph: WorkflowGraphPayload): WorkflowGraphPayload {
const record = node as unknown as Record<string, unknown>
const position = isRecord(record.position) ? record.position : {}
const params = isRecord(record.params) ? { ...record.params } : {}
+ // Drop legacy load_image.path — assets use asset_batch_id + embedded registry.
+ if (String(record.type ?? '') === 'load_image') {
+ delete params.path
+ }
return {
id: String(record.id ?? ''),
type: String(record.type ?? ''),
@@ -90,6 +112,38 @@ function normalizeGraph(graph: WorkflowGraphPayload): WorkflowGraphPayload {
}
}
+function normalizeAssets(value: unknown): WorkflowAssets | undefined {
+ if (!isRecord(value)) return undefined
+ const assets: WorkflowAssets = {}
+ for (const [batchId, rawBatch] of Object.entries(value)) {
+ if (!isRecord(rawBatch) || !Array.isArray(rawBatch.files)) continue
+ const kindRaw = String(rawBatch.kind ?? 'external')
+ const kind: WorkflowAssetBatch['kind'] =
+ kindRaw === 'staged' || kindRaw === 'folder' ? kindRaw : 'external'
+ const files: WorkflowAssetFile[] = []
+ for (const item of rawBatch.files) {
+ if (!isRecord(item)) continue
+ const name = String(item.name ?? '').trim()
+ if (!name) continue
+ const path = typeof item.path === 'string' ? item.path : undefined
+ const content_b64 =
+ typeof item.content_b64 === 'string' ? item.content_b64 : undefined
+ files.push({
+ name,
+ ...(path ? { path } : {}),
+ ...(content_b64 ? { content_b64 } : {}),
+ })
+ }
+ if (files.length === 0) continue
+ assets[batchId] = {
+ kind,
+ root: typeof rawBatch.root === 'string' ? rawBatch.root : null,
+ files,
+ }
+ }
+ return Object.keys(assets).length > 0 ? assets : undefined
+}
+
function readIterationCount(value: Record<string, unknown>): number {
const raw =
typeof value.iterationCount === 'number'
@@ -138,6 +192,7 @@ export function coerceWorkflowDocument(value: unknown): WorkflowDocument {
const id = readOptionalString(value.id)
const createdAt = readOptionalString(value.createdAt)
const updatedAt = readOptionalString(value.updatedAt)
+ const assets = normalizeAssets(value.assets)
return {
version: 1,
@@ -149,6 +204,7 @@ export function coerceWorkflowDocument(value: unknown): WorkflowDocument {
seed: typeof value.seed === 'number' ? value.seed : 0,
iterationCount: readIterationCount(value),
graph: normalizeGraph(graph),
+ ...(assets ? { assets } : {}),
}
}
diff --git a/frontend/src/workflow/workflowAssets.ts b/frontend/src/workflow/workflowAssets.ts
new file mode 100644
index 0000000..d67685a
--- /dev/null
+++ b/frontend/src/workflow/workflowAssets.ts
@@ -0,0 +1,209 @@
+/** Collect / restore asset registry entries embedded in workflow exports. */
+
+import {
+ assetPreviewUrl,
+ batchFilePaths,
+ batchPreviewUrls,
+ registerLocalPaths,
+ uploadImageFiles,
+} from '../api/assets'
+import type { AssetBatch } from '../types'
+import type {
+ WorkflowAssetBatch,
+ WorkflowAssetFile,
+ WorkflowAssets,
+ WorkflowDocument,
+} from './io'
+
+function referencedBatchIds(doc: WorkflowDocument): string[] {
+ const ids = new Set<string>()
+ for (const node of doc.graph.nodes) {
+ if (node.type !== 'load_image') continue
+ const batchId = String(node.params.asset_batch_id ?? '').trim()
+ if (batchId) ids.add(batchId)
+ }
+ return [...ids]
+}
+
+/** Drop legacy load_image.path from all nodes. */
+export function stripLegacyLoadPaths(doc: WorkflowDocument): WorkflowDocument {
+ return {
+ ...doc,
+ graph: {
+ ...doc.graph,
+ nodes: doc.graph.nodes.map((node) => {
+ if (node.type !== 'load_image') return node
+ const params = { ...node.params }
+ delete params.path
+ return { ...node, params }
+ }),
+ },
+ }
+}
+
+function arrayBufferToBase64(buffer: ArrayBuffer): string {
+ const bytes = new Uint8Array(buffer)
+ const chunk = 0x8000
+ let binary = ''
+ for (let i = 0; i < bytes.length; i += chunk) {
+ binary += String.fromCharCode(...bytes.subarray(i, i + chunk))
+ }
+ return btoa(binary)
+}
+
+function base64ToUint8Array(value: string): Uint8Array {
+ const binary = atob(value)
+ const bytes = new Uint8Array(binary.length)
+ for (let i = 0; i < binary.length; i += 1) {
+ bytes[i] = binary.charCodeAt(i)
+ }
+ return bytes
+}
+
+async function fetchBatch(batchId: string): Promise<AssetBatch> {
+ const response = await fetch(`/api/assets/${batchId}`)
+ if (!response.ok) {
+ throw new Error(`Could not read asset batch ${batchId}`)
+ }
+ const payload = (await response.json()) as { batch?: AssetBatch } | AssetBatch
+ if (payload && typeof payload === 'object' && 'batch' in payload && payload.batch) {
+ return payload.batch
+ }
+ return payload as AssetBatch
+}
+
+async function embedBatch(batchId: string): Promise<WorkflowAssetBatch> {
+ const batch = await fetchBatch(batchId)
+ const sourceFiles = Array.isArray(batch.files) ? batch.files : []
+ if (sourceFiles.length === 0) {
+ throw new Error(`Asset batch ${batchId} has no files to export`)
+ }
+ const files: WorkflowAssetFile[] = []
+ for (const file of sourceFiles) {
+ const response = await fetch(assetPreviewUrl(batchId, file.name))
+ if (!response.ok) {
+ throw new Error(`Could not read asset file ${file.name}`)
+ }
+ const buffer = await response.arrayBuffer()
+ files.push({
+ name: file.name,
+ path: file.path,
+ content_b64: arrayBufferToBase64(buffer),
+ })
+ }
+ return {
+ kind: batch.kind,
+ root: batch.root ?? null,
+ files,
+ }
+}
+
+/** Attach referenced registry batches (with file bytes) and strip legacy path. */
+export async function buildExportDocument(
+ doc: WorkflowDocument,
+): Promise<WorkflowDocument> {
+ const cleaned = stripLegacyLoadPaths(doc)
+ const assets: WorkflowAssets = {}
+ for (const batchId of referencedBatchIds(cleaned)) {
+ assets[batchId] = await embedBatch(batchId)
+ }
+ if (Object.keys(assets).length === 0) {
+ return omitAssets(cleaned)
+ }
+ return { ...omitAssets(cleaned), assets }
+}
+
+async function importBatch(
+ batch: WorkflowAssetBatch,
+): Promise<{ batchId: string; previewUrls: string[]; filePaths: string[] }> {
+ const withContent = batch.files.filter((file) => file.content_b64)
+ if (withContent.length > 0) {
+ const files = withContent.map((file) => {
+ const bytes = base64ToUint8Array(file.content_b64!)
+ const copy = new Uint8Array(bytes.byteLength)
+ copy.set(bytes)
+ return new File([copy], file.name)
+ })
+ const result = await uploadImageFiles(files, {
+ asFolder: batch.kind === 'folder' || files.length > 1,
+ })
+ const registered = await fetchBatch(result.asset_batch_id)
+ return {
+ batchId: result.asset_batch_id,
+ previewUrls: batchPreviewUrls(registered),
+ filePaths: batchFilePaths(registered),
+ }
+ }
+
+ const paths = batch.files
+ .map((file) => file.path?.trim())
+ .filter((path): path is string => Boolean(path))
+ if (paths.length === 0 && batch.root?.trim()) {
+ paths.push(batch.root.trim())
+ }
+ if (paths.length === 0) {
+ throw new Error('Embedded asset batch has no file content or paths')
+ }
+ const result = await registerLocalPaths(paths, {
+ asFolder: batch.kind === 'folder' || paths.length > 1,
+ })
+ return {
+ batchId: result.batch.id,
+ previewUrls: batchPreviewUrls(result.batch),
+ filePaths: batchFilePaths(result.batch),
+ }
+}
+
+function omitAssets(doc: WorkflowDocument): WorkflowDocument {
+ if (!('assets' in doc)) return doc
+ const next = { ...doc }
+ delete next.assets
+ return next
+}
+
+export type RehydratedAssets = {
+ doc: WorkflowDocument
+ /** new batch id → preview urls / files for local canvas thumbnails */
+ previews: Record<string, { urls: string[]; files: string[] }>
+}
+
+/** Re-register embedded assets and remap load_image asset_batch_id values. */
+export async function rehydrateWorkflowAssets(
+ doc: WorkflowDocument,
+): Promise<RehydratedAssets> {
+ const embedded = doc.assets
+ const cleaned = stripLegacyLoadPaths(doc)
+ if (!embedded || Object.keys(embedded).length === 0) {
+ return { doc: omitAssets(cleaned), previews: {} }
+ }
+
+ const idMap = new Map<string, string>()
+ const previews: RehydratedAssets['previews'] = {}
+ for (const [oldId, batch] of Object.entries(embedded)) {
+ const imported = await importBatch(batch)
+ idMap.set(oldId, imported.batchId)
+ previews[imported.batchId] = {
+ urls: imported.previewUrls,
+ files: imported.filePaths,
+ }
+ }
+
+ const nodes = cleaned.graph.nodes.map((node) => {
+ if (node.type !== 'load_image') return node
+ const params = { ...node.params }
+ delete params.path
+ const oldId = String(params.asset_batch_id ?? '').trim()
+ if (oldId && idMap.has(oldId)) {
+ params.asset_batch_id = idMap.get(oldId)
+ }
+ return { ...node, params }
+ })
+
+ return {
+ doc: {
+ ...omitAssets(cleaned),
+ graph: { ...cleaned.graph, nodes },
+ },
+ previews,
+ }
+}