Switch to @vercel/kv
This commit is contained in:
parent
45d3a8d0ee
commit
fc79a708f4
20 changed files with 188 additions and 1079 deletions
|
|
@ -1,8 +1,6 @@
|
|||
"use server";
|
||||
|
||||
import { auth } from "@/auth";
|
||||
import { chats, db } from "@/lib/db/schema";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { kv } from "@vercel/kv";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
export async function removeChat({
|
||||
|
|
@ -14,18 +12,15 @@ export async function removeChat({
|
|||
userId: string;
|
||||
path: string;
|
||||
}) {
|
||||
// @todo next-auth doesn't work in server actions yet
|
||||
// @todo next-auth@v5 doesn't work in server actions yet
|
||||
// const session = await auth();
|
||||
|
||||
// const userId = session?.user?.email;
|
||||
// if (!userId || !id) {
|
||||
// throw new Error("Unauthorized");
|
||||
// }
|
||||
|
||||
await db
|
||||
.delete(chats)
|
||||
.where(and(eq(chats.id, id), eq(chats.userId, userId)))
|
||||
.execute();
|
||||
const uid = await kv.hget<string>(`chat:${id}`, "userId");
|
||||
if (uid !== userId) {
|
||||
throw new Error("Unauthorized");
|
||||
}
|
||||
await kv.del(`chat:${id}`);
|
||||
await kv.zrem(`user:chat:${userId}`, `chat:${id}`);
|
||||
|
||||
revalidatePath("/");
|
||||
revalidatePath("/chat/[id]");
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { auth } from "@/auth";
|
||||
import { chats, db } from "@/lib/db/schema";
|
||||
import { kv } from "@vercel/kv";
|
||||
import { nanoid } from "@/lib/utils";
|
||||
import { OpenAIStream, StreamingTextResponse } from "ai-connector";
|
||||
import { Configuration, OpenAIApi } from "openai-edge";
|
||||
|
||||
|
|
@ -41,10 +42,14 @@ export const POST = auth(async function POST(req: Request) {
|
|||
return;
|
||||
}
|
||||
const title = json.messages[0].content.substring(0, 20);
|
||||
const userId = (req as any).auth?.user?.email;
|
||||
const id = json.id ?? nanoid();
|
||||
const createdAt = Date.now();
|
||||
const payload = {
|
||||
id: crypto.randomUUID(),
|
||||
id,
|
||||
title,
|
||||
userId: (req as any).auth?.user?.email,
|
||||
userId,
|
||||
createdAt,
|
||||
messages: [
|
||||
...messages,
|
||||
{
|
||||
|
|
@ -53,15 +58,11 @@ export const POST = auth(async function POST(req: Request) {
|
|||
},
|
||||
],
|
||||
};
|
||||
const chat = await db
|
||||
.insert(chats)
|
||||
.values(payload)
|
||||
// .onConflictDoUpdate({
|
||||
// target: payload.id,
|
||||
// set: payload,
|
||||
// })
|
||||
.returning();
|
||||
console.log(chat);
|
||||
await kv.hmset(`chat:${id}`, payload);
|
||||
await kv.zadd(`user:chat:${userId}`, {
|
||||
score: createdAt,
|
||||
member: `chat:${id}`,
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
import { Sidebar } from "@/app/sidebar";
|
||||
|
||||
import { type Metadata } from "next";
|
||||
import { auth } from "@/auth";
|
||||
import { db, chats } from "@/lib/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { type Metadata } from "next";
|
||||
|
||||
import { Chat } from "@/app/chat";
|
||||
import { type Chat as ChatType } from "@/lib/types";
|
||||
import { kv } from "@vercel/kv";
|
||||
import { Message } from "ai-connector";
|
||||
|
||||
export const runtime = "edge";
|
||||
|
|
@ -18,9 +19,8 @@ export interface ChatPageProps {
|
|||
export async function generateMetadata({
|
||||
params,
|
||||
}: ChatPageProps): Promise<Metadata> {
|
||||
const chat = await db.query.chats.findFirst({
|
||||
where: eq(chats.id, params.id),
|
||||
});
|
||||
const session = await auth();
|
||||
const chat = await getChat(params.id, session?.user?.email ?? "");
|
||||
return {
|
||||
title: chat?.title.slice(0, 50) ?? "Chat",
|
||||
};
|
||||
|
|
@ -28,13 +28,7 @@ export async function generateMetadata({
|
|||
|
||||
export default async function ChatPage({ params }: ChatPageProps) {
|
||||
const session = await auth();
|
||||
const chat = await db.query.chats.findFirst({
|
||||
where: eq(chats.id, params.id),
|
||||
});
|
||||
|
||||
if (!chat) {
|
||||
throw new Error("Chat not found");
|
||||
}
|
||||
const chat = await getChat(params.id, session?.user?.email ?? "");
|
||||
|
||||
return (
|
||||
<div className="relative flex h-full w-full overflow-hidden">
|
||||
|
|
@ -47,3 +41,16 @@ export default async function ChatPage({ params }: ChatPageProps) {
|
|||
}
|
||||
|
||||
ChatPage.displayName = "ChatPage";
|
||||
|
||||
async function getChat(id: string, userId: string) {
|
||||
const chat = await kv.hgetall<ChatType>(`chat:${id}`);
|
||||
if (!chat) {
|
||||
throw new Error("Not found");
|
||||
}
|
||||
|
||||
if (userId && chat.userId !== userId) {
|
||||
throw new Error("Unauthorized");
|
||||
}
|
||||
|
||||
return chat;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,14 @@
|
|||
import { Login } from "@/components/ui/login";
|
||||
import { NextChatLogo } from "@/components/ui/nextchat-logo";
|
||||
import { UserMenu } from "@/components/ui/user-menu";
|
||||
import { db, chats } from "@/lib/db/schema";
|
||||
import { kv } from "@vercel/kv";
|
||||
import { Chat } from "@/lib/types";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { type Session } from "@auth/nextjs/types";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { Plus } from "lucide-react";
|
||||
import { unstable_cache } from "next/cache";
|
||||
import Link from "next/link";
|
||||
import { SidebarItem } from "./sidebar-item";
|
||||
import { ExternalLink } from "./external-link";
|
||||
import { SidebarItem } from "./sidebar-item";
|
||||
|
||||
export interface SidebarProps {
|
||||
session?: Session;
|
||||
|
|
@ -87,23 +86,7 @@ export function Sidebar({ session, newChat }: SidebarProps) {
|
|||
Sidebar.displayName = "Sidebar";
|
||||
|
||||
async function SidebarList({ session }: { session?: Session }) {
|
||||
const results: any[] = await (
|
||||
await unstable_cache(
|
||||
() =>
|
||||
db.query.chats.findMany({
|
||||
columns: {
|
||||
id: true,
|
||||
title: true,
|
||||
},
|
||||
where: eq(chats.userId, session?.user?.email || ""),
|
||||
}),
|
||||
// @ts-ignore
|
||||
[session?.user?.email ?? ""],
|
||||
{
|
||||
revalidate: 3600,
|
||||
}
|
||||
)
|
||||
)();
|
||||
const results: Chat[] = await getChats(session?.user?.email ?? "");
|
||||
|
||||
return results.map((c) => (
|
||||
<SidebarItem
|
||||
|
|
@ -115,3 +98,20 @@ async function SidebarList({ session }: { session?: Session }) {
|
|||
/>
|
||||
));
|
||||
}
|
||||
|
||||
async function getChats(userId: string) {
|
||||
try {
|
||||
const pipeline = kv.pipeline();
|
||||
const chats: string[] = await kv.zrange(`user:chat:${userId}`, 0, -1);
|
||||
|
||||
for (const chat of chats) {
|
||||
pipeline.hgetall(chat);
|
||||
}
|
||||
|
||||
const results = await pipeline.exec();
|
||||
|
||||
return results as Chat[];
|
||||
} catch (error) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue