Fix stray bracket after AI chat chips (#23798)

Chips in the AI chat sometimes rendered with a leftover `]` after them.

The reference marker is bracket-asymmetric: it opens with `[[` and
closes with `[[/kind]]`, so a complete reference holds four `[` and only
two `]`. The model balances that by writing `…[[/object]]]`, and the
parser ended the match exactly at the close tag, leaving the extra
bracket as prose next to the chip.

The parser now absorbs up to as many surplus `]` as the reference opened
with, and accepts an opener with extra `[` so an over-wrapped marker
doesn't leak one either. The system prompt also tells the model the
marker is complete as written.

<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/twentyhq/twenty/pull/23798?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
This commit is contained in:
Raphaël Bosi
2026-08-05 14:04:49 +02:00
committed by GitHub
parent f893b214e2
commit 76bf3651bb
12 changed files with 194 additions and 6 deletions
@@ -1,6 +1,8 @@
import { type ChatReferenceStart } from '@/ai/types/ChatReferenceStart';
import { isDefined } from 'twenty-shared/utils';
const OPEN_BRACKETS_REGEX = /^\[+/;
export const getChatReferenceStartFromMatch = (
match: RegExpExecArray,
): ChatReferenceStart => {
@@ -12,7 +14,15 @@ export const getChatReferenceStartFromMatch = (
recordId,
} = match.groups ?? {};
const position = { index: match.index, prefixLength: match[0].length };
const openBracketsMatch = OPEN_BRACKETS_REGEX.exec(match[0]);
const position = {
index: match.index,
prefixLength: match[0].length,
openBracketLength: isDefined(openBracketsMatch)
? openBracketsMatch[0].length
: 0,
};
if (isDefined(objectNameSingular)) {
return { ...position, identity: { kind: 'object', objectNameSingular } };