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): make a wildcard round-trip an identity
joshistoastd1ca9072ed59
diff --git a/invokeai/frontend/webv2/public/locales/en.json b/invokeai/frontend/webv2/public/locales/en.jsonindex ee91e2e6ac..3fe5094545 100644--- a/invokeai/frontend/webv2/public/locales/en.json+++ b/invokeai/frontend/webv2/public/locales/en.json@@ -1006,6 +1006,8 @@"importRejectedValueTooLong": "A value is too long","importedCount_one": "Imported {{count}} wildcard","importedCount_other": "Imported {{count}} wildcards",+ "wildcardTooManyValues": "Too many values (limit 10,000)",+ "wildcardValueTooLong": "One value is too long (limit 2,000 characters)","importFiles": "From files…","importFolder": "From a folder…","importNoWildcardFiles": "That folder has no wildcard files in it",diff --git a/invokeai/frontend/webv2/src/features/generation/core/dynamicPrompts.test.ts b/invokeai/frontend/webv2/src/features/generation/core/dynamicPrompts.test.tsindex 24c1bc7994..499d7f70d5 100644--- a/invokeai/frontend/webv2/src/features/generation/core/dynamicPrompts.test.ts+++ b/invokeai/frontend/webv2/src/features/generation/core/dynamicPrompts.test.ts@@ -2,8 +2,10 @@ import { describe, expect, it } from 'vitest';import {getWildcardNameError,+ getWildcardValuesError,hasDynamicPromptSyntax,matchesKnownWildcard,+ normalizeWildcardValues,sanitizeDynamicPromptsConfig,sanitizeMaxPrompts,} from './dynamicPrompts';@@ -144,3 +146,32 @@ describe('matchesKnownWildcard', () => {expect(performance.now() - started).toBeLessThan(100);});});++describe('normalizeWildcardValues', () => {+ // What the editor stores has to be what an export writes and a re-import+ // reads back. Blank lines used to survive into the catalog and then vanish on+ // the next round trip, so the two disagreed.+ it('drops blank lines and trims the rest', () => {+ expect(normalizeWildcardValues(['red', '', ' green ', ' ', 'blue'])).toEqual(['red', 'green', 'blue']);+ });++ it('leaves an already-clean list alone', () => {+ expect(normalizeWildcardValues(['red', 'green'])).toEqual(['red', 'green']);+ });+});++describe('getWildcardValuesError', () => {+ it('accepts an ordinary list', () => {+ expect(getWildcardValuesError(['red', 'green'])).toBeNull();+ });++ it('tells a long list apart from a long value', () => {+ expect(getWildcardValuesError(Array.from({ length: 10_001 }, () => 'v'))).toBe('tooManyValues');+ expect(getWildcardValuesError(['ok', 'x'.repeat(2_001)])).toBe('valueTooLong');+ });++ it('accepts the limits exactly', () => {+ expect(getWildcardValuesError(Array.from({ length: 10_000 }, () => 'v'))).toBeNull();+ expect(getWildcardValuesError(['x'.repeat(2_000)])).toBeNull();+ });+});diff --git a/invokeai/frontend/webv2/src/features/generation/core/dynamicPrompts.ts b/invokeai/frontend/webv2/src/features/generation/core/dynamicPrompts.tsindex 553faee0c9..679eff366d 100644--- a/invokeai/frontend/webv2/src/features/generation/core/dynamicPrompts.ts+++ b/invokeai/frontend/webv2/src/features/generation/core/dynamicPrompts.ts@@ -145,6 +145,40 @@ export const matchesKnownWildcard = (path: string, knownNames: ReadonlySet<strin/** Mirrors the backend's `MAX_WILDCARD_NAME_LENGTH`. */export const MAX_WILDCARD_NAME_LENGTH = 128;+/**+ * The backend's bounds on a values list, mirrored here so both ways of writing+ * one can say what is wrong before sending it.+ *+ * These lived next to the import planner, which meant an import pre-flighted+ * them politely while the editor discovered them as a raw error from the server.+ */+export const MAX_WILDCARD_VALUES = 10_000;+export const MAX_WILDCARD_VALUE_LENGTH = 2_000;++/** Why a values list would be rejected, or `null` if it would be accepted. */+export const getWildcardValuesError = (values: readonly string[]): 'tooManyValues' | 'valueTooLong' | null => {+ if (values.length > MAX_WILDCARD_VALUES) {+ return 'tooManyValues';+ }+ // Kept apart from the count: told "too many values", someone holding three+ // values and one long paragraph has no idea which one to shorten.+ if (values.some((value) => value.length > MAX_WILDCARD_VALUE_LENGTH)) {+ return 'valueTooLong';+ }++ return null;+};++/**+ * The values as they will be stored: trimmed, with the blanks dropped.+ *+ * Applied wherever a list is authored, so that what is saved is what an export+ * writes and a re-import reads back. Without it a stray blank line survived into+ * the catalog and vanished on the next round trip.+ */+export const normalizeWildcardValues = (values: readonly string[]): string[] =>+ values.map((value) => value.trim()).filter((value) => value.length > 0);+/*** Why a wildcard name would be rejected, or `null` if it would be accepted.*diff --git a/invokeai/frontend/webv2/src/features/generation/core/wildcardTransfer.ts b/invokeai/frontend/webv2/src/features/generation/core/wildcardTransfer.tsindex 60135146c2..ba5f1a18d1 100644--- a/invokeai/frontend/webv2/src/features/generation/core/wildcardTransfer.ts+++ b/invokeai/frontend/webv2/src/features/generation/core/wildcardTransfer.ts@@ -20,7 +20,7 @@* written rather than riding along in the generate widget's chunk.*/-import { getWildcardNameError, MAX_WILDCARD_NAME_LENGTH } from './dynamicPrompts';+import { getWildcardNameError, getWildcardValuesError, MAX_WILDCARD_NAME_LENGTH } from './dynamicPrompts';export interface ParsedWildcard {name: string;@@ -28,8 +28,6 @@ export interface ParsedWildcard {}/** Mirrors the backend's per-wildcard limits, so a doomed request is never sent. */-const MAX_WILDCARD_VALUES = 10_000;-const MAX_WILDCARD_VALUE_LENGTH = 2_000;/*** `animals/dogs.txt` → `animals/dogs`. Directory picking hands back a relative@@ -211,22 +209,13 @@ export const planWildcardImport = (if (nameError !== null) {return 'invalid';}- if (seen.has(wildcard.name)) {+ if (seen.has(wildcard.name.trim())) {return 'duplicate';}if (wildcard.values.length === 0) {return 'noValues';}- if (wildcard.values.length > MAX_WILDCARD_VALUES) {- return 'tooManyValues';- }- // Kept apart from the count: told "too many values", someone holding three- // values and one long paragraph has no idea which one to shorten.- if (wildcard.values.some((value) => value.length > MAX_WILDCARD_VALUE_LENGTH)) {- return 'valueTooLong';- }-- return null;+ return getWildcardValuesError(wildcard.values);};return parsed.map((wildcard) => {@@ -237,12 +226,15 @@ export const planWildcardImport = (// made a populated `colours.yaml` later in it read as a repeat, and neither// was imported.if (rejection === null) {- seen.add(wildcard.name);+ seen.add(wildcard.name.trim());}return {- conflictId: existingByName.get(wildcard.name) ?? null,- name: wildcard.name,+ conflictId: existingByName.get(wildcard.name.trim()) ?? null,+ // The trimmed form throughout: validation trims before deciding, so+ // storing the untrimmed one would send the server something the client+ // never checked, and look it up under a name it was not filed under.+ name: wildcard.name.trim(),rejection,values: wildcard.values,};diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/WildcardsPanel.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/WildcardsPanel.tsxindex 6ccf9e530c..cf0ffb5f98 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/WildcardsPanel.tsx+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/WildcardsPanel.tsx@@ -4,7 +4,11 @@ import type { WildcardCatalog } from '@features/generation/ui/useWildcards';import type { ChangeEvent } from 'react';import { HStack, Input, Separator, Stack, Text } from '@chakra-ui/react';-import { getWildcardNameError } from '@features/generation/core/dynamicPrompts';+import {+ getWildcardNameError,+ getWildcardValuesError,+ normalizeWildcardValues,+} from '@features/generation/core/dynamicPrompts';import { filterWildcards, groupWildcardsByPrefix } from '@features/generation/core/wildcardCatalog';import { useGenerationUi } from '@features/generation/ui/GenerationUiContext';import { PANEL_HEADER_CONTROL_HEIGHT, PromptPanelHeader } from '@features/generation/ui/promptFields/PromptPanelHeader';@@ -22,7 +26,15 @@ import { useTranslation } from 'react-i18next';/** One value per line: the same shape the user is editing a variant in. */const toValuesText = (values: string[]): string => values.join('\n');-const fromValuesText = (text: string): string[] => text.split('\n');+// Normalized on the way in, so what is stored is what an export writes and a+// re-import reads back. A stray blank line used to survive into the catalog and+// then vanish on the next round trip.+const fromValuesText = (text: string): string[] => normalizeWildcardValues(text.split('\n'));++const VALUES_ERROR_KEY = {+ tooManyValues: 'widgets.generate.dynamicPrompts.wildcardTooManyValues',+ valueTooLong: 'widgets.generate.dynamicPrompts.wildcardValueTooLong',+} as const;const NAME_ERROR_KEY = {invalid: 'widgets.generate.dynamicPrompts.wildcardNameInvalid',@@ -62,6 +74,12 @@ export const WildcardsPanel = ({const nameError = draft? getWildcardNameError(draft.name, new Set(catalog.wildcards.filter((w) => w.id !== draft.id).map((w) => w.name))): null;+ // Pre-flighted here as well as on the import path, which is where these bounds+ // used to live — the editor discovered them as a raw error from the server.+ const valuesError = draft ? getWildcardValuesError(fromValuesText(draft.valuesText)) : null;+ // The server's own explanation wins when there is one; otherwise say which+ // bound the draft is over before it is sent.+ const draftError = error ?? (valuesError ? t(VALUES_ERROR_KEY[valuesError]) : null);const startCreate = useCallback(() => {setError(null);@@ -173,9 +191,9 @@ export const WildcardsPanel = ({setDraft({ ...draft, valuesText: event.currentTarget.value })}/>- {error ? (+ {draftError ? (<Text color="fg.error" fontSize="2xs" wordBreak="break-word">- {error}+ {draftError}</Text>) : null}<HStack justify="end">@@ -183,7 +201,7 @@ export const WildcardsPanel = ({<XIcon />{t('common.cancel')}</Button>- <Button disabled={nameError !== null} size="xs" onClick={() => void save()}>+ <Button disabled={nameError !== null || valuesError !== null} size="xs" onClick={() => void save()}><CheckIcon />{t('common.save')}</Button>