-
Notifications
You must be signed in to change notification settings - Fork 930
fix: reduce streaming stutter in the web chat #1083
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
The head ref may contain hidden characters: "\u67E5\u770B\u5361\u987F\u7684\u539F\u56E0"
Closed
Changes from all commits
Commits
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.
Because
processEventis also what advancesrawState.lastSeqBySession, routing assistant/tool/task deltas through the rAF batcher leaves the app-level cursor stale until the next frame. If the user switches away and back to a still-streaming session before the queued deltas flush,subscribeToSessionEventssends the old seq, and the server replays those same already-queued deltas; when both the original queue and the replay drain, text/tool output is appended twice. Flush before re-subscribing or advance the per-session cursor synchronously when enqueueing a durable event.Useful? React with 👍 / 👎.