-
Notifications
You must be signed in to change notification settings - Fork 916
fix: reduce streaming stutter in the web chat #1085
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
06b94dd
fix(web): coalesce streaming token updates into one render per frame
wbxl2000 a6b0a1e
fix(server): disable Nagle on WebSocket socket for lower streaming la…
wbxl2000 17e8f1b
fix(web): flush pending streaming deltas before re-subscribing
wbxl2000 35726c7
fix(web): flush pending streaming deltas before forgetting a session
wbxl2000 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 @@ | ||
| --- | ||
| "@moonshot-ai/server": patch | ||
| --- | ||
|
|
||
| Reduce streaming latency by disabling Nagle's algorithm on WebSocket connections. |
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 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-web": patch | ||
| --- | ||
|
|
||
| Fix stuttery streaming in the web chat by coalescing rapid token updates into a single render per frame. |
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,80 @@ | ||
| // apps/kimi-web/src/composables/client/eventBatcher.ts | ||
| // Coalesce high-frequency streaming events onto the next animation frame. | ||
| // | ||
| // Pure logic (no Vue, no DOM) so it is unit-testable in isolation. See | ||
| // useKimiWebClient.ts for where it is wired into the WS event pipeline. | ||
|
|
||
| import type { AppEvent } from '../../api/types'; | ||
|
|
||
| // Events that merely append a chunk to something already streaming. They can | ||
| // arrive dozens to hundreds of times per second, so they are worth coalescing. | ||
| const RENDER_EVENT_TYPES: ReadonlySet<AppEvent['type']> = new Set<AppEvent['type']>([ | ||
| 'assistantDelta', | ||
| 'agentDelta', | ||
| 'toolOutput', | ||
| 'taskProgress', | ||
| ]); | ||
|
|
||
| /** True for high-frequency render-only events that are safe to delay to the | ||
| next animation frame. Everything else (lifecycle / control-flow) must apply | ||
| immediately so turn-end cleanup etc. is not delayed by a throttled rAF. */ | ||
| export function isRenderEvent(appEvent: AppEvent): boolean { | ||
| return RENDER_EVENT_TYPES.has(appEvent.type); | ||
| } | ||
|
|
||
| function defaultScheduleFrame(cb: () => void): number { | ||
| return typeof requestAnimationFrame === 'function' | ||
| ? requestAnimationFrame(cb) | ||
| : (setTimeout(cb, 16) as unknown as number); | ||
| } | ||
|
|
||
| /** | ||
| * Coalesce batchable items onto a single scheduled callback, while applying | ||
| * non-batchable items immediately. | ||
| * | ||
| * A non-batchable item first drains any pending batchable items (in arrival | ||
| * order) so overall ordering is preserved — a lifecycle event never overtakes | ||
| * the deltas that arrived before it. | ||
| * | ||
| * The returned handle is itself callable (enqueue) and also exposes `flush()` | ||
| * to synchronously drain pending batchable items. Callers that replace state | ||
| * authoritatively (e.g. applying a server snapshot) must `flush()` first so | ||
| * stale queued deltas are not applied on top of the new state. | ||
| */ | ||
| export interface EventBatcher<T> { | ||
| (item: T): void; | ||
| /** Synchronously drain any pending batchable items in arrival order. */ | ||
| flush(): void; | ||
| } | ||
|
|
||
| export function createEventBatcher<T>( | ||
| process: (item: T) => void, | ||
| isBatchable: (item: T) => boolean, | ||
| schedule: (cb: () => void) => number = defaultScheduleFrame, | ||
| ): EventBatcher<T> { | ||
| let pending: T[] = []; | ||
| let handle: number | null = null; | ||
|
|
||
| const drain = (): void => { | ||
| handle = null; | ||
| if (pending.length === 0) return; | ||
| const batch = pending; | ||
| pending = []; | ||
| for (const item of batch) process(item); | ||
| }; | ||
|
|
||
| const enqueue = ((item: T) => { | ||
| if (isBatchable(item)) { | ||
| pending.push(item); | ||
| if (handle === null) handle = schedule(drain); | ||
| return; | ||
| } | ||
| // Immediate item: flush pending batchables first to preserve order. | ||
| drain(); | ||
| process(item); | ||
| }) as EventBatcher<T>; | ||
|
|
||
| enqueue.flush = drain; | ||
|
|
||
| return enqueue; | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a user archives a streaming session and the archive response returns before the next animation frame, this newly queued
assistantDelta/toolOutputcan survivearchiveSession -> forgetSession, which unsubscribes and deletes the per-session maps. The later rAF drain still callsreduceAppEventfor that session, recreating entries such asmessagesBySession[sessionId]/lastSeqBySessionafter teardown; if that id is fetched again,hasLoadedMessages()can then treat the stale/empty cache as authoritative and skip the snapshot. Please flush or discard pending events for the session before clearing it.Useful? React with 👍 / 👎.