|
| 1 | +import * as React from 'react'; |
| 2 | +import { MessageBox, ChatbotWelcomePrompt } from '@patternfly/chatbot'; |
| 3 | +import { ChatbotSourceSettings, MCPServerFromAPI, TokenInfo } from '~/app/types'; |
| 4 | +import { ServerStatusInfo } from '~/app/hooks/useMCPServerStatuses'; |
| 5 | +import useChatbotMessages, { UseChatbotMessagesReturn } from './hooks/useChatbotMessages'; |
| 6 | +import { |
| 7 | + useChatbotConfigStore, |
| 8 | + selectSystemInstruction, |
| 9 | + selectTemperature, |
| 10 | + selectStreamingEnabled, |
| 11 | + selectSelectedModel, |
| 12 | + selectSelectedMcpServerIds, |
| 13 | +} from './store'; |
| 14 | +import { ChatbotMessages } from './ChatbotMessagesList'; |
| 15 | +import { sampleWelcomePrompts } from './const'; |
| 16 | + |
| 17 | +interface ChatbotConfigInstanceProps { |
| 18 | + configId: string; |
| 19 | + username?: string; |
| 20 | + selectedSourceSettings: ChatbotSourceSettings | null; |
| 21 | + isRawUploaded: boolean; |
| 22 | + currentVectorStoreId: string | null; |
| 23 | + mcpServers: MCPServerFromAPI[]; |
| 24 | + mcpServerStatuses: Map<string, ServerStatusInfo>; |
| 25 | + mcpServerTokens: Map<string, TokenInfo>; |
| 26 | + namespace?: string; |
| 27 | + showWelcomePrompt?: boolean; |
| 28 | + onMessagesHookReady?: (hook: UseChatbotMessagesReturn) => void; |
| 29 | +} |
| 30 | + |
| 31 | +export const ChatbotConfigInstance: React.FC<ChatbotConfigInstanceProps> = ({ |
| 32 | + configId, |
| 33 | + username, |
| 34 | + selectedSourceSettings, |
| 35 | + isRawUploaded, |
| 36 | + currentVectorStoreId, |
| 37 | + mcpServers, |
| 38 | + mcpServerStatuses, |
| 39 | + mcpServerTokens, |
| 40 | + namespace, |
| 41 | + showWelcomePrompt = false, |
| 42 | + onMessagesHookReady, |
| 43 | +}) => { |
| 44 | + const systemInstruction = useChatbotConfigStore(selectSystemInstruction(configId)); |
| 45 | + const temperature = useChatbotConfigStore(selectTemperature(configId)); |
| 46 | + const isStreamingEnabled = useChatbotConfigStore(selectStreamingEnabled(configId)); |
| 47 | + const selectedModel = useChatbotConfigStore(selectSelectedModel(configId)); |
| 48 | + const selectedMcpServerIds = useChatbotConfigStore(selectSelectedMcpServerIds(configId)); |
| 49 | + |
| 50 | + const getToolSelections = React.useCallback( |
| 51 | + (namespaceName: string, serverUrl: string) => |
| 52 | + useChatbotConfigStore.getState().getToolSelections(configId, namespaceName, serverUrl), |
| 53 | + [configId], |
| 54 | + ); |
| 55 | + |
| 56 | + const messagesHook = useChatbotMessages({ |
| 57 | + modelId: selectedModel, |
| 58 | + selectedSourceSettings, |
| 59 | + systemInstruction, |
| 60 | + isRawUploaded, |
| 61 | + username, |
| 62 | + isStreamingEnabled, |
| 63 | + temperature, |
| 64 | + currentVectorStoreId, |
| 65 | + selectedServerIds: selectedMcpServerIds, |
| 66 | + mcpServers, |
| 67 | + mcpServerStatuses, |
| 68 | + mcpServerTokens, |
| 69 | + toolSelections: getToolSelections, |
| 70 | + namespace, |
| 71 | + }); |
| 72 | + |
| 73 | + // Expose the messages hook to parent and update when it changes |
| 74 | + React.useEffect(() => { |
| 75 | + if (onMessagesHookReady) { |
| 76 | + onMessagesHookReady(messagesHook); |
| 77 | + } |
| 78 | + }, [messagesHook, onMessagesHookReady]); |
| 79 | + |
| 80 | + return ( |
| 81 | + <MessageBox position="top"> |
| 82 | + {showWelcomePrompt && ( |
| 83 | + <ChatbotWelcomePrompt |
| 84 | + title={username ? `Hello, ${username}` : 'Hello'} |
| 85 | + description="Welcome to the playground" |
| 86 | + data-testid="chatbot-welcome-prompt" |
| 87 | + style={{ |
| 88 | + cursor: 'default', |
| 89 | + pointerEvents: 'none', |
| 90 | + }} |
| 91 | + prompts={sampleWelcomePrompts} |
| 92 | + /> |
| 93 | + )} |
| 94 | + <ChatbotMessages |
| 95 | + messageList={messagesHook.messages} |
| 96 | + scrollRef={messagesHook.scrollToBottomRef} |
| 97 | + isLoading={messagesHook.isLoading} |
| 98 | + isStreamingWithoutContent={messagesHook.isStreamingWithoutContent} |
| 99 | + /> |
| 100 | + </MessageBox> |
| 101 | + ); |
| 102 | +}; |
0 commit comments