-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchatHistory.ts
More file actions
169 lines (153 loc) · 4.52 KB
/
chatHistory.ts
File metadata and controls
169 lines (153 loc) · 4.52 KB
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
"use server";
import { headers } from "next/headers";
import { getAuthServer } from "./auth";
import { getDrizzle } from "./drizzle";
import { chat, message } from "@/schema/chat";
import { and, asc, eq } from "drizzle-orm";
import { Auth } from "better-auth";
import { revalidateTag, unstable_cacheLife } from "next/cache";
import { isCloudflare } from "./detectCloudflare";
import { unstable_cacheTag } from "next/cache";
export interface CreateChatMessage {
role: "user" | "ai" | "error";
content: string;
}
// cacheに使うキーで、実際のURLではない
const CACHE_KEY_BASE = "https://my-code.utcode.net/chatHistory";
interface Context {
drizzle: Awaited<ReturnType<typeof getDrizzle>>;
auth: Auth;
userId?: string;
}
/**
* drizzleが初期化されてなければ初期化し、
* authが初期化されてなければ初期化し、
* userIdがなければセッションから取得してセットする。
*/
export async function initContext(ctx?: Partial<Context>): Promise<Context> {
if (!ctx) {
ctx = {};
}
if (!ctx.drizzle) {
ctx.drizzle = await getDrizzle();
}
if (!ctx.auth) {
ctx.auth = await getAuthServer(ctx.drizzle);
}
if (!ctx.userId) {
const session = await ctx.auth.api.getSession({
headers: await headers(),
});
if (session) {
ctx.userId = session.user.id;
}
}
return ctx as Context;
}
export async function addChat(
docsId: string,
sectionId: string,
messages: CreateChatMessage[],
context?: Partial<Context>
) {
const { drizzle, userId } = await initContext(context);
if (!userId) {
throw new Error("Not authenticated");
}
const [newChat] = await drizzle
.insert(chat)
.values({
userId,
docsId,
sectionId,
})
.returning();
const chatMessages = await drizzle
.insert(message)
.values(
messages.map((msg) => ({
chatId: newChat.chatId,
role: msg.role,
content: msg.content,
}))
)
.returning();
revalidateTag(`${CACHE_KEY_BASE}/getChat?docsId=${docsId}&userId=${userId}`);
if (isCloudflare()) {
const cache = await caches.open("chatHistory");
console.log(
`deleting cache for chatHistory/getChat for user ${userId} and docs ${docsId}`
);
await cache.delete(
`${CACHE_KEY_BASE}/getChat?docsId=${docsId}&userId=${userId}`
);
}
return {
...newChat,
messages: chatMessages,
};
}
export type ChatWithMessages = Awaited<ReturnType<typeof addChat>>;
export async function getChat(
docsId: string,
context?: Partial<Context>
): Promise<ChatWithMessages[]> {
const { drizzle, userId } = await initContext(context);
if (!userId) {
return [];
}
const chats = await drizzle.query.chat.findMany({
where: and(eq(chat.userId, userId), eq(chat.docsId, docsId)),
with: {
messages: {
orderBy: [asc(message.createdAt)],
},
},
orderBy: [asc(chat.createdAt)],
});
if (isCloudflare()) {
const cache = await caches.open("chatHistory");
await cache.put(
`${CACHE_KEY_BASE}/getChat?docsId=${docsId}&userId=${userId}`,
new Response(JSON.stringify(chats), {
headers: { "Cache-Control": "max-age=86400, s-maxage=86400" },
})
);
}
return chats;
}
export async function getChatFromCache(docsId: string, context: Context) {
"use cache";
unstable_cacheLife("days");
// cacheされる関数の中でheader()にはアクセスできない。
// なので外でinitContext()を呼んだものを引数に渡す必要がある。
// しかし、drizzleオブジェクトは外から渡せないのでgetChatの中で改めてinitContext()を呼んでdrizzleだけ再初期化している
const { auth, userId } = context;
unstable_cacheTag(
`${CACHE_KEY_BASE}/getChat?docsId=${docsId}&userId=${userId}`
);
if (!userId) {
return [];
}
if (isCloudflare()) {
const cache = await caches.open("chatHistory");
const cachedResponse = await cache.match(
`${CACHE_KEY_BASE}/getChat?docsId=${docsId}&userId=${userId}`
);
if (cachedResponse) {
console.log("Cache hit for chatHistory/getChat");
const data = (await cachedResponse.json()) as ChatWithMessages[];
return data;
} else {
console.log("Cache miss for chatHistory/getChat");
}
}
return await getChat(docsId, { auth, userId });
}
export async function migrateChatUser(oldUserId: string, newUserId: string) {
const drizzle = await getDrizzle();
await drizzle
.update(chat)
.set({ userId: newUserId })
.where(eq(chat.userId, oldUserId));
}