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): treat the prompt as literal when merging a template

joshistoast37c61ae3ac9d
diff --git a/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.test.ts b/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.test.ts
index dae921dd54..f1e8889034 100644
--- a/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.test.ts
+++ b/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.test.ts
@@ -116,15 +116,44 @@ describe('getPromptTemplateChunks', () => {
// The chunks are only ever rendered, never submitted, so their concatenation
// has to equal what `applyPromptTemplate` produces or the preview lies.
+ //
+ // The prompts here are the interesting half: the chunks split (literal) while
+ // the merge replaces, and `$&` and friends are special in a replacement string
+ // whatever the pattern is. Holding this over one prompt let that diverge.
it('concatenates to the merged prompt', () => {
+ const prompts = ['a cat', 'a poster reading $$$', 'x $& y', "x $' y", 'x $` y', 'a $1 note'];
+
for (const templatePrompt of ['', 'oil painting', 'a photo of {prompt}, 8k', '{prompt} by {prompt}']) {
- expect(getPromptTemplateChunks('a cat', templatePrompt).join('')).toBe(
- applyPromptTemplate(templatePrompt, 'a cat')
- );
+ for (const prompt of prompts) {
+ expect(getPromptTemplateChunks(prompt, templatePrompt).join('')).toBe(
+ applyPromptTemplate(templatePrompt, prompt)
+ );
+ }
}
});
});
+describe('applyPromptTemplate', () => {
+ // Regression: the prompt was passed as a replacement *string*, so `$&` put the
+ // placeholder back and the expander turned it into the word "prompt", while
+ // `$$` swallowed a character outright.
+ it('substitutes the prompt literally, dollars and all', () => {
+ expect(applyPromptTemplate('a photo of {prompt}, 8k', 'x $& y')).toBe('a photo of x $& y, 8k');
+ expect(applyPromptTemplate('a photo of {prompt}, 8k', 'a poster reading $$$')).toBe(
+ 'a photo of a poster reading $$$, 8k'
+ );
+ expect(applyPromptTemplate('a photo of {prompt}, 8k', "x $' y")).toBe("a photo of x $' y, 8k");
+ });
+
+ it('substitutes only the first placeholder', () => {
+ expect(applyPromptTemplate('{prompt} by {prompt}', 'a cat')).toBe('a cat by {prompt}');
+ });
+
+ it('appends a template that carries no placeholder', () => {
+ expect(applyPromptTemplate('oil painting', 'a cat')).toBe('a cat oil painting');
+ });
+});
+
describe('sanitizePromptTemplateSnapshot', () => {
it('reads a well-formed snapshot', () => {
const snapshot = template({ negativePrompt: 'blurry', positivePrompt: '{prompt}, 8k' });
diff --git a/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.ts b/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.ts
index b0f6b9578d..dee64383ba 100644
--- a/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.ts
+++ b/invokeai/frontend/webv2/src/features/generation/core/promptTemplates.ts
@@ -48,10 +48,18 @@ interface AuthoredPrompts {
* Ported verbatim from the legacy `buildPresetModifiedPrompt` so a template
* written in either client reads the same: the placeholder is substituted (first
* occurrence only), and a template without one is appended after a space.
+ *
+ * The replacement is a function rather than the prompt itself. Passing a string
+ * makes `$$`, `$&`, `` $` `` and `$'` inside it special even against a string
+ * pattern, so `a poster reading $$$` lost a character and `x $& y` re-inserted
+ * the literal `{prompt}` — which then reached the expander and came back as the
+ * word "prompt", the exact failure merging first is supposed to prevent. It also
+ * has to stay literal for `getPromptTemplateChunks` below, which splits, to keep
+ * describing what actually generates.
*/
export const applyPromptTemplate = (templatePrompt: string, prompt: string): string =>
templatePrompt.includes(PROMPT_TEMPLATE_PLACEHOLDER)
- ? templatePrompt.replace(PROMPT_TEMPLATE_PLACEHOLDER, prompt)
+ ? templatePrompt.replace(PROMPT_TEMPLATE_PLACEHOLDER, () => prompt)
: `${prompt} ${templatePrompt}`;
/** The prompts that will actually generate. Identity when no template is active. */