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
+44
View File
@@ -0,0 +1,44 @@
import { NextRequest, NextResponse } from "next/server"
import { prisma } from "@//app/lib/prisma"
// 更新智能体
export async function PUT(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
const data = await request.json()
const agent = await prisma.agent.update({
where: { id: parseInt(id) },
data: {
name: data.name,
slug: data.slug,
description: data.description,
icon: data.icon || null,
categoryId: data.categoryId ? parseInt(data.categoryId) : null,
features: data.features || "",
status: data.status || "active",
},
})
return NextResponse.json(agent)
} catch (error) {
return NextResponse.json({ error: "更新失败" }, { status: 500 })
}
}
// 删除智能体
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
await prisma.agent.delete({
where: { id: parseInt(id) },
})
return NextResponse.json({ success: true })
} catch (error) {
return NextResponse.json({ error: "删除失败" }, { status: 500 })
}
}
+35
View File
@@ -0,0 +1,35 @@
import { NextRequest, NextResponse } from "next/server"
import { prisma } from "@//app/lib/prisma"
// 创建智能体
export async function POST(request: NextRequest) {
try {
const data = await request.json()
const agent = await prisma.agent.create({
data: {
name: data.name,
slug: data.slug,
description: data.description,
icon: data.icon || null,
categoryId: data.categoryId ? parseInt(data.categoryId) : null,
features: data.features || "",
status: data.status || "active",
},
})
return NextResponse.json(agent)
} catch (error) {
return NextResponse.json({ error: "创建失败" }, { status: 500 })
}
}
// 获取所有智能体
export async function GET() {
try {
const agents = await prisma.agent.findMany({
include: { category: true },
})
return NextResponse.json(agents)
} catch (error) {
return NextResponse.json({ error: "获取失败" }, { status: 500 })
}
}
+40
View File
@@ -0,0 +1,40 @@
import { NextRequest, NextResponse } from "next/server"
import { prisma } from "@//app/lib/prisma"
// 更新分类
export async function PUT(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
const data = await request.json()
const category = await prisma.category.update({
where: { id: parseInt(id) },
data: {
name: data.name,
icon: data.icon || null,
description: data.description || null,
},
})
return NextResponse.json(category)
} catch (error) {
return NextResponse.json({ error: "更新失败" }, { status: 500 })
}
}
// 删除分类
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
await prisma.category.delete({
where: { id: parseInt(id) },
})
return NextResponse.json({ success: true })
} catch (error) {
return NextResponse.json({ error: "删除失败" }, { status: 500 })
}
}
+31
View File
@@ -0,0 +1,31 @@
import { NextRequest, NextResponse } from "next/server"
import { prisma } from "@//app/lib/prisma"
// 创建分类
export async function POST(request: NextRequest) {
try {
const data = await request.json()
const category = await prisma.category.create({
data: {
name: data.name,
icon: data.icon || null,
description: data.description || null,
},
})
return NextResponse.json(category)
} catch (error) {
return NextResponse.json({ error: "创建失败" }, { status: 500 })
}
}
// 获取所有分类
export async function GET() {
try {
const categories = await prisma.category.findMany({
include: { _count: { select: { agents: true } } },
})
return NextResponse.json(categories)
} catch (error) {
return NextResponse.json({ error: "获取失败" }, { status: 500 })
}
}
+41
View File
@@ -0,0 +1,41 @@
import { NextRequest, NextResponse } from "next/server"
import { prisma } from "@//app/lib/prisma"
// 更新新闻
export async function PUT(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
const data = await request.json()
const news = await prisma.news.update({
where: { id: parseInt(id) },
data: {
title: data.title,
content: data.content,
type: data.type || "news",
published: data.published || false,
},
})
return NextResponse.json(news)
} catch (error) {
return NextResponse.json({ error: "更新失败" }, { status: 500 })
}
}
// 删除新闻
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
await prisma.news.delete({
where: { id: parseInt(id) },
})
return NextResponse.json({ success: true })
} catch (error) {
return NextResponse.json({ error: "删除失败" }, { status: 500 })
}
}
+32
View File
@@ -0,0 +1,32 @@
import { NextRequest, NextResponse } from "next/server"
import { prisma } from "@//app/lib/prisma"
// 创建新闻
export async function POST(request: NextRequest) {
try {
const data = await request.json()
const news = await prisma.news.create({
data: {
title: data.title,
content: data.content,
type: data.type || "news",
published: data.published || false,
},
})
return NextResponse.json(news)
} catch (error) {
return NextResponse.json({ error: "创建失败" }, { status: 500 })
}
}
// 获取所有新闻
export async function GET() {
try {
const news = await prisma.news.findMany({
orderBy: { createdAt: "desc" },
})
return NextResponse.json(news)
} catch (error) {
return NextResponse.json({ error: "获取失败" }, { status: 500 })
}
}
+19
View File
@@ -0,0 +1,19 @@
import { prisma } from "@/app/lib/prisma"
import { NextResponse } from "next/server"
export async function GET(
request: Request,
{ params }: { params: Promise<{ slug: string }> }
) {
const { slug } = await params
const agent = await prisma.agent.findUnique({
where: { slug },
include: { category: true },
})
if (!agent) {
return NextResponse.json({ error: "Agent not found" }, { status: 404 })
}
return NextResponse.json(agent)
}
+67
View File
@@ -0,0 +1,67 @@
import NextAuth from "next-auth"
import CredentialsProvider from "next-auth/providers/credentials"
import { prisma } from "@/app/lib/prisma"
import bcrypt from "bcryptjs"
const handler = NextAuth({
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
username: { label: "用户名", type: "text" },
password: { label: "密码", type: "password" },
},
async authorize(credentials) {
if (!credentials?.username || !credentials?.password) {
return null
}
const user = await prisma.user.findUnique({
where: { username: credentials.username as string },
})
if (!user) {
return null
}
const isValid = await bcrypt.compare(
credentials.password as string,
user.password
)
if (!isValid) {
return null
}
return {
id: user.id.toString(),
email: user.email,
name: user.name || user.username,
role: user.role,
}
},
}),
],
session: {
strategy: "jwt",
},
callbacks: {
async jwt({ token, user }) {
if (user) {
token.role = user.role
}
return token
},
async session({ session, token }) {
if (session.user) {
(session.user as any).role = token.role
}
return session
},
},
pages: {
signIn: "/admin/login",
},
})
export { handler as GET, handler as POST }
+38
View File
@@ -0,0 +1,38 @@
import { NextResponse } from "next/server"
const API_KEY = 'app-lbe2lglt7taGtZk0dG7pAhbx'
const API_URL = 'http://df.clkeji.com/v1/chat-messages'
export async function POST(request: Request) {
try {
const body = await request.json()
const response = await fetch(API_URL, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
})
if (body.response_mode === 'streaming') {
return new NextResponse(response.body, {
headers: {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
},
})
}
const data = await response.json()
return NextResponse.json(data)
} catch (error) {
console.error('Chat API error:', error)
return NextResponse.json(
{ error: 'Failed to connect to chat service' },
{ status: 500 }
)
}
}
@@ -0,0 +1,30 @@
import { prisma } from "@/app/lib/prisma"
import { NextResponse } from "next/server"
export async function POST(
request: Request,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params
const body = await request.json()
const { role, content } = body
if (!role || !content) {
return NextResponse.json({ error: "role and content are required" }, { status: 400 })
}
const message = await prisma.message.create({
data: {
conversationId: parseInt(id),
role,
content,
},
})
await prisma.conversation.update({
where: { id: parseInt(id) },
data: { updatedAt: new Date() },
})
return NextResponse.json(message)
}
+37
View File
@@ -0,0 +1,37 @@
import { prisma } from "@/app/lib/prisma"
import { NextResponse } from "next/server"
export async function GET(
request: Request,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params
const conversation = await prisma.conversation.findUnique({
where: { id: parseInt(id) },
include: {
messages: {
orderBy: { timestamp: "asc" },
},
},
})
if (!conversation) {
return NextResponse.json({ error: "Conversation not found" }, { status: 404 })
}
return NextResponse.json(conversation)
}
export async function DELETE(
request: Request,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params
await prisma.conversation.delete({
where: { id: parseInt(id) },
})
return NextResponse.json({ success: true })
}
+42
View File
@@ -0,0 +1,42 @@
import { prisma } from "@/app/lib/prisma"
import { NextResponse } from "next/server"
export async function GET(request: Request) {
const { searchParams } = new URL(request.url)
const agentId = searchParams.get("agentId")
if (!agentId) {
return NextResponse.json({ error: "agentId is required" }, { status: 400 })
}
const conversations = await prisma.conversation.findMany({
where: { agentId: parseInt(agentId) },
orderBy: { updatedAt: "desc" },
include: {
messages: {
orderBy: { timestamp: "asc" },
take: 1,
},
},
})
return NextResponse.json(conversations)
}
export async function POST(request: Request) {
const body = await request.json()
const { agentId, title } = body
if (!agentId) {
return NextResponse.json({ error: "agentId is required" }, { status: 400 })
}
const conversation = await prisma.conversation.create({
data: {
agentId: parseInt(agentId),
title: title || "新对话",
},
})
return NextResponse.json(conversation)
}