import * as React from 'react'; import { useInjectable, useUpdateOnEvent } from '@opensumi/ide-core-browser'; import { Icon, Tooltip } from '@opensumi/ide-core-browser/lib/components'; import { withPrevented } from '@opensumi/ide-core-browser/lib/dom/event'; import { ActionSourceEnum, ActionTypeEnum, ChatFeatureRegistryToken, ChatRenderRegistryToken, ChatServiceToken, localize, } from '@opensumi/ide-core-common'; import { isMarkdownString } from '@opensumi/monaco-editor-core/esm/vs/base/common/htmlContent'; import 'react-chat-elements/dist/main.css'; import { IChatAgentService, ISampleQuestions } from '../../common'; import { ChatService } from '../chat/chat.api.service'; import { ChatFeatureRegistry } from '../chat/chat.feature.registry'; import { ChatRenderRegistry } from '../chat/chat.render.registry'; import { ChatMarkdown } from '../components/ChatMarkdown'; import { ChatThinking } from '../components/ChatThinking'; import { extractIcon } from '../components/utils'; import styles from './components.module.less'; export const WelcomeMessage = () => { const aiChatService = useInjectable(ChatServiceToken); const chatAgentService = useInjectable(IChatAgentService); const chatFeatureRegistry = useInjectable(ChatFeatureRegistryToken); const chatRenderRegistry = useInjectable(ChatRenderRegistryToken); const [sampleQuestions, setSampleQuestions] = React.useState([]); useUpdateOnEvent(chatFeatureRegistry.onDidWelcomeMessageChange); const welcomeSampleQuestions = React.useMemo(() => { if (!chatFeatureRegistry.chatWelcomeMessageModel) { return []; } const { sampleQuestions = [] } = chatFeatureRegistry.chatWelcomeMessageModel; return sampleQuestions.map(extractIcon); }, [chatFeatureRegistry.chatWelcomeMessageModel?.sampleQuestions]); const welcomeMessage = React.useMemo(() => { if (!chatFeatureRegistry.chatWelcomeMessageModel) { return ''; } const { content } = chatFeatureRegistry.chatWelcomeMessageModel; return content; }, [chatFeatureRegistry.chatWelcomeMessageModel?.content]); React.useEffect(() => { const disposer = chatAgentService.onDidChangeAgents(async () => { const sampleQuestions = await chatAgentService.getAllSampleQuestions(); const lists = sampleQuestions.map(extractIcon); setSampleQuestions(lists); }); return () => disposer.dispose(); }, []); if (!welcomeMessage) { return ; } const allSampleQuestions = React.useMemo( () => welcomeSampleQuestions.concat(sampleQuestions), [welcomeSampleQuestions, sampleQuestions], ); const welcomeRender = React.useMemo(() => { if (chatRenderRegistry.chatWelcomeRender) { const Render = chatRenderRegistry.chatWelcomeRender; return ; } return (
{isMarkdownString(welcomeMessage) ? : welcomeMessage}
); }, [chatRenderRegistry.chatWelcomeRender, welcomeMessage, allSampleQuestions]); return welcomeRender as React.JSX.Element; };