Files
OllaHub/database/repositories/conversation.ts
T
Evandro Fonseca Junior 0cc7a81319 feat: add database support, conversation sidebar, and improve chat streaming
- 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.
2025-10-28 14:14:07 -03:00

79 lines
1.5 KiB
TypeScript

import Database from "better-sqlite3";
export interface Conversation {
id: string;
title: string | null;
model: string;
created_at: number;
updated_at: number;
}
export class ConversationRepository {
constructor(private db: Database.Database) {}
create(model: string, title?: string): string {
const id = crypto.randomUUID();
const now = Date.now();
this.db
.prepare(
`
INSERT INTO conversations (id, title, model, created_at, updated_at)
VALUES (?, ?, ?, ?, ?)
`
)
.run(id, title || "Nova conversa", model, now, now);
return id;
}
findAll(): Conversation[] {
return this.db
.prepare(
`
SELECT * FROM conversations
ORDER BY updated_at DESC
`
)
.all() as Conversation[];
}
findById(id: string): Conversation | null {
return this.db
.prepare(
`
SELECT * FROM conversations WHERE id = ?
`
)
.get(id) as Conversation | null;
}
updateTitle(id: string, title: string): void {
this.db
.prepare(
`
UPDATE conversations
SET title = ?, updated_at = ?
WHERE id = ?
`
)
.run(title, Date.now(), id);
}
updateTimestamp(id: string): void {
this.db
.prepare(
`
UPDATE conversations
SET updated_at = ?
WHERE id = ?
`
)
.run(Date.now(), id);
}
delete(id: string): void {
this.db.prepare("DELETE FROM conversations WHERE id = ?").run(id);
}
}