diff --git a/website/src/apps/mochi/src/renderer/ChatPanel.tsx b/website/src/apps/mochi/src/renderer/ChatPanel.tsx index b4e283e1edb9..3e19d35959e9 100644 --- a/website/src/apps/mochi/src/renderer/ChatPanel.tsx +++ b/website/src/apps/mochi/src/renderer/ChatPanel.tsx @@ -1865,10 +1865,11 @@ const StreamingMarkdown = React.memo<{ content: string }>(({ content }) => { }) /** Ensure blank line before fences glued to text, and close any unclosed fence. */ -function fixStreamingFences(s: string): string { - // The info string is the whole backtick-free line, including attributes and - // a leading space, matching the dashboard's FENCE_OPEN. - s = s.replace(/([^\n])(\n?)(```[^`\n]*\n)/g, (_, pre, nl, fence) => +export function fixStreamingFences(s: string): string { + // The info string may be a tag plus an optional `{...}` attribute block, + // matching the dashboard's fixCodeFences: a ``` run followed by more words + // is a mid-sentence mention, not a fence. + s = s.replace(/([^\n])(\n?)(```(?:[ \t]*[^`\s]+)?(?:[ \t]+\{[^`\n]*\})?[ \t\r]*\n)/g, (_, pre, nl, fence) => nl ? pre + nl + fence : pre + '\n\n' + fence ) // If there's an odd number of ``` fences, the last one is unclosed — close it diff --git a/website/src/apps/mochi/test/mochiStreamingFences.test.ts b/website/src/apps/mochi/test/mochiStreamingFences.test.ts new file mode 100644 index 000000000000..358de3c2ba54 --- /dev/null +++ b/website/src/apps/mochi/test/mochiStreamingFences.test.ts @@ -0,0 +1,21 @@ +/** + * Mochi's streaming fence repair moves a fence glued to text onto its own line + * and closes a fence the stream has not closed yet. A ``` run in the middle of + * prose is not a fence, so neither step may turn it into one. + */ +import { describe, expect, it } from 'vitest' + +import { fixStreamingFences } from '../src/renderer/ChatPanel' + +describe('fixStreamingFences', () => { + it('leaves a mid-sentence ``` run and the prose after it unchanged', () => { + for (const input of [ + 'Use a ```diff block for file changes.\nThis line stays prose.', + '- Type ``` then Enter to start a code block\n- second item', + ]) expect(fixStreamingFences(input)).toBe(input) + }) + + it('separates and closes a glued fence that is still streaming', () => { + expect(fixStreamingFences('Here is code:```python\nprint(1)')).toBe('Here is code:\n\n```python\nprint(1)\n```') + }) +}) diff --git a/website/src/components/MarkdownRenderer.tsx b/website/src/components/MarkdownRenderer.tsx index 9e79fb2a5e06..2d195fca349e 100644 --- a/website/src/components/MarkdownRenderer.tsx +++ b/website/src/components/MarkdownRenderer.tsx @@ -495,7 +495,8 @@ const MD_COMPONENTS = { // rule as FENCE_OPEN (useBlockAssembler) / fixCodeFences. const match = /language-(\S+)/.exec(className || '') const lang = match?.[1] - const codeStr = String(children).replace(/\n$/, '') + // An empty fence has no children; String(undefined) would show "undefined". + const codeStr = String(children ?? '').replace(/\n$/, '') // `rehypeRedactionMarkers` stamps the ordinal of this block's first // credential tag when it holds any this reply has a record for. It is @@ -642,9 +643,11 @@ export function fixCodeFences(s: string): string { return num + '\\.' + trail }) // Ensure blank line before opening fences that are glued to preceding text. - // The info string is the whole backtick-free line, including attributes and - // a leading space, matching FENCE_OPEN in useBlockAssembler. - s = s.replace(/([^\n])(\n?)(```[^`\n]*\n)/g, (_, pre, nl, fence) => + // The info string may be a tag (FENCE_OPEN's tag rule in useBlockAssembler, + // after optional whitespace) plus an optional `{...}` attribute block, and + // nothing more: a ``` run followed by more words is a mid-sentence mention, + // and moving it onto its own line would open a fence over the prose after it. + s = s.replace(/([^\n])(\n?)(```(?:[ \t]*[^`\s]+)?(?:[ \t]+\{[^`\n]*\})?[ \t\r]*\n)/g, (_, pre, nl, fence) => nl ? pre + nl + fence : pre + '\n\n' + fence ) // Split closing fences glued to trailing text: ```358KB → ```\n358KB diff --git a/website/src/hooks/useBlockAssembler.ts b/website/src/hooks/useBlockAssembler.ts index 2e7c3dd7b5d9..fa24ba79f92b 100644 --- a/website/src/hooks/useBlockAssembler.ts +++ b/website/src/hooks/useBlockAssembler.ts @@ -11,7 +11,7 @@ import type { ContentBlock } from '../types' // whose label was truncated to the first `\w+` run, followed by a phantom empty // "code" block that ran to the end of the message. Group 2 is the tag; the // rest of the info string is accepted and ignored, including leading whitespace -// before the tag. Same rule fixCodeFences and the code-block label regex apply. +// before the tag. Same tag rule fixCodeFences and the code-block label regex apply. const FENCE_OPEN = /^(`{3,})\s*([^`\s]*)[^`]*$/ // Escape ALL regex metacharacters before interpolating a captured fence run // into a dynamic RegExp. The capture is currently backtick-only, but a diff --git a/website/src/test/MarkdownRendererCoverage.test.tsx b/website/src/test/MarkdownRendererCoverage.test.tsx index e4bf275226e2..9d7f4855f9e3 100644 --- a/website/src/test/MarkdownRendererCoverage.test.tsx +++ b/website/src/test/MarkdownRendererCoverage.test.tsx @@ -296,6 +296,39 @@ describe('MarkdownRenderer structural elements', () => { }) }) +describe('MarkdownRenderer mid-sentence ``` runs', () => { + const labels = (c: HTMLElement) => Array.from(c.querySelectorAll('.code-block')).map(b => b.querySelector('span')?.textContent) + + it('renders the run and the prose after it as prose, beside a real code block', () => { + const content = [ + 'Use a ```diff block for file changes.', + 'This line stays prose.', + '', + '```text', + 'separator', + '```', + ].join('\n') + const { container } = render() + expect(labels(container)).toEqual(['text']) + expect(container.textContent).toContain('Use a ```diff block for file changes.') + expect(container.textContent).toContain('This line stays prose.') + }) + + it('still renders a streaming glued fence as a code block', () => { + const { container } = render() + expect(labels(container)).toEqual(['python']) + expect(container.querySelector('.code-block')!.textContent).toContain('print(1)') + }) + + it('renders an empty fence without the word "undefined"', () => { + // parseBlocks keeps the indented fence in the markdown block; the glue pass + // moves it to column 0, and it reaches the fenced-code override with no children. + const { container } = render() + expect(labels(container)).toEqual(['diff']) + expect(container.querySelector('.code-block')!.textContent).not.toContain('undefined') + }) +}) + // ── block routing ────────────────────────────────────────────────────────── describe('MarkdownRenderer block routing', () => { diff --git a/website/src/test/fixCodeFences.test.ts b/website/src/test/fixCodeFences.test.ts index 8dc3cbb2b5ed..67d0c96cdb11 100644 --- a/website/src/test/fixCodeFences.test.ts +++ b/website/src/test/fixCodeFences.test.ts @@ -151,3 +151,23 @@ describe('fixCodeFences — info strings beyond \\w', () => { } }) }) + +describe('fixCodeFences — a ``` run in the middle of prose', () => { + const line = "plus the subagent's programmatic edits (which need a ```diff block). Let me produce a compact structural diff." + + it('leaves the line and the prose after it unchanged', () => { + for (const input of [ + line + '\n', + line + '\nThis line stays prose.\nSo does this one.', + 'Use a ```diff block for file changes.\n\n```text\nseparator\n```\n\nAfter.', + 'Use a ```diff block for file changes.\n\n```\nseparator\n```\n\nAfter.', + '- Type ``` then Enter to start a code block\n- second item', + ]) expect(fixCodeFences(input)).toBe(input) + }) + + it('still separates a glued fence whose closer has not streamed in yet', () => { + // While a reply streams, the markdown block ends inside the fence body. + expect(fixCodeFences('Here is code:```python\nprint(1)')).toBe('Here is code:\n\n```python\nprint(1)') + expect(fixCodeFences('note```js {1,3}\r\nx')).toBe('note\n\n```js {1,3}\r\nx') + }) +})