实现了AI助手
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { AIClient } from '@/lib/ai-assistant-openai'
|
||||
import { getUser } from '@/lib/auth'
|
||||
import { ComponentService } from '@/lib/services/component-service'
|
||||
import { Component } from '@prisma/client'
|
||||
|
||||
type Message = {
|
||||
role: 'user' | 'assistant'
|
||||
content: string
|
||||
} | {
|
||||
role: 'card'
|
||||
components: Component[]
|
||||
}
|
||||
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
// 验证用户身份
|
||||
const user = await getUser(request)
|
||||
if (!user) {
|
||||
return NextResponse.json(
|
||||
{ message: '请先登录' },
|
||||
{ status: 401 }
|
||||
)
|
||||
}
|
||||
|
||||
const client = new AIClient()
|
||||
const conversation = await client.getConversation((await params).id, user.id)
|
||||
|
||||
if (!conversation) {
|
||||
return NextResponse.json(
|
||||
{ message: '对话不存在或无权限访问' },
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
|
||||
// 转换为Markdown
|
||||
const filteredMessages = conversation.messages.filter((m, index) => !(index == 0 || m.role == 'user' && typeof m.content != 'string'))
|
||||
|
||||
const res: Message[] = []
|
||||
let appendMessage: Message | null = null
|
||||
for (const message of filteredMessages) {
|
||||
let content: string
|
||||
|
||||
if (typeof message.content === "string") {
|
||||
content = message.content
|
||||
} else {
|
||||
const contentParts: string[] = []
|
||||
for (const part of message.content) {
|
||||
if (part.type === 'text') {
|
||||
contentParts.push(part.text)
|
||||
} else if (part.type === 'tool_use') {
|
||||
contentParts.push(`**工具调用**: ${part.name}\n\`\`\`\n${JSON.stringify(part.input, null, 2)}\n\`\`\``)
|
||||
if (part.name === "show-components") {
|
||||
const ids = (part.input as { component_ids: string[] }).component_ids
|
||||
const components = await ComponentService.getComponentsByIds(ids)
|
||||
appendMessage = {
|
||||
role: 'card',
|
||||
components: components.filter(c => !!c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
content = contentParts.join('\n\n')
|
||||
}
|
||||
|
||||
res.push({
|
||||
...message,
|
||||
content
|
||||
})
|
||||
if (appendMessage) {
|
||||
res.push(appendMessage)
|
||||
appendMessage = null
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json(res)
|
||||
|
||||
} catch (error) {
|
||||
console.error('获取对话详情错误:', error)
|
||||
return NextResponse.json(
|
||||
{ message: '获取对话详情失败' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
// 验证用户身份
|
||||
const user = await getUser(request)
|
||||
if (!user) {
|
||||
return NextResponse.json(
|
||||
{ message: '请先登录' },
|
||||
{ status: 401 }
|
||||
)
|
||||
}
|
||||
|
||||
const client = new AIClient()
|
||||
const success = await client.deleteConversation(params.id, user.id)
|
||||
|
||||
if (!success) {
|
||||
return NextResponse.json(
|
||||
{ message: '对话不存在或无权限删除' },
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
|
||||
return NextResponse.json({ message: '对话删除成功' })
|
||||
|
||||
} catch (error) {
|
||||
console.error('删除对话错误:', error)
|
||||
return NextResponse.json(
|
||||
{ message: '删除对话失败' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
// 验证用户身份
|
||||
const user = await getUser(request)
|
||||
if (!user) {
|
||||
return NextResponse.json(
|
||||
{ message: '请先登录' },
|
||||
{ status: 401 }
|
||||
)
|
||||
}
|
||||
|
||||
const { title } = await request.json()
|
||||
if (!title || typeof title !== 'string') {
|
||||
return NextResponse.json(
|
||||
{ message: '请提供有效的标题' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
const client = new AIClient()
|
||||
const success = await client.updateConversationTitle(params.id, user.id, title)
|
||||
|
||||
if (!success) {
|
||||
return NextResponse.json(
|
||||
{ message: '对话不存在或无权限修改' },
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
|
||||
return NextResponse.json({ message: '标题更新成功' })
|
||||
|
||||
} catch (error) {
|
||||
console.error('更新对话标题错误:', error)
|
||||
return NextResponse.json(
|
||||
{ message: '更新对话标题失败' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user