refactor: restructure navigation and module layout

Reorganize navigation and module structure based on new requirements.

Co-authored-by: null <4804959+fnvtk@users.noreply.github.com>
This commit is contained in:
v0
2026-01-31 04:32:36 +00:00
parent 22e725887a
commit b17b488f8e
105 changed files with 20530 additions and 3622 deletions

432
app/ai-agent/chat/page.tsx Normal file
View File

@@ -0,0 +1,432 @@
"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<any[]>([
{
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<HTMLDivElement>(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 (
<div className="space-y-3">
<p className="whitespace-pre-wrap">{message.content}</p>
{message.result?.type === "table" && (
<Card className="bg-gray-50 border-0">
<CardContent className="p-4">
<div className="flex items-center justify-between mb-3">
<h4 className="font-medium text-gray-900">{message.result.title}</h4>
<Badge variant="secondary"> {message.result.total} </Badge>
</div>
<div className="bg-gray-100 rounded p-2 mb-3 font-mono text-xs text-gray-600 overflow-x-auto">
{message.result.sql}
</div>
<div className="overflow-x-auto">
<table className="w-full text-sm">
<thead>
<tr className="border-b">
<th className="text-left py-2 px-3">ID</th>
<th className="text-left py-2 px-3"></th>
<th className="text-left py-2 px-3"></th>
<th className="text-left py-2 px-3"></th>
<th className="text-left py-2 px-3"></th>
</tr>
</thead>
<tbody>
{message.result.data.map((row: any, index: number) => (
<tr key={index} className="border-b border-gray-100">
<td className="py-2 px-3">{row.id}</td>
<td className="py-2 px-3">{row.name}</td>
<td className="py-2 px-3">{row.phone}</td>
<td className="py-2 px-3 text-green-600 font-medium">{row.spending}</td>
<td className="py-2 px-3">{row.lastActive}</td>
</tr>
))}
</tbody>
</table>
</div>
<div className="flex items-center gap-2 mt-3">
<Button size="sm" variant="outline">
<Download className="h-4 w-4 mr-1" />
Excel
</Button>
<Button size="sm" variant="outline">
<Copy className="h-4 w-4 mr-1" />
SQL
</Button>
</div>
</CardContent>
</Card>
)}
{message.result?.type === "confirmation" && (
<Card className="bg-blue-50 border-blue-200">
<CardContent className="p-4">
<h4 className="font-medium text-gray-900 mb-2">{message.result.title}</h4>
<p className="text-sm text-gray-600 mb-3">{message.result.description}</p>
<div className="flex items-center gap-4 text-sm mb-3">
<span><strong>{message.result.affectedCount.toLocaleString()}</strong></span>
<span><strong>{(message.result.confidence * 100).toFixed(0)}%</strong></span>
</div>
{message.result.needReview && (
<div className="flex items-center gap-2">
<Button size="sm"></Button>
<Button size="sm" variant="outline"></Button>
<Button size="sm" variant="ghost"></Button>
</div>
)}
</CardContent>
</Card>
)}
{message.result?.type === "cleaning" && (
<Card className="bg-yellow-50 border-yellow-200">
<CardContent className="p-4">
<h4 className="font-medium text-gray-900 mb-3">{message.result.title}</h4>
<div className="space-y-3">
{message.result.issues.map((issue: any, index: number) => (
<div key={index} className="flex items-start justify-between p-3 bg-white rounded-lg">
<div>
<div className="flex items-center gap-2">
<Badge variant="secondary">{issue.field}</Badge>
<span className="text-sm font-medium">{issue.issue}</span>
</div>
{issue.example && (
<p className="text-xs text-gray-500 mt-1">{issue.example}</p>
)}
{issue.suggestion && (
<p className="text-xs text-gray-500 mt-1">{issue.suggestion}</p>
)}
</div>
<Badge className="bg-red-100 text-red-700">{issue.count} </Badge>
</div>
))}
</div>
<div className="flex items-center gap-2 mt-3">
<Button size="sm"></Button>
<Button size="sm" variant="outline"></Button>
</div>
</CardContent>
</Card>
)}
{message.result?.type === "report" && (
<Card className="bg-green-50 border-green-200">
<CardContent className="p-4">
<h4 className="font-medium text-gray-900 mb-2">{message.result.title}</h4>
<p className="text-sm text-gray-600 mb-3">{message.result.summary}</p>
<div className="space-y-2">
<p className="text-sm font-medium text-gray-700"></p>
{message.result.highlights.map((highlight: string, index: number) => (
<div key={index} className="flex items-start gap-2 text-sm text-gray-600">
<span className="text-green-500"></span>
{highlight}
</div>
))}
</div>
<div className="flex items-center gap-2 mt-3">
<Button size="sm">
<Download className="h-4 w-4 mr-1" />
</Button>
<Button size="sm" variant="outline">
<BarChart3 className="h-4 w-4 mr-1" />
</Button>
</div>
</CardContent>
</Card>
)}
</div>
)
}
return (
<div className="min-h-screen bg-gradient-to-br from-slate-50 via-blue-50/30 to-purple-50/20">
<div className="flex h-[calc(100vh-64px)]">
{/* 左侧:功能面板 */}
<div className="w-64 border-r bg-white/80 backdrop-blur p-4 hidden lg:block">
<h3 className="font-semibold text-gray-900 mb-4">AI能力</h3>
<div className="space-y-2">
{supportedTasks.map((task) => (
<div
key={task.type}
className="flex items-start gap-3 p-3 rounded-lg hover:bg-gray-50 cursor-pointer transition-colors"
>
<div className="p-2 rounded-lg bg-blue-50">
<task.icon className="h-4 w-4 text-blue-500" />
</div>
<div>
<p className="text-sm font-medium text-gray-900">{task.label}</p>
<p className="text-xs text-gray-500">{task.desc}</p>
</div>
</div>
))}
</div>
<div className="mt-6">
<h3 className="font-semibold text-gray-900 mb-4"></h3>
<div className="space-y-2">
{quickCommands.map((cmd, index) => (
<button
key={index}
onClick={() => handleQuickCommand(cmd.command)}
className="w-full flex items-center gap-2 p-2 rounded-lg text-left text-sm text-gray-600 hover:bg-gray-50 transition-colors"
>
<cmd.icon className="h-4 w-4 text-gray-400" />
{cmd.label}
</button>
))}
</div>
</div>
</div>
{/* 中间:对话区域 */}
<div className="flex-1 flex flex-col">
{/* 消息列表 */}
<ScrollArea className="flex-1 p-6" ref={scrollRef}>
<div className="max-w-3xl mx-auto space-y-6">
{messages.map((message) => (
<div
key={message.id}
className={`flex gap-4 ${message.role === "user" ? "flex-row-reverse" : ""}`}
>
<div
className={`w-10 h-10 rounded-full flex items-center justify-center shrink-0 ${
message.role === "user"
? "bg-blue-500"
: "bg-gradient-to-br from-purple-500 to-blue-500"
}`}
>
{message.role === "user" ? (
<User className="h-5 w-5 text-white" />
) : (
<Bot className="h-5 w-5 text-white" />
)}
</div>
<div
className={`flex-1 ${message.role === "user" ? "text-right" : ""}`}
>
<div
className={`inline-block max-w-full text-left rounded-2xl px-4 py-3 ${
message.role === "user"
? "bg-blue-500 text-white"
: "bg-white shadow-sm"
}`}
>
{message.role === "user" ? (
<p>{message.content}</p>
) : (
renderMessageContent(message)
)}
</div>
{message.role === "assistant" && (
<div className="flex items-center gap-2 mt-2">
<Button variant="ghost" size="sm" className="h-7 px-2">
<ThumbsUp className="h-3 w-3" />
</Button>
<Button variant="ghost" size="sm" className="h-7 px-2">
<ThumbsDown className="h-3 w-3" />
</Button>
<Button variant="ghost" size="sm" className="h-7 px-2">
<Copy className="h-3 w-3" />
</Button>
<Button variant="ghost" size="sm" className="h-7 px-2">
<RefreshCw className="h-3 w-3" />
</Button>
</div>
)}
</div>
</div>
))}
{isLoading && (
<div className="flex gap-4">
<div className="w-10 h-10 rounded-full bg-gradient-to-br from-purple-500 to-blue-500 flex items-center justify-center">
<Bot className="h-5 w-5 text-white" />
</div>
<div className="bg-white rounded-2xl px-4 py-3 shadow-sm">
<div className="flex items-center gap-2 text-gray-500">
<Loader2 className="h-4 w-4 animate-spin" />
<span>AI正在思考...</span>
</div>
</div>
</div>
)}
</div>
</ScrollArea>
{/* 输入区域 */}
<div className="border-t bg-white/80 backdrop-blur p-4">
<div className="max-w-3xl mx-auto">
<div className="flex items-center gap-3">
<div className="flex-1 relative">
<Input
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
onKeyDown={(e) => e.key === "Enter" && !e.shiftKey && handleSend()}
placeholder="输入您的问题例如帮我找出最近30天的高价值用户..."
className="pr-12 py-6 bg-white"
/>
<Button
size="icon"
className="absolute right-2 top-1/2 -translate-y-1/2"
onClick={handleSend}
disabled={isLoading || !inputValue.trim()}
>
<Send className="h-4 w-4" />
</Button>
</div>
</div>
<p className="text-xs text-gray-400 mt-2 text-center">
AI生成的内容可能存在误差
</p>
</div>
</div>
</div>
</div>
</div>
)
}

336
app/ai-agent/nlq/page.tsx Normal file
View File

@@ -0,0 +1,336 @@
"use client"
import { useState } from "react"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Button } from "@/components/ui/button"
import { Badge } from "@/components/ui/badge"
import { Input } from "@/components/ui/input"
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import {
Search,
Play,
Download,
Copy,
History,
Star,
StarOff,
Clock,
Database,
Loader2,
ChevronRight,
Table,
BarChart3,
} from "lucide-react"
// 第四部分AI Agent - 自然语言查询 (NLQ)
export default function NLQPage() {
const [query, setQuery] = useState("")
const [isLoading, setIsLoading] = useState(false)
const [activeTab, setActiveTab] = useState("query")
const [showResult, setShowResult] = useState(false)
const [queryResult, setQueryResult] = useState<any>(null)
// 示例查询
const exampleQueries = [
{ label: "高价值用户", query: "查找最近30天消费超过1万元的用户" },
{ label: "流失用户分析", query: "统计最近7天未登录的活跃用户数量" },
{ label: "地域分布", query: "按省份统计用户数量,按数量降序排列" },
{ label: "消费趋势", query: "统计每天的交易金额最近30天" },
]
// 历史查询
const historyQueries = [
{ id: "1", query: "查找最近30天消费超过1万元的用户", time: "2分钟前", starred: true },
{ id: "2", query: "统计各渠道用户转化率", time: "1小时前", starred: false },
{ id: "3", query: "获取VIP用户的平均消费金额", time: "2小时前", starred: true },
{ id: "4", query: "按年龄段统计用户数量", time: "昨天", starred: false },
{ id: "5", query: "查询高活跃度但低消费的用户群体", time: "昨天", starred: false },
]
const handleExecuteQuery = () => {
if (!query.trim()) return
setIsLoading(true)
setShowResult(false)
// 模拟AI解析和查询
setTimeout(() => {
setQueryResult({
sql: `SELECT u.id, u.name, u.phone, SUM(t.amount) as total_spending
FROM users u
JOIN transactions t ON u.id = t.user_id
WHERE t.created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY)
GROUP BY u.id, u.name, u.phone
HAVING total_spending > 10000
ORDER BY total_spending DESC
LIMIT 100`,
data: [
{ id: "U001", name: "张三", phone: "138****1234", total_spending: 25680 },
{ id: "U002", name: "李四", phone: "139****5678", total_spending: 18920 },
{ id: "U003", name: "王五", phone: "137****9012", total_spending: 15340 },
{ id: "U004", name: "赵六", phone: "136****3456", total_spending: 13280 },
{ id: "U005", name: "钱七", phone: "135****7890", total_spending: 12150 },
],
totalCount: 823,
executionTime: "0.23s",
explanation: "我将您的查询解析为从用户表和交易表中联合查询筛选最近30天内消费总额超过10000元的用户按消费金额降序排列。",
})
setShowResult(true)
setIsLoading(false)
}, 1500)
}
const handleExampleClick = (exampleQuery: string) => {
setQuery(exampleQuery)
}
const handleHistoryClick = (historyQuery: string) => {
setQuery(historyQuery)
}
return (
<div className="min-h-screen bg-gradient-to-br from-slate-50 via-blue-50/30 to-purple-50/20">
<div className="p-6 space-y-6">
{/* 顶部标题 */}
<div className="flex flex-col md:flex-row md:items-center md:justify-between gap-4">
<div>
<h1 className="text-2xl font-bold text-gray-900"></h1>
<p className="text-sm text-gray-500 mt-1">AI自动转换为SQL并执行查询</p>
</div>
</div>
<div className="grid grid-cols-1 lg:grid-cols-4 gap-6">
{/* 左侧:历史记录 */}
<div className="lg:col-span-1">
<Card className="bg-white/80 backdrop-blur border-0 shadow-sm">
<CardHeader className="pb-2">
<CardTitle className="text-base font-semibold flex items-center gap-2">
<History className="h-4 w-4" />
</CardTitle>
</CardHeader>
<CardContent>
<div className="space-y-2">
{historyQueries.map((item) => (
<button
key={item.id}
onClick={() => handleHistoryClick(item.query)}
className="w-full text-left p-3 rounded-lg hover:bg-gray-50 transition-colors group"
>
<div className="flex items-start justify-between">
<p className="text-sm text-gray-700 line-clamp-2 group-hover:text-blue-600">
{item.query}
</p>
{item.starred ? (
<Star className="h-4 w-4 text-yellow-500 shrink-0" />
) : (
<StarOff className="h-4 w-4 text-gray-300 shrink-0 opacity-0 group-hover:opacity-100" />
)}
</div>
<div className="flex items-center gap-1 mt-1 text-xs text-gray-400">
<Clock className="h-3 w-3" />
{item.time}
</div>
</button>
))}
</div>
</CardContent>
</Card>
</div>
{/* 右侧:查询区域 */}
<div className="lg:col-span-3 space-y-6">
{/* 查询输入 */}
<Card className="bg-white/80 backdrop-blur border-0 shadow-sm">
<CardContent className="p-6">
<div className="space-y-4">
<div className="relative">
<Search className="absolute left-4 top-1/2 -translate-y-1/2 h-5 w-5 text-gray-400" />
<Input
value={query}
onChange={(e) => setQuery(e.target.value)}
onKeyDown={(e) => e.key === "Enter" && handleExecuteQuery()}
placeholder="输入您的查询例如查找最近30天消费超过1万元的用户"
className="pl-12 py-6 text-lg bg-white"
/>
</div>
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<span className="text-sm text-gray-500"></span>
{exampleQueries.map((example, index) => (
<button
key={index}
onClick={() => handleExampleClick(example.query)}
className="text-sm text-blue-600 hover:text-blue-700 hover:underline"
>
{example.label}
</button>
))}
</div>
<Button onClick={handleExecuteQuery} disabled={isLoading || !query.trim()}>
{isLoading ? (
<>
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
...
</>
) : (
<>
<Play className="h-4 w-4 mr-2" />
</>
)}
</Button>
</div>
</div>
</CardContent>
</Card>
{/* 查询结果 */}
{showResult && queryResult && (
<div className="space-y-4">
{/* AI解释 */}
<Card className="bg-blue-50 border-blue-200">
<CardContent className="p-4">
<div className="flex items-start gap-3">
<div className="p-2 rounded-lg bg-blue-100">
<Database className="h-4 w-4 text-blue-600" />
</div>
<div className="flex-1">
<p className="text-sm text-blue-800">{queryResult.explanation}</p>
<div className="flex items-center gap-4 mt-2 text-xs text-blue-600">
<span>{queryResult.executionTime}</span>
<span>{queryResult.totalCount} </span>
</div>
</div>
</div>
</CardContent>
</Card>
{/* SQL和结果 */}
<Card className="bg-white/80 backdrop-blur border-0 shadow-sm">
<CardHeader className="pb-2">
<div className="flex items-center justify-between">
<Tabs value={activeTab} onValueChange={setActiveTab}>
<TabsList className="bg-gray-100">
<TabsTrigger value="query">
<Table className="h-4 w-4 mr-1" />
</TabsTrigger>
<TabsTrigger value="sql">
<Database className="h-4 w-4 mr-1" />
SQL语句
</TabsTrigger>
<TabsTrigger value="chart">
<BarChart3 className="h-4 w-4 mr-1" />
</TabsTrigger>
</TabsList>
</Tabs>
<div className="flex items-center gap-2">
<Button variant="outline" size="sm">
<Copy className="h-4 w-4 mr-1" />
</Button>
<Button variant="outline" size="sm">
<Download className="h-4 w-4 mr-1" />
</Button>
</div>
</div>
</CardHeader>
<CardContent>
<TabsContent value="query" className="mt-0">
<div className="overflow-x-auto">
<table className="w-full text-sm">
<thead>
<tr className="border-b bg-gray-50">
<th className="text-left py-3 px-4 font-medium">ID</th>
<th className="text-left py-3 px-4 font-medium"></th>
<th className="text-left py-3 px-4 font-medium"></th>
<th className="text-right py-3 px-4 font-medium"></th>
</tr>
</thead>
<tbody>
{queryResult.data.map((row: any, index: number) => (
<tr key={index} className="border-b border-gray-100 hover:bg-gray-50">
<td className="py-3 px-4 font-mono text-gray-600">{row.id}</td>
<td className="py-3 px-4">{row.name}</td>
<td className="py-3 px-4 text-gray-500">{row.phone}</td>
<td className="py-3 px-4 text-right text-green-600 font-semibold">
¥{row.total_spending.toLocaleString()}
</td>
</tr>
))}
</tbody>
</table>
</div>
<div className="flex items-center justify-between mt-4 pt-4 border-t">
<span className="text-sm text-gray-500">
1-{queryResult.data.length} {queryResult.totalCount}
</span>
<div className="flex items-center gap-2">
<Button variant="outline" size="sm" disabled>
</Button>
<Button variant="outline" size="sm">
<ChevronRight className="h-4 w-4 ml-1" />
</Button>
</div>
</div>
</TabsContent>
<TabsContent value="sql" className="mt-0">
<div className="bg-gray-900 rounded-lg p-4 font-mono text-sm text-gray-100 overflow-x-auto">
<pre className="whitespace-pre-wrap">{queryResult.sql}</pre>
</div>
<div className="flex items-center gap-2 mt-4">
<Button variant="outline" size="sm">
<Copy className="h-4 w-4 mr-1" />
SQL
</Button>
<Button variant="outline" size="sm">
SQL编辑器中打开
</Button>
</div>
</TabsContent>
<TabsContent value="chart" className="mt-0">
<div className="h-64 flex items-center justify-center bg-gray-50 rounded-lg">
<div className="text-center text-gray-500">
<BarChart3 className="h-12 w-12 mx-auto mb-2 text-gray-300" />
<p></p>
</div>
</div>
</TabsContent>
</CardContent>
</Card>
</div>
)}
{/* 无结果时的提示 */}
{!showResult && !isLoading && (
<Card className="bg-white/80 backdrop-blur border-0 shadow-sm">
<CardContent className="p-12 text-center">
<Search className="h-12 w-12 mx-auto mb-4 text-gray-300" />
<h3 className="text-lg font-medium text-gray-900 mb-2"></h3>
<p className="text-sm text-gray-500 mb-6">
AI将自动转换为SQL并执行
</p>
<div className="flex flex-wrap justify-center gap-2">
{exampleQueries.map((example, index) => (
<button
key={index}
onClick={() => handleExampleClick(example.query)}
className="px-4 py-2 rounded-full bg-gray-100 text-sm text-gray-600 hover:bg-gray-200 transition-colors"
>
{example.label}
</button>
))}
</div>
</CardContent>
</Card>
)}
</div>
</div>
</div>
</div>
)
}

View File

@@ -0,0 +1,550 @@
"use client"
import { useState } from "react"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Button } from "@/components/ui/button"
import { Badge } from "@/components/ui/badge"
import { Input } from "@/components/ui/input"
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import { Textarea } from "@/components/ui/textarea"
import { Label } from "@/components/ui/label"
import { Checkbox } from "@/components/ui/checkbox"
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
import {
Tags,
Plus,
Search,
Play,
Pause,
CheckCircle2,
Clock,
AlertCircle,
Settings,
FileText,
RefreshCw,
Sparkles,
ThumbsUp,
ThumbsDown,
Eye,
MoreVertical,
Bot,
Zap,
} from "lucide-react"
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
// 第四部分AI Agent - AI打标
export default function AISmartTagPage() {
const [activeTab, setActiveTab] = useState("tasks")
const [searchQuery, setSearchQuery] = useState("")
const [showCreateDialog, setShowCreateDialog] = useState(false)
const [showReviewDialog, setShowReviewDialog] = useState(false)
const [selectedTask, setSelectedTask] = useState<any>(null)
const [selectedItems, setSelectedItems] = useState<string[]>([])
// AI打标任务列表
const tagTasks = [
{
id: "1",
name: "高价值用户识别",
prompt: "根据用户的消费金额、频次、最近活跃时间,识别高价值用户",
status: "completed",
targetTag: "高价值用户",
dataSource: "用户表+交易表",
affectedCount: 45678,
confidence: 0.92,
createdAt: "2026-01-30 10:00",
completedAt: "2026-01-30 10:15",
needReview: false,
},
{
id: "2",
name: "流失风险预警",
prompt: "识别最近30天未登录且之前活跃的用户标记为流失风险",
status: "pending_review",
targetTag: "流失风险",
dataSource: "用户行为表",
affectedCount: 12456,
confidence: 0.87,
createdAt: "2026-01-31 09:00",
completedAt: "2026-01-31 09:12",
needReview: true,
reviewStats: { approved: 0, rejected: 0, pending: 12456 },
},
{
id: "3",
name: "兴趣标签推断",
prompt: "根据用户浏览和购买记录,推断用户兴趣偏好",
status: "running",
targetTag: "兴趣标签",
dataSource: "行为日志表",
affectedCount: 0,
confidence: 0,
createdAt: "2026-01-31 14:00",
progress: 67,
},
{
id: "4",
name: "地域特征识别",
prompt: "根据IP地址和收货地址识别用户地域特征",
status: "failed",
targetTag: "地域标签",
dataSource: "用户表+订单表",
affectedCount: 0,
confidence: 0,
createdAt: "2026-01-29 15:00",
error: "数据源连接超时",
},
]
// 待审核的标签结果
const pendingReviews = [
{ id: "r1", userId: "U10001", userName: "张三", currentTags: ["活跃用户"], newTag: "流失风险", confidence: 0.92, reason: "30天未登录" },
{ id: "r2", userId: "U10002", userName: "李四", currentTags: ["普通用户"], newTag: "流失风险", confidence: 0.88, reason: "25天未登录" },
{ id: "r3", userId: "U10003", userName: "王五", currentTags: ["新用户"], newTag: "流失风险", confidence: 0.75, reason: "注册后未活跃" },
{ id: "r4", userId: "U10004", userName: "赵六", currentTags: ["VIP用户"], newTag: "流失风险", confidence: 0.65, reason: "消费频次下降" },
{ id: "r5", userId: "U10005", userName: "钱七", currentTags: ["活跃用户"], newTag: "流失风险", confidence: 0.58, reason: "互动减少" },
]
const getStatusBadge = (status: string) => {
switch (status) {
case "completed":
return <Badge className="bg-green-100 text-green-700"></Badge>
case "running":
return <Badge className="bg-blue-100 text-blue-700"></Badge>
case "pending_review":
return <Badge className="bg-yellow-100 text-yellow-700"></Badge>
case "failed":
return <Badge className="bg-red-100 text-red-700"></Badge>
default:
return <Badge variant="secondary"></Badge>
}
}
const getConfidenceColor = (confidence: number) => {
if (confidence >= 0.8) return "text-green-600"
if (confidence >= 0.6) return "text-yellow-600"
return "text-red-600"
}
const handleOpenReview = (task: any) => {
setSelectedTask(task)
setShowReviewDialog(true)
}
const handleBatchApprove = () => {
console.log("批量通过", selectedItems)
setSelectedItems([])
}
const handleBatchReject = () => {
console.log("批量拒绝", selectedItems)
setSelectedItems([])
}
return (
<div className="min-h-screen bg-gradient-to-br from-slate-50 via-blue-50/30 to-purple-50/20">
<div className="p-6 space-y-6">
{/* 顶部标题 */}
<div className="flex flex-col md:flex-row md:items-center md:justify-between gap-4">
<div>
<h1 className="text-2xl font-bold text-gray-900">AI智能打标</h1>
<p className="text-sm text-gray-500 mt-1">使AI自动识别用户特征并打标签</p>
</div>
<div className="flex items-center gap-3">
<Button onClick={() => setShowCreateDialog(true)}>
<Plus className="h-4 w-4 mr-2" />
</Button>
</div>
</div>
{/* 统计卡片 */}
<div className="grid grid-cols-1 md:grid-cols-4 gap-4">
<Card className="bg-white/80 backdrop-blur border-0 shadow-sm">
<CardContent className="p-4">
<div className="flex items-center justify-between">
<div>
<p className="text-sm text-gray-500"></p>
<p className="text-2xl font-bold text-gray-900">{tagTasks.length}</p>
</div>
<Bot className="h-8 w-8 text-purple-500" />
</div>
</CardContent>
</Card>
<Card className="bg-white/80 backdrop-blur border-0 shadow-sm">
<CardContent className="p-4">
<div className="flex items-center justify-between">
<div>
<p className="text-sm text-gray-500"></p>
<p className="text-2xl font-bold text-yellow-600">
{tagTasks.filter((t) => t.status === "pending_review").length}
</p>
</div>
<Clock className="h-8 w-8 text-yellow-500" />
</div>
</CardContent>
</Card>
<Card className="bg-white/80 backdrop-blur border-0 shadow-sm">
<CardContent className="p-4">
<div className="flex items-center justify-between">
<div>
<p className="text-sm text-gray-500"></p>
<p className="text-2xl font-bold text-green-600">58,134</p>
</div>
<Tags className="h-8 w-8 text-green-500" />
</div>
</CardContent>
</Card>
<Card className="bg-white/80 backdrop-blur border-0 shadow-sm">
<CardContent className="p-4">
<div className="flex items-center justify-between">
<div>
<p className="text-sm text-gray-500"></p>
<p className="text-2xl font-bold text-blue-600">87.5%</p>
</div>
<Sparkles className="h-8 w-8 text-blue-500" />
</div>
</CardContent>
</Card>
</div>
{/* Tabs */}
<Tabs value={activeTab} onValueChange={setActiveTab}>
<div className="flex items-center justify-between">
<TabsList className="bg-white">
<TabsTrigger value="tasks"></TabsTrigger>
<TabsTrigger value="review"></TabsTrigger>
<TabsTrigger value="history"></TabsTrigger>
</TabsList>
<div className="relative">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-gray-400" />
<Input
placeholder="搜索任务..."
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
className="pl-9 w-64 bg-white"
/>
</div>
</div>
<TabsContent value="tasks" className="mt-4">
<div className="space-y-4">
{tagTasks.map((task) => (
<Card key={task.id} className="bg-white/80 backdrop-blur border-0 shadow-sm">
<CardContent className="p-5">
<div className="flex items-start justify-between">
<div className="flex-1">
<div className="flex items-center gap-3 mb-2">
<h3 className="font-semibold text-gray-900">{task.name}</h3>
{getStatusBadge(task.status)}
<Badge variant="outline">{task.targetTag}</Badge>
</div>
<p className="text-sm text-gray-500 mb-3">{task.prompt}</p>
<div className="flex items-center gap-6 text-sm text-gray-500">
<span>{task.dataSource}</span>
{task.affectedCount > 0 && (
<span>{task.affectedCount.toLocaleString()}</span>
)}
{task.confidence > 0 && (
<span className={getConfidenceColor(task.confidence)}>
{(task.confidence * 100).toFixed(0)}%
</span>
)}
<span>{task.createdAt}</span>
</div>
{task.status === "running" && (
<div className="mt-3">
<div className="flex items-center justify-between text-sm mb-1">
<span className="text-gray-500"></span>
<span className="text-blue-600">{task.progress}%</span>
</div>
<div className="w-full h-2 bg-gray-100 rounded-full overflow-hidden">
<div
className="h-full bg-blue-500 rounded-full transition-all"
style={{ width: `${task.progress}%` }}
/>
</div>
</div>
)}
{task.status === "failed" && (
<div className="mt-3 p-3 bg-red-50 rounded-lg text-sm text-red-600">
{task.error}
</div>
)}
</div>
<div className="flex items-center gap-2">
{task.status === "pending_review" && (
<Button size="sm" onClick={() => handleOpenReview(task)}>
<Eye className="h-4 w-4 mr-1" />
</Button>
)}
{task.status === "failed" && (
<Button size="sm" variant="outline">
<RefreshCw className="h-4 w-4 mr-1" />
</Button>
)}
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" size="icon">
<MoreVertical className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem>
<FileText className="h-4 w-4 mr-2" />
</DropdownMenuItem>
<DropdownMenuItem>
<Settings className="h-4 w-4 mr-2" />
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
</div>
</CardContent>
</Card>
))}
</div>
</TabsContent>
<TabsContent value="review" className="mt-4">
<Card className="bg-white/80 backdrop-blur border-0 shadow-sm">
<CardHeader className="pb-2">
<div className="flex items-center justify-between">
<CardTitle className="text-base font-semibold"> {pendingReviews.length} </CardTitle>
<div className="flex items-center gap-2">
{selectedItems.length > 0 && (
<>
<span className="text-sm text-gray-500"> {selectedItems.length} </span>
<Button size="sm" onClick={handleBatchApprove}>
<ThumbsUp className="h-4 w-4 mr-1" />
</Button>
<Button size="sm" variant="outline" onClick={handleBatchReject}>
<ThumbsDown className="h-4 w-4 mr-1" />
</Button>
</>
)}
</div>
</div>
</CardHeader>
<CardContent>
<div className="space-y-3">
{pendingReviews.map((item) => (
<div
key={item.id}
className={`flex items-center justify-between p-4 rounded-lg border transition-colors ${
selectedItems.includes(item.id) ? "bg-blue-50 border-blue-200" : "bg-gray-50 border-transparent"
}`}
>
<div className="flex items-center gap-4">
<Checkbox
checked={selectedItems.includes(item.id)}
onCheckedChange={(checked) => {
if (checked) {
setSelectedItems([...selectedItems, item.id])
} else {
setSelectedItems(selectedItems.filter((id) => id !== item.id))
}
}}
/>
<div>
<div className="flex items-center gap-2">
<span className="font-medium text-gray-900">{item.userName}</span>
<span className="text-sm text-gray-500">({item.userId})</span>
</div>
<div className="flex items-center gap-2 mt-1">
<span className="text-sm text-gray-500"></span>
{item.currentTags.map((tag, index) => (
<Badge key={index} variant="secondary" className="text-xs">
{tag}
</Badge>
))}
<span className="text-gray-400"></span>
<Badge className="bg-yellow-100 text-yellow-700 text-xs">
+ {item.newTag}
</Badge>
</div>
<p className="text-xs text-gray-400 mt-1">{item.reason}</p>
</div>
</div>
<div className="flex items-center gap-4">
<div className="text-right">
<p className="text-sm text-gray-500"></p>
<p className={`font-semibold ${getConfidenceColor(item.confidence)}`}>
{(item.confidence * 100).toFixed(0)}%
</p>
</div>
<div className="flex items-center gap-2">
<Button size="sm" variant="outline" className="text-green-600 border-green-200 hover:bg-green-50 bg-transparent">
<ThumbsUp className="h-4 w-4" />
</Button>
<Button size="sm" variant="outline" className="text-red-600 border-red-200 hover:bg-red-50 bg-transparent">
<ThumbsDown className="h-4 w-4" />
</Button>
</div>
</div>
</div>
))}
</div>
</CardContent>
</Card>
</TabsContent>
<TabsContent value="history" className="mt-4">
<Card className="bg-white/80 backdrop-blur border-0 shadow-sm">
<CardContent className="p-6 text-center text-gray-500">
</CardContent>
</Card>
</TabsContent>
</Tabs>
{/* 创建任务弹窗 */}
<Dialog open={showCreateDialog} onOpenChange={setShowCreateDialog}>
<DialogContent className="max-w-2xl">
<DialogHeader>
<DialogTitle>AI打标任务</DialogTitle>
<DialogDescription>AI打标规则AI将自动识别符合条件的用户并打标签</DialogDescription>
</DialogHeader>
<div className="space-y-4 py-4">
<div className="space-y-2">
<Label></Label>
<Input placeholder="例如:高价值用户识别" />
</div>
<div className="space-y-2">
<Label>AI提示词</Label>
<Textarea
placeholder="描述你希望AI如何识别用户例如根据用户的消费金额、频次、最近活跃时间识别高价值用户"
rows={4}
/>
<p className="text-xs text-gray-400">
AI将自动分析数据并打标签
</p>
</div>
<div className="grid grid-cols-2 gap-4">
<div className="space-y-2">
<Label></Label>
<Select>
<SelectTrigger>
<SelectValue placeholder="选择或创建标签" />
</SelectTrigger>
<SelectContent>
<SelectItem value="high-value"></SelectItem>
<SelectItem value="churn-risk"></SelectItem>
<SelectItem value="new-user"></SelectItem>
<SelectItem value="vip">VIP用户</SelectItem>
<SelectItem value="custom">...</SelectItem>
</SelectContent>
</Select>
</div>
<div className="space-y-2">
<Label></Label>
<Select>
<SelectTrigger>
<SelectValue placeholder="选择数据源" />
</SelectTrigger>
<SelectContent>
<SelectItem value="users"></SelectItem>
<SelectItem value="transactions"></SelectItem>
<SelectItem value="behaviors"></SelectItem>
<SelectItem value="all"></SelectItem>
</SelectContent>
</Select>
</div>
</div>
<div className="space-y-2">
<Label></Label>
<div className="flex items-center gap-4">
<Input type="number" defaultValue="0.7" min="0" max="1" step="0.1" className="w-24" />
<span className="text-sm text-gray-500"></span>
</div>
</div>
<div className="flex items-center gap-2">
<Checkbox id="auto-approve" />
<Label htmlFor="auto-approve" className="text-sm font-normal">
90%
</Label>
</div>
</div>
<DialogFooter>
<Button variant="outline" onClick={() => setShowCreateDialog(false)}>
</Button>
<Button onClick={() => setShowCreateDialog(false)}>
<Zap className="h-4 w-4 mr-2" />
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
{/* 审核弹窗 */}
<Dialog open={showReviewDialog} onOpenChange={setShowReviewDialog}>
<DialogContent className="max-w-4xl max-h-[80vh]">
<DialogHeader>
<DialogTitle> - {selectedTask?.name}</DialogTitle>
<DialogDescription>
{selectedTask?.affectedCount.toLocaleString()} AI打标结果
</DialogDescription>
</DialogHeader>
<div className="py-4 max-h-96 overflow-y-auto">
<div className="space-y-3">
{pendingReviews.map((item) => (
<div key={item.id} className="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
<div>
<span className="font-medium">{item.userName}</span>
<span className="text-gray-500 ml-2">({item.userId})</span>
<p className="text-sm text-gray-500 mt-1">{item.reason}</p>
</div>
<div className="flex items-center gap-3">
<span className={getConfidenceColor(item.confidence)}>
{(item.confidence * 100).toFixed(0)}%
</span>
<Button size="sm" variant="outline" className="text-green-600 bg-transparent">
<ThumbsUp className="h-4 w-4" />
</Button>
<Button size="sm" variant="outline" className="text-red-600 bg-transparent">
<ThumbsDown className="h-4 w-4" />
</Button>
</div>
</div>
))}
</div>
</div>
<DialogFooter>
<Button variant="outline" onClick={() => setShowReviewDialog(false)}>
</Button>
<Button variant="outline"></Button>
<Button></Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
</div>
)
}