Skip to content

Commit caa072c

Browse files
committed
feat: Models Ai chat integration
1 parent de88e30 commit caa072c

15 files changed

Lines changed: 1669 additions & 229 deletions

File tree

bun.lock

Lines changed: 46 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/actions/getChat.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"use server";
2+
3+
import { auth } from "@/server/auth";
4+
import { db } from "@/server/db";
5+
6+
type MessageRole = "user" | "system" | "assistant" | "data";
7+
8+
export async function getChat(chatId: string) {
9+
try {
10+
const authResult = await auth();
11+
if (!authResult?.user?.id) {
12+
return { success: false, error: "Unauthorized" };
13+
}
14+
15+
const userId = authResult.user.id;
16+
17+
18+
const chat = await db.chat.findFirst({
19+
where: {
20+
id: chatId,
21+
userId,
22+
},
23+
include: {
24+
messages: {
25+
orderBy: {
26+
createdAt: "asc",
27+
},
28+
},
29+
},
30+
});
31+
32+
if (!chat) {
33+
return { success: false, error: "Chat not found" };
34+
}
35+
36+
37+
const formattedMessages = chat.messages.map((message) => ({
38+
id: message.id,
39+
role: message.role as MessageRole,
40+
content: message.content,
41+
}));
42+
43+
return {
44+
success: true,
45+
chat: {
46+
id: chat.id,
47+
messages: formattedMessages,
48+
},
49+
};
50+
} catch (error) {
51+
console.error("Failed to get chat:", error);
52+
return { success: false, error: "Failed to get chat" };
53+
}
54+
}

src/actions/getChats.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"use server";
2+
3+
import { auth } from "@/server/auth";
4+
import { db } from "@/server/db";
5+
6+
export async function getChats() {
7+
try {
8+
const authResult = await auth();
9+
if (!authResult?.user?.id) {
10+
return { success: false, error: "Unauthorized", chats: [] };
11+
}
12+
13+
const userId = authResult.user.id;
14+
15+
16+
const chats = await db.chat.findMany({
17+
where: {
18+
userId,
19+
},
20+
include: {
21+
messages: {
22+
take: 1,
23+
orderBy: {
24+
createdAt: "desc",
25+
},
26+
},
27+
},
28+
orderBy: {
29+
updatedAt: "desc",
30+
},
31+
});
32+
33+
return {
34+
success: true,
35+
chats: chats.map((chat) => ({
36+
id: chat.id,
37+
createdAt: chat.createdAt,
38+
updatedAt: chat.updatedAt,
39+
lastMessage: chat.messages[0]?.content ?? "",
40+
})),
41+
};
42+
} catch (error) {
43+
console.error("Failed to get chats:", error);
44+
return { success: false, error: "Failed to get chats", chats: [] };
45+
}
46+
}

src/actions/saveChat.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"use server";
2+
3+
import { auth } from "@/server/auth";
4+
import { db } from "@/server/db";
5+
6+
type MessageRole = "user" | "system" | "assistant" | "data";
7+
8+
interface Message {
9+
id: string;
10+
role: MessageRole;
11+
content: string;
12+
}
13+
14+
export async function saveChat(messages: Message[]) {
15+
try {
16+
const authResult = await auth();
17+
if (!authResult?.user?.id) {
18+
return { success: false, error: "Unauthorized" };
19+
}
20+
21+
const userId = authResult.user.id;
22+
23+
24+
let chat = await db.chat.findFirst({
25+
where: {
26+
userId,
27+
messages: {
28+
some: {
29+
id: messages[0]?.id,
30+
},
31+
},
32+
},
33+
});
34+
35+
if (!chat) {
36+
37+
chat ??= await db.chat.create({
38+
data: {
39+
userId,
40+
},
41+
});
42+
}
43+
44+
45+
for (const message of messages) {
46+
47+
const existingMessage = await db.message.findFirst({
48+
where: {
49+
id: message.id,
50+
chatId: chat.id,
51+
},
52+
});
53+
54+
if (!existingMessage) {
55+
await db.message.create({
56+
data: {
57+
id: message.id,
58+
chatId: chat.id,
59+
content: message.content,
60+
role: message.role,
61+
},
62+
});
63+
}
64+
}
65+
66+
return { success: true, chatId: chat.id };
67+
} catch (error) {
68+
console.error("Failed to save chat:", error);
69+
return { success: false, error: "Failed to save chat" };
70+
}
71+
}

0 commit comments

Comments
 (0)