Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

feat(webv2): save an image's prompt as a template

joshistoast767187e3a808
diff --git a/invokeai/frontend/webv2/src/features/generation/react.ts b/invokeai/frontend/webv2/src/features/generation/react.ts
index e8130bdcf9..971a94dc78 100644
--- a/invokeai/frontend/webv2/src/features/generation/react.ts
+++ b/invokeai/frontend/webv2/src/features/generation/react.ts
@@ -12,3 +12,4 @@ export { notifyGenerateModelSelectionCleared } from './ui/modelSelectionNotice';
export { adjustFocusedPromptAttention } from './ui/promptFields/promptAttentionHotkeys';
export { focusPositivePrompt } from './ui/promptFields/promptFocus';
export { promptHistoryNavigation } from './ui/promptFields/promptHistoryNavigation';
+export { setPendingPromptTemplateDraft } from './ui/promptTemplateDraftStore';
diff --git a/invokeai/frontend/webv2/src/workbench/image-actions/ImageContextMenu.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/image-actions/ImageContextMenu.browser.test.tsx
index 95761466fc..f8822728ae 100644
--- a/invokeai/frontend/webv2/src/workbench/image-actions/ImageContextMenu.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/workbench/image-actions/ImageContextMenu.browser.test.tsx
@@ -54,6 +54,7 @@ const createActions = (deleteImages: ImageActions['deleteImages']): ImageActions
selectForCompare: vi.fn(),
sendToCanvas: vi.fn(() => Promise.resolve()),
setImagesStarred: vi.fn(() => Promise.resolve()),
+ savePromptAsTemplate: vi.fn(),
useAsReferenceImage: vi.fn(),
});
diff --git a/invokeai/frontend/webv2/src/workbench/image-actions/ImageContextMenu.tsx b/invokeai/frontend/webv2/src/workbench/image-actions/ImageContextMenu.tsx
index 6e025fee5c..b1067b2bb2 100644
--- a/invokeai/frontend/webv2/src/workbench/image-actions/ImageContextMenu.tsx
+++ b/invokeai/frontend/webv2/src/workbench/image-actions/ImageContextMenu.tsx
@@ -313,6 +313,7 @@ const SingleImageMenuItems = ({
const handleRecallDimensions = useRecallImageDataHandler(actions, image, 'dimensions');
const handleRecallClipSkip = useRecallImageDataHandler(actions, image, 'clipSkip');
const handleSelectForCompare = useSelectForCompareHandler(actions, image);
+ const handleSavePromptAsTemplate = useCallback(() => void actions.savePromptAsTemplate(image), [actions, image]);
const handleUseAsReferenceImage = useUseAsReferenceImageHandler(actions, image);
const { generation, widgets } = useWorkbenchCommands();
const openWidget = useOpenWorkbenchWidget();
@@ -388,7 +389,12 @@ const SingleImageMenuItems = ({
value="use-as-reference-image"
onClick={handleUseAsReferenceImage}
/>
- <ContextMenuItem disabled icon={TypeIcon} label="Use as Prompt Template" value="use-as-prompt-template" />
+ <ContextMenuItem
+ icon={TypeIcon}
+ label="Use as Prompt Template"
+ value="use-as-prompt-template"
+ onClick={handleSavePromptAsTemplate}
+ />
<ContextMenuItem
icon={ImagesIcon}
label="Select for Compare"
diff --git a/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.ts b/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.ts
index 9f4c8e5be6..452e648112 100644
--- a/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.ts
+++ b/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.ts
@@ -8,6 +8,7 @@ import {
type GalleryImage,
} from '@features/gallery';
import { invalidateGallery, invalidateGalleryImages, patchGalleryImageCaches } from '@features/gallery/queries';
+import { setPendingPromptTemplateDraft } from '@features/generation/react';
import { getMaxReferenceImages, isVaeModelConfig, isSupportedGenerateModel } from '@features/generation/settings';
import { ensureModelsLoaded, useModelsSelector } from '@features/models';
import { useMountEffect } from '@platform/react/useMountEffect';
@@ -33,6 +34,7 @@ import { appendReferenceImage } from './appendReferenceImage';
import { recordCanvasImportError } from './canvasImportError';
import { executeImageRecall, getCurrentGenerateValues } from './executeImageRecall';
import {
+ getMetadataPrompts,
EMPTY_IMAGE_RECALL_CAPABILITIES,
getImageRecallCapabilities,
type ImageRecallCapabilities,
@@ -55,6 +57,8 @@ export interface ImageActions {
moveImagesToBoard: (imageNames: string[], boardId: string) => Promise<void>;
openImageInPreview: (image: GalleryImage) => void;
recallImageData: (image: GalleryImage, kind: ImageRecallKind) => Promise<void>;
+ /** Opens the generate widget's template editor prefilled from this image's prompts. */
+ savePromptAsTemplate: (image: GalleryImage) => Promise<void>;
selectForCompare: (image: GalleryImage) => void;
sendToCanvas: (images: readonly GalleryImage[], destination: GalleryCanvasImportDestination) => Promise<void>;
setImagesStarred: (imageNames: string[], starred: boolean) => Promise<void>;
@@ -257,6 +261,33 @@ export const useImageActions = ({
};
}
},
+ savePromptAsTemplate: async (image) => {
+ const owner = captureAccountScope();
+
+ try {
+ const metadata = await galleryImages.metadata(image.imageName, owner.signal);
+
+ assertAccountScopeCurrent(owner);
+
+ const { negativePrompt, positivePrompt } = getMetadataPrompts(metadata);
+
+ if (!positivePrompt && !negativePrompt) {
+ notifications.add({ kind: 'info', title: 'This image has no prompt to save' });
+ return;
+ }
+
+ // The widget hosts the editor, so it has to be on screen for the
+ // handoff to be visible.
+ openWorkbenchWidget('generate', { preferredRegions: ['left'] });
+ setPendingPromptTemplateDraft({ negativePrompt, positivePrompt });
+ } catch (error: unknown) {
+ if (!isAccountScopeCurrent(owner)) {
+ return;
+ }
+
+ recordError(error);
+ }
+ },
moveImagesToBoard: async (imageNames, boardId) => {
const owner = captureAccountScope();