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
fix(webv2): store a template snapshot, not a catalog record
joshistoastb30718f25644
diff --git a/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.test.ts b/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.test.tsindex f1e8889034..3cd6a2b36c 100644--- a/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.test.ts+++ b/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.test.ts@@ -185,7 +185,25 @@ describe('syncPromptTemplateWithCatalog', () => {const stored = template({ positivePrompt: '{prompt}, cinematic' });const edited = template({ name: 'Cinematic v2', positivePrompt: '{prompt}, cinematic, 35mm' });- expect(syncPromptTemplateWithCatalog(stored, [edited])).toBe(edited);+ expect(syncPromptTemplateWithCatalog(stored, [edited])).toEqual(edited);+ });++ // The catalog holds records, which carry `isDefault` and a host-specific+ // `imageUrl`. Adopting one wholesale persisted both into project state and+ // into every queue item, and put five keys on something the canonical check+ // requires to have four.+ it('takes only the snapshot fields from the catalog entry', () => {+ const stored = template({ positivePrompt: '{prompt}, cinematic' });+ const record = {+ ...template({ positivePrompt: '{prompt}, cinematic, 35mm' }),+ imageUrl: 'http://some-host:9090/api/v1/style_presets/t1/image',+ isDefault: false,+ };++ expect(syncPromptTemplateWithCatalog(stored, [record])).toEqual({+ ...stored,+ positivePrompt: '{prompt}, cinematic, 35mm',+ });});// The callers diff with `Object.is`, so an equal-but-new object each passdiff --git a/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.ts b/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.tsindex dee64383ba..63e4540c4e 100644--- a/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.ts+++ b/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.ts@@ -163,5 +163,17 @@ export const syncPromptTemplateWithCatalog = (const current = catalog.find((template) => template.id === promptTemplate.id);- return current && !areSnapshotsEqual(current, promptTemplate) ? current : promptTemplate;+ if (!current || areSnapshotsEqual(current, promptTemplate)) {+ return promptTemplate;+ }++ // Copied down to the four fields rather than adopted whole: the catalog holds+ // records, which carry a couple of extra keys that have no business being+ // persisted into project state. See `toPromptTemplateSnapshot`.+ return {+ id: current.id,+ name: current.name,+ negativePrompt: current.negativePrompt,+ positivePrompt: current.positivePrompt,+ };};diff --git a/invokeai/frontend/webv2/src/features/generation/data/promptTemplates.ts b/invokeai/frontend/webv2/src/features/generation/data/promptTemplates.tsindex 74aa26ebdb..437aa99cf9 100644--- a/invokeai/frontend/webv2/src/features/generation/data/promptTemplates.ts+++ b/invokeai/frontend/webv2/src/features/generation/data/promptTemplates.ts@@ -38,6 +38,24 @@ export interface PromptTemplateRecord extends PromptTemplateSnapshot {imageUrl: string | null;}+/**+ * The four fields a record shares with a snapshot, and no more.+ *+ * A record is assignable to a snapshot, so applying one type-checks and keeps+ * `isDefault` and `imageUrl` along for the ride. Both then land in persisted+ * project state and in every queue item's widget snapshot — an absolute,+ * host-specific image URL among them, stale the moment the server moves. It+ * also puts five keys on an object `isCanonicalPromptTemplateSnapshot` requires+ * to have exactly four, so the identity short-circuits built around it in+ * `normalizeGenerateSettings` and `isGenerateSettings` never fired.+ */+export const toPromptTemplateSnapshot = ({+ id,+ name,+ negativePrompt,+ positivePrompt,+}: PromptTemplateSnapshot): PromptTemplateSnapshot => ({ id, name, negativePrompt, positivePrompt });+export interface PromptTemplateDraft {name: string;negativePrompt: string;diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplates.browser.test.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplates.browser.test.tsxindex 7f56cdfdb2..dec71459f5 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplates.browser.test.tsx+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplates.browser.test.tsx@@ -173,7 +173,14 @@ describe('the prompt templates panel', () => {await userEvent.click(buttonWithText('Cinematic'));});- expect(onApply).toHaveBeenCalledWith(userTemplate);+ // The four snapshot fields only. `isDefault` and the absolute `imageUrl`+ // belong to the catalog, not to persisted project state.+ expect(onApply).toHaveBeenCalledWith({+ id: userTemplate.id,+ name: userTemplate.name,+ negativePrompt: userTemplate.negativePrompt,+ positivePrompt: userTemplate.positivePrompt,+ });});it('clears the applied template', async () => {diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplatesButton.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplatesButton.tsxindex cefe963356..51c35663e3 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplatesButton.tsx+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplatesButton.tsx@@ -3,6 +3,7 @@ import type { PromptTemplateRecord } from '@features/generation/data/promptTemplimport type { PendingPromptTemplateDraft } from '@features/generation/ui/promptTemplateDraftStore';import { Popover, Portal, Text } from '@chakra-ui/react';+import { toPromptTemplateSnapshot } from '@features/generation/data/promptTemplates';import { PromptTemplateEditor } from '@features/generation/ui/promptFields/PromptTemplateEditor';import { PromptTemplatesPanel } from '@features/generation/ui/promptFields/PromptTemplatesPanel';import { useOnPendingPromptTemplateDraft } from '@features/generation/ui/promptTemplateDraftStore';@@ -75,7 +76,7 @@ export const PromptTemplatesButton = ({const handleSaved = useCallback((saved: PromptTemplateRecord) => {if (activeTemplate?.id === saved.id) {- onApply(saved);+ onApply(toPromptTemplateSnapshot(saved));}setEditorTarget(null);diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplatesPanel.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplatesPanel.tsxindex 146194a734..2bcccac91b 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplatesPanel.tsx+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplatesPanel.tsx@@ -5,6 +5,7 @@ import type { ChangeEvent } from 'react';import { Box, HStack, Image, Input, Separator, Stack, Text } from '@chakra-ui/react';import { PROMPT_TEMPLATE_PLACEHOLDER } from '@features/generation/core/promptTemplates';+import { toPromptTemplateSnapshot } from '@features/generation/data/promptTemplates';import { useGenerationUi } from '@features/generation/ui/GenerationUiContext';import { PANEL_HEADER_CONTROL_HEIGHT, PromptPanelHeader } from '@features/generation/ui/promptFields/PromptPanelHeader';import { getApiErrorMessage } from '@platform/transport/http';@@ -320,7 +321,7 @@ const TemplateRow = ({onEdit?: (template: PromptTemplateRecord) => void;}) => {const { t } = useTranslation();- const handleApply = useCallback(() => onApply(template), [onApply, template]);+ const handleApply = useCallback(() => onApply(toPromptTemplateSnapshot(template)), [onApply, template]);const handleEdit = useCallback(() => onEdit?.(template), [onEdit, template]);const handleDelete = useCallback(() => onDelete?.(template), [onDelete, template]);// The prompt with its placeholder is the whole content of a template, so the