From 31d97834dcf87815780b8a739da8bdfdd135249c Mon Sep 17 00:00:00 2001 From: imeepos Date: Tue, 22 Jul 2025 13:53:25 +0800 Subject: [PATCH] fix: markdown render --- .../src/components/EnhancedChatMessageV2.tsx | 13 ++ .../components/EnhancedMarkdownRenderer.tsx | 173 +++++------------- 2 files changed, 62 insertions(+), 124 deletions(-) diff --git a/apps/desktop/src/components/EnhancedChatMessageV2.tsx b/apps/desktop/src/components/EnhancedChatMessageV2.tsx index fdbe8a3..99b5c12 100644 --- a/apps/desktop/src/components/EnhancedChatMessageV2.tsx +++ b/apps/desktop/src/components/EnhancedChatMessageV2.tsx @@ -73,6 +73,19 @@ export const EnhancedChatMessageV2: React.FC = ({ const groundingMetadata = message.metadata?.grounding_metadata; const sources = message.metadata?.sources || []; + // 调试信息 + console.log('🔍 EnhancedChatMessageV2 Debug:', { + messageId: message.id, + messageType: message.type, + contentLength: message.content.length, + hasMetadata: !!message.metadata, + hasGroundingMetadata: !!groundingMetadata, + groundingSupportsCount: groundingMetadata?.grounding_supports?.length || 0, + sourcesCount: sources.length, + enableReferences, + groundingMetadata + }); + // 复制消息内容 const handleCopy = useCallback(async () => { try { diff --git a/apps/desktop/src/components/EnhancedMarkdownRenderer.tsx b/apps/desktop/src/components/EnhancedMarkdownRenderer.tsx index 96998b5..bfb7f19 100644 --- a/apps/desktop/src/components/EnhancedMarkdownRenderer.tsx +++ b/apps/desktop/src/components/EnhancedMarkdownRenderer.tsx @@ -1,20 +1,7 @@ -import React, { useMemo, useState } from 'react'; +import React from 'react'; import ReactMarkdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; -import { GroundingMetadata, GroundingSupport, GroundingSource } from '../types/ragGrounding'; -import { ReferenceFootnote } from './ReferenceFootnote'; - -/** - * 文本片段与引用信息 - */ -interface TextSegmentWithReference { - text: string; - startIndex: number; - endIndex: number; - groundingSupport?: GroundingSupport; - sources?: GroundingSource[]; - referenceIndex?: number; -} +import { GroundingMetadata } from '../types/ragGrounding'; /** * 增强Markdown渲染器属性接口 @@ -30,108 +17,39 @@ interface EnhancedMarkdownRendererProps { enableMarkdown?: boolean; /** 自定义样式类名 */ className?: string; - /** 引用点击回调 */ - onReferenceClick?: (sources: GroundingSource[], index: number) => void; } /** * 增强Markdown渲染器组件 - * 支持角标引用和grounding高亮 + * 暂时简化版本,专注于正确渲染Markdown内容 */ export const EnhancedMarkdownRenderer: React.FC = ({ content, groundingMetadata, enableReferences = true, enableMarkdown = true, - className = '', - onReferenceClick + className = '' }) => { - const [selectedReference, setSelectedReference] = useState(null); - // 解析文本片段和引用信息 - const textSegments = useMemo(() => { - if (!groundingMetadata?.grounding_supports?.length) { - return [{ text: content, startIndex: 0, endIndex: content.length }]; - } + // 调试信息 + console.log('📝 EnhancedMarkdownRenderer Debug:', {content, groundingMetadata}); - const segments: TextSegmentWithReference[] = []; - const supports = [...groundingMetadata.grounding_supports].sort( - (a, b) => a.start_index - b.start_index - ); - - let currentIndex = 0; - let referenceCounter = 1; - - supports.forEach((support) => { - // 添加支持前的文本 - if (currentIndex < support.start_index) { - segments.push({ - text: content.slice(currentIndex, support.start_index), - startIndex: currentIndex, - endIndex: support.start_index - }); - } - - // 获取相关来源 - const sources = groundingMetadata.sources?.filter(source => - support.grounding_chunk_indices?.includes(source.chunk_index || 0) - ) || []; - - // 添加支持的文本片段 - segments.push({ - text: content.slice(support.start_index, support.end_index), - startIndex: support.start_index, - endIndex: support.end_index, - groundingSupport: support, - sources, - referenceIndex: sources.length > 0 ? referenceCounter++ : undefined - }); - - currentIndex = support.end_index; - }); - - // 添加剩余文本 - if (currentIndex < content.length) { - segments.push({ - text: content.slice(currentIndex), - startIndex: currentIndex, - endIndex: content.length - }); - } - - return segments; - }, [content, groundingMetadata]); - - // 处理引用点击 - const handleReferenceClick = (sources: GroundingSource[], index: number) => { - setSelectedReference(selectedReference === index ? null : index); - onReferenceClick?.(sources, index); - }; - - // 渲染文本片段 - const renderSegment = (segment: TextSegmentWithReference, index: number) => { - const hasReference = segment.referenceIndex && segment.sources?.length; - const isHighlighted = hasReference && selectedReference === segment.referenceIndex; - - return ( - - {enableMarkdown ? ( + // 暂时禁用角标功能,直接渲染Markdown以避免重复渲染问题 + // TODO: 未来需要实现更复杂的Markdown + 角标集成方案 + return ( +
+ {enableMarkdown ? ( +
<>{children}, - h1: ({ children }) => {children}, - h2: ({ children }) => {children}, - h3: ({ children }) => {children}, - ul: ({ children }) =>
{children}
, - ol: ({ children }) =>
{children}
, - li: ({ children }) =>
{children}
, + p: ({ children }) =>

{children}

, + h1: ({ children }) =>

{children}

, + h2: ({ children }) =>

{children}

, + h3: ({ children }) =>

{children}

, + ul: ({ children }) =>
    {children}
, + ol: ({ children }) =>
    {children}
, + li: ({ children }) =>
  • {children}
  • , strong: ({ children }) => {children}, em: ({ children }) => {children}, code: ({ children }) => ( @@ -140,36 +58,43 @@ export const EnhancedMarkdownRenderer: React.FC = ), blockquote: ({ children }) => ( -
    +
    {children}
    ), }} > - {segment.text} + {content} - ) : ( - segment.text - )} - - {/* 角标引用 */} - {enableReferences && hasReference && ( - handleReferenceClick(sources, segment.referenceIndex!)} - /> - )} - - ); - }; +
    + ) : ( +
    {content}
    + )} - return ( -
    -
    - {textSegments.map(renderSegment)} -
    + {/* 显示引用来源信息(如果有的话) */} + {enableReferences && groundingMetadata?.sources && groundingMetadata.sources.length > 0 && ( +
    +
    + 基于 {groundingMetadata.sources.length} 个来源的信息 +
    +
    + {groundingMetadata.sources.slice(0, 5).map((source, index) => ( + + {index + 1} + + ))} + {groundingMetadata.sources.length > 5 && ( + + +{groundingMetadata.sources.length - 5} 更多 + + )} +
    +
    + )}
    ); };