"use client" import Link from "next/link" import { useState, useEffect, useRef } from "react" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { Textarea } from "@/components/ui/textarea" import { Cpu, BarChart3, TrendingUp, Users, Target, Brain, ArrowRight, Send, Sparkles, Loader2 } from "lucide-react" interface ChatMessage { role: "user" | "assistant" content: string timestamp: string } interface AIStatus { status: string model: string database?: { connected: boolean totalUsers: number latency: number } } export default function AIAssistant() { const [message, setMessage] = useState("") const [isLoading, setIsLoading] = useState(false) const [aiStatus, setAIStatus] = useState(null) const chatEndRef = useRef(null) const [chatHistory, setChatHistory] = useState([ { role: "assistant", content: "你好!我是神射手AI助手,可以帮助你查询用户数据、分析RFM估值。\n\n💡 试试:\n- 查 13407000001\n- 28533368 qq\n- 系统状态\n- 帮助", timestamp: new Date().toLocaleTimeString("zh-CN", { hour: "2-digit", minute: "2-digit" }), }, ]) // 获取 AI 状态 useEffect(() => { fetch("/api/ai-chat") .then(res => res.json()) .then(data => setAIStatus(data)) .catch(console.error) }, []) // 自动滚动到底部 useEffect(() => { chatEndRef.current?.scrollIntoView({ behavior: "smooth" }) }, [chatHistory]) const aiFeatures = [ { icon: , title: "数据分析", description: "自动分析用户行为,生成洞察报告", color: "from-blue-500 to-cyan-500", href: "/ai-assistant/data-analysis", stats: { reports: 1258, insights: 856 }, }, { icon: , title: "趋势预测", description: "预测用户行为和价值变化趋势", color: "from-purple-500 to-pink-500", href: "/ai-assistant/trend-prediction", stats: { accuracy: 94.2, predictions: 2450 }, }, { icon: , title: "用户画像", description: "智能生成用户群体画像", color: "from-green-500 to-emerald-500", href: "/ai-assistant/user-profiling", stats: { profiles: 4285, segments: 128 }, }, { icon: , title: "精准推荐", description: "推荐最佳营销策略和用户群", color: "from-orange-500 to-red-500", href: "/ai-assistant/recommendation", stats: { campaigns: 586, conversion: 18.5 }, }, ] const quickQuestions = ["查 13407000001", "系统状态", "RFM分析", "高价值用户 TOP10", "帮助"] const handleSend = async () => { if (!message.trim() || isLoading) return const userMessage: ChatMessage = { role: "user", content: message, timestamp: new Date().toLocaleTimeString("zh-CN", { hour: "2-digit", minute: "2-digit" }), } setChatHistory(prev => [...prev, userMessage]) setMessage("") setIsLoading(true) try { const response = await fetch("/api/ai-chat", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ message: userMessage.content }) }) const data = await response.json() if (data.success && data.response) { setChatHistory(prev => [...prev, { role: "assistant", content: data.response.content, timestamp: data.response.timestamp || new Date().toLocaleTimeString("zh-CN", { hour: "2-digit", minute: "2-digit" }) }]) } else { setChatHistory(prev => [...prev, { role: "assistant", content: `⚠️ ${data.error || "请求失败,请稍后重试"}`, timestamp: new Date().toLocaleTimeString("zh-CN", { hour: "2-digit", minute: "2-digit" }) }]) } } catch (error: any) { setChatHistory(prev => [...prev, { role: "assistant", content: `⚠️ 网络错误: ${error.message}`, timestamp: new Date().toLocaleTimeString("zh-CN", { hour: "2-digit", minute: "2-digit" }) }]) } finally { setIsLoading(false) } } return (

AI智能助手

数据分析、报告生成与智能预测

AI运行状态
系统状态
{aiStatus?.status === 'online' ? '运行中' : '加载中'}
模型版本 {aiStatus?.model || '神射手 AI'}
响应延迟 {aiStatus?.database?.latency || '--'}ms
数据量 {aiStatus?.database?.totalUsers ? `${(aiStatus.database.totalUsers / 1e8).toFixed(1)}亿` : '--'}
{aiFeatures.map((feature, index) => (
{feature.icon}

{feature.title}

{feature.description}

{Object.entries(feature.stats).map(([key, value], i) => (
{key === "reports" ? "报告数" : key === "insights" ? "洞察数" : key === "accuracy" ? "准确率" : key === "predictions" ? "预测数" : key === "profiles" ? "画像数" : key === "segments" ? "分组数" : key === "campaigns" ? "活动数" : "转化率"}
{typeof value === "number" && value % 1 !== 0 ? `${value}%` : value}
))}
))}
AI对话
{chatHistory.map((chat, index) => (
{chat.content}
{chat.timestamp}
))} {isLoading && (
正在查询中...
)}
{quickQuestions.map((question, index) => ( ))}