claude-code-cchui/src/app/api/conversations/[id]/route.ts
gaoziman a5fcc9edae feat(对话): 扩展对话管理支持助手关联
- 对话创建接口支持关联助手ID和系统提示词
- 对话查询接口返回关联的助手信息
- 聊天接口支持使用助手系统提示词
- useConversations Hook 扩展助手相关参数
2025-12-20 20:46:35 +08:00

184 lines
4.6 KiB
TypeScript

import { NextResponse } from 'next/server';
import { db } from '@/drizzle/db';
import { conversations, messages, assistants } from '@/drizzle/schema';
import { eq, asc, and } from 'drizzle-orm';
import { getCurrentUser } from '@/lib/auth';
interface RouteParams {
params: Promise<{ id: string }>;
}
// GET /api/conversations/[id] - 获取单个对话及其消息
export async function GET(request: Request, { params }: RouteParams) {
try {
// 获取当前用户
const user = await getCurrentUser();
if (!user) {
return NextResponse.json(
{ error: '未登录' },
{ status: 401 }
);
}
const { id } = await params;
const conversation = await db.query.conversations.findFirst({
where: and(
eq(conversations.conversationId, id),
eq(conversations.userId, user.userId)
),
});
if (!conversation) {
return NextResponse.json(
{ error: 'Conversation not found' },
{ status: 404 }
);
}
const messageList = await db.query.messages.findMany({
where: eq(messages.conversationId, id),
orderBy: [asc(messages.createdAt)],
});
// 如果有关联助手,获取助手信息
let assistant = null;
if (conversation.assistantId) {
assistant = await db.query.assistants.findFirst({
where: eq(assistants.id, conversation.assistantId),
});
}
return NextResponse.json({
...conversation,
messages: messageList,
assistant: assistant ? {
id: assistant.id,
name: assistant.name,
icon: assistant.icon,
description: assistant.description,
} : null,
});
} catch (error) {
console.error('Failed to get conversation:', error);
return NextResponse.json(
{ error: 'Failed to get conversation' },
{ status: 500 }
);
}
}
// PUT /api/conversations/[id] - 更新对话
export async function PUT(request: Request, { params }: RouteParams) {
try {
// 获取当前用户
const user = await getCurrentUser();
if (!user) {
return NextResponse.json(
{ error: '未登录' },
{ status: 401 }
);
}
const { id } = await params;
const body = await request.json();
const { title, isPinned, isArchived, model } = body;
// 验证对话属于当前用户
const existingConversation = await db.query.conversations.findFirst({
where: and(
eq(conversations.conversationId, id),
eq(conversations.userId, user.userId)
),
});
if (!existingConversation) {
return NextResponse.json(
{ error: 'Conversation not found' },
{ status: 404 }
);
}
const updateData: Record<string, unknown> = {
updatedAt: new Date(),
};
if (title !== undefined) {
updateData.title = title;
}
if (isPinned !== undefined) {
updateData.isPinned = isPinned;
}
if (isArchived !== undefined) {
updateData.isArchived = isArchived;
}
if (model !== undefined) {
updateData.model = model;
}
const [updated] = await db
.update(conversations)
.set(updateData)
.where(eq(conversations.conversationId, id))
.returning();
return NextResponse.json(updated);
} catch (error) {
console.error('Failed to update conversation:', error);
return NextResponse.json(
{ error: 'Failed to update conversation' },
{ status: 500 }
);
}
}
// DELETE /api/conversations/[id] - 删除对话
export async function DELETE(request: Request, { params }: RouteParams) {
try {
// 获取当前用户
const user = await getCurrentUser();
if (!user) {
return NextResponse.json(
{ error: '未登录' },
{ status: 401 }
);
}
const { id } = await params;
// 验证对话属于当前用户
const existingConversation = await db.query.conversations.findFirst({
where: and(
eq(conversations.conversationId, id),
eq(conversations.userId, user.userId)
),
});
if (!existingConversation) {
return NextResponse.json(
{ error: 'Conversation not found' },
{ status: 404 }
);
}
// 先删除相关消息
await db.delete(messages).where(eq(messages.conversationId, id));
// 再删除对话
await db
.delete(conversations)
.where(eq(conversations.conversationId, id));
return NextResponse.json({ success: true });
} catch (error) {
console.error('Failed to delete conversation:', error);
return NextResponse.json(
{ error: 'Failed to delete conversation' },
{ status: 500 }
);
}
}