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(webv2): reach embeddings whose names contain spaces

joshistoast4160a61194be
diff --git a/invokeai/frontend/webv2/public/locales/en.json b/invokeai/frontend/webv2/public/locales/en.json
index 79a9b89204..ee91e2e6ac 100644
--- a/invokeai/frontend/webv2/public/locales/en.json
+++ b/invokeai/frontend/webv2/public/locales/en.json
@@ -816,6 +816,7 @@
"concepts": "Concepts",
"conceptWeight": "{{name}} weight",
"compatibleEmbeddings": "Compatible embeddings",
+ "completionSuggestions": "Completions",
"couldNotExpandPrompt": "Could not expand the prompt.",
"couldNotGeneratePromptFromImage": "Could not generate a prompt from the selected image.",
"deletePromptHistoryItem": "Delete prompt history item",
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PositivePromptField.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PositivePromptField.tsx
index df893392fe..174dc14dbd 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PositivePromptField.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PositivePromptField.tsx
@@ -136,8 +136,6 @@ export const PositivePromptField = ({
return;
}
- // The autocomplete gets first refusal: while its list is open the arrow
- // keys belong to it rather than to prompt history.
autocomplete.handleKeyDown(event);
},
[autocomplete]
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerAutocomplete.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerAutocomplete.tsx
index 5a0a8392f6..52f71a609c 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerAutocomplete.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerAutocomplete.tsx
@@ -8,6 +8,7 @@ import type { MouseEvent } from 'react';
import { Box, Portal, Stack, Text } from '@chakra-ui/react';
import { groupPromptTriggerOptions } from '@features/generation/ui/promptFields/promptTriggerOptions';
import { useCallback, useLayoutEffect, useMemo, useRef } from 'react';
+import { useTranslation } from 'react-i18next';
const LIST_WIDTH_PX = 260;
const MAX_LIST_HEIGHT_PX = 220;
@@ -44,6 +45,7 @@ export const PromptTriggerAutocomplete = ({
options: readonly PromptTriggerOption[];
onSelect: (option: PromptTriggerOption) => void;
}) => {
+ const { t } = useTranslation();
const listRef = useRef<HTMLDivElement | null>(null);
// Each group carries where it starts in the flat list. The hook selects with
// `options[activeIndex]`, so the ids have to be numbered the same way — and
@@ -93,6 +95,7 @@ export const PromptTriggerAutocomplete = ({
return (
<Portal>
<Box
+ aria-label={t('widgets.generate.completionSuggestions')}
bg="bg.muted"
borderColor="border.emphasized"
borderRadius="md"
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptFocus.test.ts b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptFocus.test.ts
index e652ce906b..8adce7e17c 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptFocus.test.ts
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptFocus.test.ts
@@ -57,6 +57,25 @@ describe('getActiveTriggerQuery', () => {
expect(query('a __col our')).toBeNull();
});
+ // Embedding names routinely have spaces in them — `bad hands 5`, `Negative
+ // Hand` — so ending the attempt at the first space put a whole class of them
+ // out of reach inline, even though the filter would have matched.
+ it('reads on through spaces inside an unclosed angle bracket', () => {
+ expect(query('a photo of <bad hands')).toEqual({ key: '<', query: 'bad hands', range: { end: 21, start: 11 } });
+ });
+
+ it('still ends at a space when only wildcards are on offer', () => {
+ expect(query('a photo of <bad hands', ['_'])).toBeNull();
+ });
+
+ it('stays shut once the angle bracket is closed', () => {
+ expect(query('a <bad hands 5> photo of ')).toBeNull();
+ });
+
+ it('does not cross a newline looking for an angle bracket', () => {
+ expect(query('a <bad\nhands')).toBeNull();
+ });
+
it('ends at a newline', () => {
expect(query('a __col\nmore')).toBeNull();
});
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptFocus.ts b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptFocus.ts
index 0cff97c6aa..f397f7a4ef 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptFocus.ts
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptFocus.ts
@@ -117,7 +117,7 @@ export const getActiveTriggerQuery = (
for (let index = caret - 1; index >= limit; index--) {
const char = value[index];
- if (char === undefined || char === '\n' || char === '>' || /\s/.test(char)) {
+ if (char === '\n' || char === '>') {
return null;
}
@@ -125,6 +125,15 @@ export const getActiveTriggerQuery = (
return { key: '<', query: value.slice(index + 1, caret), range: { end: caret, start: index } };
}
+ // A space ends a wildcard attempt but not an embedding one. Embedding model
+ // names routinely have spaces in them — `bad hands 5` — so stopping here
+ // meant `<bad ` closed the list for good and the name could never be
+ // reached inline, even though the filter would have matched it. A wildcard
+ // name cannot contain a space, so there the space really is the end.
+ if (/\s/.test(char ?? '')) {
+ return keys.includes('<') ? findEnclosingEmbedding(value, caret, limit, index) : null;
+ }
+
if (char === '_' && value[index - 1] === '_' && keys.includes('_') && !isWordCharacter(value[index - 2])) {
const query = value.slice(index + 1, caret);
@@ -136,3 +145,31 @@ export const getActiveTriggerQuery = (
return null;
};
+
+/**
+ * Keeps scanning back past spaces for an unclosed `<`, having already met one.
+ *
+ * Split out so the common path — no space between the caret and the trigger —
+ * stays a single loop, and so the two stopping rules read separately: this one
+ * only stops at a newline or a `>`, which closes the token.
+ */
+const findEnclosingEmbedding = (
+ value: string,
+ caret: number,
+ limit: number,
+ from: number
+): PromptTriggerQuery | null => {
+ for (let index = from; index >= limit; index--) {
+ const char = value[index];
+
+ if (char === '\n' || char === '>') {
+ return null;
+ }
+
+ if (char === '<') {
+ return { key: '<', query: value.slice(index + 1, caret), range: { end: caret, start: index } };
+ }
+ }
+
+ return null;
+};
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 570773f445..2e4f904edd 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerAutocomplete.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerAutocomplete.tsx
@@ -61,8 +61,17 @@ export interface PromptTriggerAutocompleteApi {
close: () => void;
/** Re-read the trigger under the caret. Call on input, and on click. */
refresh: (textarea: HTMLTextAreaElement | null) => void;
- /** `true` when the key drove the list and the field should not also act on it. */
- handleKeyDown: (event: KeyboardEvent<HTMLTextAreaElement>) => boolean;
+ /**
+ * Drives the list from the textarea's own keydown.
+ *
+ * Returns nothing on purpose. It used to report whether it had consumed the
+ * key, on the theory that the field would otherwise also act on it — but both
+ * fields discarded that, and there is no contention to resolve: the only other
+ * keyboard feature here is prompt history, which is modifier-gated and so
+ * never reaches this at all. Anything this does take, it takes by calling
+ * `preventDefault` itself.
+ */
+ handleKeyDown: (event: KeyboardEvent<HTMLTextAreaElement>) => void;
}
export const usePromptTriggerAutocomplete = ({
@@ -153,20 +162,20 @@ export const usePromptTriggerAutocomplete = ({
);
const handleKeyDown = useCallback(
- (event: KeyboardEvent<HTMLTextAreaElement>): boolean => {
+ (event: KeyboardEvent<HTMLTextAreaElement>): void => {
// While an IME is composing, the arrows move through its candidate window
// and Enter commits the composition. Taking either would leave a Japanese
// or Chinese user unable to pick a candidate at all. `keyCode === 229` is
// the same signal for the browsers that do not set `isComposing`.
if (!isOpen || event.nativeEvent.isComposing || event.keyCode === 229) {
- return false;
+ return;
}
// A caret jump leaves the query behind, so the list goes with it — but the
// key still belongs to the textarea.
if (['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(event.key)) {
setState(null);
- return false;
+ return;
}
if (event.key === 'Escape') {
@@ -174,7 +183,7 @@ export const usePromptTriggerAutocomplete = ({
// Otherwise the popover or dialog this prompt sits in closes too.
event.stopPropagation();
setState(null);
- return true;
+ return;
}
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
@@ -182,7 +191,7 @@ export const usePromptTriggerAutocomplete = ({
event.preventDefault();
setActiveIndex((current) => (current + step + matches.length) % matches.length);
- return true;
+ return;
}
if (event.key === 'Enter' || event.key === 'Tab') {
@@ -191,11 +200,8 @@ export const usePromptTriggerAutocomplete = ({
if (option) {
event.preventDefault();
selectOption(option);
- return true;
}
}
-
- return false;
},
[activeIndex, isOpen, matches, selectOption]
);