-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(chat): preserve literal code around source chips #7456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+235
−51
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 110 additions & 47 deletions
157
...ce/[workspaceId]/home/components/message-content/components/chat-content/chat-sanitize.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,66 +1,129 @@ | ||
| const HIDDEN_INLINE_REFERENCE_PATTERN = | ||
| /`[^`\n]*(?:internal\/tool-results\/|internal\/blocktips\/|components\/integrations\/[^`\n]*README)[^`\n]*`/g | ||
|
|
||
| /** JSON strings own their escaped quotes, backticks, and any quoted tag markers. */ | ||
| const JSON_STRING_SOURCE = String.raw`"(?:[^"\\\r\n]|\\[^\r\n])*"` | ||
| /** JSON strings own their escaped quotes, backticks, and quoted tag markers. */ | ||
| const JSON_STRING_SOURCE = '"(?:\\\\(?:["\\\\/bfnrt]|u[0-9a-fA-F]{4})|[^"\\\\\\r\\n])*"' | ||
|
|
||
| /** | ||
| * Complete chip tags consume JSON strings atomically. Outside strings, a new | ||
| * opener or backtick ends the candidate, so prose mentions cannot join into a | ||
| * tag and repeated unclosed openers cannot repeatedly scan the same suffix. | ||
| */ | ||
| const COMPLETE_TAG_SOURCE = `<(?<chipTag>workspace_resource|source)>\\s*\\{(?:${JSON_STRING_SOURCE}|[^"\`<])*?\\}\\s*</\\k<chipTag>>` | ||
| /** Unquoted openers, backticks, and invalid backslashes bound failed payload scans. */ | ||
| const COMPLETE_TAG_SOURCE = `<(?<chipTag>workspace_resource|source)>\\s*\\{(?:${JSON_STRING_SOURCE}|[^"\`<\\\\])*?\\}\\s*</\\k<chipTag>>` | ||
|
|
||
| const CHIP_OR_CODE_DELIMITER = new RegExp(`${COMPLETE_TAG_SOURCE}|\`|\n`, 'g') | ||
| const INLINE_CHIP_OR_DELIMITER = new RegExp(`${COMPLETE_TAG_SOURCE}|\`+|\\n`, 'g') | ||
| const CHIP_OR_PARAGRAPH_BREAK = new RegExp(`${COMPLETE_TAG_SOURCE}|\\n[\\t \\r]*\\n`, 'g') | ||
|
|
||
| /** | ||
| * Pair Markdown delimiters outside chip payloads in one forward pass. A pair | ||
| * containing a chip is unwrapped; a lone delimiter is removed only when flush | ||
| * against a chip. Neighbouring code spans and multiline fences keep their pairs. | ||
| */ | ||
| export function sanitizeChatDisplayContent(content: string): string { | ||
| const removedDelimiters: number[] = [] | ||
| let openingTick = -1 | ||
| let containsChip = false | ||
| let adjacentToChip = false | ||
| let lastChipEnd = -1 | ||
| interface OpenCodeSpan { | ||
| index: number | ||
| containsChip: boolean | ||
| touchesChip: boolean | ||
| } | ||
|
|
||
| for (const match of content.matchAll(CHIP_OR_CODE_DELIMITER)) { | ||
| const index = match.index | ||
| if (match.groups?.chipTag) { | ||
| if (openingTick !== -1) { | ||
| containsChip = true | ||
| adjacentToChip ||= index === openingTick + 1 | ||
| } | ||
| lastChipEnd = index + match[0].length | ||
| continue | ||
| /** Only matched multi-backtick runs are code; an unmatched run remains ordinary prose. */ | ||
| function unwrapInlineParagraph(content: string): string { | ||
| const remainingRuns = new Map<number, number>() | ||
| for (const [value] of content.matchAll(INLINE_CHIP_OR_DELIMITER)) { | ||
| if (value.startsWith('`') && value.length > 1) { | ||
| remainingRuns.set(value.length, (remainingRuns.get(value.length) ?? 0) + 1) | ||
| } | ||
| } | ||
| const removedDelimiters: number[] = [] | ||
| let openSpan: OpenCodeSpan | null = null | ||
| let previousChipEnd = -1 | ||
| let protectedRunLength: number | null = null | ||
|
|
||
| if (match[0] === '\n') { | ||
| if (openingTick !== -1 && adjacentToChip) removedDelimiters.push(openingTick) | ||
| openingTick = -1 | ||
| lastChipEnd = -1 | ||
| continue | ||
| } | ||
| const finishLine = () => { | ||
| if (openSpan?.touchesChip) removedDelimiters.push(openSpan.index) | ||
| openSpan = null | ||
| } | ||
|
|
||
| if (openingTick === -1) { | ||
| openingTick = index | ||
| containsChip = false | ||
| adjacentToChip = lastChipEnd === index | ||
| for (const token of content.matchAll(INLINE_CHIP_OR_DELIMITER)) { | ||
| const [value] = token | ||
| const index = token.index | ||
| if (value === '\n') { | ||
| finishLine() | ||
| previousChipEnd = -1 | ||
| } else if (value.startsWith('`')) { | ||
| if (value.length > 1) { | ||
| remainingRuns.set(value.length, (remainingRuns.get(value.length) ?? 1) - 1) | ||
| } | ||
| if (protectedRunLength !== null) { | ||
| if (value.length === protectedRunLength) protectedRunLength = null | ||
| continue | ||
| } | ||
| if (value.length > 1) { | ||
| if (!openSpan && remainingRuns.get(value.length)) protectedRunLength = value.length | ||
| continue | ||
| } | ||
| if (openSpan) { | ||
| if (openSpan.containsChip) removedDelimiters.push(openSpan.index, index) | ||
| openSpan = null | ||
| } else { | ||
| openSpan = { index, containsChip: false, touchesChip: previousChipEnd === index } | ||
| } | ||
| } else { | ||
| if (containsChip) removedDelimiters.push(openingTick, index) | ||
| openingTick = -1 | ||
| if (openSpan) { | ||
| openSpan.containsChip = true | ||
| openSpan.touchesChip ||= index === openSpan.index + 1 | ||
| } | ||
| previousChipEnd = index + value.length | ||
| } | ||
| } | ||
|
|
||
| if (openingTick !== -1 && adjacentToChip) removedDelimiters.push(openingTick) | ||
| finishLine() | ||
|
|
||
| const parts: string[] = [] | ||
| let start = 0 | ||
| let cursor = 0 | ||
| for (const index of removedDelimiters) { | ||
| parts.push(content.slice(start, index)) | ||
| start = index + 1 | ||
| parts.push(content.slice(cursor, index)) | ||
| cursor = index + 1 | ||
| } | ||
| parts.push(content.slice(cursor)) | ||
| return parts.join('') | ||
| } | ||
|
|
||
| /** Paragraph breaks end inline spans, but blank lines inside chip JSON belong to the payload. */ | ||
| function unwrapInlineChips(content: string): string { | ||
| const parts: string[] = [] | ||
| let cursor = 0 | ||
|
|
||
| for (const match of content.matchAll(CHIP_OR_PARAGRAPH_BREAK)) { | ||
| if (!match[0].startsWith('\n')) continue | ||
| parts.push(unwrapInlineParagraph(content.slice(cursor, match.index)), match[0]) | ||
| cursor = match.index + match[0].length | ||
| } | ||
| parts.push(unwrapInlineParagraph(content.slice(cursor))) | ||
| return parts.join('') | ||
| } | ||
|
|
||
| /** Fenced blocks are literal, including unclosed streaming fences and longer closing runs. */ | ||
| export function sanitizeChatDisplayContent(content: string): string { | ||
| const parts: string[] = [] | ||
| let cursor = 0 | ||
| let fenceStart: number | null = null | ||
| let fence = '' | ||
|
|
||
| for (const line of content.matchAll(/^ {0,3}(`{3,}|~{3,})([^\n]*)(?:\n|$)/gm)) { | ||
| const [, delimiter, info] = line | ||
| if (fenceStart === null) { | ||
| if (delimiter[0] === '`' && info.includes('`')) continue | ||
| fenceStart = line.index | ||
| fence = delimiter | ||
| } else if ( | ||
| delimiter[0] === fence[0] && | ||
| delimiter.length >= fence.length && | ||
| /^[\t \r]*$/.test(info) | ||
| ) { | ||
| const end = line.index + line[0].length | ||
| parts.push( | ||
| unwrapInlineChips(content.slice(cursor, fenceStart)), | ||
| content.slice(fenceStart, end) | ||
| ) | ||
| cursor = end | ||
| fenceStart = null | ||
| } | ||
| } | ||
|
|
||
| if (fenceStart === null) { | ||
| parts.push(unwrapInlineChips(content.slice(cursor))) | ||
| } else { | ||
| parts.push(unwrapInlineChips(content.slice(cursor, fenceStart)), content.slice(fenceStart)) | ||
| } | ||
| parts.push(content.slice(start)) | ||
| return parts.join('').replace(HIDDEN_INLINE_REFERENCE_PATTERN, '') | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.