Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions website/src/apps/mochi/src/renderer/ChatPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions website/src/apps/mochi/test/mochiStreamingFences.test.ts
Original file line number Diff line number Diff line change
@@ -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```')
})
})
11 changes: 7 additions & 4 deletions website/src/components/MarkdownRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion website/src/hooks/useBlockAssembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions website/src/test/MarkdownRendererCoverage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<MarkdownRenderer content={content} />)
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(<MarkdownRenderer content={'Here is code:```python\nprint(1)'} streaming />)
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(<MarkdownRenderer content={' ```diff\n ```'} />)
expect(labels(container)).toEqual(['diff'])
expect(container.querySelector('.code-block')!.textContent).not.toContain('undefined')
})
})

// ── block routing ──────────────────────────────────────────────────────────

describe('MarkdownRenderer block routing', () => {
Expand Down
20 changes: 20 additions & 0 deletions website/src/test/fixCodeFences.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
Loading