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): re-expand prompts after a wildcard edit
joshistoastd09ed2520509
diff --git a/invokeai/frontend/webv2/src/features/generation/data/dynamicPromptsQueries.test.ts b/invokeai/frontend/webv2/src/features/generation/data/dynamicPromptsQueries.test.tsnew file mode 100644index 0000000000..05e4e2d7f1--- /dev/null+++ b/invokeai/frontend/webv2/src/features/generation/data/dynamicPromptsQueries.test.ts@@ -0,0 +1,47 @@+import { QueryClient } from '@tanstack/react-query';+import { beforeEach, describe, expect, it, vi } from 'vitest';++const parseDynamicPrompts = vi.hoisted(() => vi.fn());++vi.mock('@features/generation/data/promptUtilities', () => ({ parseDynamicPrompts }));++import { dynamicPromptsKeys, resolveDynamicPrompts } from './dynamicPromptsQueries';++const request = { combinatorial: true, max_prompts: 100, prompt: 'a __colors__ ball', seed: null };++let queryClient: QueryClient;++beforeEach(() => {+ queryClient = new QueryClient();+ parseDynamicPrompts.mockReset();+ parseDynamicPrompts.mockResolvedValue({ error: null, prompts: ['a red ball'] });+});++describe('resolveDynamicPrompts', () => {+ it('serves a repeat request from the cache', async () => {+ await resolveDynamicPrompts(queryClient, request);+ await resolveDynamicPrompts(queryClient, request);++ expect(parseDynamicPrompts).toHaveBeenCalledTimes(1);+ });++ // Regression: `ensureQueryData` hands back cached data whenever it exists,+ // invalidated or not, so editing a wildcard left the submit path enqueueing an+ // expansion built from the old values.+ it('re-expands after the wildcard catalog is invalidated', async () => {+ await expect(resolveDynamicPrompts(queryClient, request)).resolves.toMatchObject({ prompts: ['a red ball'] });++ parseDynamicPrompts.mockResolvedValue({ error: null, prompts: ['a blue ball'] });+ await queryClient.invalidateQueries({ queryKey: dynamicPromptsKeys.all });++ await expect(resolveDynamicPrompts(queryClient, request)).resolves.toMatchObject({ prompts: ['a blue ball'] });+ expect(parseDynamicPrompts).toHaveBeenCalledTimes(2);+ });++ it('keys separate prompts apart', async () => {+ await resolveDynamicPrompts(queryClient, request);+ await resolveDynamicPrompts(queryClient, { ...request, prompt: 'a __shades__ ball' });++ expect(parseDynamicPrompts).toHaveBeenCalledTimes(2);+ });+});diff --git a/invokeai/frontend/webv2/src/features/generation/data/dynamicPromptsQueries.ts b/invokeai/frontend/webv2/src/features/generation/data/dynamicPromptsQueries.tsindex f8e9c9d818..8c7cd34d39 100644--- a/invokeai/frontend/webv2/src/features/generation/data/dynamicPromptsQueries.ts+++ b/invokeai/frontend/webv2/src/features/generation/data/dynamicPromptsQueries.ts@@ -53,8 +53,16 @@ export const dynamicPromptsQueryOptions = (request: ParseDynamicPromptsRequest)});})();-/** Cache-first expansion for the enqueue path, which cannot render a loading state. */+/**+ * Cache-first expansion for the enqueue path, which cannot render a loading state.+ *+ * `fetchQuery` rather than `ensureQueryData`: the latter returns whatever is in+ * the cache the moment it holds data, invalidated or not. Under `staleTime:+ * Infinity` that means an edited wildcard would keep serving its old expansion+ * to the submit path. `fetchQuery` consults staleness, which invalidation drives,+ * so an untouched entry still costs no round trip.+ */export const resolveDynamicPrompts = (queryClient: QueryClient,request: ParseDynamicPromptsRequest-): Promise<ParseDynamicPromptsResponse> => queryClient.ensureQueryData(dynamicPromptsQueryOptions(request));+): Promise<ParseDynamicPromptsResponse> => queryClient.fetchQuery(dynamicPromptsQueryOptions(request));diff --git a/invokeai/frontend/webv2/src/features/generation/data/wildcards.ts b/invokeai/frontend/webv2/src/features/generation/data/wildcards.tsindex 0347e7f895..18f4c8d9ec 100644--- a/invokeai/frontend/webv2/src/features/generation/data/wildcards.ts+++ b/invokeai/frontend/webv2/src/features/generation/data/wildcards.ts@@ -59,6 +59,9 @@ export const deleteWildcard = async (id: string): Promise<void> => {* prompt, and the expansion cache is keyed only on the request (with* `staleTime: Infinity`). Both caches must therefore be dropped together, or the* preview and the submitted batch would both keep serving the old expansion.+ *+ * Invalidation is the whole mechanism, so every reader has to consult staleness+ * — see the `fetchQuery` note in `resolveDynamicPrompts`.*/export const invalidateWildcardDependents = async (queryClient: QueryClient): Promise<void> => {await Promise.all([