|
| 1 | +import React from 'react'; |
| 2 | +import { LuCircleAlert } from 'react-icons/lu'; |
| 3 | +import { SimpleContextUsage } from '../ai-elements/context'; |
| 4 | +import { |
| 5 | + Conversation, |
| 6 | + ConversationContent, |
| 7 | + ConversationScrollButton, |
| 8 | +} from '../ai-elements/conversation'; |
| 9 | +import { |
| 10 | + PromptInput, |
| 11 | + PromptInputTextarea, |
| 12 | + PromptInputToolbar, |
| 13 | + PromptInputTools, |
| 14 | + PromptInputSubmit, |
| 15 | +} from '../ai-elements/prompt-input'; |
| 16 | +import { Suggestions, Suggestion } from '../ai-elements/suggestion'; |
| 17 | +import { AlertTitle, AlertDescription, Alert } from '../ui/alert'; |
| 18 | +import { AIResponseMessages } from './AIResponseMessages'; |
| 19 | +import { AbstractChat, ChatStatus, LanguageModelUsage, UIMessage } from 'ai'; |
| 20 | +import { useTranslation } from '@i18next-toolkit/react'; |
| 21 | +import { useGlobalConfig } from '@/hooks/useConfig'; |
| 22 | +import { Button } from '../ui/button'; |
| 23 | +import { cn } from '@/utils/style'; |
| 24 | + |
| 25 | +interface AIChatbotProps { |
| 26 | + className?: string; |
| 27 | + messages: UIMessage[]; |
| 28 | + status: ChatStatus; |
| 29 | + error?: Error; |
| 30 | + input: string; |
| 31 | + setInput: (input: string) => void; |
| 32 | + placeholder?: string; |
| 33 | + usage?: LanguageModelUsage; |
| 34 | + isDisabled?: boolean; |
| 35 | + suggestions?: string[]; |
| 36 | + alert?: React.ReactNode; |
| 37 | + selectedContext?: React.ReactNode; |
| 38 | + tools?: React.ReactNode; |
| 39 | + onSubmit?: (e: React.FormEvent<HTMLFormElement>) => void; |
| 40 | + onReset?: () => void; |
| 41 | + onRegenerate?: () => void; |
| 42 | + onSuggestionClick?: (suggestion: string) => void; |
| 43 | + onAddToolResult: ( |
| 44 | + result: Parameters<AbstractChat<UIMessage>['addToolResult']>[0] |
| 45 | + ) => void; |
| 46 | +} |
| 47 | +export const AIChatbot: React.FC<AIChatbotProps> = React.memo((props) => { |
| 48 | + const { t } = useTranslation(); |
| 49 | + const { ai } = useGlobalConfig(); |
| 50 | + |
| 51 | + return ( |
| 52 | + <div className={cn('flex flex-col', props.className)}> |
| 53 | + <Conversation> |
| 54 | + <ConversationContent className="space-y-2"> |
| 55 | + <AIResponseMessages |
| 56 | + messages={props.messages} |
| 57 | + status={props.status} |
| 58 | + onRegenerate={props.onRegenerate} |
| 59 | + onAddToolResult={props.onAddToolResult} |
| 60 | + /> |
| 61 | + </ConversationContent> |
| 62 | + <ConversationScrollButton /> |
| 63 | + </Conversation> |
| 64 | + |
| 65 | + {props.error && ( |
| 66 | + <div className="p-3"> |
| 67 | + <Alert variant="destructive"> |
| 68 | + <LuCircleAlert className="h-4 w-4" /> |
| 69 | + <AlertTitle>{t('An error occurred.')}</AlertTitle> |
| 70 | + <AlertDescription>{String(props.error)}</AlertDescription> |
| 71 | + <div className="mt-2 flex items-center gap-2"> |
| 72 | + <Button size="sm" variant="destructive" onClick={props.onReset}> |
| 73 | + {t('Retry')} |
| 74 | + </Button> |
| 75 | + </div> |
| 76 | + </Alert> |
| 77 | + </div> |
| 78 | + )} |
| 79 | + |
| 80 | + {!props.isDisabled && ( |
| 81 | + <Suggestions className="p-3 pb-0"> |
| 82 | + {(props.suggestions ?? []).map((suggestion) => ( |
| 83 | + <Suggestion |
| 84 | + key={suggestion} |
| 85 | + onClick={props.onSuggestionClick} |
| 86 | + suggestion={suggestion} |
| 87 | + /> |
| 88 | + ))} |
| 89 | + </Suggestions> |
| 90 | + )} |
| 91 | + |
| 92 | + <div className="p-3"> |
| 93 | + {props.alert} |
| 94 | + |
| 95 | + {props.selectedContext} |
| 96 | + |
| 97 | + {props.usage && |
| 98 | + props.status === 'ready' && |
| 99 | + props.messages.length > 0 && ( |
| 100 | + <div className="relative"> |
| 101 | + <div className="absolute -top-10 right-0 px-4 py-1 text-right text-xs text-opacity-40"> |
| 102 | + <SimpleContextUsage |
| 103 | + maxTokens={ai.contextWindow} |
| 104 | + usage={props.usage} |
| 105 | + usedTokens={props.usage.totalTokens ?? 0} |
| 106 | + /> |
| 107 | + </div> |
| 108 | + </div> |
| 109 | + )} |
| 110 | + |
| 111 | + <PromptInput onSubmit={props.onSubmit}> |
| 112 | + <PromptInputTextarea |
| 113 | + value={props.input} |
| 114 | + onChange={(e) => props.setInput(e.target.value)} |
| 115 | + disabled={props.isDisabled} |
| 116 | + placeholder={props.placeholder} |
| 117 | + /> |
| 118 | + <PromptInputToolbar> |
| 119 | + <PromptInputTools>{props.tools}</PromptInputTools> |
| 120 | + <PromptInputSubmit |
| 121 | + disabled={ |
| 122 | + !props.input && |
| 123 | + ['ready', 'submitted', 'error'].includes(props.status) |
| 124 | + } |
| 125 | + status={props.status} |
| 126 | + /> |
| 127 | + </PromptInputToolbar> |
| 128 | + </PromptInput> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + ); |
| 132 | +}); |
| 133 | +AIChatbot.displayName = 'AIChatbot'; |
0 commit comments