Skip to content

Commit fb65cf6

Browse files
authored
Remove throttling of AI answer after 5 seconds (#2315)
* Remove throttling of AI answer after 5 seconds * Apply suggestion from @reakaleek
1 parent 86ed9ed commit fb65cf6

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/AskAi/MessageThrottler.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
/**
22
* Message throttler for controlling the speed of streaming updates.
33
* Non-React version that can be used in Zustand stores or other non-component code.
4+
*
5+
* Automatically speeds up after a configurable duration to drain buffers faster.
46
*/
57

68
const DEFAULT_THROTTLE_DELAY_MS = 25
9+
const DEFAULT_FAST_DELAY_MS = 0
10+
const DEFAULT_SPEEDUP_AFTER_MS = 5_000
711

812
export interface MessageThrottlerOptions<T> {
913
delayInMs?: number
14+
fastDelayInMs?: number
15+
speedupAfterMs?: number
1016
onMessage: (message: T) => void
1117
}
1218

@@ -15,16 +21,42 @@ export class MessageThrottler<T> {
1521
private timer: ReturnType<typeof setTimeout> | null = null
1622
private isProcessing = false
1723
private delayInMs: number
24+
private fastDelayInMs: number
25+
private speedupAfterMs: number
26+
private startTime: number | null = null
1827
private onMessage: (message: T) => void
1928

2029
constructor({
2130
delayInMs = DEFAULT_THROTTLE_DELAY_MS,
31+
fastDelayInMs = DEFAULT_FAST_DELAY_MS,
32+
speedupAfterMs = DEFAULT_SPEEDUP_AFTER_MS,
2233
onMessage,
2334
}: MessageThrottlerOptions<T>) {
2435
this.delayInMs = delayInMs
36+
this.fastDelayInMs = fastDelayInMs
37+
this.speedupAfterMs = speedupAfterMs
2538
this.onMessage = onMessage
2639
}
2740

41+
/**
42+
* Start the speedup timer. Call this when the first meaningful content arrives.
43+
*/
44+
startSpeedupTimer(): void {
45+
if (this.startTime === null) {
46+
this.startTime = Date.now()
47+
}
48+
}
49+
50+
private getCurrentDelay(): number {
51+
if (this.startTime === null) {
52+
return this.delayInMs
53+
}
54+
const elapsed = Date.now() - this.startTime
55+
return elapsed >= this.speedupAfterMs
56+
? this.fastDelayInMs
57+
: this.delayInMs
58+
}
59+
2860
private processNext = () => {
2961
if (this.queue.length === 0) {
3062
this.isProcessing = false
@@ -36,7 +68,7 @@ export class MessageThrottler<T> {
3668
this.onMessage(nextMessage)
3769

3870
if (this.queue.length > 0) {
39-
this.timer = setTimeout(this.processNext, this.delayInMs)
71+
this.timer = setTimeout(this.processNext, this.getCurrentDelay())
4072
} else {
4173
this.isProcessing = false
4274
this.timer = null
@@ -47,7 +79,7 @@ export class MessageThrottler<T> {
4779
this.queue.push(message)
4880
if (!this.isProcessing) {
4981
this.isProcessing = true
50-
this.timer = setTimeout(this.processNext, this.delayInMs)
82+
this.timer = setTimeout(this.processNext, this.getCurrentDelay())
5183
}
5284
}
5385

src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/AskAi/chat.store.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export interface ChatMessage {
3232
}
3333

3434
const API_ENDPOINT = '/docs/_api/v1/ask-ai/stream'
35-
const MESSAGE_THROTTLE_MS = 25
3635

3736
interface ActiveStream {
3837
controller: AbortController
@@ -244,8 +243,8 @@ async function startStream(
244243
const controller = new AbortController()
245244

246245
// Create a throttler for this stream to control update frequency
246+
// Throttler automatically speeds up after 10 seconds to drain buffers faster
247247
const throttler = new MessageThrottler<AskAiEvent>({
248-
delayInMs: MESSAGE_THROTTLE_MS,
249248
onMessage: (event) => handleStreamEvent(messageId, event),
250249
})
251250

@@ -368,6 +367,8 @@ function handleStreamEvent(messageId: string, event: AskAiEvent): void {
368367
if (event.type === 'conversation_start' && event.conversationId) {
369368
actions.setConversationId(event.conversationId)
370369
} else if (event.type === 'message_chunk') {
370+
// Start speedup timer on first content chunk
371+
stream.throttler.startSpeedupTimer()
371372
stream.content += event.content
372373
actions.updateAiMessage(messageId, stream.content, 'streaming')
373374
} else if (event.type === 'error') {

0 commit comments

Comments
 (0)