-
Notifications
You must be signed in to change notification settings - Fork 120
perf(components): use Streamdown static mode for finished markdown #494
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
9eb1607
2c1bc7e
7e561eb
a2da324
d98de52
59dcfd3
a04b9a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| const FENCE_LINE = /^( {0,3})(`{3,}|~{3,})(.*)$/; | ||
|
|
||
| export function markdownHasUnclosedFence(text: string): boolean { | ||
| let open: { char: string; len: number } | null = null; | ||
| for (const raw of text.split('\n')) { | ||
| const line = raw.endsWith('\r') ? raw.slice(0, -1) : raw; | ||
| const match = FENCE_LINE.exec(line); | ||
| if (!match) continue; | ||
| const marker = match[2]; | ||
| const char = marker[0]; | ||
| const len = marker.length; | ||
| const info = match[3]; | ||
| if (!open) { | ||
| if (char === '`' && info.includes('`')) continue; | ||
| open = { char, len }; | ||
| continue; | ||
| } | ||
| if (char === open.char && len >= open.len && info.trim() === '') { | ||
| open = null; | ||
| } | ||
| } | ||
| return open !== null; | ||
| } | ||
|
|
||
| export function resolveMarkdownStreamdownMode( | ||
| isStreaming: boolean, | ||
| text = '' | ||
| ): 'static' | 'streaming' { | ||
| if (isStreaming || markdownHasUnclosedFence(text)) return 'streaming'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a cancelled response opens a fence on a continuation line of an existing list, such as AGENTS.md reference: packages/components/src/components/ai-gui/AGENTS.md:L96-L96 Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving this. Open-fence detection now uses the same per-line CommonMark scanner as |
||
| return 'static'; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { resolveMarkdownStreamdownMode } from '../src/components/ai-gui/markdown-streamdown-mode'; | ||
|
|
||
| describe('resolveMarkdownStreamdownMode', () => { | ||
| it('uses static mode for finished complete markdown', () => { | ||
| expect(resolveMarkdownStreamdownMode(false, 'Hello **world**.')).toBe('static'); | ||
| }); | ||
|
|
||
| it('uses streaming mode while a turn is still growing', () => { | ||
| expect(resolveMarkdownStreamdownMode(true, 'Hello **wor')).toBe('streaming'); | ||
| }); | ||
|
|
||
| it('keeps streaming mode for a finished turn with an unclosed fence', () => { | ||
| expect(resolveMarkdownStreamdownMode(false, '```ts\nconst x = 1;\n')).toBe('streaming'); | ||
| }); | ||
|
|
||
| it('keeps streaming mode for an unclosed indented backtick fence', () => { | ||
| expect(resolveMarkdownStreamdownMode(false, '1. example:\n ```ts\n const x = 1;\n')).toBe( | ||
| 'streaming' | ||
| ); | ||
| }); | ||
|
|
||
| it('keeps streaming mode for an unclosed tilde fence', () => { | ||
| expect(resolveMarkdownStreamdownMode(false, '~~~js\nconst x = 1;\n')).toBe('streaming'); | ||
| }); | ||
|
|
||
| it('uses static mode when an indented fence is closed', () => { | ||
| expect( | ||
| resolveMarkdownStreamdownMode(false, '1. example:\n ```ts\n const x = 1;\n ```\n') | ||
| ).toBe('static'); | ||
| }); | ||
|
|
||
| it('does not treat a 4-space indent as a fence', () => { | ||
| expect(resolveMarkdownStreamdownMode(false, ' ```ts\n const x = 1;\n')).toBe('static'); | ||
| }); | ||
|
|
||
| it('keeps streaming mode when a shorter closer cannot end a longer fence', () => { | ||
| expect(resolveMarkdownStreamdownMode(false, '````ts\nconst x = 1;\n```\n')).toBe('streaming'); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.