"use client" import { useState, useRef, useEffect } from "react" import { Card, CardContent } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Badge } from "@/components/ui/badge" import { Input } from "@/components/ui/input" import { ScrollArea } from "@/components/ui/scroll-area" import { Bot, Send, User, Sparkles, Database, Tags, FileText, Download, Copy, ThumbsUp, ThumbsDown, Loader2, Zap, Search, BarChart3, Table, RefreshCw, } from "lucide-react" // 第四部分:AI Agent智能系统 - 智能对话 export default function AIAgentChatPage() { const [messages, setMessages] = useState([ { id: "1", role: "assistant", content: "您好!我是神射手AI Agent,可以帮您完成以下任务:\n\n- **自然语言查询**:用自然语言查询用户数据\n- **智能打标**:AI自动为用户打标签\n- **数据清洗**:AI识别并清洗异常数据\n- **智能报告**:自动生成数据分析报告\n\n请问有什么可以帮您?", timestamp: new Date(), }, ]) const [inputValue, setInputValue] = useState("") const [isLoading, setIsLoading] = useState(false) const scrollRef = useRef(null) // 快捷指令 const quickCommands = [ { icon: Search, label: "查询高价值用户", command: "帮我找出最近30天内消费超过1万元的高价值用户" }, { icon: Tags, label: "AI打标签", command: "为最近7天未登录的用户打上流失风险标签" }, { icon: Zap, label: "数据清洗", command: "检查用户表中的手机号格式,清洗异常数据" }, { icon: FileText, label: "生成报告", command: "生成本月用户增长分析报告" }, ] // 支持的指令类型 const supportedTasks = [ { type: "NLQ", label: "自然语言查询", desc: "用自然语言查询数据库", icon: Search }, { type: "TAG", label: "AI打标", desc: "智能为用户打标签", icon: Tags }, { type: "CLEAN", label: "数据清洗", desc: "AI识别清洗异常数据", icon: Zap }, { type: "REPORT", label: "智能报告", desc: "自动生成分析报告", icon: FileText }, ] useEffect(() => { if (scrollRef.current) { scrollRef.current.scrollTop = scrollRef.current.scrollHeight } }, [messages]) const handleSend = async () => { if (!inputValue.trim() || isLoading) return const userMessage = { id: Date.now().toString(), role: "user", content: inputValue, timestamp: new Date(), } setMessages((prev) => [...prev, userMessage]) setInputValue("") setIsLoading(true) // 模拟AI响应 setTimeout(() => { let response: any = { id: (Date.now() + 1).toString(), role: "assistant", timestamp: new Date(), } // 根据输入内容生成不同类型的响应 if (inputValue.includes("高价值") || inputValue.includes("消费") || inputValue.includes("查询")) { response.content = "我已理解您的需求。正在为您查询符合条件的用户..." response.taskType = "NLQ" response.result = { type: "table", title: "高价值用户查询结果", sql: "SELECT * FROM users WHERE total_spending > 10000 AND last_active_date >= DATE_SUB(NOW(), INTERVAL 30 DAY)", data: [ { id: "U001", name: "张三", phone: "138****1234", spending: "¥15,680", lastActive: "2天前" }, { id: "U002", name: "李四", phone: "139****5678", spending: "¥12,340", lastActive: "1天前" }, { id: "U003", name: "王五", phone: "137****9012", spending: "¥11,890", lastActive: "3天前" }, ], total: 823, } } else if (inputValue.includes("打标") || inputValue.includes("标签")) { response.content = "我将为符合条件的用户打上「流失风险」标签。这是一个AI打标任务,需要您确认后执行。" response.taskType = "TAG" response.result = { type: "confirmation", title: "AI打标任务确认", description: "为最近7天未登录的用户打上「流失风险」标签", affectedCount: 12456, confidence: 0.87, needReview: true, } } else if (inputValue.includes("清洗") || inputValue.includes("异常")) { response.content = "我已扫描用户表,发现以下数据质量问题:" response.taskType = "CLEAN" response.result = { type: "cleaning", title: "数据清洗建议", issues: [ { field: "phone", issue: "格式不规范", count: 234, example: "1381234567 → 138****4567" }, { field: "email", issue: "缺失值", count: 1256, suggestion: "填充默认值或标记" }, { field: "birth_date", issue: "超出范围", count: 45, example: "2099-01-01" }, ], needReview: true, } } else if (inputValue.includes("报告") || inputValue.includes("分析")) { response.content = "正在为您生成用户增长分析报告..." response.taskType = "REPORT" response.result = { type: "report", title: "用户增长分析报告(2026年1月)", summary: "本月新增用户23,456人,同比增长15.2%;活跃用户856,234人,DAU/MAU比值0.32。", highlights: [ "新用户转化率提升至18.5%,环比增长3.2%", "高价值用户占比8.7%,贡献65%的交易额", "用户流失率下降至2.1%,创历史新低", ], } } else { response.content = "我理解您的需求。请问您想要:\n1. 查询用户数据\n2. AI打标签\n3. 数据清洗\n4. 生成报告\n\n请选择或详细描述您的需求。" } setMessages((prev) => [...prev, response]) setIsLoading(false) }, 1500) } const handleQuickCommand = (command: string) => { setInputValue(command) } const renderMessageContent = (message: any) => { return (

{message.content}

{message.result?.type === "table" && (

{message.result.title}

共 {message.result.total} 条
{message.result.sql}
{message.result.data.map((row: any, index: number) => ( ))}
ID 姓名 手机 消费金额 最近活跃
{row.id} {row.name} {row.phone} {row.spending} {row.lastActive}
)} {message.result?.type === "confirmation" && (

{message.result.title}

{message.result.description}

影响用户数:{message.result.affectedCount.toLocaleString()} 置信度:{(message.result.confidence * 100).toFixed(0)}%
{message.result.needReview && (
)}
)} {message.result?.type === "cleaning" && (

{message.result.title}

{message.result.issues.map((issue: any, index: number) => (
{issue.field} {issue.issue}
{issue.example && (

示例:{issue.example}

)} {issue.suggestion && (

建议:{issue.suggestion}

)}
{issue.count} 条
))}
)} {message.result?.type === "report" && (

{message.result.title}

{message.result.summary}

核心发现:

{message.result.highlights.map((highlight: string, index: number) => (
{highlight}
))}
)}
) } return (
{/* 左侧:功能面板 */}

AI能力

{supportedTasks.map((task) => (

{task.label}

{task.desc}

))}

快捷指令

{quickCommands.map((cmd, index) => ( ))}
{/* 中间:对话区域 */}
{/* 消息列表 */}
{messages.map((message) => (
{message.role === "user" ? ( ) : ( )}
{message.role === "user" ? (

{message.content}

) : ( renderMessageContent(message) )}
{message.role === "assistant" && (
)}
))} {isLoading && (
AI正在思考...
)}
{/* 输入区域 */}
setInputValue(e.target.value)} onKeyDown={(e) => e.key === "Enter" && !e.shiftKey && handleSend()} placeholder="输入您的问题,例如:帮我找出最近30天的高价值用户..." className="pr-12 py-6 bg-white" />

AI生成的内容可能存在误差,重要操作请人工确认

) }