feat(api): 添加对话数据导出和清除 API
- 新增 GET /api/conversations/export 导出所有对话数据 - 新增 GET /api/conversations/all 获取对话统计信息 - 新增 DELETE /api/conversations/all 清除所有对话和消息
This commit is contained in:
parent
8aab630af6
commit
199772a95d
52
src/app/api/conversations/all/route.ts
Normal file
52
src/app/api/conversations/all/route.ts
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import { NextResponse } from 'next/server';
|
||||||
|
import { db } from '@/drizzle/db';
|
||||||
|
import { conversations, messages } from '@/drizzle/schema';
|
||||||
|
import { sql } from 'drizzle-orm';
|
||||||
|
|
||||||
|
// GET /api/conversations/all - 获取统计信息
|
||||||
|
export async function GET() {
|
||||||
|
try {
|
||||||
|
// 获取对话数量
|
||||||
|
const conversationCount = await db
|
||||||
|
.select({ count: sql<number>`count(*)` })
|
||||||
|
.from(conversations);
|
||||||
|
|
||||||
|
// 获取消息数量
|
||||||
|
const messageCount = await db
|
||||||
|
.select({ count: sql<number>`count(*)` })
|
||||||
|
.from(messages);
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
conversationCount: Number(conversationCount[0]?.count || 0),
|
||||||
|
messageCount: Number(messageCount[0]?.count || 0),
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to get stats:', error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: 'Failed to get stats' },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DELETE /api/conversations/all - 清除所有对话和消息
|
||||||
|
export async function DELETE() {
|
||||||
|
try {
|
||||||
|
// 先删除所有消息
|
||||||
|
await db.delete(messages);
|
||||||
|
|
||||||
|
// 再删除所有对话
|
||||||
|
await db.delete(conversations);
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
success: true,
|
||||||
|
message: 'All conversations and messages have been deleted',
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to delete all conversations:', error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: 'Failed to delete all conversations' },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
73
src/app/api/conversations/export/route.ts
Normal file
73
src/app/api/conversations/export/route.ts
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
import { NextResponse } from 'next/server';
|
||||||
|
import { db } from '@/drizzle/db';
|
||||||
|
import { conversations, messages } from '@/drizzle/schema';
|
||||||
|
import { desc, eq } from 'drizzle-orm';
|
||||||
|
|
||||||
|
// GET /api/conversations/export - 导出所有对话数据
|
||||||
|
export async function GET() {
|
||||||
|
try {
|
||||||
|
// 获取所有对话
|
||||||
|
const allConversations = await db.query.conversations.findMany({
|
||||||
|
orderBy: [desc(conversations.createdAt)],
|
||||||
|
});
|
||||||
|
|
||||||
|
// 获取所有消息
|
||||||
|
const allMessages = await db.query.messages.findMany({
|
||||||
|
orderBy: [desc(messages.createdAt)],
|
||||||
|
});
|
||||||
|
|
||||||
|
// 按对话组织数据
|
||||||
|
const exportData = {
|
||||||
|
exportedAt: new Date().toISOString(),
|
||||||
|
version: '1.0',
|
||||||
|
totalConversations: allConversations.length,
|
||||||
|
totalMessages: allMessages.length,
|
||||||
|
conversations: allConversations.map((conv) => {
|
||||||
|
// 获取该对话的所有消息
|
||||||
|
const convMessages = allMessages
|
||||||
|
.filter((msg) => msg.conversationId === conv.conversationId)
|
||||||
|
.sort((a, b) => new Date(a.createdAt!).getTime() - new Date(b.createdAt!).getTime())
|
||||||
|
.map((msg) => ({
|
||||||
|
messageId: msg.messageId,
|
||||||
|
role: msg.role,
|
||||||
|
content: msg.content,
|
||||||
|
thinkingContent: msg.thinkingContent,
|
||||||
|
toolCalls: msg.toolCalls,
|
||||||
|
toolResults: msg.toolResults,
|
||||||
|
inputTokens: msg.inputTokens,
|
||||||
|
outputTokens: msg.outputTokens,
|
||||||
|
status: msg.status,
|
||||||
|
feedback: msg.feedback,
|
||||||
|
createdAt: msg.createdAt,
|
||||||
|
}));
|
||||||
|
|
||||||
|
return {
|
||||||
|
conversationId: conv.conversationId,
|
||||||
|
title: conv.title,
|
||||||
|
summary: conv.summary,
|
||||||
|
model: conv.model,
|
||||||
|
tools: conv.tools,
|
||||||
|
enableThinking: conv.enableThinking,
|
||||||
|
systemPrompt: conv.systemPrompt,
|
||||||
|
temperature: conv.temperature,
|
||||||
|
messageCount: conv.messageCount,
|
||||||
|
totalTokens: conv.totalTokens,
|
||||||
|
isArchived: conv.isArchived,
|
||||||
|
isPinned: conv.isPinned,
|
||||||
|
createdAt: conv.createdAt,
|
||||||
|
updatedAt: conv.updatedAt,
|
||||||
|
lastMessageAt: conv.lastMessageAt,
|
||||||
|
messages: convMessages,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
|
||||||
|
return NextResponse.json(exportData);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to export conversations:', error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: 'Failed to export conversations' },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user