From abcdb68852e79a71847ab9616f806eb20471a9c9 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:14:09 -0800 Subject: [PATCH] Tweak: adjust chat timing for streaming --- .../chat/chat/chat.component.spec.ts | 56 +++++++++++++++++++ .../components/chat/chat/chat.component.ts | 42 ++++++++++---- 2 files changed, 87 insertions(+), 11 deletions(-) diff --git a/src-ui/src/app/components/chat/chat/chat.component.spec.ts b/src-ui/src/app/components/chat/chat/chat.component.spec.ts index b38cef7efa..5c6ec39e52 100644 --- a/src-ui/src/app/components/chat/chat/chat.component.spec.ts +++ b/src-ui/src/app/components/chat/chat/chat.component.spec.ts @@ -188,6 +188,62 @@ describe('ChatComponent', () => { expect(component.loading()).toBe(false) }) + it('should reveal a complete answer within one second', () => { + component.input.set('Hello') + component.sendMessage() + + const answer = 'A'.repeat(660) + mockStream$.next(answer) + mockStream$.complete() + + expect(component.messages()[1].content.length).toBeGreaterThan(0) + expect(component.messages()[1].content.length).toBeLessThan(answer.length) + expect(component.loading()).toBe(true) + + jest.advanceTimersByTime(800) + expect(component.messages()[1].content).toBe(answer) + expect(component.loading()).toBe(false) + }) + + it('should give a long answer more time to reveal', () => { + component.input.set('Hello') + component.sendMessage() + + const answer = 'A'.repeat(3000) + mockStream$.next(answer) + mockStream$.complete() + + jest.advanceTimersByTime(800) + expect(component.messages()[1].content.length).toBeLessThan(answer.length) + expect(component.loading()).toBe(true) + + jest.advanceTimersByTime(2500) + expect(component.messages()[1].content).toBe(answer) + expect(component.loading()).toBe(false) + }) + + it('should reveal fast incoming chunks without making them pop in', () => { + component.input.set('Hello') + component.sendMessage() + + let answer = '' + for (let i = 0; i < 20; i++) { + answer += 'A'.repeat(100) + const visibleBefore = component.messages()[1].content.length + mockStream$.next(answer) + jest.advanceTimersByTime(10) + expect( + component.messages()[1].content.length - visibleBefore + ).toBeLessThan(100) + } + expect(component.messages()[1].content.length).toBeLessThan(answer.length) + + mockStream$.complete() + jest.advanceTimersByTime(5200) + expect(component.messages()[1].content).toBe(answer) + expect(component.loading()).toBe(false) + }) + it('should show a slow response message while waiting', () => { component.input.set('Hello') component.sendMessage() diff --git a/src-ui/src/app/components/chat/chat/chat.component.ts b/src-ui/src/app/components/chat/chat/chat.component.ts index cdd3f8473d..cfa0855696 100644 --- a/src-ui/src/app/components/chat/chat/chat.component.ts +++ b/src-ui/src/app/components/chat/chat/chat.component.ts @@ -21,6 +21,9 @@ import { import { pngxPopperOptions } from 'src/app/utils/popper-options' const SLOW_RESPONSE_DELAY_MS = 8000 +const TYPEWRITER_MIN_DURATION_MS = 750 +const TYPEWRITER_MAX_DURATION_MS = 3000 +const TYPEWRITER_INTERVAL_MS = 16 @Component({ selector: 'pngx-chat', @@ -73,6 +76,7 @@ export class ChatComponent implements OnInit { private typewriterBuffer: string[] = [] private typewriterActive = false private typewriterTimeout: ReturnType + private typewriterDeadline = 0 private streamSubscription: Subscription private streamingMessage: ChatMessage private streamFinished = false @@ -134,7 +138,7 @@ export class ChatComponent implements OnInit { if (nextResponse.content.length < lastVisibleContent.length) { this.resetTypewriter(assistantMessage, nextResponse.content) lastVisibleContent = nextResponse.content - } else { + } else if (nextResponse.content.length > lastVisibleContent.length) { const visibleDelta = nextResponse.content.substring( lastVisibleContent.length ) @@ -214,6 +218,12 @@ export class ChatComponent implements OnInit { if (!chunk) return this.typewriterBuffer.push(...chunk.split('')) + // Longer bursts need more time to remain readable as the chat scrolls. + const revealDuration = Math.min( + TYPEWRITER_MAX_DURATION_MS, + Math.max(TYPEWRITER_MIN_DURATION_MS, this.typewriterBuffer.length) + ) + this.typewriterDeadline = Date.now() + revealDuration if (!this.typewriterActive) { this.typewriterActive = true @@ -222,19 +232,29 @@ export class ChatComponent implements OnInit { } playTypewriter(message: ChatMessage): void { - if (this.typewriterBuffer.length === 0) { - this.typewriterActive = false - if (this.streamFinished) this.finishStreaming(message) - return - } - - const nextChar = this.typewriterBuffer.shift() - message.content += nextChar + // Spread the queued text across the updates left before the deadline + const remainingUpdates = Math.max( + 1, + Math.ceil((this.typewriterDeadline - Date.now()) / TYPEWRITER_INTERVAL_MS) + ) + const charactersToShow = Math.ceil( + this.typewriterBuffer.length / remainingUpdates + ) + message.content += this.typewriterBuffer + .splice(0, charactersToShow) + .join('') this.notifyMessagesChanged() this.scrollToBottom() - // 10ms per character - this.typewriterTimeout = setTimeout(() => this.playTypewriter(message), 10) + if (this.typewriterBuffer.length === 0) { + this.typewriterActive = false + if (this.streamFinished) this.finishStreaming(message) + } else { + this.typewriterTimeout = setTimeout( + () => this.playTypewriter(message), + TYPEWRITER_INTERVAL_MS + ) + } } private notifyMessagesChanged(): void {