🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix: show artifact source while streaming (#15310)
YuTengjing2194b2339072
diff --git a/src/features/Portal/Artifacts/Body/index.test.tsx b/src/features/Portal/Artifacts/Body/index.test.tsxnew file mode 100644index 00000000000..038fbda77e5--- /dev/null+++ b/src/features/Portal/Artifacts/Body/index.test.tsx@@ -0,0 +1,124 @@+import { render, screen } from '@testing-library/react';+import type { CSSProperties, ReactNode } from 'react';+import { beforeEach, describe, expect, it, vi } from 'vitest';++import { ArtifactDisplayMode } from '@/store/chat/slices/portal/initialState';+import { ArtifactType } from '@/types/artifact';++import ArtifactsUI from './index';++const mockArtifactState = vi.hoisted(() => ({+ artifactCodeLanguage: undefined as string | undefined,+ artifactContent: '',+ artifactIdentifier: 'snake-game',+ artifactMessageId: 'message-1',+ artifactType: 'text/html' as string | undefined,+ displayMode: 'preview',+ isArtifactTagClosed: false,+ isMessageGenerating: true,+ setState: vi.fn(),+}));++vi.mock('@lobehub/ui', () => ({+ Flexbox: ({ children, style }: { children: ReactNode; style?: CSSProperties }) => (+ <div data-testid={style?.overflow === 'auto' ? 'artifact-scroll-container' : undefined}>+ {children}+ </div>+ ),+ Highlighter: ({+ animated,+ children,+ language,+ }: {+ animated?: boolean;+ children: ReactNode;+ language?: string;+ }) => (+ <pre+ data-animated={String(Boolean(animated))}+ data-language={language}+ data-testid="artifact-code"+ >+ {children}+ </pre>+ ),+}));++vi.mock('@/store/chat', () => ({+ useChatStore: Object.assign(+ (selector: (state: Record<PropertyKey, unknown>) => unknown) => {+ return selector({ portalArtifactDisplayMode: mockArtifactState.displayMode });+ },+ {+ setState: mockArtifactState.setState,+ },+ ),+}));++vi.mock('@/store/chat/selectors', () => ({+ chatPortalSelectors: {+ artifactCode: () => () => mockArtifactState.artifactContent,+ artifactCodeLanguage: () => mockArtifactState.artifactCodeLanguage,+ artifactIdentifier: () => mockArtifactState.artifactIdentifier,+ artifactMessageId: () => mockArtifactState.artifactMessageId,+ artifactType: () => mockArtifactState.artifactType,+ isArtifactTagClosed: () => () => mockArtifactState.isArtifactTagClosed,+ },+ messageStateSelectors: {+ isMessageGenerating: () => () => mockArtifactState.isMessageGenerating,+ },+}));++vi.mock('./Renderer', () => ({+ default: ({ animated, content }: { animated?: boolean; content: string; type?: string }) => (+ <div data-animated={String(Boolean(animated))} data-testid="artifact-preview">+ {content}+ </div>+ ),+}));++describe('ArtifactsUI', () => {+ beforeEach(() => {+ mockArtifactState.artifactCodeLanguage = undefined;+ mockArtifactState.artifactContent = '<!doctype html><html><body><script>';+ mockArtifactState.artifactIdentifier = 'snake-game';+ mockArtifactState.artifactMessageId = 'message-1';+ mockArtifactState.artifactType = 'text/html';+ mockArtifactState.displayMode = ArtifactDisplayMode.Preview;+ mockArtifactState.isArtifactTagClosed = false;+ mockArtifactState.isMessageGenerating = true;+ mockArtifactState.setState.mockClear();+ });++ it('shows scrollable source while an HTML artifact is still streaming', () => {+ render(<ArtifactsUI />);++ expect(screen.getByTestId('artifact-code')).toHaveTextContent('<script>');+ expect(screen.getByTestId('artifact-code')).toHaveAttribute('data-animated', 'true');+ expect(screen.getByTestId('artifact-code')).toHaveAttribute('data-language', 'html');+ expect(screen.getByTestId('artifact-scroll-container')).toBeDefined();+ expect(screen.queryByTestId('artifact-preview')).toBeNull();+ expect(mockArtifactState.setState).not.toHaveBeenCalled();+ });++ it('renders the final preview after the artifact tag closes', () => {+ mockArtifactState.artifactContent = '<!doctype html><html><body>Done</body></html>';+ mockArtifactState.isArtifactTagClosed = true;++ render(<ArtifactsUI />);++ expect(screen.getByTestId('artifact-preview')).toHaveTextContent('Done');+ expect(screen.queryByTestId('artifact-code')).toBeNull();+ });++ it('keeps explicit code artifacts in source mode after they close', () => {+ mockArtifactState.artifactContent = 'console.log("done");';+ mockArtifactState.artifactType = ArtifactType.Code;+ mockArtifactState.isArtifactTagClosed = true;++ render(<ArtifactsUI />);++ expect(screen.getByTestId('artifact-code')).toHaveTextContent('console.log("done");');+ expect(screen.queryByTestId('artifact-preview')).toBeNull();+ });+});diff --git a/src/features/Portal/Artifacts/Body/index.tsx b/src/features/Portal/Artifacts/Body/index.tsxindex d957b9e805f..f04caea57e5 100644--- a/src/features/Portal/Artifacts/Body/index.tsx+++ b/src/features/Portal/Artifacts/Body/index.tsx@@ -8,8 +8,6 @@ import { ArtifactType } from '@/types/artifact';import Renderer from './Renderer';-const TEXT_HTML_ARTIFACT_TYPE = 'text/html';-const ArtifactsUI = memo(() => {const [messageId,@@ -34,21 +32,12 @@ const ArtifactsUI = memo(() => {];});- const isHtmlArtifact =- !artifactType ||- artifactType === ArtifactType.Default ||- artifactType === TEXT_HTML_ARTIFACT_TYPE;-useEffect(() => {- // Prefer live preview while HTML artifacts stream; reset stale code mode from previous artifacts.- if (- isMessageGenerating &&- displayMode === ArtifactDisplayMode.Code &&- (isArtifactTagClosed || (isHtmlArtifact && !isArtifactTagClosed))- ) {+ // When generation completes, switch from the live source stream to the final preview.+ if (isMessageGenerating && displayMode === ArtifactDisplayMode.Code && isArtifactTagClosed) {useChatStore.setState({ portalArtifactDisplayMode: ArtifactDisplayMode.Preview });}- }, [isMessageGenerating, displayMode, isArtifactTagClosed, isHtmlArtifact]);+ }, [isMessageGenerating, displayMode, isArtifactTagClosed]);const language = useMemo(() => {switch (artifactType) {@@ -70,11 +59,11 @@ const ArtifactsUI = memo(() => {}}, [artifactType, artifactCodeLanguage]);- // Keep incomplete non-HTML artifacts in code mode, but let HTML stream into the preview.+ // Keep incomplete artifacts in code mode so users can inspect and scroll the generated source.const showCode =artifactType === ArtifactType.Code ||- (!isArtifactTagClosed && !isHtmlArtifact) ||- (displayMode === ArtifactDisplayMode.Code && !(isHtmlArtifact && !isArtifactTagClosed));+ !isArtifactTagClosed ||+ displayMode === ArtifactDisplayMode.Code;const isStreamingCode = isMessageGenerating && showCode && !isArtifactTagClosed;const isStreamingArtifact = isMessageGenerating && !isArtifactTagClosed;