Initial open-source release
This commit is contained in:
66
src/ipc/handlers/chat_handlers.ts
Normal file
66
src/ipc/handlers/chat_handlers.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { ipcMain } from "electron";
|
||||
import { db } from "../../db";
|
||||
import { chats } from "../../db/schema";
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
import type { ChatSummary } from "../../lib/schemas";
|
||||
|
||||
export function registerChatHandlers() {
|
||||
ipcMain.handle("create-chat", async (_, appId: number) => {
|
||||
// Create a new chat
|
||||
const [chat] = await db
|
||||
.insert(chats)
|
||||
.values({
|
||||
appId,
|
||||
})
|
||||
.returning();
|
||||
|
||||
return chat.id;
|
||||
});
|
||||
|
||||
ipcMain.handle("get-chat", async (_, chatId: number) => {
|
||||
const chat = await db.query.chats.findFirst({
|
||||
where: eq(chats.id, chatId),
|
||||
with: {
|
||||
messages: {
|
||||
orderBy: (messages, { asc }) => [asc(messages.createdAt)],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!chat) {
|
||||
throw new Error("Chat not found");
|
||||
}
|
||||
|
||||
return chat;
|
||||
});
|
||||
|
||||
ipcMain.handle(
|
||||
"get-chats",
|
||||
async (_, appId?: number): Promise<ChatSummary[]> => {
|
||||
// If appId is provided, filter chats for that app
|
||||
const query = appId
|
||||
? db.query.chats.findMany({
|
||||
where: eq(chats.appId, appId),
|
||||
columns: {
|
||||
id: true,
|
||||
title: true,
|
||||
createdAt: true,
|
||||
appId: true,
|
||||
},
|
||||
orderBy: [desc(chats.createdAt)],
|
||||
})
|
||||
: db.query.chats.findMany({
|
||||
columns: {
|
||||
id: true,
|
||||
title: true,
|
||||
createdAt: true,
|
||||
appId: true,
|
||||
},
|
||||
orderBy: [desc(chats.createdAt)],
|
||||
});
|
||||
|
||||
const allChats = await query;
|
||||
return allChats;
|
||||
}
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user