Update application code and dependencies
This commit is contained in:
@@ -0,0 +1,196 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from "react"
|
||||
import { useRouter } from "next/navigation"
|
||||
|
||||
export default function AgentForm({
|
||||
categories,
|
||||
agent,
|
||||
}: {
|
||||
categories: { id: number; name: string }[]
|
||||
agent?: {
|
||||
id?: number
|
||||
name: string
|
||||
slug: string
|
||||
description: string
|
||||
icon?: string
|
||||
categoryId?: number
|
||||
features?: string
|
||||
status?: string
|
||||
}
|
||||
}) {
|
||||
const router = useRouter()
|
||||
const [formData, setFormData] = useState({
|
||||
name: agent?.name || "",
|
||||
slug: agent?.slug || "",
|
||||
description: agent?.description || "",
|
||||
icon: agent?.icon || "",
|
||||
categoryId: agent?.categoryId || "",
|
||||
features: agent?.features || "",
|
||||
status: agent?.status || "active",
|
||||
})
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState("")
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault()
|
||||
setLoading(true)
|
||||
setError("")
|
||||
|
||||
try {
|
||||
const url = agent?.id
|
||||
? `/api/admin/agents/${agent.id}`
|
||||
: "/api/admin/agents"
|
||||
|
||||
const method = agent?.id ? "PUT" : "POST"
|
||||
|
||||
const res = await fetch(url, {
|
||||
method,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(formData),
|
||||
})
|
||||
|
||||
if (res.ok) {
|
||||
router.push("/admin/agents")
|
||||
} else {
|
||||
setError("保存失败")
|
||||
}
|
||||
} catch (err) {
|
||||
setError("保存失败,请稍后重试")
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className="space-y-6">
|
||||
{error && (
|
||||
<div className="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg text-sm">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
名称
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
required
|
||||
value={formData.name}
|
||||
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
|
||||
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
|
||||
placeholder="智能客服助手"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
Slug
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
required
|
||||
value={formData.slug}
|
||||
onChange={(e) => setFormData({ ...formData, slug: e.target.value })}
|
||||
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
|
||||
placeholder="smart-customer-service"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
描述
|
||||
</label>
|
||||
<textarea
|
||||
required
|
||||
rows={4}
|
||||
value={formData.description}
|
||||
onChange={(e) => setFormData({ ...formData, description: e.target.value })}
|
||||
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
|
||||
placeholder="描述智能体的功能和特点..."
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-6">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
图标 (Emoji)
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={formData.icon}
|
||||
onChange={(e) => setFormData({ ...formData, icon: e.target.value })}
|
||||
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
|
||||
placeholder="🤖"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
分类
|
||||
</label>
|
||||
<select
|
||||
value={formData.categoryId}
|
||||
onChange={(e) => setFormData({ ...formData, categoryId: e.target.value })}
|
||||
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
|
||||
>
|
||||
<option value="">请选择分类</option>
|
||||
{categories.map((cat) => (
|
||||
<option key={cat.id} value={cat.id}>
|
||||
{cat.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
状态
|
||||
</label>
|
||||
<select
|
||||
value={formData.status}
|
||||
onChange={(e) => setFormData({ ...formData, status: e.target.value })}
|
||||
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
|
||||
>
|
||||
<option value="active">运行中</option>
|
||||
<option value="maintenance">维护中</option>
|
||||
<option value="inactive">未激活</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
功能特性 (逗号分隔)
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={formData.features}
|
||||
onChange={(e) => setFormData({ ...formData, features: e.target.value })}
|
||||
className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500"
|
||||
placeholder="智能问答, 知识库查询, 工单提交"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex gap-4">
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
className="bg-blue-600 text-white px-6 py-2 rounded-lg hover:bg-blue-700 disabled:bg-blue-400"
|
||||
>
|
||||
{loading ? "保存中..." : "保存"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => window.history.back()}
|
||||
className="border border-gray-300 text-gray-700 px-6 py-2 rounded-lg hover:bg-gray-50"
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
'use client'
|
||||
|
||||
import { useRouter } from "next/navigation"
|
||||
|
||||
export default function DeleteButton({ id }: { id: number }) {
|
||||
const router = useRouter()
|
||||
|
||||
const handleDelete = async () => {
|
||||
if (!confirm("确定要删除这个智能体吗?")) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/admin/agents/${id}`, {
|
||||
method: "DELETE",
|
||||
})
|
||||
|
||||
if (res.ok) {
|
||||
router.refresh()
|
||||
}
|
||||
} catch (error) {
|
||||
alert("删除失败")
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
onClick={handleDelete}
|
||||
className="text-red-600 hover:text-red-700 text-sm"
|
||||
>
|
||||
删除
|
||||
</button>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import { getServerSession } from "next-auth/next"
|
||||
import { redirect } from "next/navigation"
|
||||
import Link from "next/link"
|
||||
import { prisma } from "@/app/lib/prisma"
|
||||
import AgentForm from "../../AgentForm"
|
||||
|
||||
export default async function EditAgentPage({
|
||||
params,
|
||||
}: {
|
||||
params: Promise<{ id: string }>
|
||||
}) {
|
||||
const session = await getServerSession()
|
||||
|
||||
if (!session) {
|
||||
redirect("/admin/login")
|
||||
}
|
||||
|
||||
const { id } = await params
|
||||
const agent = await prisma.agent.findUnique({
|
||||
where: { id: parseInt(id) },
|
||||
})
|
||||
|
||||
if (!agent) {
|
||||
redirect("/admin/agents")
|
||||
}
|
||||
|
||||
const categories = await prisma.category.findMany()
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50">
|
||||
<nav className="bg-white border-b border-gray-200">
|
||||
<div className="max-w-7xl mx-auto px-6 py-4 flex items-center justify-between">
|
||||
<h1 className="text-xl font-bold text-gray-900">编辑智能体</h1>
|
||||
<Link href="/admin/agents" className="text-sm text-gray-600 hover:text-gray-900">
|
||||
返回列表
|
||||
</Link>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<main className="max-w-3xl mx-auto px-6 py-8">
|
||||
<div className="bg-white rounded-2xl shadow-sm border border-gray-200 p-6">
|
||||
<AgentForm
|
||||
categories={categories}
|
||||
agent={{
|
||||
id: agent.id,
|
||||
name: agent.name,
|
||||
slug: agent.slug,
|
||||
description: agent.description,
|
||||
icon: agent.icon || "",
|
||||
categoryId: agent.categoryId || undefined,
|
||||
features: agent.features,
|
||||
status: agent.status,
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { getServerSession } from "next-auth/next"
|
||||
import { redirect } from "next/navigation"
|
||||
import Link from "next/link"
|
||||
import { prisma } from "@/app/lib/prisma"
|
||||
import AgentForm from "../AgentForm"
|
||||
|
||||
export default async function NewAgentPage() {
|
||||
const session = await getServerSession()
|
||||
|
||||
if (!session) {
|
||||
redirect("/admin/login")
|
||||
}
|
||||
|
||||
const categories = await prisma.category.findMany()
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50">
|
||||
<nav className="bg-white border-b border-gray-200">
|
||||
<div className="max-w-7xl mx-auto px-6 py-4 flex items-center justify-between">
|
||||
<h1 className="text-xl font-bold text-gray-900">添加智能体</h1>
|
||||
<Link href="/admin/agents" className="text-sm text-gray-600 hover:text-gray-900">
|
||||
返回列表
|
||||
</Link>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<main className="max-w-3xl mx-auto px-6 py-8">
|
||||
<div className="bg-white rounded-2xl shadow-sm border border-gray-200 p-6">
|
||||
<AgentForm categories={categories} />
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
import { getServerSession } from "next-auth/next"
|
||||
import { redirect } from "next/navigation"
|
||||
import { prisma } from "@//app/lib/prisma"
|
||||
import Link from "next/link"
|
||||
import DeleteButton from "./DeleteButton"
|
||||
|
||||
export default async function AdminAgentsPage() {
|
||||
const session = await getServerSession()
|
||||
|
||||
if (!session) {
|
||||
redirect("/admin/login")
|
||||
}
|
||||
|
||||
const agents = await prisma.agent.findMany({
|
||||
include: { category: true },
|
||||
orderBy: { createdAt: "desc" },
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50">
|
||||
<nav className="bg-white border-b border-gray-200">
|
||||
<div className="max-w-7xl mx-auto px-6 py-4 flex items-center justify-between">
|
||||
<h1 className="text-xl font-bold text-gray-900">智能体管理</h1>
|
||||
<div className="flex items-center gap-4">
|
||||
<Link href="/admin" className="text-sm text-gray-600 hover:text-gray-900">
|
||||
返回仪表盘
|
||||
</Link>
|
||||
<Link href="/admin/agents/new" className="bg-blue-600 text-white px-4 py-2 rounded-lg text-sm hover:bg-blue-700">
|
||||
添加智能体
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<main className="max-w-7xl mx-auto px-6 py-8">
|
||||
<div className="bg-white rounded-2xl shadow-sm border border-gray-200 overflow-hidden">
|
||||
<table className="w-full">
|
||||
<thead className="bg-gray-50 border-b border-gray-200">
|
||||
<tr>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">名称</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">分类</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">状态</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">使用次数</th>
|
||||
<th className="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-200">
|
||||
{agents.map((agent) => (
|
||||
<tr key={agent.id} className="hover:bg-gray-50">
|
||||
<td className="px-6 py-4">
|
||||
<div className="flex items-center gap-3">
|
||||
<span className="text-2xl">{agent.icon || "🤖"}</span>
|
||||
<div>
|
||||
<div className="font-medium text-gray-900">{agent.name}</div>
|
||||
<div className="text-sm text-gray-500">{agent.slug}</div>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
<td className="px-6 py-4 text-sm text-gray-600">
|
||||
{agent.category?.name || "-"}
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
<span className={`inline-flex px-2 py-1 text-xs font-medium rounded-full ${
|
||||
agent.status === "active"
|
||||
? "bg-green-100 text-green-700"
|
||||
: "bg-gray-100 text-gray-700"
|
||||
}`}>
|
||||
{agent.status === "active" ? "运行中" : agent.status}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-6 py-4 text-sm text-gray-600">
|
||||
{agent.usageCount}
|
||||
</td>
|
||||
<td className="px-6 py-4 text-right">
|
||||
<div className="flex items-center justify-end gap-2">
|
||||
<Link
|
||||
href={`/admin/agents/${agent.id}/edit`}
|
||||
className="text-blue-600 hover:text-blue-700 text-sm"
|
||||
>
|
||||
编辑
|
||||
</Link>
|
||||
<DeleteButton id={agent.id} />
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
{agents.length === 0 && (
|
||||
<div className="text-center py-12 text-gray-500">
|
||||
暂无智能体,<Link href="/admin/agents/new" className="text-blue-600">点击添加</Link>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user