Try drizzle
This commit is contained in:
parent
17bfd0cac1
commit
c332a1b6f1
10 changed files with 107 additions and 47 deletions
|
|
@ -10,13 +10,13 @@ export const POST = auth(async function POST(req: Request) {
|
|||
const json = await req.json();
|
||||
// @ts-ignore
|
||||
console.log(req.auth); // todo fix types
|
||||
|
||||
const messages = json.messages.map((m: any) => ({
|
||||
content: m.content,
|
||||
role: m.role,
|
||||
}));
|
||||
const res = await openai.createChatCompletion({
|
||||
model: "gpt-3.5-turbo",
|
||||
messages: json.messages.map((m: any) => ({
|
||||
content: m.content,
|
||||
role: m.role,
|
||||
})),
|
||||
messages,
|
||||
temperature: 0.7,
|
||||
top_p: 1,
|
||||
frequency_penalty: 1,
|
||||
|
|
@ -33,23 +33,26 @@ export const POST = auth(async function POST(req: Request) {
|
|||
}
|
||||
const title = json.messages[0].content.substring(0, 20);
|
||||
const payload = {
|
||||
id: json.id ?? crypto.randomUUID(),
|
||||
id: crypto.randomUUID(),
|
||||
title,
|
||||
userId: (req as any).auth?.user?.email,
|
||||
messages: json.messages.concat({
|
||||
content: completion,
|
||||
role: "assistant",
|
||||
id: nanoid(),
|
||||
}),
|
||||
messages: [
|
||||
...messages,
|
||||
{
|
||||
content: completion,
|
||||
role: "assistant",
|
||||
},
|
||||
],
|
||||
};
|
||||
await db
|
||||
const chat = await db
|
||||
.insert(chats)
|
||||
.values(payload)
|
||||
.onConflictDoUpdate({
|
||||
target: json.id,
|
||||
set: payload,
|
||||
})
|
||||
.execute();
|
||||
// .onConflictDoUpdate({
|
||||
// target: payload.id,
|
||||
// set: payload,
|
||||
// })
|
||||
.returning();
|
||||
console.log(chat);
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
"use client";
|
||||
|
||||
import { type Message } from "@prisma/client";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { ChatList } from "./chat-list";
|
||||
import { Prompt } from "./prompt";
|
||||
import { useChat } from "ai-connector";
|
||||
import { useChat, type Message } from "ai-connector";
|
||||
|
||||
export interface ChatProps {
|
||||
// create?: (input: string) => Chat | undefined;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
import { Sidebar } from "@/app/sidebar";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
import { Chat } from "../../chat";
|
||||
import { type Metadata } from "next";
|
||||
import { auth } from "@/auth";
|
||||
|
||||
import { db, chats } from "@/lib/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { Chat } from "@/app/chat";
|
||||
import { Message } from "ai-connector";
|
||||
export interface ChatPageProps {
|
||||
params: {
|
||||
id: string;
|
||||
|
|
@ -14,10 +15,8 @@ export interface ChatPageProps {
|
|||
export async function generateMetadata({
|
||||
params,
|
||||
}: ChatPageProps): Promise<Metadata> {
|
||||
const chat = await prisma.chat.findFirst({
|
||||
where: {
|
||||
id: params.id,
|
||||
},
|
||||
const chat = await db.query.chats.findFirst({
|
||||
where: eq(chats.id, params.id),
|
||||
});
|
||||
return {
|
||||
title: chat?.title.slice(0, 50) ?? "Chat",
|
||||
|
|
@ -30,14 +29,10 @@ export const preferredRegion = "home";
|
|||
export const dynamic = "force-dynamic";
|
||||
export default async function ChatPage({ params }: ChatPageProps) {
|
||||
const session = await auth();
|
||||
const chat = await prisma.chat.findFirst({
|
||||
where: {
|
||||
id: params.id,
|
||||
},
|
||||
include: {
|
||||
messages: true,
|
||||
},
|
||||
const chat = await db.query.chats.findFirst({
|
||||
where: eq(chats.id, params.id),
|
||||
});
|
||||
|
||||
if (!chat) {
|
||||
throw new Error("Chat not found");
|
||||
}
|
||||
|
|
@ -46,7 +41,7 @@ export default async function ChatPage({ params }: ChatPageProps) {
|
|||
<div className="relative flex h-full w-full overflow-hidden">
|
||||
<Sidebar session={session} />
|
||||
<div className="flex h-full min-w-0 flex-1 flex-col">
|
||||
<Chat id={chat.id} initialMessages={chat.messages} />
|
||||
<Chat id={chat.id} initialMessages={chat.messages as Message[]} />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -2,10 +2,8 @@ import { Chat } from "./chat";
|
|||
import { Sidebar } from "./sidebar";
|
||||
import { auth } from "@/auth";
|
||||
|
||||
// Prisma does not support Edge without the Data Proxy currently
|
||||
export const runtime = "nodejs"; // default
|
||||
export const runtime = "edge"; // default
|
||||
export const preferredRegion = "home";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export default async function IndexPage() {
|
||||
const session = await auth();
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
import { NextChatLogo } from "@/components/ui/nextchat-logo";
|
||||
import { Login } from "@/components/ui/login";
|
||||
import { NextChatLogo } from "@/components/ui/nextchat-logo";
|
||||
import { UserMenu } from "@/components/ui/user-menu";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { type Session } from "@auth/nextjs/types";
|
||||
import { db, chats } from "@/lib/db/schema";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { type Session } from "@auth/nextjs/types";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { Plus } from "lucide-react";
|
||||
import Link from "next/link";
|
||||
import { unstable_cache } from "next/cache";
|
||||
import Link from "next/link";
|
||||
import { SidebarItem } from "./sidebar-item";
|
||||
import { db } from "@/lib/db/schema";
|
||||
|
||||
export interface SidebarProps {
|
||||
session?: Session;
|
||||
|
|
@ -59,9 +59,16 @@ export function Sidebar({ session, newChat }: SidebarProps) {
|
|||
Sidebar.displayName = "Sidebar";
|
||||
|
||||
async function SidebarList({ session }: { session?: Session }) {
|
||||
const chats = await (
|
||||
const results: any[] = await (
|
||||
await unstable_cache(
|
||||
() => db.query.chats.findMany({}),
|
||||
() =>
|
||||
db.query.chats.findMany({
|
||||
columns: {
|
||||
id: true,
|
||||
title: true,
|
||||
},
|
||||
where: eq(chats.userId, session?.user?.email || ""),
|
||||
}),
|
||||
// @ts-ignore
|
||||
[session?.user?.id || ""],
|
||||
{
|
||||
|
|
@ -70,7 +77,7 @@ async function SidebarList({ session }: { session?: Session }) {
|
|||
)
|
||||
)();
|
||||
|
||||
return chats.map((c) => (
|
||||
return results.map((c) => (
|
||||
<SidebarItem key={c.id} title={c.title} href={`/chat/${c.id}`} id={c.id} />
|
||||
));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue