import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { useInjectable } from '@opensumi/ide-core-browser'; import { EnhanceIcon, Thumbs } from '@opensumi/ide-core-browser/lib/components/ai-native'; import { Progress } from '@opensumi/ide-core-browser/lib/progress/progress-bar'; import { ChatRenderRegistryToken, isUndefined, localize } from '@opensumi/ide-core-common'; import { IChatInternalService } from '../../common/index'; import { ChatInternalService } from '../chat/chat.internal.service'; import { ChatRenderRegistry } from '../chat/chat.render.registry'; import styles from './components.module.less'; interface ITinkingProps { children?: React.ReactNode | string | React.ReactNode[]; hasMessage?: boolean; message?: string; onRegenerate?: () => void; requestId?: string; thinkingText?: string; showRegenerate?: boolean; } export const ChatThinking = (props: ITinkingProps) => { const { children, message, thinkingText } = props; const chatRenderRegistry = useInjectable(ChatRenderRegistryToken); const CustomThinkingRender = useMemo( () => chatRenderRegistry.chatThinkingRender, [chatRenderRegistry, chatRenderRegistry.chatThinkingRender], ); const isEmptyChildren = useMemo(() => { if (Array.isArray(children)) { return children.length === 0; } return !children; }, [children]); const renderContent = useCallback(() => { if (isEmptyChildren) { if (CustomThinkingRender) { return ; } return {thinkingText || 'Thinking...'}; } return children; }, [message, children, thinkingText, CustomThinkingRender]); return ( <>
{renderContent()}
{!CustomThinkingRender && ( {/* 保持动画效果一致 */} {isEmptyChildren && } )} {/* {showStop && (
{localize('aiNative.operate.stop.title')}
)} */}
); }; export const ChatThinkingResult = ({ children, message, onRegenerate, requestId, hasMessage = true, showRegenerate, }: ITinkingProps) => { const aiChatService = useInjectable(IChatInternalService); const [latestRequestId, setLatestRequestId] = useState(aiChatService.latestRequestId); const chatRenderRegistry = useInjectable(ChatRenderRegistryToken); useEffect(() => { const dispose = aiChatService.onChangeRequestId((id) => { setLatestRequestId(id); }); return () => dispose.dispose(); }, [aiChatService]); const CustomThinkingResultRender = useMemo( () => chatRenderRegistry.chatThinkingResultRender, [chatRenderRegistry, chatRenderRegistry.chatThinkingResultRender], ); const handleRegenerate = useCallback(() => { if (onRegenerate) { onRegenerate(); } }, [onRegenerate]); const renderContent = useCallback(() => { if (typeof hasMessage === 'boolean' ? !hasMessage : !message?.trim()) { const stopimmediately = localize('aiNative.chat.stop.immediately'); if (CustomThinkingResultRender) { return ; } return {stopimmediately}; } return children; }, [message, hasMessage, children]); const isRenderRegenerate = useMemo(() => { if (isUndefined(showRegenerate)) { return latestRequestId === requestId && !!requestId; } return !!showRegenerate; }, [latestRequestId, requestId, showRegenerate]); return ( <>
{renderContent()}
{isRenderRegenerate ? ( {localize('aiNative.operate.afresh.title')} ) : null}
); };