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 res = await openai.createChatCompletion({
|
||||
model: "gpt-3.5-turbo",
|
||||
messages: json.messages.map((m: any) => ({
|
||||
const messages = json.messages.map((m: any) => ({
|
||||
content: m.content,
|
||||
role: m.role,
|
||||
})),
|
||||
}));
|
||||
const res = await openai.createChatCompletion({
|
||||
model: "gpt-3.5-turbo",
|
||||
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({
|
||||
messages: [
|
||||
...messages,
|
||||
{
|
||||
content: completion,
|
||||
role: "assistant",
|
||||
id: nanoid(),
|
||||
}),
|
||||
},
|
||||
],
|
||||
};
|
||||
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} />
|
||||
));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import * as dotenv from "dotenv";
|
|||
|
||||
dotenv.config();
|
||||
|
||||
// @ts-ignore
|
||||
migrate(db, {
|
||||
migrationsFolder: "lib/db/migrations",
|
||||
})
|
||||
|
|
|
|||
1
lib/db/migrations/0002_striped_purifiers.sql
Normal file
1
lib/db/migrations/0002_striped_purifiers.sql
Normal file
|
|
@ -0,0 +1 @@
|
|||
ALTER TABLE "chats" ADD COLUMN "messages" json DEFAULT '{}'::json NOT NULL;
|
||||
49
lib/db/migrations/meta/0002_snapshot.json
Normal file
49
lib/db/migrations/meta/0002_snapshot.json
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
{
|
||||
"version": "5",
|
||||
"dialect": "pg",
|
||||
"id": "e3f4035a-a940-4529-b037-1737e178d88b",
|
||||
"prevId": "e8eef642-fc25-4737-8e75-5ad1e8c53b84",
|
||||
"tables": {
|
||||
"chats": {
|
||||
"name": "chats",
|
||||
"schema": "",
|
||||
"columns": {
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "text",
|
||||
"primaryKey": true,
|
||||
"notNull": true
|
||||
},
|
||||
"role": {
|
||||
"name": "role",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true
|
||||
},
|
||||
"userId": {
|
||||
"name": "userId",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true
|
||||
},
|
||||
"messages": {
|
||||
"name": "messages",
|
||||
"type": "json",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"default": "'{}'::json"
|
||||
}
|
||||
},
|
||||
"indexes": {},
|
||||
"foreignKeys": {},
|
||||
"compositePrimaryKeys": {}
|
||||
}
|
||||
},
|
||||
"enums": {},
|
||||
"schemas": {},
|
||||
"_meta": {
|
||||
"schemas": {},
|
||||
"tables": {},
|
||||
"columns": {}
|
||||
}
|
||||
}
|
||||
|
|
@ -15,6 +15,13 @@
|
|||
"when": 1685719963202,
|
||||
"tag": "0001_naive_gunslinger",
|
||||
"breakpoints": false
|
||||
},
|
||||
{
|
||||
"idx": 2,
|
||||
"version": "5",
|
||||
"when": 1685720130302,
|
||||
"tag": "0002_striped_purifiers",
|
||||
"breakpoints": false
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
import { sql } from "@vercel/postgres";
|
||||
import { pgTable, text } from "drizzle-orm/pg-core";
|
||||
import { json, pgTable, text } from "drizzle-orm/pg-core";
|
||||
import { drizzle } from "drizzle-orm/vercel-postgres";
|
||||
|
||||
// const connection = createPool({ connectionString: process.env.POSTGRES_URL });
|
||||
|
|
@ -8,7 +8,7 @@ export const chats = pgTable("chats", {
|
|||
id: text("id").notNull().primaryKey(),
|
||||
title: text("role").notNull(),
|
||||
userId: text("userId").notNull(),
|
||||
// messages: json("messages").notNull().default({}),
|
||||
messages: json("messages").notNull().default({}),
|
||||
// .references(() => users.id),
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue