-
Notifications
You must be signed in to change notification settings - Fork 3.1k
perf(ui): render streamed markdown incrementally #11038
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
70f9441
perf(ui): render streamed markdown incrementally
marius-kilocode fe1ac4b
test(ui): declare markdown DOM test dependency
marius-kilocode 9c490cf
test(ui): run markdown DOM coverage in browser
marius-kilocode d363965
test(ui): restore incremental markdown coverage
marius-kilocode 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "kilo-code": patch | ||
| --- | ||
|
|
||
| Speed up long streamed responses by preserving completed Markdown blocks and updating only the active tail. |
208 changes: 208 additions & 0 deletions
208
packages/kilo-vscode/tests/markdown-incremental-dom.spec.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 |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| import { expect, test } from "@playwright/test" | ||
| import { build } from "esbuild" | ||
| import { fileURLToPath } from "node:url" | ||
|
|
||
| const source = fileURLToPath(new URL("../../ui/src/kilocode/markdown-incremental-dom.ts", import.meta.url)) | ||
| const bundle = await build({ | ||
| stdin: { | ||
| contents: ` | ||
| import { createIncrementalMarkdown } from ${JSON.stringify(source)} | ||
| globalThis.createIncrementalMarkdown = createIncrementalMarkdown | ||
| `, | ||
| resolveDir: fileURLToPath(new URL(".", import.meta.url)), | ||
| }, | ||
| bundle: true, | ||
| format: "iife", | ||
| platform: "browser", | ||
| write: false, | ||
| }) | ||
| const script = bundle.outputFiles[0]!.text | ||
|
|
||
| test.beforeEach(async ({ page }) => { | ||
| await page.goto("about:blank") | ||
| await page.addScriptTag({ content: script }) | ||
| }) | ||
|
|
||
| test("keeps completed Markdown nodes while replacing only the live tail", async ({ page }) => { | ||
| const result = await page.evaluate(() => { | ||
| const create = ( | ||
| globalThis as typeof globalThis & { | ||
| createIncrementalMarkdown: (decorate: () => void) => { | ||
| update: ( | ||
| container: HTMLDivElement, | ||
| blocks: Array<{ key: string; hash: string; html: string; mode: "full" | "live" }>, | ||
| labels: { copy: string; copied: string }, | ||
| ) => boolean | ||
| } | ||
| } | ||
| ).createIncrementalMarkdown | ||
| const labels = { copy: "Copy", copied: "Copied" } | ||
| const container = document.createElement("div") | ||
| document.body.append(container) | ||
| const renderer = create(() => {}) | ||
| const heading = { key: "0", hash: "heading", html: "<h2>Heading</h2>", mode: "full" as const } | ||
| const tail = { key: "1", hash: "tail-a", html: "<p>Tail A</p>", mode: "live" as const } | ||
|
|
||
| renderer.update(container, [heading, tail], labels) | ||
| const stable = container.children[0] | ||
| const previous = container.children[1] | ||
| renderer.update(container, [heading, { ...tail, hash: "tail-b", html: "<p>Tail B</p>" }], labels) | ||
|
|
||
| return { | ||
| stable: container.children[0] === stable, | ||
| replaced: container.children[1] !== previous, | ||
| tags: Array.from(container.children).map((child) => child.tagName), | ||
| text: container.textContent, | ||
| } | ||
| }) | ||
|
|
||
| expect(result).toEqual({ stable: true, replaced: true, tags: ["H2", "P"], text: "HeadingTail B" }) | ||
| }) | ||
|
|
||
| test("promotes an unchanged tail and appends the next block without wrappers", async ({ page }) => { | ||
| const result = await page.evaluate(() => { | ||
| const create = ( | ||
| globalThis as typeof globalThis & { | ||
| createIncrementalMarkdown: (decorate: () => void) => { | ||
| update: ( | ||
| container: HTMLDivElement, | ||
| blocks: Array<{ key: string; hash: string; html: string; mode: "full" | "live" }>, | ||
| labels: { copy: string; copied: string }, | ||
| ) => boolean | ||
| } | ||
| } | ||
| ).createIncrementalMarkdown | ||
| const labels = { copy: "Copy", copied: "Copied" } | ||
| const container = document.createElement("div") | ||
| document.body.append(container) | ||
| const renderer = create(() => {}) | ||
| const heading = { key: "0", hash: "heading", html: "<h2>Heading</h2>", mode: "full" as const } | ||
| const paragraph = { key: "1", hash: "paragraph", html: "<p>Stable</p>", mode: "live" as const } | ||
|
|
||
| renderer.update(container, [heading, paragraph], labels) | ||
| const stable = container.children[1] | ||
| renderer.update( | ||
| container, | ||
| [ | ||
| heading, | ||
| { ...paragraph, mode: "full" }, | ||
| { key: "2", hash: "tail", html: "<ul><li>Next</li></ul>", mode: "live" as const }, | ||
| ], | ||
| labels, | ||
| ) | ||
|
|
||
| return { | ||
| stable: container.children[1] === stable, | ||
| tags: Array.from(container.children).map((child) => child.tagName), | ||
| } | ||
| }) | ||
|
|
||
| expect(result).toEqual({ stable: true, tags: ["H2", "P", "UL"] }) | ||
| }) | ||
|
|
||
| test("runs streaming hooks only when incremental rendering handles the update", async ({ page }) => { | ||
| const result = await page.evaluate(() => { | ||
| const create = ( | ||
| globalThis as typeof globalThis & { | ||
| createIncrementalMarkdown: ( | ||
| decorate: () => void, | ||
| hooks: { | ||
| cancel: () => void | ||
| ready: (container: HTMLDivElement, labels: { copy: string; copied: string }, context: string) => void | ||
| }, | ||
| ) => { | ||
| render: ( | ||
| streaming: boolean, | ||
| container: HTMLDivElement, | ||
| blocks: Array<{ key: string; hash: string; html: string; mode: "full" | "live" }>, | ||
| labels: { copy: string; copied: string }, | ||
| context: string, | ||
| ) => boolean | ||
| } | ||
| } | ||
| ).createIncrementalMarkdown | ||
| const labels = { copy: "Copy", copied: "Copied" } | ||
| const container = document.createElement("div") | ||
| document.body.append(container) | ||
| const calls: string[] = [] | ||
| const renderer = create(() => {}, { | ||
| cancel: () => calls.push("cancel"), | ||
| ready: (_container, _labels, context) => calls.push(context), | ||
| }) | ||
| const stable = { key: "0", hash: "stable", html: "<p>Stable</p>", mode: "full" as const } | ||
| const tail = { key: "1", hash: "tail", html: "<p>Tail</p>", mode: "live" as const } | ||
|
|
||
| const completed = renderer.render(false, container, [stable, tail], labels, "ready") | ||
| const unsupported = renderer.render(true, container, [tail], labels, "unsupported") | ||
| const streaming = renderer.render(true, container, [stable, tail], labels, "ready") | ||
| return { calls, completed, streaming, unsupported } | ||
| }) | ||
|
|
||
| expect(result).toEqual({ calls: ["cancel", "ready"], completed: false, streaming: true, unsupported: false }) | ||
| }) | ||
|
|
||
| test("falls back when there is no stable prefix", async ({ page }) => { | ||
| const result = await page.evaluate(() => { | ||
| const create = ( | ||
| globalThis as typeof globalThis & { | ||
| createIncrementalMarkdown: (decorate: () => void) => { | ||
| update: ( | ||
| container: HTMLDivElement, | ||
| blocks: Array<{ key: string; hash: string; html: string; mode: "full" | "live" }>, | ||
| labels: { copy: string; copied: string }, | ||
| ) => boolean | ||
| } | ||
| } | ||
| ).createIncrementalMarkdown | ||
| const labels = { copy: "Copy", copied: "Copied" } | ||
| const container = document.createElement("div") | ||
| const renderer = create(() => {}) | ||
| const first = { key: "0", hash: "first", html: "<p>First</p>", mode: "live" as const } | ||
| const second = { key: "1", hash: "second", html: "<p>Second</p>", mode: "live" as const } | ||
|
|
||
| return { | ||
| multiple: renderer.update(container, [first, second], labels), | ||
| single: renderer.update(container, [first], labels), | ||
| } | ||
| }) | ||
|
|
||
| expect(result).toEqual({ multiple: false, single: false }) | ||
| }) | ||
|
|
||
| test("rebuilds safely when a Markdown boundary disappears", async ({ page }) => { | ||
| const result = await page.evaluate(() => { | ||
| const create = ( | ||
| globalThis as typeof globalThis & { | ||
| createIncrementalMarkdown: (decorate: () => void) => { | ||
| update: ( | ||
| container: HTMLDivElement, | ||
| blocks: Array<{ key: string; hash: string; html: string; mode: "full" | "live" }>, | ||
| labels: { copy: string; copied: string }, | ||
| ) => boolean | ||
| } | ||
| } | ||
| ).createIncrementalMarkdown | ||
| const labels = { copy: "Copy", copied: "Copied" } | ||
| const container = document.createElement("div") | ||
| document.body.append(container) | ||
| const renderer = create(() => {}) | ||
| const blocks = [ | ||
| { key: "0", hash: "stable", html: "<p>Stable</p>", mode: "full" as const }, | ||
| { key: "1", hash: "middle", html: "<p>Middle</p>", mode: "full" as const }, | ||
| { key: "2", hash: "tail", html: "<p>Tail</p>", mode: "live" as const }, | ||
| ] | ||
|
|
||
| renderer.update(container, blocks, labels) | ||
| const comments = Array.from(container.childNodes).filter((node) => node.nodeType === Node.COMMENT_NODE) | ||
| comments.at(-1)?.remove() | ||
| const updated = renderer.update(container, [blocks[0]!, { ...blocks[1]!, mode: "live" }], labels) | ||
|
|
||
| return { | ||
| updated, | ||
| tags: Array.from(container.children).map((child) => child.tagName), | ||
| text: container.textContent, | ||
| } | ||
| }) | ||
|
|
||
| expect(result).toEqual({ updated: true, tags: ["P", "P"], text: "StableMiddle" }) | ||
| }) | ||
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
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.
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.