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.

refactor(webv2): state a snapshot's fields once

joshistoast5e542545a85a
diff --git a/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.test.ts b/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.test.ts
index 3cd6a2b36c..42173a5ffe 100644
--- a/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.test.ts
+++ b/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.test.ts
@@ -4,6 +4,7 @@ import {
applyPromptTemplate,
getEffectivePrompts,
getPromptTemplateChunks,
+ isCanonicalPromptTemplateSnapshot,
type PromptTemplateSnapshot,
sanitizePromptTemplateSnapshot,
syncPromptTemplateWithCatalog,
@@ -227,3 +228,36 @@ describe('syncPromptTemplateWithCatalog', () => {
expect(syncPromptTemplateWithCatalog(null, [template()])).toBeNull();
});
});
+
+// The whole point of the check is to let normalization hand a stored object
+// straight back. It gates `isGenerateSettings`, so a snapshot it wrongly
+// rejects re-normalises every settings read for as long as one is applied.
+describe('isCanonicalPromptTemplateSnapshot', () => {
+ it('accepts exactly a snapshot', () => {
+ expect(isCanonicalPromptTemplateSnapshot(template())).toBe(true);
+ expect(isCanonicalPromptTemplateSnapshot(template({ name: '', negativePrompt: 'blurry' }))).toBe(true);
+ });
+
+ // A catalog record is a snapshot plus `isDefault` and a host-specific
+ // `imageUrl`; storing one of those was how the fast path came to be dead.
+ it('rejects an object carrying anything more', () => {
+ expect(isCanonicalPromptTemplateSnapshot({ ...template(), imageUrl: null, isDefault: false })).toBe(false);
+ });
+
+ it('rejects a missing or mistyped field', () => {
+ const { negativePrompt: _dropped, ...missing } = template();
+
+ expect(isCanonicalPromptTemplateSnapshot(missing)).toBe(false);
+ expect(isCanonicalPromptTemplateSnapshot({ ...template(), positivePrompt: 42 })).toBe(false);
+ });
+
+ it('rejects an empty id, which resolves against no catalog entry', () => {
+ expect(isCanonicalPromptTemplateSnapshot(template({ id: '' }))).toBe(false);
+ });
+
+ it('rejects what is not a record at all', () => {
+ expect(isCanonicalPromptTemplateSnapshot(null)).toBe(false);
+ expect(isCanonicalPromptTemplateSnapshot([])).toBe(false);
+ expect(isCanonicalPromptTemplateSnapshot('template-1')).toBe(false);
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.ts b/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.ts
index 63e4540c4e..b004939f66 100644
--- a/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.ts
+++ b/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.ts
@@ -102,6 +102,26 @@ export const getPromptTemplateChunks = (prompt: string, templatePrompt: string):
const isRecord = (value: unknown): value is Record<string, unknown> =>
typeof value === 'object' && value !== null && !Array.isArray(value);
+/**
+ * A snapshot's fields, stated once.
+ *
+ * `satisfies` is doing real work here: a field added to the interface and not to
+ * this list fails to compile, and so does a name here that the interface does
+ * not have. The check below used to carry a hardcoded `length === 4` alongside
+ * four `typeof` tests — two spellings of one fact, and a fifth field would have
+ * quietly made every valid snapshot non-canonical rather than failing the build.
+ *
+ * They are all strings; when one is not, this becomes a map of predicates.
+ */
+const SNAPSHOT_FIELDS = {
+ id: true,
+ name: true,
+ negativePrompt: true,
+ positivePrompt: true,
+} satisfies Record<keyof PromptTemplateSnapshot, true>;
+
+const SNAPSHOT_FIELD_NAMES = Object.keys(SNAPSHOT_FIELDS);
+
/**
* True when a stored value is already exactly a snapshot, so normalization can
* hand back the same object instead of allocating an equal one. `null` is not
@@ -109,12 +129,9 @@ const isRecord = (value: unknown): value is Record<string, unknown> =>
*/
export const isCanonicalPromptTemplateSnapshot = (value: unknown): value is PromptTemplateSnapshot =>
isRecord(value) &&
- typeof value.id === 'string' &&
- value.id.length > 0 &&
- typeof value.name === 'string' &&
- typeof value.negativePrompt === 'string' &&
- typeof value.positivePrompt === 'string' &&
- Object.keys(value).length === 4;
+ Object.keys(value).length === SNAPSHOT_FIELD_NAMES.length &&
+ SNAPSHOT_FIELD_NAMES.every((field) => typeof value[field] === 'string') &&
+ value.id !== '';
/** Reads an untrusted persisted/transported snapshot, or `null` when unusable. */
export const sanitizePromptTemplateSnapshot = (value: unknown): PromptTemplateSnapshot | null => {