- Update .gitignore to exclude database files, extensions, and .cursor directory - Enhance chat streaming API with safe enqueue and close functions to prevent errors - Integrate ConversationSidebar component into ClientHome for better UI - Add dependencies like better-sqlite3 and esbuild packages for database functionality This commit introduces database persistence for conversations, improves error handling in streaming responses, and adds a sidebar for managing chats, enhancing overall app robustness and user experience.
55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getDatabase } from "@/database/client";
|
|
import { ConversationRepository } from "@/database/repositories/conversation";
|
|
import { MessageRepository } from "@/database/repositories/message";
|
|
import { generateChatTitle } from "@/lib/embeddings";
|
|
|
|
export async function GET() {
|
|
try {
|
|
const db = getDatabase();
|
|
const conversationRepo = new ConversationRepository(db);
|
|
|
|
const conversations = conversationRepo.findAll();
|
|
|
|
return NextResponse.json(conversations);
|
|
} catch (error) {
|
|
console.error("Erro ao buscar conversas:", error);
|
|
return NextResponse.json(
|
|
{ error: "Erro interno do servidor" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const { model, firstMessage } = await request.json();
|
|
|
|
if (!model) {
|
|
return NextResponse.json(
|
|
{ error: "Modelo é obrigatório" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const db = getDatabase();
|
|
const conversationRepo = new ConversationRepository(db);
|
|
|
|
// Gerar título se primeira mensagem fornecida
|
|
let title = "Nova conversa";
|
|
if (firstMessage) {
|
|
title = await generateChatTitle(firstMessage);
|
|
}
|
|
|
|
const conversationId = conversationRepo.create(model, title);
|
|
|
|
return NextResponse.json({ id: conversationId, title });
|
|
} catch (error) {
|
|
console.error("Erro ao criar conversa:", error);
|
|
return NextResponse.json(
|
|
{ error: "Erro interno do servidor" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|