import { Component, Fragment, memo, useEffect, useId, useLayoutEffect, useRef, useState } from 'react';
import type { ReactNode } from 'react';
import type { ImageAttachmentRef } from '@deepseek-ai/dsh-attachment';
import type { AssistantBlock, MessageImagesOwnerProps, MessageImageLoader, UserMessageNode } from '@deepseek-ai/dsh-client-ui-conversation/client';
import { JsonBlock, MarkdownText, writeClipboard } from '@deepseek-ai/dsh-client-ui-primitives';
import { ComposerFillContext, McpAppFrame } from './McpAppFrame.js';
import { formatMessageClock } from './message-chrome.js';
import { markdownLabels, truncatedJsonLabel } from './primitive-labels.js';
import type { BlockRenderProps, ReaderBlockOwner } from './types.js';
import { useStreamingText } from './streaming.js';
import { MotionMarkdown } from './word-motion.js';
import { activeTextTransform, displayStreaming, originalAnswerText, readerBlockOwner, useDisplayText } from './text-transform.js';
import type { TextTransformRegistry } from './text-transform.js';
import css from './Reader.module.css';
const IMAGE_TYPES = new Set(['image/png', 'image/jpeg', 'image/webp', 'image/gif']);
export class BlockBoundary extends Component<{ children: ReactNode }, { failed: boolean; detail: string }> {
state = { failed: false, detail: '' };
// Temporary diagnostic: surface the reason inline so a recurrence is reportable
// without a console. Remove once the reported block crash is identified.
static getDerivedStateFromError(error: unknown) {
const err = error as { message?: unknown } | undefined;
return { failed: true, detail: String(err?.message ?? error).slice(0, 160) };
}
render() {
return this.state.failed
?
此内容暂时无法在阅读页显示;原对话中的记录未受影响。({this.state.detail})
: this.props.children;
}
}
type ImageBlockProps = { attachment: ImageAttachmentRef; label?: string } & (
| { loadImage: BlockRenderProps['loadImage']; loadUrl?: never }
| { loadImage?: never; loadUrl: MessageImageLoader }
);
export const ImageBlock = memo(function ImageBlock({ attachment, label = attachment.name, loadImage, loadUrl }: ImageBlockProps) {
const [attempt, setAttempt] = useState(0);
const [url, setUrl] = useState(null);
const [error, setError] = useState(false);
const [decoded, setDecoded] = useState(false);
const dialog = useRef(null);
const opener = useRef(null);
useEffect(() => {
let active = true;
let owned: string | null = null;
const timeout = setTimeout(() => { if (active) { setError(true); active = false; } }, 20000);
setError(false); setDecoded(false); setUrl(null);
const resolve = async () => {
// The cached preview can be revoked when the canonical attachment arrives.
if (loadUrl) return loadUrl(attachment);
const result = await loadImage(attachment);
if (!active) return null;
if (!IMAGE_TYPES.has(result.mediaType)) throw new Error('Unsupported image media type');
const bytes = Uint8Array.from(result.data);
owned = URL.createObjectURL(new Blob([bytes.buffer], { type: result.mediaType }));
return owned;
};
void resolve().then(result => {
if (!active) return;
clearTimeout(timeout);
setUrl(result);
}).catch(() => { clearTimeout(timeout); if (active) setError(true); });
return () => { active = false; clearTimeout(timeout); if (owned) URL.revokeObjectURL(owned); };
}, [attachment.attachmentId, attempt, loadImage, loadUrl]);
const width = Number.isFinite(attachment.width) && attachment.width > 0 ? attachment.width : 4;
const height = Number.isFinite(attachment.height) && attachment.height > 0 ? attachment.height : 3;
return
{!error && url && }
{(!decoded || error) &&
{error ? '图片未能加载' : '正在加载图片'}
{error && }
}
{label && {label}}
;
});
/** The host's user content factory omits its images when no gallery occupies the slot. */
export function UserImageFallback({ images, loadImage }: MessageImagesOwnerProps) {
return
{images.map((image, index) => 'attachment' in image
?
: )}
;
}
export function contentBlocks(content: UserMessageNode['content']): AssistantBlock[] {
return content.map(block => {
if (block.type === 'text') return { kind: 'text', text: block.text };
if (block.type === 'image') return { kind: 'image', attachment: block.attachment };
return { kind: 'other', block };
});
}
type TextPresentation = { startedAt?: number; interrupted?: boolean; liveText?: boolean };
type MarkdownStreamProps = {
text: string;
streaming: boolean;
holdFormatting: boolean;
kind?: 'body' | 'reasoning';
fileMentions?: BlockRenderProps['fileMentions'];
transformed?: boolean;
} & TextPresentation;
function MarkdownStream({ text, streaming, holdFormatting, startedAt, interrupted = false, liveText = false, kind = 'body', fileMentions, transformed = false }: MarkdownStreamProps) {
const root = useRef(null);
const presentation = useStreamingText(text, streaming, { startedAt, interrupted: interrupted || !liveText, selected: holdFormatting });
// Native Markdown changes block keys for its full final parse. Keep the last
// committed mode while this answer is selected, then finish formatting on
// deselection. Business status and the source text still update normally.
const committedMode = useRef(streaming);
const effectiveMode = holdFormatting ? committedMode.current : presentation.formatStreaming;
useLayoutEffect(() => { committedMode.current = effectiveMode; }, [effectiveMode]);
return