fix(autocomplete): escape parentheses in prompt tag insertion (#951)

This commit is contained in:
Will Miao
2026-05-31 15:40:19 +08:00
parent d02a0611d3
commit 1b202f8ec7

View File

@@ -183,6 +183,13 @@ function parseSearchTokens(term = '') {
return { include, exclude }; return { include, exclude };
} }
function escapePromptParentheses(text) {
// In ComfyUI's CLIP text encoder, bare parentheses are weight adjustment syntax.
// Tags containing literal parentheses must be escaped with backslash to prevent
// them from being interpreted as weight modifiers. e.g. "foo (bar)" → "foo \(bar\)"
return text.replace(/\(/g, '\\(').replace(/\)/g, '\\)');
}
function formatAutocompleteInsertion(text = '') { function formatAutocompleteInsertion(text = '') {
const trimmed = typeof text === 'string' ? text.trim() : ''; const trimmed = typeof text === 'string' ? text.trim() : '';
if (!trimmed) { if (!trimmed) {
@@ -253,7 +260,7 @@ function createDefaultBehavior(modelType) {
if (!trimmed) { if (!trimmed) {
return ''; return '';
} }
return formatAutocompleteInsertion(trimmed); return formatAutocompleteInsertion(escapePromptParentheses(trimmed));
}, },
}; };
} }
@@ -352,7 +359,7 @@ const MODEL_BEHAVIORS = {
custom_words: { custom_words: {
enablePreview: false, enablePreview: false,
async getInsertText(_instance, relativePath) { async getInsertText(_instance, relativePath) {
return formatAutocompleteInsertion(relativePath); return formatAutocompleteInsertion(escapePromptParentheses(relativePath));
}, },
}, },
prompt: { prompt: {
@@ -399,6 +406,8 @@ const MODEL_BEHAVIORS = {
tagText = tagText.replace(/_/g, ' '); tagText = tagText.replace(/_/g, ' ');
} }
tagText = escapePromptParentheses(tagText);
return formatAutocompleteInsertion(tagText); return formatAutocompleteInsertion(tagText);
} }
}, },