-
Notifications
You must be signed in to change notification settings - Fork 63
Add MessageEntry component for unified chatbot #3160
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
openshift-merge-bot
merged 1 commit into
openshift-assisted:master
from
rawagner:unified_chatbot
Sep 9, 2025
Merged
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,172 @@ | ||
| import * as React from 'react'; | ||
| import { Message as MessageType } from '@redhat-cloud-services/ai-client-state'; | ||
| import { LightSpeedCoreAdditionalProperties } from '@redhat-cloud-services/lightspeed-client'; | ||
| import { Message } from '@patternfly/chatbot'; | ||
| import { saveAs } from 'file-saver'; | ||
| import { Button, Stack, StackItem } from '@patternfly-6/react-core'; | ||
| import { DownloadIcon, ExternalLinkAltIcon } from '@patternfly-6/react-icons'; | ||
|
|
||
| import { isToolArgStreamEvent, isToolResponseStreamEvent, StreamEvent } from './types'; | ||
| import { getToolAction, MsgAction } from './helpers'; | ||
| import FeedbackForm from './FeedbackCard'; | ||
| import { FeedbackRequest } from './BotMessage'; | ||
|
|
||
| export type MessageEntryProps = { | ||
| openClusterDetails: (clusterId: string) => void; | ||
| message: MessageType<LightSpeedCoreAdditionalProperties>; | ||
| avatar: string; | ||
| onApiCall: typeof fetch; | ||
| }; | ||
|
|
||
| const MessageEntry = ({ message, avatar, openClusterDetails, onApiCall }: MessageEntryProps) => { | ||
| const [openFeedback, setOpenFeedback] = React.useState(false); | ||
| const onFeedbackSubmit = React.useCallback( | ||
| async (req: FeedbackRequest): Promise<void> => { | ||
| const resp = await onApiCall('/v1/feedback', { | ||
| method: 'POST', | ||
| body: JSON.stringify({ | ||
| conversation_id: message.additionalAttributes?.conversationId, | ||
| user_question: 'TODO', | ||
| user_feedback: req.userFeedback, | ||
| llm_response: message.answer, | ||
| sentiment: req.sentiment, | ||
| category: req.category, | ||
| }), | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }); | ||
| if (!resp.ok) { | ||
| throw new Error(`${resp.status} ${resp.statusText}`); | ||
| } | ||
| }, | ||
| [onApiCall, message], | ||
| ); | ||
|
|
||
| const messageDate = `${message.date?.toLocaleDateString()} ${message.date?.toLocaleTimeString()}`; | ||
| const isLoading = message.role === 'bot' && message.answer === ''; | ||
|
|
||
| const toolArgs: { [key: string]: { [key: string]: string } } = {}; | ||
| const actions = | ||
| message.role === 'user' || isLoading | ||
| ? [] | ||
| : (message.additionalAttributes?.toolCalls as StreamEvent[])?.reduce<MsgAction[]>( | ||
| (acc, ev) => { | ||
| if (isToolArgStreamEvent(ev)) { | ||
| toolArgs[ev.data.id] = ev.data.token.arguments; | ||
| } else if (isToolResponseStreamEvent(ev)) { | ||
| const action = getToolAction({ | ||
| toolName: ev.data.token.tool_name, | ||
| response: ev.data.token.response, | ||
| args: toolArgs[ev.data.id], | ||
| }); | ||
| if (action) { | ||
| acc.push(action); | ||
| } | ||
| } | ||
| return acc; | ||
| }, | ||
| [], | ||
| ); | ||
|
rawagner marked this conversation as resolved.
|
||
|
|
||
| const feedback = | ||
| message.role === 'user' || isLoading | ||
| ? undefined | ||
| : { | ||
| positive: { | ||
| ariaLabel: 'Good response', | ||
| tooltipContent: 'Good response', | ||
| clickedTooltipContent: 'Feedback sent', | ||
| onClick: () => { | ||
| void onFeedbackSubmit({ | ||
| userFeedback: '', | ||
| sentiment: 1, | ||
| }); | ||
| }, | ||
| }, | ||
|
rawagner marked this conversation as resolved.
|
||
| negative: { | ||
| ariaLabel: 'Bad response', | ||
| tooltipContent: 'Bad response', | ||
| clickedTooltipContent: 'Feedback sent', | ||
| onClick: () => setOpenFeedback(true), | ||
| }, | ||
| copy: { | ||
| isDisabled: !message.answer, | ||
| onClick: () => { | ||
| void navigator.clipboard.writeText(message.answer || ''); | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <Message | ||
| id={`message-${message.id}`} | ||
| // Don't want users to paste MD and display it | ||
| isMarkdownDisabled={message.role === 'user'} | ||
| isLoading={isLoading} | ||
| role={message.role} | ||
| avatar={avatar} | ||
| content={message.answer} | ||
| aria-label={`${message.role === 'user' ? 'Your message' : 'AI response'}: ${ | ||
| message.answer | ||
| }`} | ||
| timestamp={messageDate} | ||
| actions={feedback} | ||
| extraContent={{ | ||
| afterMainContent: ( | ||
| <> | ||
| {actions?.length && ( | ||
| <Stack hasGutter> | ||
| {actions.map(({ title, url, clusterId }, idx) => ( | ||
| <StackItem key={idx}> | ||
| {url && ( | ||
| <Button | ||
| onClick={(e) => { | ||
| e.preventDefault(); | ||
| try { | ||
| saveAs(url); | ||
| } catch (error) { | ||
| // eslint-disable-next-line | ||
| console.error('Download failed: ', error); | ||
| } | ||
| }} | ||
| variant="secondary" | ||
| component="a" | ||
| href={url} | ||
| icon={<DownloadIcon />} | ||
| > | ||
| {title} | ||
| </Button> | ||
| )} | ||
| {clusterId && ( | ||
| <Button | ||
| onClick={() => openClusterDetails(clusterId)} | ||
| variant="secondary" | ||
| icon={<ExternalLinkAltIcon />} | ||
| > | ||
| {title} | ||
| </Button> | ||
| )} | ||
| </StackItem> | ||
| ))} | ||
| </Stack> | ||
| )} | ||
| </> | ||
| ), | ||
| endContent: openFeedback && ( | ||
| <FeedbackForm | ||
| onFeedbackSubmit={async (req: FeedbackRequest) => { | ||
| await onFeedbackSubmit(req); | ||
| setOpenFeedback(false); | ||
| }} | ||
| onClose={() => setOpenFeedback(false)} | ||
| /> | ||
| ), | ||
| }} | ||
| /> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export default MessageEntry; | ||
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
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 |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| export { default as ChatBot } from './components/ChatBot/ChatBot'; | ||
| export type { ChatBotWindowProps } from './components/ChatBot/ChatBotWindow'; | ||
| export { default as MessageEntry } from './components/ChatBot/MessageEntry'; | ||
| export type { MessageEntryProps } from './components/ChatBot/MessageEntry'; |
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.