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.

fix: restore prompt template catalog search

joshistoasted97639e2ae2
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.tsx
index be4aeca314..6796ea065d 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
@@ -136,7 +136,7 @@ const defaultTemplate: PromptTemplateRecord = {
isDefault: true,
isPublic: false,
name: 'Photography',
- negativePrompt: '',
+ negativePrompt: 'low contrast',
positivePrompt: '{prompt}. photography, bokeh',
userId: 'system',
};
@@ -294,7 +294,7 @@ describe('the prompt templates panel', () => {
expect(labels.filter((label) => label === 'Delete')).toHaveLength(1);
});
- it('does not search prompt prose', async () => {
+ it('finds fuzzy template names', async () => {
await render(
<PromptTemplatesPanel
activeTemplate={null}
@@ -308,11 +308,11 @@ describe('the prompt templates panel', () => {
);
await act(async () => {
- await userEvent.fill(host!.querySelector('input')!, 'bokeh');
+ await userEvent.fill(host!.querySelector('input')!, 'cnmt');
});
+ expect(host!.textContent).toContain('Cinematic');
expect(host!.textContent).not.toContain('Photography');
- expect(host!.textContent).not.toContain('Cinematic');
});
// The snapshot deliberately keeps applying after the template is gone, so a
@@ -352,12 +352,27 @@ describe('the prompt templates panel', () => {
expect(host!.textContent).not.toContain('was deleted');
});
- it('does not fuzzy-match a template name', async () => {
+ it.each([
+ ['twilight', 'Cinematic'],
+ ['WATERMARK', 'Cinematic'],
+ ['volumetric', 'Community'],
+ ['FINGERS', 'Community'],
+ ['bokeh', 'Photography'],
+ ['LOW CONTRAST', 'Photography'],
+ ])('finds case-insensitive prompt prose in the owning group (%s)', async (search, expectedName) => {
+ const catalog = createCatalog({
+ defaultTemplates: [defaultTemplate],
+ personalTemplates: [{ ...userTemplate, negativePrompt: 'watermark', positivePrompt: '{prompt}, twilight' }],
+ sharedTemplates: [
+ { ...sharedTemplate, negativePrompt: 'extra fingers', positivePrompt: '{prompt}, volumetric rays' },
+ ],
+ });
+
await render(
<PromptTemplatesPanel
activeTemplate={null}
isActiveTemplateMissing={false}
- catalog={createCatalog()}
+ catalog={catalog}
onApply={vi.fn()}
onCreate={vi.fn()}
onDetach={vi.fn()}
@@ -366,19 +381,27 @@ describe('the prompt templates panel', () => {
);
await act(async () => {
- await userEvent.fill(host!.querySelector('input')!, 'cnmt');
+ await userEvent.fill(host!.querySelector('input')!, search);
});
- expect(host!.textContent).not.toContain('Cinematic');
- expect(host!.textContent).not.toContain('Photography');
+ expect(host!.textContent).toContain(expectedName);
});
- it('matches a case-insensitive name substring', async () => {
+ it('keeps name hits ahead of prose hits without moving templates between their groups', async () => {
+ const catalog = createCatalog({
+ defaultTemplates: [defaultTemplate],
+ personalTemplates: [
+ { ...userTemplate, name: 'Cinematic', positivePrompt: '{prompt}, bokeh' },
+ { ...userTemplate, id: 'user-2', name: 'Bokeh study', positivePrompt: '{prompt}, portrait' },
+ ],
+ sharedTemplates: [{ ...sharedTemplate, positivePrompt: '{prompt}, bokeh' }],
+ });
+
await render(
<PromptTemplatesPanel
activeTemplate={null}
isActiveTemplateMissing={false}
- catalog={createCatalog()}
+ catalog={catalog}
onApply={vi.fn()}
onCreate={vi.fn()}
onDetach={vi.fn()}
@@ -387,11 +410,16 @@ describe('the prompt templates panel', () => {
);
await act(async () => {
- await userEvent.fill(host!.querySelector('input')!, 'NEMA');
+ await userEvent.fill(host!.querySelector('input')!, 'BOKEH');
});
- expect(host!.textContent).toContain('Cinematic');
- expect(host!.textContent).not.toContain('Photography');
+ const content = host!.textContent!;
+ expect(content.indexOf('Your templates')).toBeLessThan(content.indexOf('Bokeh study'));
+ expect(content.indexOf('Bokeh study')).toBeLessThan(content.indexOf('Cinematic'));
+ expect(content.indexOf('Cinematic')).toBeLessThan(content.indexOf('Shared templates'));
+ expect(content.indexOf('Shared templates')).toBeLessThan(content.indexOf('Community'));
+ expect(content.indexOf('Community')).toBeLessThan(content.indexOf('Built-in templates'));
+ expect(content.indexOf('Built-in templates')).toBeLessThan(content.indexOf('Photography'));
});
it('renders shared templates without edit or delete controls', async () => {
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplatesPanel.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplatesPanel.tsx
index 942788831c..3121976140 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplatesPanel.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplatesPanel.tsx
@@ -4,6 +4,7 @@ import type { PromptTemplateCatalog } from '@features/generation/ui/usePromptTem
import type { ChangeEvent } from 'react';
import { Box, HStack, Input, Separator, Stack, Text } from '@chakra-ui/react';
+import { searchCatalog } from '@features/generation/core/catalogSearch';
import { PROMPT_TEMPLATE_PLACEHOLDER } from '@features/generation/core/promptTemplates';
import { toPromptTemplateSnapshot } from '@features/generation/data/promptTemplates';
import { useGenerationUi } from '@features/generation/ui/GenerationUiContext';
@@ -50,14 +51,10 @@ interface PromptTemplatesPanelProps {
onCreate: () => void;
}
-const filterTemplatesByName = (
- templates: readonly PromptTemplateRecord[],
- searchTerm: string
-): PromptTemplateRecord[] => {
- const query = searchTerm.trim().toLocaleLowerCase();
-
- return query ? templates.filter((template) => template.name.toLocaleLowerCase().includes(query)) : [...templates];
-};
+const getTemplateProse = (template: PromptTemplateRecord): readonly string[] => [
+ template.positivePrompt,
+ template.negativePrompt,
+];
export const PromptTemplatesPanel = ({
activeTemplate,
@@ -75,15 +72,15 @@ export const PromptTemplatesPanel = ({
const [pendingDelete, setPendingDelete] = useState<PromptTemplateRecord | null>(null);
const personalTemplates = useMemo(
- () => filterTemplatesByName(catalog.personalTemplates, searchTerm),
+ () => searchCatalog(catalog.personalTemplates, searchTerm, getTemplateProse),
[catalog.personalTemplates, searchTerm]
);
const sharedTemplates = useMemo(
- () => filterTemplatesByName(catalog.sharedTemplates, searchTerm),
+ () => searchCatalog(catalog.sharedTemplates, searchTerm, getTemplateProse),
[catalog.sharedTemplates, searchTerm]
);
const defaultTemplates = useMemo(
- () => filterTemplatesByName(catalog.defaultTemplates, searchTerm),
+ () => searchCatalog(catalog.defaultTemplates, searchTerm, getTemplateProse),
[catalog.defaultTemplates, searchTerm]
);
const hasResults = personalTemplates.length > 0 || sharedTemplates.length > 0 || defaultTemplates.length > 0;
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/usePromptTemplates.browser.test.tsx b/invokeai/frontend/webv2/src/features/generation/ui/usePromptTemplates.browser.test.tsx
new file mode 100644
index 0000000000..f7a93d2dd8
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/generation/ui/usePromptTemplates.browser.test.tsx
@@ -0,0 +1,145 @@
+import type { PromptTemplateRecord } from '@features/generation/data/promptTemplates';
+
+import { promptTemplateKeys } from '@features/generation/data/promptTemplates';
+import { usePromptTemplates } from '@features/generation/ui/usePromptTemplates';
+import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
+import { act, useEffect } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+const transport = vi.hoisted(() => ({
+ apiFetch: vi.fn(),
+ apiFetchJson: vi.fn(),
+}));
+
+vi.mock('@platform/transport/http', () => ({
+ apiFetch: transport.apiFetch,
+ apiFetchJson: transport.apiFetchJson,
+}));
+
+vi.mock('@platform/state/accountLifecycle', () => ({
+ assertAccountScopeCurrent: vi.fn(),
+ captureAccountScope: () => ({ signal: new AbortController().signal }),
+}));
+
+vi.mock('@features/generation/ui/GenerationUiContext', () => ({
+ useGenerationUi: () => ({ account: { currentUserId: 'user-1', multiuserEnabled: true } }),
+}));
+
+const dto = {
+ id: 'template-1',
+ image: null,
+ is_public: false,
+ name: 'Cinematic',
+ preset_data: { negative_prompt: '', positive_prompt: '{prompt}, cinematic' },
+ type: 'user' as const,
+ user_id: 'user-1',
+};
+
+const template: PromptTemplateRecord = {
+ hasImage: true,
+ id: dto.id,
+ isDefault: false,
+ isPublic: false,
+ name: dto.name,
+ negativePrompt: dto.preset_data.negative_prompt,
+ positivePrompt: dto.preset_data.positive_prompt,
+ userId: dto.user_id,
+};
+
+let host: HTMLDivElement | null = null;
+let root: Root | null = null;
+let catalog: ReturnType<typeof usePromptTemplates> | null = null;
+let queryClient: QueryClient;
+(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+
+const Probe = () => {
+ const value = usePromptTemplates({ isEnabled: false });
+
+ useEffect(() => {
+ catalog = value;
+ }, [value]);
+
+ return null;
+};
+
+beforeEach(async () => {
+ vi.clearAllMocks();
+ host = document.createElement('div');
+ document.body.append(host);
+ root = createRoot(host);
+ queryClient = new QueryClient();
+
+ await act(() => {
+ root?.render(
+ <QueryClientProvider client={queryClient}>
+ <Probe />
+ </QueryClientProvider>
+ );
+ });
+});
+
+afterEach(async () => {
+ await act(() => root?.unmount());
+ host?.remove();
+ host = null;
+ root = null;
+ catalog = null;
+});
+
+describe('prompt template mutation invalidation', () => {
+ it('invalidates the template list exactly once after creation', async () => {
+ transport.apiFetchJson.mockResolvedValue(dto);
+ const invalidateQueries = vi.spyOn(queryClient, 'invalidateQueries');
+
+ await act(async () => {
+ await catalog!.create({
+ image: null,
+ name: dto.name,
+ negativePrompt: '',
+ positivePrompt: dto.preset_data.positive_prompt,
+ });
+ });
+
+ expect(invalidateQueries).toHaveBeenCalledTimes(1);
+ expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: promptTemplateKeys.list(), exact: true });
+ });
+
+ it.each([
+ [
+ 'update',
+ (value: ReturnType<typeof usePromptTemplates>) =>
+ value.update(template, {
+ image: { kind: 'preserve' },
+ name: dto.name,
+ negativePrompt: '',
+ positivePrompt: dto.preset_data.positive_prompt,
+ }),
+ ],
+ ['delete', (value: ReturnType<typeof usePromptTemplates>) => value.remove(template)],
+ ])('invalidates the list and affected image exactly once after %s', async (_operation, run) => {
+ transport.apiFetchJson.mockResolvedValue(dto);
+ transport.apiFetch.mockResolvedValue(new Response());
+ const invalidateQueries = vi.spyOn(queryClient, 'invalidateQueries');
+
+ await act(async () => {
+ await run(catalog!);
+ });
+
+ expect(invalidateQueries).toHaveBeenCalledTimes(2);
+ expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: promptTemplateKeys.list(), exact: true });
+ expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: promptTemplateKeys.image(template.id), exact: true });
+ });
+
+ it('invalidates the template list exactly once after import', async () => {
+ transport.apiFetch.mockResolvedValue(new Response());
+ const invalidateQueries = vi.spyOn(queryClient, 'invalidateQueries');
+
+ await act(async () => {
+ await catalog!.importFile(new File(['name,prompt'], 'templates.csv', { type: 'text/csv' }));
+ });
+
+ expect(invalidateQueries).toHaveBeenCalledTimes(1);
+ expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: promptTemplateKeys.list(), exact: true });
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/usePromptTemplates.ts b/invokeai/frontend/webv2/src/features/generation/ui/usePromptTemplates.ts
index 61edddb4cb..ae830e7969 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/usePromptTemplates.ts
+++ b/invokeai/frontend/webv2/src/features/generation/ui/usePromptTemplates.ts
@@ -74,11 +74,9 @@ export const usePromptTemplates = ({ isEnabled = true }: { isEnabled?: boolean }
const create = useCallback(
async (draft: PromptTemplateCreateDraft) => {
- const saved = await runAndInvalidate(() => createPromptTemplate(draft));
- await invalidatePromptTemplates(queryClient, saved.id);
- return saved;
+ return await runAndInvalidate(() => createPromptTemplate(draft));
},
- [queryClient, runAndInvalidate]
+ [runAndInvalidate]
);
const update = useCallback(