Skip to content

Commit 5d92861

Browse files
committed
Simplify streaming message
1 parent 986c816 commit 5d92861

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/AskAi/ChatMessage.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,6 @@ interface ChatMessageProps {
6363
onRetry?: () => void
6464
}
6565

66-
const getAccumulatedContent = (messages: AskAiEvent[]) => {
67-
return messages
68-
.filter((m) => m.type === EventTypes.MESSAGE_CHUNK)
69-
.map((m) => m.content)
70-
.join('')
71-
}
72-
7366
const splitContentAndReferences = (
7467
content: string
7568
): { mainContent: string; referencesJson: string | null } => {
@@ -279,9 +272,9 @@ export const ChatMessage = ({
279272
)
280273
}
281274

282-
const content =
283-
streamingContent ||
284-
(events.length > 0 ? getAccumulatedContent(events) : message.content)
275+
// Use streamingContent during streaming, otherwise use message.content from store
276+
// message.content is updated atomically with status when CONVERSATION_END arrives
277+
const content = streamingContent || message.content
285278

286279
const hasError = message.status === 'error' || !!error
287280

src/Elastic.Documentation.Site/Assets/web-components/SearchOrAskAi/AskAi/StreamingAiMessage.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
useConversationId,
77
} from './chat.store'
88
import { useAskAi } from './useAskAi'
9-
import * as React from 'react'
109
import { useEffect, useRef } from 'react'
1110

1211
interface StreamingAiMessageProps {
@@ -45,7 +44,7 @@ export const StreamingAiMessage = ({
4544
'error'
4645
)
4746
} else if (event.type === EventTypes.CONVERSATION_END) {
48-
updateAiMessage(message.id, contentRef.current, 'complete')
47+
updateAiMessage(message.id, message.content || contentRef.current, 'complete')
4948
}
5049
},
5150
onError: (error) => {
@@ -85,15 +84,16 @@ export const StreamingAiMessage = ({
8584
markMessageAsSent,
8685
])
8786

87+
// Always use contentRef.current if it has content (regardless of status)
88+
// This way we don't need to save to message.content and can just use streamingContent
89+
const streamingContentToPass =
90+
isLast && contentRef.current ? contentRef.current : undefined
91+
8892
return (
8993
<ChatMessage
9094
message={message}
9195
events={isLast ? events : []}
92-
streamingContent={
93-
isLast && message.status === 'streaming'
94-
? contentRef.current
95-
: undefined
96-
}
96+
streamingContent={streamingContentToPass}
9797
/>
9898
)
9999
}

0 commit comments

Comments
 (0)