Update application code and dependencies

This commit is contained in:
root
2026-05-06 17:22:50 +08:00
parent efc8f4bf78
commit a3ee04379d
60 changed files with 6793 additions and 860 deletions
+196
View File
@@ -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>
)
}
+34
View File
@@ -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>
)
}
+59
View File
@@ -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>
)
}
+34
View File
@@ -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>
)
}
+99
View File
@@ -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>
)
}