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.

feat(webv2): scope each inline trigger to its own kind

joshistoast0c6bcc41d778
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerPicker.browser.test.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerPicker.browser.test.tsx
index f0c0d43171..a80e7f24b7 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerPicker.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerPicker.browser.test.tsx
@@ -15,9 +15,12 @@ vi.mock('@features/generation/ui/promptFields/PositivePromptActions', () => ({
PromptTriggerPopover: () => null,
}));
+const MODEL_CATALOG = [{ base: 'sdxl', name: 'easynegative', type: 'embedding' }];
+const SELECTED_MODEL = { base: 'sdxl', name: 'Juggernaut', trigger_phrases: ['jugg'] };
+
vi.mock('@features/generation/ui/GenerationUiContext', async (importOriginal) => ({
...(await importOriginal<object>()),
- useGenerationUi: () => ({ models: { catalog: [], ensureLoaded: vi.fn() } }),
+ useGenerationUi: () => ({ models: { catalog: MODEL_CATALOG, ensureLoaded: vi.fn() } }),
}));
const WILDCARDS = [
@@ -54,7 +57,7 @@ const render = async (isTemplateViewMode = false) => {
isTemplateViewMode={isTemplateViewMode}
loras={[]}
projectId="project-1"
- selectedModel={undefined}
+ selectedModel={SELECTED_MODEL as never}
showSyntaxHighlighting={false}
value=""
onChange={vi.fn()}
@@ -132,6 +135,26 @@ describe('the caret autocomplete', () => {
expect(optionLabels()).toEqual(['colors', 'moods']);
});
+ // Each delimiter offers only what it can actually open. `jugg`, the model's
+ // trigger phrase, belongs to neither and stays with the `+` button.
+ it('offers embeddings for `<` and wildcards for `__`, and phrases for neither', async () => {
+ await type('a photo of <');
+
+ expect(optionLabels()).toEqual(['easynegative']);
+
+ await press('{Escape}');
+ await type('__');
+
+ expect(optionLabels()).toEqual(['colors', 'moods']);
+ });
+
+ it('inserts an embedding with its angle brackets', async () => {
+ await type('a photo of <easy');
+ await press('{Enter}');
+
+ expect(textarea().value).toBe('a photo of <easynegative>');
+ });
+
// The whole point: nothing is swallowed, so there is nothing to give back.
it('leaves the typed characters in the prompt while it is open', async () => {
await type('a photo of __');
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptTriggerOptions.test.ts b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptTriggerOptions.test.ts
new file mode 100644
index 0000000000..76182ceb9c
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptTriggerOptions.test.ts
@@ -0,0 +1,82 @@
+import { describe, expect, it } from 'vitest';
+
+import type { PromptTriggerOption } from './promptTriggerOptions';
+
+import { filterPromptTriggerOptions, getInlineTriggerOptions, getPromptTriggerOptions } from './promptTriggerOptions';
+
+const OPTIONS: PromptTriggerOption[] = [
+ { group: 'Wildcards', kind: 'wildcard', label: 'colours', value: '__colours__' },
+ { group: 'Wildcards', kind: 'wildcard', label: 'moods', value: '__moods__' },
+ { group: 'Compatible embeddings', kind: 'embedding', label: 'easynegative', value: '<easynegative>' },
+ { group: 'My LoRA', kind: 'phrase', label: 'a photo of sks', value: 'a photo of sks' },
+];
+
+describe('getPromptTriggerOptions', () => {
+ const build = () =>
+ getPromptTriggerOptions({
+ compatibleEmbeddingsLabel: 'Compatible embeddings',
+ loras: [
+ { isEnabled: true, model: { name: 'My LoRA', trigger_phrases: ['sks'] } },
+ { isEnabled: false, model: { name: 'Off LoRA', trigger_phrases: ['nope'] } },
+ ] as never,
+ mainModelLabel: 'Main model',
+ models: [
+ { base: 'sdxl', name: 'easynegative', type: 'embedding' },
+ { base: 'sd-1', name: 'wrongbase', type: 'embedding' },
+ ] as never,
+ selectedModel: { base: 'sdxl', name: 'Juggernaut', trigger_phrases: ['jugg'] } as never,
+ wildcards: [{ name: 'colours', values: ['red'] }],
+ wildcardsLabel: 'Wildcards',
+ });
+
+ it('tags every option with what it inserts', () => {
+ expect(build().map((option) => [option.kind, option.value])).toEqual([
+ ['wildcard', '__colours__'],
+ ['phrase', 'jugg'],
+ ['phrase', 'sks'],
+ ['embedding', '<easynegative>'],
+ ]);
+ });
+
+ it('skips disabled LoRAs and embeddings of another base', () => {
+ const labels = build().map((option) => option.label);
+
+ expect(labels).not.toContain('nope');
+ expect(labels).not.toContain('wrongbase');
+ });
+});
+
+describe('getInlineTriggerOptions', () => {
+ // `<` opens an embedding token and `__` a wildcard reference, so offering the
+ // other kind under either is offering something the user cannot have meant.
+ it('offers only wildcards for `__`', () => {
+ expect(getInlineTriggerOptions(OPTIONS, '_', '').map((option) => option.label)).toEqual(['colours', 'moods']);
+ });
+
+ it('offers only embeddings for `<`', () => {
+ expect(getInlineTriggerOptions(OPTIONS, '<', '').map((option) => option.label)).toEqual(['easynegative']);
+ });
+
+ // They belong to no delimiter, and stay reachable through the `+` button.
+ it('offers trigger phrases under neither', () => {
+ const everything = [...getInlineTriggerOptions(OPTIONS, '_', ''), ...getInlineTriggerOptions(OPTIONS, '<', '')];
+
+ expect(everything.some((option) => option.kind === 'phrase')).toBe(false);
+ });
+
+ it('narrows by label', () => {
+ expect(getInlineTriggerOptions(OPTIONS, '_', 'mo').map((option) => option.label)).toEqual(['moods']);
+ expect(getInlineTriggerOptions(OPTIONS, '_', 'MO').map((option) => option.label)).toEqual(['moods']);
+ });
+
+ // `__w` matching every wildcard on the strength of the word "Wildcards" is
+ // what folding the group into the query would do.
+ it('ignores the group name, unlike the browse search', () => {
+ expect(getInlineTriggerOptions(OPTIONS, '_', 'wildcard')).toEqual([]);
+ expect(filterPromptTriggerOptions(OPTIONS, 'wildcard')).toHaveLength(2);
+ });
+
+ it('returns nothing when the query matches no label', () => {
+ expect(getInlineTriggerOptions(OPTIONS, '_', 'zzz')).toEqual([]);
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptTriggerOptions.ts b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptTriggerOptions.ts
index 08f1ff6320..fc8e6cf86c 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptTriggerOptions.ts
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptTriggerOptions.ts
@@ -9,14 +9,23 @@
import type { GenerationModelCatalogItem as ModelConfig } from '@features/generation/contracts';
import type { GenerateLora, GenerateModelConfig } from '@features/generation/core/types';
+import type { PromptTriggerKey } from '@features/generation/ui/promptFields/promptFocus';
import { useGenerationUi } from '@features/generation/ui/GenerationUiContext';
import { useWildcards } from '@features/generation/ui/useWildcards';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
+/**
+ * What an option inserts. Carried explicitly rather than sniffed back out of
+ * `value`, because the inline autocomplete offers only the kind its delimiter
+ * actually stands for.
+ */
+export type PromptTriggerKind = 'embedding' | 'phrase' | 'wildcard';
+
export interface PromptTriggerOption {
group: string;
+ kind: PromptTriggerKind;
label: string;
value: string;
}
@@ -60,11 +69,11 @@ export const getPromptTriggerOptions = ({
// A wildcard inserts as `__name__`, which is just another trigger the picker
// can splice in, so it rides in the same list as embeddings and LoRA phrases.
for (const wildcard of wildcards) {
- options.push({ group: wildcardsLabel, label: wildcard.name, value: `__${wildcard.name}__` });
+ options.push({ group: wildcardsLabel, kind: 'wildcard', label: wildcard.name, value: `__${wildcard.name}__` });
}
for (const phrase of getTriggerPhrases(selectedModel)) {
- options.push({ group: selectedModel?.name ?? mainModelLabel, label: phrase, value: phrase });
+ options.push({ group: selectedModel?.name ?? mainModelLabel, kind: 'phrase', label: phrase, value: phrase });
}
for (const lora of loras) {
@@ -73,14 +82,19 @@ export const getPromptTriggerOptions = ({
}
for (const phrase of getTriggerPhrases(lora.model)) {
- options.push({ group: lora.model.name, label: phrase, value: phrase });
+ options.push({ group: lora.model.name, kind: 'phrase', label: phrase, value: phrase });
}
}
if (selectedModel) {
for (const model of models) {
if (model.type === 'embedding' && model.base === selectedModel.base) {
- options.push({ group: compatibleEmbeddingsLabel, label: model.name, value: `<${model.name}>` });
+ options.push({
+ group: compatibleEmbeddingsLabel,
+ kind: 'embedding',
+ label: model.name,
+ value: `<${model.name}>`,
+ });
}
}
}
@@ -99,6 +113,7 @@ export const getPromptTriggerOptions = ({
});
};
+/** The `+` button's search box, which browses the whole list by name or group. */
export const filterPromptTriggerOptions = (
options: readonly PromptTriggerOption[],
search: string
@@ -110,6 +125,32 @@ export const filterPromptTriggerOptions = (
);
};
+/** Which kind each delimiter stands for. Trigger phrases have no delimiter. */
+const KIND_BY_TRIGGER_KEY: Record<PromptTriggerKey, PromptTriggerKind> = { '<': 'embedding', _: 'wildcard' };
+
+/**
+ * The options an inline `<` or `__` should offer.
+ *
+ * Narrowed to the delimiter's own kind: `<` opens an embedding token and `__` a
+ * wildcard reference, so offering the other under either is offering something
+ * the user cannot have meant. Model and LoRA trigger phrases belong to no
+ * delimiter and stay with the `+` button, which browses everything.
+ *
+ * Matching is on the label alone, unlike the browse search above. The query here
+ * is a name being typed, and folding in the group would make `__w` match every
+ * wildcard on the strength of the word "Wildcards".
+ */
+export const getInlineTriggerOptions = (
+ options: readonly PromptTriggerOption[],
+ key: PromptTriggerKey,
+ search: string
+): PromptTriggerOption[] => {
+ const kind = KIND_BY_TRIGGER_KEY[key];
+ const query = search.trim().toLowerCase();
+
+ return options.filter((option) => option.kind === kind && (!query || option.label.toLowerCase().includes(query)));
+};
+
export const groupPromptTriggerOptions = (options: readonly PromptTriggerOption[]): PromptTriggerOptionGroup[] =>
options.reduce<PromptTriggerOptionGroup[]>((groups, option) => {
const existing = groups.find((group) => group.group === option.group);
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerAutocomplete.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerAutocomplete.tsx
index 86bd33de8d..dd19213c6b 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerAutocomplete.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerAutocomplete.tsx
@@ -23,7 +23,7 @@ import { getTextareaCaretRect } from '@features/generation/ui/promptFields/promp
import { getActiveTriggerQuery, insertPromptText } from '@features/generation/ui/promptFields/promptFocus';
import { PromptTriggerAutocomplete } from '@features/generation/ui/promptFields/PromptTriggerAutocomplete';
import {
- filterPromptTriggerOptions,
+ getInlineTriggerOptions,
usePromptTriggerOptions,
} from '@features/generation/ui/promptFields/promptTriggerOptions';
import { useCallback, useId, useMemo, useState } from 'react';
@@ -72,7 +72,7 @@ export const usePromptTriggerAutocomplete = ({
const options = usePromptTriggerOptions(loras, selectedModel);
const matches = useMemo(
- () => (state ? filterPromptTriggerOptions(options, state.query.query) : []),
+ () => (state ? getInlineTriggerOptions(options, state.query.key, state.query.query) : []),
[options, state]
);
const isOpen = state !== null && matches.length > 0;