-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrevalidateChat.ts
More file actions
39 lines (36 loc) · 1.21 KB
/
revalidateChat.ts
File metadata and controls
39 lines (36 loc) · 1.21 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
"use server";
import { initContext, revalidateChat } from "@/lib/chatHistory";
import { PagePath, PagePathSchema } from "@/lib/docs";
import { setExtra, withServerActionInstrumentation } from "@sentry/nextjs";
import { headers } from "next/headers";
import { z } from "zod";
export async function revalidateChatAction(
chatId: string,
pagePath: string | PagePath
) {
return withServerActionInstrumentation(
"revalidateChatAction", // Action name for Sentry
{
headers: await headers(), // Connect client and server traces
recordResponse: true, // Include response data
},
async () => {
setExtra("args", { chatId, pagePath });
chatId = z.uuid().parse(chatId);
if (typeof pagePath === "string") {
if (!/^[a-z0-9_-]+\/[a-z0-9_-]+$/.test(pagePath)) {
throw new Error("Invalid pagePath format");
}
const [lang, page] = pagePath.split("/");
pagePath = PagePathSchema.parse({ lang, page });
} else {
pagePath = PagePathSchema.parse(pagePath);
}
const ctx = await initContext();
if (!ctx.userId) {
throw new Error("Not authenticated");
}
await revalidateChat(chatId, ctx.userId, pagePath);
}
);
}