Workflows can receive images from other workflows/historical prompts

This commit is contained in:
space-nuko
2023-05-22 18:30:25 -05:00
parent b5512e6673
commit 6817e6ad95
24 changed files with 689 additions and 142 deletions

View File

@@ -1,24 +1,28 @@
import type { SvelteComponentDev } from "svelte/internal";
import { writable, type Writable } from "svelte/store";
import { get, writable, type Writable } from "svelte/store";
import { v4 as uuidv4 } from "uuid";
export type ModalState = Record<string, any>;
export type ModalButton = {
name: string,
variant: "primary" | "secondary",
onClick: () => void,
onClick: (state: ModalData) => void,
closeOnClick?: boolean
}
export interface ModalData {
id: string,
title?: string,
onClose?: () => void,
onClose?: (modal: ModalState) => void,
svelteComponent?: typeof SvelteComponentDev,
svelteProps: Record<string, any>,
buttons: ModalButton[],
showCloseButton: boolean,
closeOnClick: boolean
closeOnClick: boolean,
state: Writable<ModalState>,
close: () => void,
}
export interface ModalState {
export interface ModalStates {
activeModals: ModalData[]
}
@@ -28,20 +32,23 @@ export interface ModalStateOps {
closeAllModals: () => void,
}
export type WritableModalStateStore = Writable<ModalState> & ModalStateOps;
const store: Writable<ModalState> = writable(
export type WritableModalStateStore = Writable<ModalStates> & ModalStateOps;
const store: Writable<ModalStates> = writable(
{
activeModals: []
})
function pushModal(data: Partial<ModalData>) {
const id = uuidv4()
const modal: ModalData = {
showCloseButton: true,
closeOnClick: true,
buttons: [],
svelteProps: {},
state: writable({}),
...data,
id: uuidv4(),
id,
close: () => closeModal(id)
}
store.update(s => {
@@ -51,7 +58,13 @@ function pushModal(data: Partial<ModalData>) {
}
function closeModal(id: string) {
const modal = get(store).activeModals.find(m => m.id === id);
if (modal == null)
return;
store.update(s => {
if (modal.onClose)
modal.onClose(modal)
s.activeModals = s.activeModals.filter(m => m.id !== id)
return s;
})

View File

@@ -1,5 +1,5 @@
import type { SerializedGraphCanvasState } from '$lib/ComfyGraphCanvas';
import { clamp, LGraphNode, type LGraphCanvas, type NodeID, type SerializedLGraph, type UUID, LGraph, LiteGraph } from '@litegraph-ts/core';
import { clamp, LGraphNode, type LGraphCanvas, type NodeID, type SerializedLGraph, type UUID, LGraph, LiteGraph, type SlotType, NodeMode } from '@litegraph-ts/core';
import { get, writable } from 'svelte/store';
import type { Readable, Writable } from 'svelte/store';
import { defaultWorkflowAttributes, type SerializedLayoutState, type WritableLayoutStateStore } from './layoutStates';
@@ -9,6 +9,7 @@ import { v4 as uuidv4 } from "uuid";
import type ComfyGraphCanvas from '$lib/ComfyGraphCanvas';
import { blankGraph } from '$lib/defaultGraph';
import type { SerializedAppState } from '$lib/components/ComfyApp';
import type ComfyReceiveOutputNode from '$lib/nodes/actions/ComfyReceiveOutputNode';
type ActiveCanvas = {
canvas: LGraphCanvas | null;
@@ -232,6 +233,11 @@ export type WorkflowState = {
activeWorkflow: ComfyWorkflow | null,
}
export type WorkflowReceiveOutputTargets = {
workflow: ComfyWorkflow,
targetNodes: ComfyReceiveOutputNode[]
}
type WorkflowStateOps = {
getWorkflow: (id: WorkflowInstID) => ComfyWorkflow | null
getWorkflowByGraph: (graph: LGraph) => ComfyWorkflow | null
@@ -243,7 +249,8 @@ type WorkflowStateOps = {
addWorkflow: (canvas: ComfyGraphCanvas, data: ComfyWorkflow) => void,
closeWorkflow: (canvas: ComfyGraphCanvas, index: number) => void,
closeAllWorkflows: (canvas: ComfyGraphCanvas) => void,
setActiveWorkflow: (canvas: ComfyGraphCanvas, index: number) => ComfyWorkflow | null
setActiveWorkflow: (canvas: ComfyGraphCanvas, index: number | WorkflowInstID) => ComfyWorkflow | null,
findReceiveOutputTargets: (type: SlotType | SlotType[]) => WorkflowReceiveOutputTargets[]
}
export type WritableWorkflowStateStore = Writable<WorkflowState> & WorkflowStateOps;
@@ -345,7 +352,7 @@ function closeAllWorkflows(canvas: ComfyGraphCanvas) {
closeWorkflow(canvas, 0)
}
function setActiveWorkflow(canvas: ComfyGraphCanvas, index: number): ComfyWorkflow | null {
function setActiveWorkflow(canvas: ComfyGraphCanvas, index: number | WorkflowInstID): ComfyWorkflow | null {
const state = get(store);
if (state.openedWorkflows.length === 0) {
@@ -354,6 +361,10 @@ function setActiveWorkflow(canvas: ComfyGraphCanvas, index: number): ComfyWorkfl
return null;
}
if (typeof index === "string") {
index = state.openedWorkflows.findIndex(w => w.id === index)
}
if (index < 0 || index >= state.openedWorkflows.length)
return state.activeWorkflow;
@@ -375,6 +386,31 @@ function setActiveWorkflow(canvas: ComfyGraphCanvas, index: number): ComfyWorkfl
return workflow;
}
function findReceiveOutputTargets(type: SlotType | SlotType[]): WorkflowReceiveOutputTargets[] {
let result = []
const state = get(store);
if (!Array.isArray(type))
type = [type]
const types = new Set(type);
for (const workflow of state.openedWorkflows) {
const targetNodes = workflow.graph
// can't use class here because of circular import
.findNodesByTypeRecursive("events/receive_output")
.filter(n => {
return types.has(n.properties.type) && n.mode === NodeMode.ALWAYS
})
if (targetNodes.length > 0)
result.push({ workflow, targetNodes });
}
return result;
}
const workflowStateStore: WritableWorkflowStateStore =
{
...store,
@@ -389,5 +425,6 @@ const workflowStateStore: WritableWorkflowStateStore =
closeWorkflow,
closeAllWorkflows,
setActiveWorkflow,
findReceiveOutputTargets
}
export default workflowStateStore;