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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
feat(ui): add canvas snapshot save/restore functionality (#8978)
Gohsuke Shimada0a09452fa3cf
diff --git a/invokeai/app/api/routers/client_state.py b/invokeai/app/api/routers/client_state.pyindex 2e34ea9fe6..cd92263f97 100644--- a/invokeai/app/api/routers/client_state.py+++ b/invokeai/app/api/routers/client_state.py@@ -45,6 +45,44 @@ async def set_client_state(raise HTTPException(status_code=500, detail="Error setting client state")+@client_state_router.get(+ "/{queue_id}/get_keys_by_prefix",+ operation_id="get_client_state_keys_by_prefix",+ response_model=list[str],+)+async def get_client_state_keys_by_prefix(+ current_user: CurrentUserOrDefault,+ queue_id: str = Path(description="The queue id (ignored, kept for backwards compatibility)"),+ prefix: str = Query(..., description="Prefix to filter keys by"),+) -> list[str]:+ """Gets client state keys matching a prefix for the current user"""+ try:+ return ApiDependencies.invoker.services.client_state_persistence.get_keys_by_prefix(+ current_user.user_id, prefix+ )+ except Exception as e:+ logging.error(f"Error getting client state keys: {e}")+ raise HTTPException(status_code=500, detail="Error getting client state keys")+++@client_state_router.post(+ "/{queue_id}/delete_by_key",+ operation_id="delete_client_state_by_key",+ responses={204: {"description": "Client state key deleted"}},+)+async def delete_client_state_by_key(+ current_user: CurrentUserOrDefault,+ queue_id: str = Path(description="The queue id (ignored, kept for backwards compatibility)"),+ key: str = Query(..., description="Key to delete"),+) -> None:+ """Deletes a specific client state key for the current user"""+ try:+ ApiDependencies.invoker.services.client_state_persistence.delete_by_key(current_user.user_id, key)+ except Exception as e:+ logging.error(f"Error deleting client state key: {e}")+ raise HTTPException(status_code=500, detail="Error deleting client state key")++@client_state_router.post("/{queue_id}/delete",operation_id="delete_client_state",diff --git a/invokeai/app/services/client_state_persistence/client_state_persistence_base.py b/invokeai/app/services/client_state_persistence/client_state_persistence_base.pyindex 99ad71bc8b..7be6841a79 100644--- a/invokeai/app/services/client_state_persistence/client_state_persistence_base.py+++ b/invokeai/app/services/client_state_persistence/client_state_persistence_base.py@@ -36,6 +36,31 @@ class ClientStatePersistenceABC(ABC):"""pass+ @abstractmethod+ def get_keys_by_prefix(self, user_id: str, prefix: str) -> list[str]:+ """+ Get all keys matching a prefix for a user.++ Args:+ user_id (str): The user ID to get keys for.+ prefix (str): The prefix to filter keys by.++ Returns:+ list[str]: A list of keys matching the prefix.+ """+ pass++ @abstractmethod+ def delete_by_key(self, user_id: str, key: str) -> None:+ """+ Delete a specific key-value pair for a user.++ Args:+ user_id (str): The user ID to delete state for.+ key (str): The key to delete.+ """+ pass+@abstractmethoddef delete(self, user_id: str) -> None:"""diff --git a/invokeai/app/services/client_state_persistence/client_state_persistence_sqlite.py b/invokeai/app/services/client_state_persistence/client_state_persistence_sqlite.pyindex 643db30685..7605de829d 100644--- a/invokeai/app/services/client_state_persistence/client_state_persistence_sqlite.py+++ b/invokeai/app/services/client_state_persistence/client_state_persistence_sqlite.py@@ -44,6 +44,31 @@ class ClientStatePersistenceSqlite(ClientStatePersistenceABC):return Nonereturn row[0]+ def get_keys_by_prefix(self, user_id: str, prefix: str) -> list[str]:+ # Escape LIKE wildcards (%, _) and the escape char itself so callers can pass+ # arbitrary strings as a literal prefix without accidental pattern matching.+ escaped_prefix = prefix.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")+ with self._db.transaction() as cursor:+ cursor.execute(+ """+ SELECT key FROM client_state+ WHERE user_id = ? AND key LIKE ? ESCAPE '\\'+ ORDER BY updated_at DESC+ """,+ (user_id, f"{escaped_prefix}%"),+ )+ return [row[0] for row in cursor.fetchall()]++ def delete_by_key(self, user_id: str, key: str) -> None:+ with self._db.transaction() as cursor:+ cursor.execute(+ """+ DELETE FROM client_state+ WHERE user_id = ? AND key = ?+ """,+ (user_id, key),+ )+def delete(self, user_id: str) -> None:with self._db.transaction() as cursor:cursor.execute(diff --git a/invokeai/frontend/web/public/locales/en.json b/invokeai/frontend/web/public/locales/en.jsonindex 65378016d4..cc52d12a0c 100644--- a/invokeai/frontend/web/public/locales/en.json+++ b/invokeai/frontend/web/public/locales/en.json@@ -3121,6 +3121,26 @@"switchOnStartDesc": "Switch on start","switchOnFinish": "On Finish","switchOnFinishDesc": "Switch on finish"+ },+ "snapshot": {+ "snapshots": "Save or Load Canvas Snapshot",+ "saveSnapshot": "Save Snapshot",+ "restoreSnapshot": "Restore Snapshot",+ "snapshotNamePlaceholder": "Snapshot name",+ "save": "Save",+ "delete": "Delete",+ "snapshotSaved": "Snapshot \"{{name}}\" saved",+ "snapshotRestored": "Snapshot \"{{name}}\" restored",+ "snapshotDeleted": "Snapshot \"{{name}}\" deleted",+ "snapshotSaveFailed": "Failed to save snapshot",+ "snapshotRestoreFailed": "Failed to restore snapshot",+ "snapshotDeleteFailed": "Failed to delete snapshot",+ "snapshotMissingImages_one": "{{count}} image referenced by this snapshot no longer exists and will appear as a placeholder",+ "snapshotMissingImages_other": "{{count}} images referenced by this snapshot no longer exist and will appear as placeholders",+ "snapshotIncompatible": "This snapshot was created with a different version and is no longer compatible",+ "overwriteSnapshotTitle": "Overwrite snapshot?",+ "overwriteSnapshotMessage": "A snapshot named \"{{name}}\" already exists. Overwrite it?",+ "overwrite": "Overwrite"}},"upscaling": {diff --git a/invokeai/frontend/web/src/features/controlLayers/components/Toolbar/CanvasToolbar.tsx b/invokeai/frontend/web/src/features/controlLayers/components/Toolbar/CanvasToolbar.tsxindex fc34f4331c..855659681d 100644--- a/invokeai/frontend/web/src/features/controlLayers/components/Toolbar/CanvasToolbar.tsx+++ b/invokeai/frontend/web/src/features/controlLayers/components/Toolbar/CanvasToolbar.tsx@@ -16,6 +16,7 @@ import { CanvasToolbarRedoButton } from 'features/controlLayers/components/Toolbimport { CanvasToolbarResetViewButton } from 'features/controlLayers/components/Toolbar/CanvasToolbarResetViewButton';import { CanvasToolbarSaveToGalleryButton } from 'features/controlLayers/components/Toolbar/CanvasToolbarSaveToGalleryButton';import { CanvasToolbarScale } from 'features/controlLayers/components/Toolbar/CanvasToolbarScale';+import { CanvasToolbarSnapshotMenuButton } from 'features/controlLayers/components/Toolbar/CanvasToolbarSnapshotMenuButton';import { CanvasToolbarUndoButton } from 'features/controlLayers/components/Toolbar/CanvasToolbarUndoButton';import { useCanvasDeleteLayerHotkey } from 'features/controlLayers/hooks/useCanvasDeleteLayerHotkey';import { useCanvasEntityQuickSwitchHotkey } from 'features/controlLayers/hooks/useCanvasEntityQuickSwitchHotkey';@@ -77,6 +78,7 @@ export const CanvasToolbar = memo(() => {<Flex alignItems="center" h="full"><CanvasToolbarProjectMenuButton /><CanvasToolbarSaveToGalleryButton />+ <CanvasToolbarSnapshotMenuButton /><CanvasToolbarUndoButton /><CanvasToolbarRedoButton /><CanvasToolbarNewSessionMenuButton />diff --git a/invokeai/frontend/web/src/features/controlLayers/components/Toolbar/CanvasToolbarSnapshotMenuButton.tsx b/invokeai/frontend/web/src/features/controlLayers/components/Toolbar/CanvasToolbarSnapshotMenuButton.tsxnew file mode 100644index 0000000000..6fe3b23a82--- /dev/null+++ b/invokeai/frontend/web/src/features/controlLayers/components/Toolbar/CanvasToolbarSnapshotMenuButton.tsx@@ -0,0 +1,238 @@+import {+ ConfirmationAlertDialog,+ Flex,+ IconButton,+ Input,+ Menu,+ MenuButton,+ MenuDivider,+ MenuGroup,+ MenuItem,+ MenuList,+ Text,+ useDisclosure,+} from '@invoke-ai/ui-library';+import type { SnapshotInfo } from 'features/controlLayers/hooks/useCanvasSnapshots';+import { useCanvasSnapshots } from 'features/controlLayers/hooks/useCanvasSnapshots';+import { useCanvasIsStaging } from 'features/controlLayers/store/canvasStagingAreaSlice';+import { toast } from 'features/toast/toast';+import type { ChangeEvent, KeyboardEvent, MouseEvent } from 'react';+import { memo, useCallback, useState } from 'react';+import { useTranslation } from 'react-i18next';+import { PiCameraBold, PiFloppyDiskBold, PiTrashBold } from 'react-icons/pi';++const SnapshotItem = memo(+ ({+ snapshot,+ onRestore,+ onDelete,+ isRestoreDisabled,+ }: {+ snapshot: SnapshotInfo;+ onRestore: (key: string, name: string) => void;+ onDelete: (e: MouseEvent, key: string, name: string) => void;+ isRestoreDisabled: boolean;+ }) => {+ const handleClick = useCallback(() => {+ onRestore(snapshot.key, snapshot.name);+ }, [onRestore, snapshot.key, snapshot.name]);++ const handleDelete = useCallback(+ (e: MouseEvent) => {+ onDelete(e, snapshot.key, snapshot.name);+ },+ [onDelete, snapshot.key, snapshot.name]+ );++ return (+ <MenuItem onClick={handleClick} isDisabled={isRestoreDisabled}>+ <Flex w="full" justifyContent="space-between" alignItems="center">+ <Text fontSize="sm" noOfLines={1}>+ {snapshot.name}+ </Text>+ <IconButton+ aria-label="Delete"+ icon={<PiTrashBold />}+ size="xs"+ variant="ghost"+ colorScheme="error"+ onClick={handleDelete}+ isDisabled={isRestoreDisabled}+ />+ </Flex>+ </MenuItem>+ );+ }+);++SnapshotItem.displayName = 'SnapshotItem';++const getDefaultSnapshotName = (): string => {+ const now = new Date();+ const y = now.getFullYear();+ const mo = String(now.getMonth() + 1).padStart(2, '0');+ const d = String(now.getDate()).padStart(2, '0');+ const h = String(now.getHours()).padStart(2, '0');+ const mi = String(now.getMinutes()).padStart(2, '0');+ return `${y}-${mo}-${d} ${h}-${mi}`;+};++export const CanvasToolbarSnapshotMenuButton = memo(() => {+ const { t } = useTranslation();+ const { snapshots, saveSnapshot, restoreSnapshot, deleteSnapshot } = useCanvasSnapshots();+ const isStaging = useCanvasIsStaging();+ const [snapshotName, setSnapshotName] = useState('');+ const overwriteDialog = useDisclosure();+ const [pendingOverwriteName, setPendingOverwriteName] = useState<string | null>(null);++ const onNameChange = useCallback((e: ChangeEvent<HTMLInputElement>) => {+ setSnapshotName(e.target.value);+ }, []);++ const doSave = useCallback(+ async (name: string) => {+ const success = await saveSnapshot(name);+ if (success) {+ toast({ title: t('controlLayers.snapshot.snapshotSaved', { name }), status: 'info' });+ setSnapshotName('');+ } else {+ toast({ title: t('controlLayers.snapshot.snapshotSaveFailed'), status: 'error' });+ }+ },+ [saveSnapshot, t]+ );++ const onSave = useCallback(async () => {+ const name = snapshotName.trim() || getDefaultSnapshotName();+ if (snapshots.some((s) => s.name === name)) {+ setPendingOverwriteName(name);+ overwriteDialog.onOpen();+ return;+ }+ await doSave(name);+ }, [snapshotName, snapshots, doSave, overwriteDialog]);++ const onConfirmOverwrite = useCallback(() => {+ if (pendingOverwriteName) {+ doSave(pendingOverwriteName);+ setPendingOverwriteName(null);+ }+ }, [pendingOverwriteName, doSave]);++ const onCloseOverwriteDialog = useCallback(() => {+ setPendingOverwriteName(null);+ overwriteDialog.onClose();+ }, [overwriteDialog]);++ const onKeyDown = useCallback(+ (e: KeyboardEvent<HTMLInputElement>) => {+ if (e.key === 'Enter') {+ e.preventDefault();+ e.stopPropagation();+ onSave();+ }+ },+ [onSave]+ );++ const onRestore = useCallback(+ async (key: string, name: string) => {+ const result = await restoreSnapshot(key);+ if (result.success) {+ if (result.missingImageCount && result.missingImageCount > 0) {+ toast({+ title: t('controlLayers.snapshot.snapshotRestored', { name }),+ description: t('controlLayers.snapshot.snapshotMissingImages', { count: result.missingImageCount }),+ status: 'warning',+ });+ } else {+ toast({ title: t('controlLayers.snapshot.snapshotRestored', { name }), status: 'info' });+ }+ } else if (result.error === 'incompatible') {+ toast({+ title: t('controlLayers.snapshot.snapshotRestoreFailed'),+ description: t('controlLayers.snapshot.snapshotIncompatible'),+ status: 'error',+ });+ } else {+ toast({ title: t('controlLayers.snapshot.snapshotRestoreFailed'), status: 'error' });+ }+ },+ [restoreSnapshot, t]+ );++ const onDelete = useCallback(+ async (e: MouseEvent, key: string, name: string) => {+ e.stopPropagation();+ const success = await deleteSnapshot(key);+ if (success) {+ toast({ title: t('controlLayers.snapshot.snapshotDeleted', { name }), status: 'info' });+ } else {+ toast({ title: t('controlLayers.snapshot.snapshotDeleteFailed'), status: 'error' });+ }+ },+ [deleteSnapshot, t]+ );++ return (+ <>+ <Menu placement="bottom-end" closeOnSelect={false}>+ <MenuButton+ as={IconButton}+ aria-label={t('controlLayers.snapshot.snapshots')}+ tooltip={t('controlLayers.snapshot.snapshots')}+ icon={<PiCameraBold />}+ variant="link"+ alignSelf="stretch"+ />+ <MenuList maxH="60vh" overflowY="auto">+ <MenuGroup title={t('controlLayers.snapshot.saveSnapshot')}>+ <Flex px={3} pb={2} gap={2} alignItems="center">+ <Input+ size="sm"+ placeholder={t('controlLayers.snapshot.snapshotNamePlaceholder')}+ value={snapshotName}+ onChange={onNameChange}+ onKeyDown={onKeyDown}+ />+ <IconButton+ aria-label={t('controlLayers.snapshot.save')}+ icon={<PiFloppyDiskBold />}+ size="sm"+ onClick={onSave}+ />+ </Flex>+ </MenuGroup>+ {snapshots.length > 0 && (+ <>+ <MenuDivider />+ <MenuGroup title={t('controlLayers.snapshot.restoreSnapshot')}>+ {snapshots.map((snapshot) => (+ <SnapshotItem+ key={snapshot.key}+ snapshot={snapshot}+ onRestore={onRestore}+ onDelete={onDelete}+ isRestoreDisabled={isStaging}+ />+ ))}+ </MenuGroup>+ </>+ )}+ </MenuList>+ </Menu>+ <ConfirmationAlertDialog+ isOpen={overwriteDialog.isOpen}+ onClose={onCloseOverwriteDialog}+ title={t('controlLayers.snapshot.overwriteSnapshotTitle')}+ acceptCallback={onConfirmOverwrite}+ acceptButtonText={t('controlLayers.snapshot.overwrite')}+ useInert={false}+ >+ <Text>{t('controlLayers.snapshot.overwriteSnapshotMessage', { name: pendingOverwriteName ?? '' })}</Text>+ </ConfirmationAlertDialog>+ </>+ );+});++CanvasToolbarSnapshotMenuButton.displayName = 'CanvasToolbarSnapshotMenuButton';diff --git a/invokeai/frontend/web/src/features/controlLayers/hooks/useCanvasSnapshots.ts b/invokeai/frontend/web/src/features/controlLayers/hooks/useCanvasSnapshots.tsnew file mode 100644index 0000000000..6352e23f4f--- /dev/null+++ b/invokeai/frontend/web/src/features/controlLayers/hooks/useCanvasSnapshots.ts@@ -0,0 +1,181 @@+import { logger } from 'app/logging/logger';+import { useAppDispatch, useAppStore } from 'app/store/storeHooks';+import { canvasSnapshotRestored } from 'features/controlLayers/store/canvasSlice';+import { selectCanvasSlice } from 'features/controlLayers/store/selectors';+import { zCanvasState } from 'features/controlLayers/store/types';+import { collectImageNames } from 'features/controlLayers/util/canvasProjectFile';+import { useCallback, useMemo } from 'react';+import { serializeError } from 'serialize-error';+import { appInfoApi } from 'services/api/endpoints/appInfo';+import {+ clientStateApi,+ useDeleteClientStateByKeyMutation,+ useGetClientStateKeysByPrefixQuery,+ useSetClientStateByKeyMutation,+} from 'services/api/endpoints/clientState';+import { getImageDTOSafe } from 'services/api/endpoints/images';+import type { JsonObject } from 'type-fest';+import { z } from 'zod';++const log = logger('canvas');++const SNAPSHOT_PREFIX = 'canvas_snapshot:';++/**+ * Quick health check to determine if the backend is reachable.+ * Uses the existing appInfoApi RTKQ endpoint for consistency.+ */+const isBackendReachable = async (dispatch: ReturnType<typeof useAppDispatch>): Promise<boolean> => {+ const req = dispatch(appInfoApi.endpoints.getAppVersion.initiate(undefined, { subscribe: false }));+ try {+ await req.unwrap();+ return true;+ } catch {+ return false;+ } finally {+ req.unsubscribe();+ }+};++/**+ * Check which image_names still exist on the server.+ * Returns the list of missing image names. If the backend is unreachable,+ * skips all checks and returns an empty array to avoid false warnings.+ */+const findMissingImages = async (+ imageNames: string[],+ dispatch: ReturnType<typeof useAppDispatch>+): Promise<string[]> => {+ // Pre-flight: verify backend is reachable before checking individual images+ if (!(await isBackendReachable(dispatch))) {+ log.warn('Backend unreachable — skipping missing image check');+ return [];+ }++ const results = await Promise.all(+ imageNames.map(async (name) => {+ const dto = await getImageDTOSafe(name);+ return dto === null ? name : null;+ })+ );+ return results.filter((name): name is string => name !== null);+};++export type SnapshotInfo = {+ key: string;+ name: string;+};++type RestoreResult = {+ success: boolean;+ missingImageCount?: number;+ error?: 'incompatible' | 'not_found' | 'unknown';+};++export const useCanvasSnapshots = () => {+ const dispatch = useAppDispatch();+ const store = useAppStore();++ const { data: keys } = useGetClientStateKeysByPrefixQuery(SNAPSHOT_PREFIX);+ const [setClientState] = useSetClientStateByKeyMutation();+ const [deleteClientState] = useDeleteClientStateByKeyMutation();++ const snapshots: SnapshotInfo[] = useMemo(+ () =>+ (keys ?? []).map((key) => ({+ key,+ name: key.slice(SNAPSHOT_PREFIX.length),+ })),+ [keys]+ );++ const saveSnapshot = useCallback(+ async (name: string) => {+ try {+ const state = selectCanvasSlice(store.getState());+ const value = JSON.stringify(state);+ const key = `${SNAPSHOT_PREFIX}${name}`;+ await setClientState({ key, value }).unwrap();+ return true;+ } catch (e) {+ log.error({ error: serializeError(e) } as JsonObject, 'Failed to save snapshot');+ return false;+ }+ },+ [store, setClientState]+ );++ const restoreSnapshot = useCallback(+ async (key: string): Promise<RestoreResult> => {+ const req = dispatch(clientStateApi.endpoints.getClientStateByKey.initiate(key, { subscribe: false }));+ try {+ const raw = await req.unwrap();+ if (raw === null) {+ throw new Error('Snapshot data not found');+ }+ const parsed = JSON.parse(raw);+ const canvasState = zCanvasState.parse(parsed);++ // Check for missing images before restoring+ // Reuse the shared collectImageNames from canvasProjectFile — snapshots only+ // contain canvas entities (no global ref images), so we pass an empty array.+ const imageNames = collectImageNames(+ {+ rasterLayers: canvasState.rasterLayers.entities,+ controlLayers: canvasState.controlLayers.entities,+ inpaintMasks: canvasState.inpaintMasks.entities,+ regionalGuidance: canvasState.regionalGuidance.entities,+ bbox: canvasState.bbox,+ selectedEntityIdentifier: canvasState.selectedEntityIdentifier,+ bookmarkedEntityIdentifier: canvasState.bookmarkedEntityIdentifier,+ },+ []+ );+ const imageNamesList = [...imageNames];+ const missingImages = imageNamesList.length > 0 ? await findMissingImages(imageNamesList, dispatch) : [];++ if (missingImages.length > 0) {+ log.warn(+ { missingCount: missingImages.length, total: imageNamesList.length } as unknown as JsonObject,+ 'Snapshot references images that no longer exist'+ );+ }++ dispatch(canvasSnapshotRestored(canvasState));+ return { success: true, missingImageCount: missingImages.length };+ } catch (e) {+ log.error({ error: serializeError(e) } as JsonObject, 'Failed to restore snapshot');+ // Distinguish Zod validation errors (incompatible snapshot) from other failures+ const isZodError = e instanceof z.ZodError;+ const isNotFound = e instanceof Error && e.message === 'Snapshot data not found';+ return {+ success: false,+ error: isZodError ? 'incompatible' : isNotFound ? 'not_found' : 'unknown',+ };+ } finally {+ req.unsubscribe();+ }+ },+ [dispatch]+ );++ const deleteSnapshot = useCallback(+ async (key: string) => {+ try {+ await deleteClientState(key).unwrap();+ return true;+ } catch (e) {+ log.error({ error: serializeError(e) } as JsonObject, 'Failed to delete snapshot');+ return false;+ }+ },+ [deleteClientState]+ );++ return {+ snapshots,+ saveSnapshot,+ restoreSnapshot,+ deleteSnapshot,+ };+};diff --git a/invokeai/frontend/web/src/features/controlLayers/store/canvasSlice.ts b/invokeai/frontend/web/src/features/controlLayers/store/canvasSlice.tsindex dad599374c..9e639c8e7a 100644--- a/invokeai/frontend/web/src/features/controlLayers/store/canvasSlice.ts+++ b/invokeai/frontend/web/src/features/controlLayers/store/canvasSlice.ts@@ -1783,11 +1783,32 @@ const slice = createSlice({state.controlLayers.entities = controlLayers;state.inpaintMasks.entities = inpaintMasks;state.regionalGuidance.entities = regionalGuidance;+ // Preserve the current modelBase to avoid desync with the currently selected model+ // (same pattern as canvasSnapshotRestored and resetState).+ const currentModelBase = state.bbox.modelBase;state.bbox = bbox;+ state.bbox.modelBase = currentModelBase;+ syncScaledSize(state);state.selectedEntityIdentifier = selectedEntityIdentifier;state.bookmarkedEntityIdentifier = bookmarkedEntityIdentifier;return state;},+ canvasSnapshotRestored: (state, action: PayloadAction<CanvasState>) => {+ const snapshot = action.payload;+ state.controlLayers = snapshot.controlLayers;+ state.inpaintMasks = snapshot.inpaintMasks;+ state.rasterLayers = snapshot.rasterLayers;+ state.regionalGuidance = snapshot.regionalGuidance;+ // Restore bbox from snapshot but preserve the current modelBase to avoid desync+ // with the currently selected model (same pattern as resetState).+ const currentModelBase = state.bbox.modelBase;+ state.bbox = snapshot.bbox;+ state.bbox.modelBase = currentModelBase;+ syncScaledSize(state);+ state.selectedEntityIdentifier = snapshot.selectedEntityIdentifier;+ state.bookmarkedEntityIdentifier = snapshot.bookmarkedEntityIdentifier;+ return state;+ },canvasUndo: () => {},canvasRedo: () => {},canvasClearHistory: () => {},@@ -1869,6 +1890,7 @@ const resetState = (state: CanvasState) => {export const {canvasMetadataRecalled,+ canvasSnapshotRestored,canvasProjectRecalled,canvasUndo,canvasRedo,@@ -1997,6 +2019,10 @@ const canvasUndoableConfig: UndoableOptions<CanvasState, UnknownAction> = {if (!action.type.startsWith(slice.name)) {return false;}+ // Snapshot restore and project load replace the canvas state and should not be undoable+ if (action.type === canvasSnapshotRestored.type || action.type === canvasProjectRecalled.type) {+ return false;+ }// Throttle rapid actions of the same typefilter = actionsThrottlingFilter(action);return filter;diff --git a/invokeai/frontend/web/src/services/api/endpoints/clientState.ts b/invokeai/frontend/web/src/services/api/endpoints/clientState.tsnew file mode 100644index 0000000000..5d3cc96d22--- /dev/null+++ b/invokeai/frontend/web/src/services/api/endpoints/clientState.ts@@ -0,0 +1,48 @@+import { api, buildV1Url, LIST_TAG } from '..';++/**+ * Builds an endpoint URL for the client_state router.+ * The queue_id path parameter is kept as 'default' for backwards compatibility.+ */+const buildClientStateUrl = (path: string, query?: Record<string, string>) =>+ buildV1Url(`client_state/default/${path}`, query);++export const clientStateApi = api.injectEndpoints({+ endpoints: (build) => ({+ getClientStateKeysByPrefix: build.query<string[], string>({+ query: (prefix) => ({+ url: buildClientStateUrl('get_keys_by_prefix', { prefix }),+ method: 'GET',+ }),+ providesTags: [{ type: 'ClientState', id: LIST_TAG }, 'FetchOnReconnect'],+ }),+ getClientStateByKey: build.query<string | null, string>({+ query: (key) => ({+ url: buildClientStateUrl('get_by_key', { key }),+ method: 'GET',+ }),+ }),+ setClientStateByKey: build.mutation<string, { key: string; value: string }>({+ query: ({ key, value }) => ({+ url: buildClientStateUrl('set_by_key', { key }),+ method: 'POST',+ // Send raw string body — the backend expects Body(...) as a plain string,+ // not JSON-encoded. Setting Content-Type to text/plain prevents fetchBaseQuery+ // from JSON-stringifying the body.+ headers: { 'Content-Type': 'text/plain' },+ body: value,+ }),+ invalidatesTags: [{ type: 'ClientState', id: LIST_TAG }],+ }),+ deleteClientStateByKey: build.mutation<void, string>({+ query: (key) => ({+ url: buildClientStateUrl('delete_by_key', { key }),+ method: 'POST',+ }),+ invalidatesTags: [{ type: 'ClientState', id: LIST_TAG }],+ }),+ }),+});++export const { useGetClientStateKeysByPrefixQuery, useSetClientStateByKeyMutation, useDeleteClientStateByKeyMutation } =+ clientStateApi;diff --git a/invokeai/frontend/web/src/services/api/schema.ts b/invokeai/frontend/web/src/services/api/schema.tsindex dde4ad0485..028a3ec4cf 100644--- a/invokeai/frontend/web/src/services/api/schema.ts+++ b/invokeai/frontend/web/src/services/api/schema.ts@@ -2580,6 +2580,46 @@ export type paths = {patch?: never;trace?: never;};+ "/api/v1/client_state/{queue_id}/get_keys_by_prefix": {+ parameters: {+ query?: never;+ header?: never;+ path?: never;+ cookie?: never;+ };+ /**+ * Get Client State Keys By Prefix+ * @description Gets client state keys matching a prefix for the current user+ */+ get: operations["get_client_state_keys_by_prefix"];+ put?: never;+ post?: never;+ delete?: never;+ options?: never;+ head?: never;+ patch?: never;+ trace?: never;+ };+ "/api/v1/client_state/{queue_id}/delete_by_key": {+ parameters: {+ query?: never;+ header?: never;+ path?: never;+ cookie?: never;+ };+ get?: never;+ put?: never;+ /**+ * Delete Client State By Key+ * @description Deletes a specific client state key for the current user+ */+ post: operations["delete_client_state_by_key"];+ delete?: never;+ options?: never;+ head?: never;+ patch?: never;+ trace?: never;+ };"/api/v1/client_state/{queue_id}/delete": {parameters: {query?: never;@@ -36641,6 +36681,83 @@ export interface operations {};};};+ get_client_state_keys_by_prefix: {+ parameters: {+ query: {+ /** @description Prefix to filter keys by */+ prefix: string;+ };+ header?: never;+ path: {+ /** @description The queue id (ignored, kept for backwards compatibility) */+ queue_id: string;+ };+ cookie?: never;+ };+ requestBody?: never;+ responses: {+ /** @description Successful Response */+ 200: {+ headers: {+ [name: string]: unknown;+ };+ content: {+ "application/json": string[];+ };+ };+ /** @description Validation Error */+ 422: {+ headers: {+ [name: string]: unknown;+ };+ content: {+ "application/json": components["schemas"]["HTTPValidationError"];+ };+ };+ };+ };+ delete_client_state_by_key: {+ parameters: {+ query: {+ /** @description Key to delete */+ key: string;+ };+ header?: never;+ path: {+ /** @description The queue id (ignored, kept for backwards compatibility) */+ queue_id: string;+ };+ cookie?: never;+ };+ requestBody?: never;+ responses: {+ /** @description Successful Response */+ 200: {+ headers: {+ [name: string]: unknown;+ };+ content: {+ "application/json": unknown;+ };+ };+ /** @description Client state key deleted */+ 204: {+ headers: {+ [name: string]: unknown;+ };+ content?: never;+ };+ /** @description Validation Error */+ 422: {+ headers: {+ [name: string]: unknown;+ };+ content: {+ "application/json": components["schemas"]["HTTPValidationError"];+ };+ };+ };+ };delete_client_state: {parameters: {query?: never;diff --git a/tests/app/routers/test_client_state_multiuser.py b/tests/app/routers/test_client_state_multiuser.pyindex 814c9182fe..4ca1de3bf4 100644--- a/tests/app/routers/test_client_state_multiuser.py+++ b/tests/app/routers/test_client_state_multiuser.py@@ -297,3 +297,148 @@ def test_complex_json_values(client: TestClient, admin_token: str):)assert get_response.status_code == status.HTTP_200_OKassert get_response.json() == complex_value+++def test_get_keys_by_prefix_without_auth(client: TestClient, monkeypatch, mock_invoker: Invoker):+ """Test that keys can be retrieved by prefix without authentication."""+ monkeypatch.setattr("invokeai.app.api.auth_dependencies.ApiDependencies", MockApiDependencies(mock_invoker))+ monkeypatch.setattr("invokeai.app.api.routers.client_state.ApiDependencies", MockApiDependencies(mock_invoker))++ # Set several keys with a common prefix directly+ for i in range(3):+ mock_invoker.services.client_state_persistence.set_by_key("system", f"canvas_snapshot:snap{i}", f"value{i}")+ mock_invoker.services.client_state_persistence.set_by_key("system", "other_key", "other_value")++ # Get keys by prefix+ response = client.get("/api/v1/client_state/default/get_keys_by_prefix?prefix=canvas_snapshot:")+ assert response.status_code == status.HTTP_200_OK+ keys = response.json()+ assert len(keys) == 3+ assert "canvas_snapshot:snap0" in keys+ assert "canvas_snapshot:snap1" in keys+ assert "canvas_snapshot:snap2" in keys+ assert "other_key" not in keys+++def test_get_keys_by_prefix_empty_without_auth(client: TestClient, monkeypatch, mock_invoker: Invoker):+ """Test that an empty list is returned when no keys match the prefix."""+ monkeypatch.setattr("invokeai.app.api.auth_dependencies.ApiDependencies", MockApiDependencies(mock_invoker))+ monkeypatch.setattr("invokeai.app.api.routers.client_state.ApiDependencies", MockApiDependencies(mock_invoker))++ response = client.get("/api/v1/client_state/default/get_keys_by_prefix?prefix=nonexistent_prefix:")+ assert response.status_code == status.HTTP_200_OK+ assert response.json() == []+++def test_delete_by_key_without_auth(client: TestClient, monkeypatch, mock_invoker: Invoker):+ """Test that a specific key can be deleted without affecting other keys."""+ monkeypatch.setattr("invokeai.app.api.auth_dependencies.ApiDependencies", MockApiDependencies(mock_invoker))+ monkeypatch.setattr("invokeai.app.api.routers.client_state.ApiDependencies", MockApiDependencies(mock_invoker))++ # Set two keys directly+ mock_invoker.services.client_state_persistence.set_by_key("system", "keep_key", "keep_value")+ mock_invoker.services.client_state_persistence.set_by_key("system", "delete_key", "delete_value")++ # Delete only one key via endpoint+ delete_response = client.post("/api/v1/client_state/default/delete_by_key?key=delete_key")+ assert delete_response.status_code == status.HTTP_200_OK++ # Verify deleted key is gone+ value = mock_invoker.services.client_state_persistence.get_by_key("system", "delete_key")+ assert value is None++ # Verify other key still exists+ value = mock_invoker.services.client_state_persistence.get_by_key("system", "keep_key")+ assert value == "keep_value"+++def test_get_keys_by_prefix(client: TestClient, admin_token: str):+ """Test that keys can be retrieved by prefix with authentication."""+ # Set several keys with a common prefix+ for i in range(3):+ client.post(+ f"/api/v1/client_state/default/set_by_key?key=canvas_snapshot:snap{i}",+ json=f"value{i}",+ headers={"Authorization": f"Bearer {admin_token}"},+ )+ # Set a key without the prefix+ client.post(+ "/api/v1/client_state/default/set_by_key?key=other_key",+ json="other_value",+ headers={"Authorization": f"Bearer {admin_token}"},+ )++ # Get keys by prefix+ response = client.get(+ "/api/v1/client_state/default/get_keys_by_prefix?prefix=canvas_snapshot:",+ headers={"Authorization": f"Bearer {admin_token}"},+ )+ assert response.status_code == status.HTTP_200_OK+ keys = response.json()+ assert len(keys) == 3+ assert "canvas_snapshot:snap0" in keys+ assert "canvas_snapshot:snap1" in keys+ assert "canvas_snapshot:snap2" in keys+ assert "other_key" not in keys+++def test_delete_by_key(client: TestClient, admin_token: str):+ """Test that a specific key can be deleted without affecting other keys."""+ # Set two keys+ client.post(+ "/api/v1/client_state/default/set_by_key?key=keep_key",+ json="keep_value",+ headers={"Authorization": f"Bearer {admin_token}"},+ )+ client.post(+ "/api/v1/client_state/default/set_by_key?key=delete_key",+ json="delete_value",+ headers={"Authorization": f"Bearer {admin_token}"},+ )++ # Delete only one key+ delete_response = client.post(+ "/api/v1/client_state/default/delete_by_key?key=delete_key",+ headers={"Authorization": f"Bearer {admin_token}"},+ )+ assert delete_response.status_code == status.HTTP_200_OK++ # Verify deleted key is gone+ get_response = client.get(+ "/api/v1/client_state/default/get_by_key?key=delete_key",+ headers={"Authorization": f"Bearer {admin_token}"},+ )+ assert get_response.json() is None++ # Verify other key still exists+ get_response = client.get(+ "/api/v1/client_state/default/get_by_key?key=keep_key",+ headers={"Authorization": f"Bearer {admin_token}"},+ )+ assert get_response.json() == "keep_value"+++def test_get_keys_by_prefix_isolation_between_users(client: TestClient, user1_token: str, user2_token: str):+ """Test that get_keys_by_prefix is isolated between users."""+ # User 1 sets keys+ client.post(+ "/api/v1/client_state/default/set_by_key?key=snapshot:u1",+ json="user1_data",+ headers={"Authorization": f"Bearer {user1_token}"},+ )++ # User 2 sets keys+ client.post(+ "/api/v1/client_state/default/set_by_key?key=snapshot:u2",+ json="user2_data",+ headers={"Authorization": f"Bearer {user2_token}"},+ )++ # User 1 should only see their own keys+ response = client.get(+ "/api/v1/client_state/default/get_keys_by_prefix?prefix=snapshot:",+ headers={"Authorization": f"Bearer {user1_token}"},+ )+ keys = response.json()+ assert "snapshot:u1" in keys+ assert "snapshot:u2" not in keys