-
Notifications
You must be signed in to change notification settings - Fork 1
/
actions.ts
174 lines (151 loc) Β· 4.05 KB
/
actions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"use server";
import { AuthError } from "next-auth";
import { auth, signIn, signOut } from "./auth";
import { revalidatePath, revalidateTag, unstable_noStore } from "next/cache";
import { chatDB } from "./database/drizzle-chat-client";
import { chats, messages } from "./database/drizzle-chat-schema";
import { asc, desc, eq } from "drizzle-orm";
import { notFound } from "next/navigation";
export const signInWithEmail = async (formData: FormData) => {
const email = formData.get("email") as string;
try {
await signIn("email", { email, redirectTo: "/" });
} catch (error) {
if (error instanceof AuthError) {
console.log("π ~ login ~ error:", error);
}
throw error;
}
};
export const signInWithGoogle = async () => {
try {
await signIn("google", { redirectTo: "/" });
} catch (error) {
if (error instanceof AuthError) {
console.log({ error });
}
throw error;
}
};
export const signInWithGithub = async () => {
try {
await signIn("github", { redirectTo: "/" });
} catch (error) {
if (error instanceof AuthError) {
console.log({ error });
}
throw error;
}
};
export const signout = async () => {
try {
await signOut();
} catch (error) {
if (error instanceof AuthError) {
console.log("π ~ logout ~ error:", error);
}
throw error;
}
};
export const sendMessage = async (formData: FormData) => {
const message = formData.get("message") as unknown as string;
try {
console.log("Loading message π");
// await new Promise((resolve) => setTimeout(resolve, 2000));
// push message to database
console.log("π ~ sendMessage ~ message", message);
revalidatePath("/chat");
} catch (error) {
console.log("π ~ sendMessage ~ error", error);
throw error;
}
};
export const createChat = async (name: string) => {
const session = await auth();
const chatId = crypto.randomUUID();
console.log("π ~ createChat ~ chatId", chatId);
console.log("π ~ createChat ~ name", name);
await chatDB.insert(chats).values({
ownerId: session?.user?.id!,
id: chatId,
createdAt: new Date(),
name,
});
return chatId;
};
export const getChat = async (chatId: string) => {
const chat = await chatDB.query.chats.findFirst({
where: eq(chats.id, chatId),
});
if (!chat || chat.name === undefined) {
notFound();
}
return chat;
};
export const createMessages = async ({
chatId,
prompt,
completion,
}: {
chatId: string;
prompt: string;
completion: string;
}) => {
await chatDB.insert(messages).values([
{
id: crypto.randomUUID(),
chatId,
content: prompt,
role: "user",
createdAt: new Date(),
},
{
id: crypto.randomUUID(),
chatId,
content: completion,
role: "assistant",
createdAt: new Date(),
},
]);
};
export const getUserChatsList = async (userId: string) =>
await chatDB
.select()
.from(chats)
.where(eq(chats.ownerId, userId))
.orderBy(desc(chats.createdAt));
export const getChatMessages = async (chatId: string) => {
// the chatId generated by `crypto.randomUUID()` is 36 characters long
if (chatId?.length !== 36) {
notFound();
}
const msgs = await chatDB
.select()
.from(messages)
.where(eq(messages.chatId, chatId))
.orderBy(asc(messages.createdAt));
if (msgs.length === 0) {
notFound();
}
return msgs;
};
export const revalidateChatsList = async () => {
revalidateTag("user-chats-list");
};
export const revalidateChatMessages = async (chatId: string) => {
revalidateTag(`chat-messages-${chatId}`);
};
export const revalidateMessages = async () => {
revalidatePath("/", "page");
};
export const revalidate = async (path: string) => {
revalidatePath(path, "page");
};
export const deleteChatMessages = async (chatId: string) => {
await chatDB.delete(messages).where(eq(messages.chatId, chatId));
revalidateChatMessages(chatId);
};
export const deleteChat = async (chatId: string) => {
await deleteChatMessages(chatId);
await chatDB.delete(chats).where(eq(chats.id, chatId));
};