Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions src/platform/workflow/core/services/workflowService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,31 +310,37 @@ export const useWorkflowService = () => {
value: string | ComfyWorkflow | null,
workflowData: ComfyWorkflowJSON
) => {
// Use workspaceStore here as it is patched in unit tests.
const workflowStore = useWorkspaceStore().workflow
if (typeof value === 'string') {
const workflow = workflowStore.getWorkflowByPath(
ComfyWorkflow.basePath + appendJsonExt(value)
)
if (workflow?.isPersisted) {
const loadedWorkflow = await workflowStore.openWorkflow(workflow)
loadedWorkflow.changeTracker.restore()
loadedWorkflow.changeTracker.reset(workflowData)
return
}
}

if (value === null || typeof value === 'string') {
const path = value as string | null

// Check if a persisted workflow with this path exists
if (path) {
const fullPath = ComfyWorkflow.basePath + appendJsonExt(path)
const existingWorkflow = workflowStore.getWorkflowByPath(fullPath)

// If the workflow exists and is NOT loaded yet (restoration case),
// use the existing workflow instead of creating a new one.
// If it IS loaded, this is a re-import case - create new with suffix.
if (existingWorkflow?.isPersisted && !existingWorkflow.isLoaded) {
const loadedWorkflow =
await workflowStore.openWorkflow(existingWorkflow)
loadedWorkflow.changeTracker.reset(workflowData)
loadedWorkflow.changeTracker.restore()
return
}
}

const tempWorkflow = workflowStore.createTemporary(
path ? appendJsonExt(path) : undefined,
workflowData
workflowData,
true
)
await workflowStore.openWorkflow(tempWorkflow)
return
}

// value is a ComfyWorkflow.
const loadedWorkflow = await workflowStore.openWorkflow(value)
loadedWorkflow.changeTracker.reset(workflowData)
loadedWorkflow.changeTracker.restore()
Expand Down
30 changes: 19 additions & 11 deletions src/platform/workflow/management/stores/workflowStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,21 +365,29 @@ export const useWorkflowStore = defineStore('workflow', () => {
return workflow
}

const createTemporary = (path?: string, workflowData?: ComfyWorkflowJSON) => {
const createTemporary = (
path?: string,
workflowData?: ComfyWorkflowJSON,
forceNew: boolean = false
) => {
const fullPath = getUnconflictedPath(
ComfyWorkflow.basePath + (path ?? 'Unsaved Workflow.json')
)
const existingWorkflow = workflows.value.find((w) => w.fullFilename == path)
if (
path &&
workflowData &&
existingWorkflow?.changeTracker &&
!existingWorkflow.directory.startsWith(
ComfyWorkflow.basePath.slice(0, -1)
if (!forceNew) {
const existingWorkflow = workflows.value.find(
(w) => w.fullFilename == path
)
) {
existingWorkflow.changeTracker.reset(workflowData)
return existingWorkflow
if (
path &&
workflowData &&
existingWorkflow?.changeTracker &&
!existingWorkflow.directory.startsWith(
ComfyWorkflow.basePath.slice(0, -1)
)
) {
existingWorkflow.changeTracker.reset(workflowData)
return existingWorkflow
}
}

const workflow = new ComfyWorkflow({
Expand Down