-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Maryhipp/canvas workflows #8596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
maryhipp
wants to merge
6
commits into
main
Choose a base branch
from
maryhipp/canvas-workflows
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b24c2cb
introduce new canvas_workflows that can be used to indicate inputs an…
f441ada
feat(ui): implement exposed fields for canvas workflows
1d0b315
cleanup types, lint
4d30a03
error handling
c08f7ff
fix circ dep
6299db7
knip cleanup
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""Canvas workflow bridge invocations.""" | ||
|
||
from invokeai.app.invocations.baseinvocation import ( | ||
BaseInvocation, | ||
Classification, | ||
invocation, | ||
) | ||
from invokeai.app.invocations.fields import ImageField, Input, InputField, WithBoard, WithMetadata | ||
from invokeai.app.invocations.primitives import ImageOutput | ||
from invokeai.app.services.shared.invocation_context import InvocationContext | ||
|
||
|
||
@invocation( | ||
"canvas_composite_raster_input", | ||
title="Canvas Composite Input", | ||
tags=["canvas", "workflow", "canvas-workflow-input"], | ||
category="canvas", | ||
version="1.0.0", | ||
classification=Classification.Beta, | ||
) | ||
class CanvasCompositeRasterInputInvocation(BaseInvocation, WithMetadata, WithBoard): | ||
"""Provides the flattened canvas raster layer to a workflow.""" | ||
|
||
image: ImageField = InputField( | ||
description="The flattened canvas raster layer.", | ||
input=Input.Direct, | ||
) | ||
|
||
def invoke(self, context: InvocationContext) -> ImageOutput: | ||
image_dto = context.images.get_dto(self.image.image_name) | ||
return ImageOutput.build(image_dto=image_dto) | ||
|
||
|
||
@invocation( | ||
"canvas_workflow_output", | ||
title="Canvas Workflow Output", | ||
tags=["canvas", "workflow", "canvas-workflow-output"], | ||
category="canvas", | ||
version="1.0.0", | ||
classification=Classification.Beta, | ||
) | ||
class CanvasWorkflowOutputInvocation(BaseInvocation, WithMetadata, WithBoard): | ||
"""Designates the workflow image output used by the canvas.""" | ||
|
||
image: ImageField = InputField( | ||
description="The workflow's resulting image.", | ||
input=Input.Connection, | ||
) | ||
|
||
def invoke(self, context: InvocationContext) -> ImageOutput: | ||
image_dto = context.images.get_dto(self.image.image_name) | ||
return ImageOutput.build(image_dto=image_dto) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...d/web/src/app/store/middleware/listenerMiddleware/listeners/canvasWorkflowFieldChanged.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import type { ActionCreatorWithPayload } from '@reduxjs/toolkit'; | ||
import type { AppStartListening, RootState } from 'app/store/store'; | ||
import * as canvasWorkflowNodesActions from 'features/controlLayers/store/canvasWorkflowNodesSlice'; | ||
import * as nodesActions from 'features/nodes/store/nodesSlice'; | ||
import type { AnyNode } from 'features/nodes/types/invocation'; | ||
|
||
/** | ||
* Listens for field value changes on nodes and redirects them to the canvas workflow nodes slice | ||
* if the node belongs to a canvas workflow. | ||
*/ | ||
export const addCanvasWorkflowFieldChangedListener = (startListening: AppStartListening) => { | ||
// List of all field mutation actions from nodesSlice with their canvas workflow counterparts | ||
const fieldMutationActionPairs: Array<{ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
nodesAction: ActionCreatorWithPayload<any>; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
canvasAction: ActionCreatorWithPayload<any>; | ||
}> = [ | ||
{ | ||
nodesAction: nodesActions.fieldStringValueChanged, | ||
canvasAction: canvasWorkflowNodesActions.fieldStringValueChanged, | ||
}, | ||
{ | ||
nodesAction: nodesActions.fieldIntegerValueChanged, | ||
canvasAction: canvasWorkflowNodesActions.fieldIntegerValueChanged, | ||
}, | ||
{ | ||
nodesAction: nodesActions.fieldFloatValueChanged, | ||
canvasAction: canvasWorkflowNodesActions.fieldFloatValueChanged, | ||
}, | ||
{ | ||
nodesAction: nodesActions.fieldBooleanValueChanged, | ||
canvasAction: canvasWorkflowNodesActions.fieldBooleanValueChanged, | ||
}, | ||
{ | ||
nodesAction: nodesActions.fieldModelIdentifierValueChanged, | ||
canvasAction: canvasWorkflowNodesActions.fieldModelIdentifierValueChanged, | ||
}, | ||
{ | ||
nodesAction: nodesActions.fieldEnumModelValueChanged, | ||
canvasAction: canvasWorkflowNodesActions.fieldEnumModelValueChanged, | ||
}, | ||
{ | ||
nodesAction: nodesActions.fieldSchedulerValueChanged, | ||
canvasAction: canvasWorkflowNodesActions.fieldSchedulerValueChanged, | ||
}, | ||
{ | ||
nodesAction: nodesActions.fieldBoardValueChanged, | ||
canvasAction: canvasWorkflowNodesActions.fieldBoardValueChanged, | ||
}, | ||
{ | ||
nodesAction: nodesActions.fieldImageValueChanged, | ||
canvasAction: canvasWorkflowNodesActions.fieldImageValueChanged, | ||
}, | ||
{ | ||
nodesAction: nodesActions.fieldColorValueChanged, | ||
canvasAction: canvasWorkflowNodesActions.fieldColorValueChanged, | ||
}, | ||
{ | ||
nodesAction: nodesActions.fieldImageCollectionValueChanged, | ||
canvasAction: canvasWorkflowNodesActions.fieldImageCollectionValueChanged, | ||
}, | ||
{ | ||
nodesAction: nodesActions.fieldStringCollectionValueChanged, | ||
canvasAction: canvasWorkflowNodesActions.fieldStringCollectionValueChanged, | ||
}, | ||
{ | ||
nodesAction: nodesActions.fieldIntegerCollectionValueChanged, | ||
canvasAction: canvasWorkflowNodesActions.fieldIntegerCollectionValueChanged, | ||
}, | ||
{ | ||
nodesAction: nodesActions.fieldFloatCollectionValueChanged, | ||
canvasAction: canvasWorkflowNodesActions.fieldFloatCollectionValueChanged, | ||
}, | ||
{ | ||
nodesAction: nodesActions.fieldFloatGeneratorValueChanged, | ||
canvasAction: canvasWorkflowNodesActions.fieldFloatGeneratorValueChanged, | ||
}, | ||
{ | ||
nodesAction: nodesActions.fieldIntegerGeneratorValueChanged, | ||
canvasAction: canvasWorkflowNodesActions.fieldIntegerGeneratorValueChanged, | ||
}, | ||
{ | ||
nodesAction: nodesActions.fieldStringGeneratorValueChanged, | ||
canvasAction: canvasWorkflowNodesActions.fieldStringGeneratorValueChanged, | ||
}, | ||
{ | ||
nodesAction: nodesActions.fieldImageGeneratorValueChanged, | ||
canvasAction: canvasWorkflowNodesActions.fieldImageGeneratorValueChanged, | ||
}, | ||
{ nodesAction: nodesActions.fieldValueReset, canvasAction: canvasWorkflowNodesActions.fieldValueReset }, | ||
]; | ||
|
||
for (const { nodesAction, canvasAction } of fieldMutationActionPairs) { | ||
startListening({ | ||
actionCreator: nodesAction, | ||
effect: (action, { dispatch, getState }) => { | ||
const state = getState() as RootState; | ||
const { nodeId } = action.payload; | ||
|
||
// Check if this node exists in canvas workflow nodes | ||
const canvasWorkflowNode = state.canvasWorkflowNodes.nodes.find((n: AnyNode) => n.id === nodeId); | ||
|
||
// If the node exists in canvas workflow, redirect the action | ||
// This ensures canvas workflow fields always update the canvas workflow nodes slice | ||
if (canvasWorkflowNode) { | ||
dispatch(canvasAction(action.payload)); | ||
} | ||
}, | ||
}); | ||
} | ||
}; |
35 changes: 35 additions & 0 deletions
35
...end/web/src/app/store/middleware/listenerMiddleware/listeners/canvasWorkflowRehydrated.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { AppStartListening } from 'app/store/store'; | ||
import { selectCanvasWorkflow } from 'features/controlLayers/store/canvasWorkflowSlice'; | ||
import { REMEMBER_REHYDRATED } from 'redux-remember'; | ||
|
||
/** | ||
* When the app rehydrates from storage, we need to populate the canvasWorkflowNodes | ||
* shadow slice if a canvas workflow was previously selected. | ||
* | ||
* This ensures that exposed fields are visible when the page loads with a workflow already selected. | ||
*/ | ||
export const addCanvasWorkflowRehydratedListener = (startListening: AppStartListening) => { | ||
startListening({ | ||
type: REMEMBER_REHYDRATED, | ||
effect: (_action, { dispatch, getState }) => { | ||
const state = getState(); | ||
const { workflow, inputNodeId } = state.canvasWorkflow; | ||
|
||
// If there's a canvas workflow already selected, we need to load it into shadow nodes | ||
if (workflow && inputNodeId) { | ||
// Manually dispatch the fulfilled action to populate shadow nodes | ||
// We can't use the thunk because the workflow is already loaded | ||
dispatch({ | ||
type: selectCanvasWorkflow.fulfilled.type, | ||
payload: { | ||
workflow, | ||
inputNodeId, | ||
outputNodeId: state.canvasWorkflow.outputNodeId, | ||
workflowId: state.canvasWorkflow.selectedWorkflowId, | ||
fieldValues: state.canvasWorkflow.fieldValues, | ||
}, | ||
}); | ||
} | ||
}, | ||
}); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
invokeai/frontend/web/src/features/controlLayers/components/CanvasWorkflowElementContext.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useAppSelector } from 'app/store/storeHooks'; | ||
import { selectCanvasWorkflowNodesSlice } from 'features/controlLayers/store/canvasWorkflowNodesSlice'; | ||
import type { FormElement } from 'features/nodes/types/workflow'; | ||
import type { PropsWithChildren } from 'react'; | ||
import { createContext, memo, useContext, useMemo } from 'react'; | ||
|
||
/** | ||
* Context that provides element lookup from canvas workflow nodes instead of regular nodes. | ||
* This ensures that when viewing canvas workflow fields, we read from the shadow slice. | ||
*/ | ||
|
||
type CanvasWorkflowElementContextValue = { | ||
getElement: (id: string) => FormElement | undefined; | ||
}; | ||
|
||
const CanvasWorkflowElementContext = createContext<CanvasWorkflowElementContextValue | null>(null); | ||
|
||
const CanvasWorkflowElementProvider = memo(({ children }: PropsWithChildren) => { | ||
const nodesState = useAppSelector(selectCanvasWorkflowNodesSlice); | ||
|
||
const value = useMemo<CanvasWorkflowElementContextValue>( | ||
() => ({ | ||
getElement: (id: string) => nodesState.form.elements[id], | ||
}), | ||
[nodesState.form.elements] | ||
); | ||
|
||
return <CanvasWorkflowElementContext.Provider value={value}>{children}</CanvasWorkflowElementContext.Provider>; | ||
}); | ||
CanvasWorkflowElementProvider.displayName = 'CanvasWorkflowElementProvider'; | ||
|
||
/** | ||
* Hook to get an element, using canvas workflow context if available, | ||
* otherwise falls back to regular nodes. | ||
*/ | ||
export const useCanvasWorkflowElement = (): ((id: string) => FormElement | undefined) | null => { | ||
return useContext(CanvasWorkflowElementContext)?.getElement ?? null; | ||
}; |
42 changes: 42 additions & 0 deletions
42
invokeai/frontend/web/src/features/controlLayers/components/CanvasWorkflowFieldsPanel.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Flex, Text } from '@invoke-ai/ui-library'; | ||
import { useAppSelector } from 'app/store/storeHooks'; | ||
import { CanvasWorkflowModeProvider } from 'features/controlLayers/components/CanvasWorkflowModeContext'; | ||
import { CanvasWorkflowRootContainer } from 'features/controlLayers/components/CanvasWorkflowRootContainer'; | ||
import { selectCanvasWorkflowNodesSlice } from 'features/controlLayers/store/canvasWorkflowNodesSlice'; | ||
import { memo } from 'react'; | ||
|
||
/** | ||
* Renders the exposed fields for a canvas workflow. | ||
* | ||
* This component renders the workflow's form in view mode. | ||
* Each field element is wrapped with the appropriate InvocationNodeContext | ||
* in CanvasWorkflowFormElementComponent. | ||
*/ | ||
export const CanvasWorkflowFieldsPanel = memo(() => { | ||
const nodesState = useAppSelector(selectCanvasWorkflowNodesSlice); | ||
|
||
// Check if form is empty | ||
const rootElement = nodesState.form.elements[nodesState.form.rootElementId]; | ||
if ( | ||
!rootElement || | ||
!('data' in rootElement) || | ||
!rootElement.data || | ||
!('children' in rootElement.data) || | ||
rootElement.data.children.length === 0 | ||
) { | ||
return ( | ||
<Flex w="full" p={4} justifyContent="center"> | ||
<Text variant="subtext">No fields exposed in this workflow</Text> | ||
</Flex> | ||
); | ||
} | ||
|
||
return ( | ||
<CanvasWorkflowModeProvider> | ||
<Flex w="full" justifyContent="center" p={4}> | ||
<CanvasWorkflowRootContainer /> | ||
</Flex> | ||
</CanvasWorkflowModeProvider> | ||
); | ||
}); | ||
CanvasWorkflowFieldsPanel.displayName = 'CanvasWorkflowFieldsPanel'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't working quite right. The nodes slice always handles the actions even if this listener triggers and dispatches the canvas-specific action also.
For example, if I change the field in the workflow editor, it also changes it in the canvas workflow.
There's an "action router" pattern I've been exploring in #8553 that I think could solve this. The idea is to inject metadata into actions and use it to decide who handles the action. In that PR, the metadata is the canvas ID. For this PR, the metadata would be a flag that says "Is this action for the node editor workflow, or canvas workflow?". Roughed out:
Then in nodesSlice and canvasWorkflowSlice we can do this:
Wrap
useAppDispatch
to automatically inject the flags:This pattern is similar to what I'm proposing in #8553 . Plays nicely with it and a future where we have multiple workflow editor instances