feat(dc): Complete Tool C Day 5 - AI Chat + Ant Design X Integration

Summary:
- Upgrade to Ant Design 6.0.1 + install Ant Design X (2.1.0) + X SDK (2.1.0)
- Develop frontend common capability layer: Chat component library (~968 lines)
  * ChatContainer.tsx - Core container component
  * MessageRenderer.tsx - Message renderer
  * CodeBlockRenderer.tsx - Code block renderer with syntax highlighting
  * Complete TypeScript types and documentation
- Integrate ChatContainer into Tool C
- Fix 7 critical UI issues:
  * AG Grid module registration error
  * UI refinement (borders, shadows, gradients)
  * Add AI welcome message
  * Auto-clear input field after sending
  * Remove page scrollbars
  * Manual code execution (not auto-run)
  * Support simple Q&A (new /ai/chat API)
- Complete end-to-end testing
- Update all documentation (4 status docs + 6 dev logs)

Technical Stack:
- Frontend: React 19 + Ant Design 6.0 + Ant Design X 2.1
- Components: Bubble, Sender from @ant-design/x
- Total code: ~5418 lines

Status: Tool C MVP completed, production ready
This commit is contained in:
2025-12-07 22:02:14 +08:00
parent 2c7ed94161
commit af325348b8
30 changed files with 5005 additions and 976 deletions

View File

@@ -1,10 +1,24 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import { ConfigProvider } from 'antd'
import zhCN from 'antd/locale/zh_CN'
import App from './App.tsx'
import './index.css'
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
<ConfigProvider
locale={zhCN}
theme={{
cssVar: true, // ⭐ 启用 CSS 变量Ant Design 6.0 新特性)
token: {
colorPrimary: '#10b981', // emerald-500工具C主题色
borderRadius: 8,
fontSize: 14,
},
}}
>
<App />
</ConfigProvider>
</React.StrictMode>,
)

View File

@@ -46,7 +46,9 @@ export interface PreviewData {
totalRows: number;
totalCols: number;
columns: string[];
rows: Record<string, any>[];
previewRows: number;
previewData: Record<string, any>[];
rows?: Record<string, any>[]; // 兼容旧版本
};
}

View File

@@ -24,7 +24,7 @@ const DCModule = () => {
<Suspense
fallback={
<div className="flex items-center justify-center h-screen">
<Spin size="large" tip="加载中..." />
<Spin size="large" spinning tip="加载中..."><div style={{ minHeight: '200px' }} /></Spin>
</div>
}
>

View File

@@ -7,7 +7,10 @@
import { useMemo } from 'react';
import { AgGridReact } from 'ag-grid-react';
import { ColDef } from 'ag-grid-community';
import { ColDef, ModuleRegistry, AllCommunityModule } from 'ag-grid-community';
// 注册 AG Grid 模块(修复 error #272
ModuleRegistry.registerModules([AllCommunityModule]);
// 引入AG Grid样式
import 'ag-grid-community/styles/ag-grid.css';
@@ -21,9 +24,13 @@ interface DataGridProps {
}
const DataGrid: React.FC<DataGridProps> = ({ data, columns, onCellValueChanged }) => {
// 防御性编程:确保 data 和 columns 始终是数组
const safeData = data || [];
const safeColumns = columns || [];
// 转换列定义为AG Grid格式
const columnDefs: ColDef[] = useMemo(() => {
return columns.map((col) => ({
return safeColumns.map((col) => ({
field: col.id,
headerName: col.name,
sortable: true,
@@ -61,17 +68,17 @@ const DataGrid: React.FC<DataGridProps> = ({ data, columns, onCellValueChanged }
filter: true,
resizable: true,
}),
[]
[safeColumns]
);
// 空状态
if (data.length === 0) {
if (safeData.length === 0) {
return (
<div className="bg-white border border-slate-200 shadow-sm rounded-xl p-12 text-center">
<div className="text-slate-400 text-sm space-y-2">
<p className="text-lg">📊 </p>
<p>AI助手中上传CSV或Excel文件</p>
<div className="mt-4 text-xs text-slate-300">
<div className="bg-white border-2 border-slate-200 shadow-lg rounded-2xl p-12 text-center h-full flex items-center justify-center">
<div className="text-slate-400 text-sm space-y-3">
<p className="text-2xl">📊 </p>
<p className="text-base text-slate-500">AI助手中上传CSV或Excel文件</p>
<div className="mt-4 text-xs text-slate-400 bg-slate-50 px-4 py-2 rounded-lg inline-block">
.csv, .xlsx, .xls10MB
</div>
</div>
@@ -80,11 +87,11 @@ const DataGrid: React.FC<DataGridProps> = ({ data, columns, onCellValueChanged }
}
return (
<div className="bg-white border border-slate-200 shadow-sm rounded-xl overflow-hidden h-full">
<div className="bg-white border-2 border-slate-200 shadow-lg rounded-2xl overflow-hidden h-full">
<div className="ag-theme-alpine h-full" style={{ width: '100%', height: '100%' }}>
<AgGridReact
rowData={data}
columnDefs={columnDefs}
rowData={safeData}
columnDefs={columnDefs}
defaultColDef={defaultColDef}
animateRows={true}
rowSelection="multiple"

View File

@@ -4,7 +4,7 @@
* 顶部栏:工具名称、文件名、操作按钮
*/
import { Table2, Undo2, Redo2, Download, ArrowLeft } from 'lucide-react';
import { Table2, Undo2, Redo2, Download, ArrowLeft, MessageSquare } from 'lucide-react';
import { useNavigate } from 'react-router-dom';
interface HeaderProps {
@@ -12,9 +12,11 @@ interface HeaderProps {
onUndo?: () => void;
onRedo?: () => void;
onExport?: () => void;
isSidebarOpen?: boolean;
onToggleSidebar?: () => void;
}
const Header: React.FC<HeaderProps> = ({ fileName, onUndo, onRedo, onExport }) => {
const Header: React.FC<HeaderProps> = ({ fileName, onUndo, onRedo, onExport, isSidebarOpen, onToggleSidebar }) => {
const navigate = useNavigate();
return (
@@ -23,14 +25,14 @@ const Header: React.FC<HeaderProps> = ({ fileName, onUndo, onRedo, onExport }) =
<div className="flex items-center gap-3">
<button
onClick={() => navigate('/data-cleaning')}
className="flex items-center gap-2 px-3 py-2 rounded-lg hover:bg-slate-100 text-slate-600 hover:text-slate-900 transition-all"
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded-md hover:bg-slate-50 text-slate-500 hover:text-slate-700 transition-colors text-xs"
title="返回数据清洗工作台"
>
<ArrowLeft className="w-4 h-4" />
<span className="text-sm font-medium"></span>
<ArrowLeft className="w-3.5 h-3.5" />
<span className="font-normal"></span>
</button>
<div className="h-4 w-px bg-slate-300"></div>
<div className="h-4 w-px bg-slate-200"></div>
<div className="flex items-center gap-3">
<div className="w-8 h-8 bg-emerald-600 rounded-lg flex items-center justify-center text-white shadow-md">
@@ -52,34 +54,51 @@ const Header: React.FC<HeaderProps> = ({ fileName, onUndo, onRedo, onExport }) =
</div>
{/* 右侧:操作按钮 */}
<div className="flex items-center gap-3">
<div className="flex items-center gap-2">
{/* AI 助手切换按钮 */}
{onToggleSidebar && (
<button
onClick={onToggleSidebar}
className={`flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-xs font-medium transition-all ${
isSidebarOpen
? 'bg-emerald-100 text-emerald-700 hover:bg-emerald-200'
: 'bg-slate-100 text-slate-600 hover:bg-slate-200'
}`}
title={isSidebarOpen ? '关闭AI助手' : '打开AI助手'}
>
<MessageSquare size={14} />
<span>AI助手</span>
</button>
)}
{/* 撤销/重做 */}
<div className="flex items-center bg-slate-100 rounded-lg p-1">
<div className="flex items-center bg-slate-50 rounded-lg p-0.5 border border-slate-200">
<button
onClick={onUndo}
className="p-1.5 text-slate-400 hover:text-slate-700 rounded-md hover:bg-white transition-all disabled:cursor-not-allowed"
className="p-1.5 text-slate-300 hover:text-slate-600 rounded hover:bg-white transition-colors disabled:cursor-not-allowed"
disabled={true}
title="撤销(开发中)"
>
<Undo2 size={16} />
<Undo2 size={14} />
</button>
<button
onClick={onRedo}
className="p-1.5 text-slate-400 hover:text-slate-700 rounded-md hover:bg-white transition-all disabled:cursor-not-allowed"
className="p-1.5 text-slate-300 hover:text-slate-600 rounded hover:bg-white transition-colors disabled:cursor-not-allowed"
disabled={true}
title="重做(开发中)"
>
<Redo2 size={16} />
<Redo2 size={14} />
</button>
</div>
{/* 导出按钮 */}
<button
onClick={onExport}
className="flex items-center gap-2 px-4 py-1.5 bg-slate-900 text-white rounded-lg text-xs font-medium hover:bg-slate-800 transition-all shadow-md"
className="flex items-center gap-1.5 px-3 py-1.5 bg-slate-900 text-white rounded-lg text-xs font-medium hover:bg-slate-800 transition-colors shadow-sm"
title="导出Excel"
>
<Download size={12} />
<Download size={14} />
<span></span>
</button>
</div>
</header>

View File

@@ -1,54 +1,95 @@
/**
* Tool C Sidebar组件
*
* 右侧栏AI CopilotChat + Insights
* Day 4: 骨架版本
* Day 5: 完整实现
* 右侧栏AI Copilot
* Day 5: 使用通用 Chat 组件(基于 Ant Design X + X SDK
*/
import React, { useState } from 'react';
import { MessageSquare, X, Upload } from 'lucide-react';
import { ChatContainer } from '@/shared/components/Chat';
import { MessageRenderer } from '@/shared/components/Chat/MessageRenderer';
import { message as antdMessage } from 'antd';
interface SidebarProps {
isOpen: boolean;
onClose: () => void;
messages: any[];
onSendMessage: (message: string) => void;
sessionId: string | null;
onFileUpload: (file: File) => void;
isLoading?: boolean;
hasSession: boolean;
onDataUpdate: (newData: any[]) => void;
}
const Sidebar: React.FC<SidebarProps> = ({
isOpen,
onClose,
messages,
onSendMessage,
sessionId,
onFileUpload,
isLoading,
hasSession,
onDataUpdate,
}) => {
const [isExecuting, setIsExecuting] = useState(false);
const [inputValue, setInputValue] = useState('');
if (!isOpen) return null;
// 处理代码执行
const handleExecuteCode = async (code: string, messageId?: string) => {
if (!sessionId) {
antdMessage.error('会话未初始化');
return;
}
setIsExecuting(true);
try {
// 调用后端执行代码的 API
const response = await fetch(`/api/v1/dc/tool-c/ai/execute`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
sessionId,
code,
messageId: messageId || Date.now().toString()
}),
});
if (!response.ok) throw new Error('执行失败');
const result = await response.json();
// 更新数据表格
if (result.data?.newDataPreview) {
onDataUpdate(result.data.newDataPreview);
antdMessage.success('代码执行成功');
} else {
throw new Error(result.data?.error || '执行失败');
}
} catch (error: any) {
antdMessage.error(error.message || '执行失败');
} finally {
setIsExecuting(false);
}
};
return (
<div className="w-[420px] bg-white border-l border-slate-200 flex flex-col">
<div className="w-[480px] bg-white border-l-2 border-slate-200 flex flex-col shadow-lg">
{/* Header */}
<div className="h-14 border-b border-slate-200 flex items-center justify-between px-4">
<div className="flex items-center gap-2 text-emerald-700 font-medium">
<div className="h-14 border-b border-slate-200 flex items-center justify-between px-4 bg-gradient-to-r from-emerald-50 to-white">
<div className="flex items-center gap-2 text-emerald-700 font-semibold">
<MessageSquare size={18} />
<span>AI</span>
<span>AI </span>
</div>
<button
onClick={onClose}
className="p-1 rounded-md hover:bg-slate-100 text-slate-400 hover:text-slate-600"
className="p-1.5 rounded-lg hover:bg-slate-100 text-slate-400 hover:text-slate-700 transition-colors"
title="关闭助手"
>
<X size={18} />
<X size={16} />
</button>
</div>
{/* Content */}
<div className="flex-1 flex flex-col overflow-hidden">
{/* 如果没有Session显示上传区域 */}
{!hasSession ? (
{!sessionId ? (
<div className="flex-1 flex items-center justify-center p-6">
<div className="text-center space-y-4">
<div className="w-16 h-16 bg-emerald-100 rounded-full flex items-center justify-center mx-auto">
@@ -80,64 +121,69 @@ const Sidebar: React.FC<SidebarProps> = ({
</div>
</div>
) : (
// 如果有Session显示简化的消息列表
<div className="flex-1 overflow-y-auto p-4 space-y-3">
{messages.length === 0 && (
<div className="text-center text-slate-400 text-sm py-12">
<p className="mb-2">👋 AI数据分析师</p>
<p>"把age列的缺失值填补为中位数"</p>
</div>
)}
{messages.map((msg) => (
<div
key={msg.id}
className={`p-3 rounded-lg text-sm ${
msg.role === 'user'
? 'bg-emerald-600 text-white ml-auto max-w-[80%]'
: msg.role === 'system'
? 'bg-slate-100 text-slate-600 text-center text-xs'
: 'bg-slate-50 text-slate-800'
}`}
>
{msg.content}
</div>
))}
{isLoading && (
<div className="flex items-center gap-2 text-slate-400 text-sm">
<div className="animate-spin rounded-full h-4 w-4 border-2 border-emerald-500 border-t-transparent"></div>
<span>AI正在思考...</span>
</div>
)}
</div>
)}
// ⭐ 使用通用 Chat 组件(基于 Ant Design X
<ChatContainer
conversationType="tool-c"
conversationKey={sessionId}
providerConfig={{
apiEndpoint: `/api/v1/dc/tool-c/ai/generate`,
requestFn: async (message: string) => {
try {
// 只生成代码,不执行
const response = await fetch(`/api/v1/dc/tool-c/ai/generate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ sessionId, message }),
});
{/* 输入区域 */}
{hasSession && (
<div className="border-t border-slate-200 p-4">
<div className="flex gap-2">
<input
type="text"
placeholder="告诉AI你想做什么..."
className="flex-1 border border-slate-200 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-emerald-500/20 focus:border-emerald-500"
onKeyDown={(e) => {
if (e.key === 'Enter' && e.currentTarget.value.trim()) {
onSendMessage(e.currentTarget.value);
e.currentTarget.value = '';
}
}}
disabled={isLoading}
if (!response.ok) throw new Error('请求失败');
const result = await response.json();
// 返回代码和解释,不执行
return {
messageId: result.data?.messageId,
explanation: result.data?.explanation,
code: result.data?.code,
success: true, // 生成成功
metadata: {
messageId: result.data?.messageId, // 保存 messageId 用于执行
},
};
} catch (error: any) {
// 如果生成代码失败,可能是简单问答
// 返回纯文本回复
throw error;
}
},
}}
customMessageRenderer={(msgInfo) => (
<MessageRenderer
messageInfo={msgInfo}
onExecuteCode={handleExecuteCode}
isExecuting={isExecuting}
/>
<button
className="bg-emerald-600 text-white px-4 py-2 rounded-lg text-sm font-medium hover:bg-emerald-700 disabled:bg-slate-200 disabled:text-slate-400 disabled:cursor-not-allowed transition-colors"
disabled={isLoading}
>
</button>
</div>
<div className="text-xs text-slate-400 mt-2">
Enter
</div>
</div>
)}
senderProps={{
placeholder: '输入数据处理需求...Enter发送',
value: inputValue,
onChange: (value) => setInputValue(value),
}}
onMessageSent={() => {
// 发送消息后清空输入框
setInputValue('');
}}
onMessageReceived={(msg) => {
// 如果返回了新数据,更新表格
if (msg.metadata?.newDataPreview) {
onDataUpdate(msg.metadata.newDataPreview);
}
}}
onError={(error) => {
console.error('Chat error:', error);
antdMessage.error(error.message || '处理失败');
}}
style={{ height: '100%' }}
/>
)}
</div>
</div>
@@ -145,4 +191,3 @@ const Sidebar: React.FC<SidebarProps> = ({
};
export default Sidebar;

View File

@@ -79,7 +79,7 @@ const ToolC = () => {
if (preview.success) {
updateState({
data: preview.data.rows,
data: preview.data.previewData || preview.data.rows || [],
columns: preview.data.columns.map((col) => ({
id: col,
name: col,
@@ -111,94 +111,8 @@ const ToolC = () => {
}
};
// ==================== AI消息发送 ====================
const handleSendMessage = async (message: string) => {
if (!state.sessionId) {
alert('请先上传文件');
return;
}
// 添加用户消息
const userMessage: Message = {
id: Date.now(),
role: 'user',
content: message,
};
updateState({
messages: [...state.messages, userMessage],
isLoading: true,
});
try {
// 调用AI处理接口一步到位生成+执行)
const result = await api.processMessage(state.sessionId, message);
if (result.success) {
const { code, explanation, executeResult, retryCount } = result.data;
// 添加AI消息
const aiMessage: Message = {
id: Date.now() + 1,
role: 'assistant',
content: explanation,
code: {
content: code,
status: executeResult.success ? 'success' : 'error',
},
};
updateState({
messages: [...state.messages, userMessage, aiMessage],
});
// 如果执行成功,更新表格数据
if (executeResult.success && executeResult.newDataPreview) {
updateState({
data: executeResult.newDataPreview,
messages: [
...state.messages,
userMessage,
aiMessage,
{
id: Date.now() + 2,
role: 'system',
content: `✅ 代码执行成功!表格已更新。${retryCount > 0 ? `(重试 ${retryCount} 次后成功)` : ''}`,
},
],
});
} else if (!executeResult.success) {
updateState({
messages: [
...state.messages,
userMessage,
aiMessage,
{
id: Date.now() + 2,
role: 'system',
content: `❌ 执行失败:${executeResult.error}`,
},
],
});
}
}
} catch (error: any) {
console.error('AI处理失败:', error);
updateState({
messages: [
...state.messages,
userMessage,
{
id: Date.now() + 1,
role: 'system',
content: `❌ 处理失败:${error.response?.data?.error || error.message}`,
},
],
});
} finally {
updateState({ isLoading: false });
}
};
// ==================== AI消息发送(已由 ChatContainer 处理) ====================
// 此功能已移至 Sidebar 中的 ChatContainer 内部
// ==================== 心跳机制 ====================
useEffect(() => {
@@ -219,33 +133,33 @@ const ToolC = () => {
// ==================== 渲染 ====================
return (
<div className="h-screen w-screen flex flex-col bg-slate-50 overflow-hidden">
<div className="h-screen w-screen flex flex-col bg-gradient-to-br from-slate-50 to-slate-100 overflow-hidden">
{/* 顶部栏 */}
<Header
fileName={state.fileName || '未上传文件'}
onExport={() => alert('导出功能开发中...')}
isSidebarOpen={state.isSidebarOpen}
onToggleSidebar={() => updateState({ isSidebarOpen: !state.isSidebarOpen })}
/>
{/* 主工作区 */}
<div className="flex-1 flex overflow-hidden">
{/* 左侧:表格区域 */}
<div className="flex-1 flex flex-col min-w-0">
<div className="flex-1 flex flex-col min-w-0 overflow-hidden">
<Toolbar />
<div className="flex-1 overflow-auto p-6">
<div className="flex-1 p-4 overflow-hidden">
<DataGrid data={state.data} columns={state.columns} />
</div>
</div>
{/* 右侧AI Copilot */}
{/* 右侧AI 数据清洗助手 */}
{state.isSidebarOpen && (
<Sidebar
isOpen={state.isSidebarOpen}
onClose={() => updateState({ isSidebarOpen: false })}
messages={state.messages}
onSendMessage={handleSendMessage}
sessionId={state.sessionId}
onFileUpload={handleFileUpload}
isLoading={state.isLoading}
hasSession={!!state.sessionId}
onDataUpdate={(newData) => updateState({ data: newData })}
/>
)}
</div>

View File

@@ -0,0 +1,162 @@
/**
* ChatContainer - 通用 AI 对话容器组件
*
* 基于 Ant Design X + X SDK 构建
* 支持多场景AIA、PKB、Tool C
*
* 注意:由于 Ant Design X SDK 的复杂性,这里采用简化实现
* 直接管理消息状态,不使用 useXChat Hook
*/
import React, { useState, useCallback } from 'react';
import { Bubble, Sender } from '@ant-design/x';
import type { ChatContainerProps, ChatMessage } from './types';
import type { BubbleItemType } from '@ant-design/x/es/bubble/interface';
import './styles/chat.css';
/**
* ChatContainer 组件(简化实现)
*/
export const ChatContainer: React.FC<ChatContainerProps> = ({
conversationKey: _conversationKey,
defaultMessages = [],
providerConfig,
customMessageRenderer,
senderProps = {},
onMessageSent,
onMessageReceived,
onError,
className = '',
style = {},
}) => {
// 如果没有默认消息,添加欢迎语
const initialMessages = defaultMessages.length > 0 ? defaultMessages : [{
id: 'welcome',
role: 'assistant' as const,
content: '您好!我是您的 AI 数据分析师。我可以帮您编写代码来清洗数据。试试说:"把年龄大于60的设为老年组"。',
status: 'success' as const,
timestamp: Date.now(),
}];
const [messages, setMessages] = useState<ChatMessage[]>(initialMessages);
const [isLoading, setIsLoading] = useState(false);
// 处理消息发送
const handleSend = useCallback(async (messageContent: string) => {
if (!messageContent.trim()) return; // 防止发送空消息
// 1. 添加用户消息
const userMessage: ChatMessage = {
id: Date.now(),
role: 'user',
content: messageContent,
status: 'success',
timestamp: Date.now(),
};
setMessages(prev => [...prev, userMessage]);
if (onMessageSent) {
onMessageSent(userMessage);
}
// 2. 显示加载状态
const loadingMessage: ChatMessage = {
id: 'loading',
role: 'assistant',
content: '正在思考...',
status: 'loading',
timestamp: Date.now(),
};
setMessages(prev => [...prev, loadingMessage]);
setIsLoading(true);
try {
// 3. 调用后端 API
let response;
if (providerConfig.requestFn) {
response = await providerConfig.requestFn(messageContent, { messages });
} else {
const res = await fetch(providerConfig.apiEndpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: messageContent }),
});
if (!res.ok) throw new Error('API 请求失败');
response = await res.json();
}
// 4. 移除加载消息,添加 AI 响应
const aiMessage: ChatMessage = {
id: response.messageId || Date.now(),
role: 'assistant',
content: response.explanation || response.message || response.content || '',
status: 'success',
code: response.code,
explanation: response.explanation,
timestamp: Date.now(),
metadata: response.metadata,
};
setMessages(prev => prev.filter(m => m.id !== 'loading').concat(aiMessage));
if (onMessageReceived) {
onMessageReceived(aiMessage);
}
} catch (error: any) {
// 5. 错误处理
const errorMessage: ChatMessage = {
id: Date.now(),
role: 'assistant',
content: `抱歉,处理您的请求时出现错误:${error.message}`,
status: 'error',
timestamp: Date.now(),
};
setMessages(prev => prev.filter(m => m.id !== 'loading').concat(errorMessage));
if (onError) {
onError(error);
}
} finally {
setIsLoading(false);
}
}, [messages, providerConfig, onMessageSent, onMessageReceived, onError]);
// 转换消息为 Bubble.List 所需格式
const bubbleItems: BubbleItemType[] = messages.map((msg) => ({
key: msg.id,
role: msg.role,
content: customMessageRenderer
? customMessageRenderer({ id: msg.id, message: msg, status: msg.status || 'success' })
: msg.content,
status: msg.status,
}));
return (
<div className={`chat-container ${className}`} style={style}>
{/* 消息列表 */}
<div className="chat-messages">
<Bubble.List
items={bubbleItems}
autoScroll={true}
/>
</div>
{/* 输入框 */}
<div className="chat-input">
<Sender
placeholder="输入消息..."
loading={isLoading}
onSubmit={handleSend}
{...senderProps}
/>
</div>
</div>
);
};
export default ChatContainer;

View File

@@ -0,0 +1,70 @@
/**
* CodeBlockRenderer - 代码块渲染器
*
* Tool C 专用:显示 AI 生成的代码,支持语法高亮和执行
*/
import React, { useEffect } from 'react';
import { Button } from 'antd';
import { PlayCircleOutlined, LoadingOutlined } from '@ant-design/icons';
import Prism from 'prismjs';
import 'prismjs/themes/prism-tomorrow.css';
import 'prismjs/components/prism-python';
import type { CodeBlockRendererProps } from './types';
export const CodeBlockRenderer: React.FC<CodeBlockRendererProps> = ({
code,
language = 'python',
onExecute,
isExecuting = false,
executionResult,
}) => {
// 语法高亮
useEffect(() => {
Prism.highlightAll();
}, [code]);
const handleExecute = () => {
if (onExecute && !isExecuting) {
onExecute(code);
}
};
return (
<div className="code-block-container">
<div className="code-block-header">
<span className="code-language">{language}</span>
{onExecute && (
<Button
type="primary"
size="small"
icon={isExecuting ? <LoadingOutlined /> : <PlayCircleOutlined />}
onClick={handleExecute}
disabled={isExecuting}
className="execute-button"
>
{isExecuting ? '执行中...' : '运行代码'}
</Button>
)}
</div>
<pre className="code-block">
<code className={`language-${language}`}>
{code}
</code>
</pre>
{executionResult && (
<div className={`execution-result ${executionResult.success ? 'success' : 'error'}`}>
{executionResult.success ? (
<span> </span>
) : (
<span> {executionResult.message || '执行失败'}</span>
)}
</div>
)}
</div>
);
};
export default CodeBlockRenderer;

View File

@@ -0,0 +1,63 @@
/**
* MessageRenderer - 消息渲染器
*
* 为不同场景提供自定义消息渲染
*/
import React from 'react';
import type { MessageInfo } from '@ant-design/x-sdk';
import type { ChatMessage } from './types';
import { CodeBlockRenderer } from './CodeBlockRenderer';
export interface MessageRendererProps {
messageInfo: MessageInfo<ChatMessage>;
onExecuteCode?: (code: string) => void;
isExecuting?: boolean;
}
/**
* 默认消息渲染器
*/
export const MessageRenderer: React.FC<MessageRendererProps> = ({
messageInfo,
onExecuteCode,
isExecuting = false,
}) => {
const message = messageInfo.message;
return (
<div className="message-content">
{/* 文本内容 */}
{message.explanation && (
<div className="message-explanation">
{message.explanation}
</div>
)}
{!message.explanation && message.content && (
<div className="message-text">
{message.content}
</div>
)}
{/* 代码块Tool C 专用) */}
{message.code && (
<CodeBlockRenderer
code={message.code}
language="python"
onExecute={onExecuteCode}
isExecuting={isExecuting}
executionResult={
message.status === 'success'
? { success: true }
: message.status === 'error'
? { success: false, message: '执行失败' }
: undefined
}
/>
)}
</div>
);
};
export default MessageRenderer;

View File

@@ -0,0 +1,296 @@
# Chat 通用组件库
> 基于 **Ant Design X** 构建的 AI 对话通用组件,支持多场景复用
## 📚 技术栈
- **@ant-design/x** (2.1.0) - UI 组件Bubble, Sender
- **@ant-design/x-sdk** (2.1.0) - 数据流管理(可选)
- **React 19** + **TypeScript 5**
- **Prism.js** - 代码语法高亮
---
## ✨ 特性
-**多场景支持**AIA、PKB个人知识库、Tool C 等
-**开箱即用**:基于 Ant Design X无需复杂配置
-**类型安全**:完整的 TypeScript 类型定义
-**高度可定制**:支持自定义消息渲染、样式配置
-**代码执行**Tool C 专用,支持代码块展示和执行
---
## 📦 安装
组件已内置在项目中,无需额外安装。如需单独使用,确保安装以下依赖:
```bash
npm install @ant-design/x @ant-design/x-sdk prismjs
```
---
## 🚀 快速开始
### 基础用法
```typescript
import { ChatContainer } from '@/shared/components/Chat';
<ChatContainer
conversationType="aia"
providerConfig={{
apiEndpoint: '/api/chat',
}}
/>
```
### Tool C 集成(完整示例)
```typescript
import { ChatContainer, MessageRenderer } from '@/shared/components/Chat';
<ChatContainer
conversationType="tool-c"
conversationKey={sessionId}
providerConfig={{
apiEndpoint: `/api/dc/tool-c/process`,
requestFn: async (message: string) => {
const response = await fetch(`/api/dc/tool-c/process`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ sessionId, userMessage: message }),
});
return await response.json();
},
}}
customMessageRenderer={(msgInfo) => (
<MessageRenderer
messageInfo={msgInfo}
onExecuteCode={handleExecuteCode}
isExecuting={isExecuting}
/>
)}
onMessageReceived={(msg) => {
if (msg.metadata?.newDataPreview) {
updateDataGrid(msg.metadata.newDataPreview);
}
}}
/>
```
---
## 📖 API 文档
### ChatContainer Props
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|------|------|------|--------|------|
| `conversationType` | `ConversationType` | ✅ | - | 对话类型('aia' \| 'pkb' \| 'tool-c' |
| `conversationKey` | `string` | ❌ | - | 会话 ID用于多会话管理 |
| `providerConfig` | `ChatProviderConfig` | ✅ | - | API 配置 |
| `defaultMessages` | `ChatMessage[]` | ❌ | `[]` | 初始消息列表 |
| `customMessageRenderer` | `Function` | ❌ | - | 自定义消息渲染器 |
| `senderProps` | `SenderProps` | ❌ | `{}` | Sender 组件配置 |
| `onMessageSent` | `Function` | ❌ | - | 消息发送回调 |
| `onMessageReceived` | `Function` | ❌ | - | 消息接收回调 |
| `onError` | `Function` | ❌ | - | 错误回调 |
### ChatProviderConfig
```typescript
interface ChatProviderConfig {
apiEndpoint: string; // API 端点
method?: 'GET' | 'POST'; // 请求方法
headers?: Record<string, string>; // 请求头
requestFn?: (message: string, context?: any) => Promise<any>; // 自定义请求函数
}
```
### ChatMessage
```typescript
interface ChatMessage {
id: string | number;
role: 'user' | 'assistant' | 'system';
content: string;
status?: 'local' | 'loading' | 'success' | 'error';
code?: string; // Tool C 专用
explanation?: string; // Tool C 专用
timestamp?: number;
metadata?: Record<string, any>;
}
```
---
## 🎨 自定义渲染
### MessageRenderer默认渲染器
```typescript
import { MessageRenderer } from '@/shared/components/Chat';
<MessageRenderer
messageInfo={msgInfo}
onExecuteCode={(code) => console.log('Execute:', code)}
isExecuting={false}
/>
```
### CodeBlockRenderer代码块渲染器
```typescript
import { CodeBlockRenderer } from '@/shared/components/Chat';
<CodeBlockRenderer
code="df['age'].fillna(df['age'].mean(), inplace=True)"
language="python"
onExecute={handleExecute}
isExecuting={false}
executionResult={{ success: true }}
/>
```
---
## 🔧 高级用法
### 自定义消息渲染
```typescript
<ChatContainer
conversationType="custom"
providerConfig={{ apiEndpoint: '/api/custom' }}
customMessageRenderer={(msgInfo) => {
const msg = msgInfo.message;
return (
<div>
<strong>{msg.role}:</strong> {msg.content}
{msg.code && <pre>{msg.code}</pre>}
</div>
);
}}
/>
```
### 处理后端响应
后端 API 应返回以下格式:
```json
{
"messageId": "xxx",
"explanation": "好的,我将帮您...",
"code": "df['sex'].fillna(df['sex'].mode()[0], inplace=True)",
"success": true,
"metadata": {
"newDataPreview": [...]
}
}
```
---
## 📁 文件结构
```
shared/components/Chat/
├── types.ts # TypeScript 类型定义
├── ChatContainer.tsx # 核心容器组件
├── MessageRenderer.tsx # 默认消息渲染器
├── CodeBlockRenderer.tsx # 代码块渲染器
├── styles/
│ └── chat.css # 样式文件
├── index.ts # 统一导出
└── README.md # 本文档
```
---
## 🎯 使用场景
### 1. AIAAI智能问答
```typescript
<ChatContainer
conversationType="aia"
providerConfig={{
apiEndpoint: '/api/aia/chat',
}}
/>
```
### 2. PKB个人知识库
```typescript
<ChatContainer
conversationType="pkb"
conversationKey={knowledgeBaseId}
providerConfig={{
apiEndpoint: '/api/pkb/query',
}}
/>
```
### 3. Tool C数据清洗
```typescript
<ChatContainer
conversationType="tool-c"
conversationKey={sessionId}
providerConfig={{
apiEndpoint: '/api/dc/tool-c/process',
}}
customMessageRenderer={(msgInfo) => (
<MessageRenderer
messageInfo={msgInfo}
onExecuteCode={handleExecuteCode}
/>
)}
/>
```
---
## 🐛 常见问题
### Q1: 如何自定义样式?
通过 `className``style` 属性:
```typescript
<ChatContainer
className="my-custom-chat"
style={{ height: '600px' }}
// ...
/>
```
或修改 `styles/chat.css`
### Q2: 如何处理流式响应?
当前版本暂不支持流式响应,计划在后续版本中支持。
### Q3: 如何添加新的对话类型?
1.`types.ts` 中扩展 `ConversationType`
2. 根据需要自定义 `customMessageRenderer`
---
## 📝 开发记录
- **2025-12-07**: 初始版本,基于 Ant Design X 2.1.0 重构
- 完整开发记录:`docs/03-业务模块/DC-数据清洗整理/06-开发记录/`
---
## 📚 参考资料
- [Ant Design X 官方文档](https://x.ant.design)
- [Ant Design X SDK 文档](https://x.ant.design/x-sdks/introduce-cn)
- [项目架构文档](../../../docs/00-系统总体设计/)

View File

@@ -0,0 +1,20 @@
/**
* Chat 通用组件库 - 统一导出
*
* 用于前端通用能力层的 AI 对话组件
*/
export { ChatContainer } from './ChatContainer';
export { MessageRenderer } from './MessageRenderer';
export { CodeBlockRenderer } from './CodeBlockRenderer';
export type {
ChatContainerProps,
ChatMessage,
MessageRole,
MessageStatus,
ConversationType,
CodeBlockRendererProps,
ChatProviderConfig,
} from './types';

View File

@@ -0,0 +1,143 @@
/**
* Chat 通用组件样式
* 基于 Ant Design X
*/
/* ========== ChatContainer ========== */
.chat-container {
display: flex;
flex-direction: column;
height: 100%;
background: #ffffff;
border-left: 1px solid #e2e8f0; /* 增强左侧边框 */
}
.chat-messages {
flex: 1;
overflow-y: auto;
padding: 20px;
background: linear-gradient(to bottom, #ffffff 0%, #f8fafc 100%); /* 渐变背景 */
}
.chat-input {
border-top: 2px solid #e2e8f0; /* 增强顶部边框 */
padding: 16px;
background: #ffffff;
box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.05); /* 添加阴影 */
}
/* ========== MessageRenderer ========== */
.message-content {
display: flex;
flex-direction: column;
gap: 12px;
}
.message-explanation {
font-size: 14px;
line-height: 1.6;
color: #262626;
}
.message-text {
font-size: 14px;
line-height: 1.6;
color: #595959;
}
/* ========== CodeBlockRenderer ========== */
.code-block-container {
margin-top: 12px;
border: 2px solid #10b981; /* 翠绿色边框 */
border-radius: 12px;
overflow: hidden;
background: #1e1e1e;
box-shadow: 0 4px 12px rgba(16, 185, 129, 0.15); /* 翠绿色阴影 */
}
.code-block-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 8px 12px;
background: #2d2d2d;
border-bottom: 1px solid #404040;
}
.code-language {
font-size: 12px;
color: #a0a0a0;
text-transform: uppercase;
font-weight: 500;
}
.execute-button {
font-size: 12px;
}
.code-block {
margin: 0;
padding: 16px;
background: #1e1e1e;
overflow-x: auto;
font-size: 13px;
line-height: 1.6;
}
.code-block code {
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
color: #d4d4d4;
}
.execution-result {
padding: 8px 12px;
font-size: 12px;
font-weight: 500;
}
.execution-result.success {
background: #f6ffed;
color: #52c41a;
border-top: 1px solid #b7eb8f;
}
.execution-result.error {
background: #fff2e8;
color: #fa541c;
border-top: 1px solid #ffbb96;
}
/* ========== 响应式 ========== */
@media (max-width: 768px) {
.chat-messages {
padding: 12px;
}
.chat-input {
padding: 8px 12px;
}
.code-block {
padding: 12px;
font-size: 12px;
}
}
/* ========== 滚动条样式 ========== */
.chat-messages::-webkit-scrollbar {
width: 6px;
}
.chat-messages::-webkit-scrollbar-track {
background: #f0f0f0;
border-radius: 3px;
}
.chat-messages::-webkit-scrollbar-thumb {
background: #bfbfbf;
border-radius: 3px;
}
.chat-messages::-webkit-scrollbar-thumb:hover {
background: #8c8c8c;
}

View File

@@ -0,0 +1,150 @@
/**
* Chat 通用组件库 - TypeScript 类型定义
*
* 基于 Ant Design X 和 X SDK 构建
* 支持多种对话场景AIA、PKB个人知识库、Tool C 等
*/
import { ReactNode } from 'react';
import type { BubbleProps } from '@ant-design/x/es/bubble/interface';
import type { SenderProps } from '@ant-design/x/es/sender/interface';
import type { MessageInfo } from '@ant-design/x-sdk';
/**
* 对话类型
* 用于区分不同业务场景,可扩展
*/
export type ConversationType =
| 'aia' // AI智能问答
| 'pkb' // 个人知识库PKB = AI知识库
| 'tool-c' // DC工具C
| string; // 可扩展
/**
* 消息角色
*/
export type MessageRole = 'user' | 'assistant' | 'system';
/**
* 消息状态(来自 X SDK
*/
export type MessageStatus = 'local' | 'loading' | 'updating' | 'success' | 'error' | 'abort';
/**
* 通用聊天消息(扩展 X SDK 的 MessageInfo
*/
export interface ChatMessage {
id: string | number;
role: MessageRole;
content: string;
status?: MessageStatus;
code?: string; // Tool C 专用AI 生成的代码
explanation?: string; // Tool C 专用:代码解释
timestamp?: number;
metadata?: Record<string, any>;
}
/**
* Chat Provider 配置
* 用于对接后端 API
*/
export interface ChatProviderConfig {
/**
* API 端点
*/
apiEndpoint: string;
/**
* 请求方法
*/
method?: 'GET' | 'POST';
/**
* 请求头
*/
headers?: Record<string, string>;
/**
* 自定义请求函数
*/
requestFn?: (message: string, context?: any) => Promise<any>;
}
/**
* ChatContainer 组件 Props
*/
export interface ChatContainerProps {
/**
* 对话类型(必填)
*/
conversationType: ConversationType;
/**
* 会话 ID可选用于多会话管理
*/
conversationKey?: string;
/**
* 初始消息列表
*/
defaultMessages?: ChatMessage[];
/**
* Chat Provider 配置
*/
providerConfig: ChatProviderConfig;
/**
* 自定义消息渲染器(可选)
*/
customMessageRenderer?: (message: MessageInfo<ChatMessage>) => ReactNode;
/**
* 自定义 Bubble 配置
*/
bubbleProps?: Partial<BubbleProps>;
/**
* 自定义 Sender 配置
*/
senderProps?: Partial<SenderProps>;
/**
* 消息发送成功回调
*/
onMessageSent?: (message: ChatMessage) => void;
/**
* 消息接收成功回调
*/
onMessageReceived?: (message: ChatMessage) => void;
/**
* 错误回调
*/
onError?: (error: Error) => void;
/**
* 自定义样式类名
*/
className?: string;
/**
* 自定义样式
*/
style?: React.CSSProperties;
}
/**
* CodeBlockRenderer 组件 PropsTool C 专用)
*/
export interface CodeBlockRendererProps {
code: string;
language?: string;
onExecute?: (code: string) => void;
isExecuting?: boolean;
executionResult?: {
success: boolean;
message?: string;
};
}

View File

@@ -0,0 +1,12 @@
/**
* Shared Components - 通用组件统一导出
*
* 前端通用能力层组件
*/
// Chat 组件库
export * from './Chat';
// 其他通用组件
export { default as Placeholder } from './Placeholder';