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();
|
const json = await req.json();
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
console.log(req.auth); // todo fix types
|
console.log(req.auth); // todo fix types
|
||||||
|
const messages = json.messages.map((m: any) => ({
|
||||||
const res = await openai.createChatCompletion({
|
|
||||||
model: "gpt-3.5-turbo",
|
|
||||||
messages: json.messages.map((m: any) => ({
|
|
||||||
content: m.content,
|
content: m.content,
|
||||||
role: m.role,
|
role: m.role,
|
||||||
})),
|
}));
|
||||||
|
const res = await openai.createChatCompletion({
|
||||||
|
model: "gpt-3.5-turbo",
|
||||||
|
messages,
|
||||||
temperature: 0.7,
|
temperature: 0.7,
|
||||||
top_p: 1,
|
top_p: 1,
|
||||||
frequency_penalty: 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 title = json.messages[0].content.substring(0, 20);
|
||||||
const payload = {
|
const payload = {
|
||||||
id: json.id ?? crypto.randomUUID(),
|
id: crypto.randomUUID(),
|
||||||
title,
|
title,
|
||||||
userId: (req as any).auth?.user?.email,
|
userId: (req as any).auth?.user?.email,
|
||||||
messages: json.messages.concat({
|
messages: [
|
||||||
|
...messages,
|
||||||
|
{
|
||||||
content: completion,
|
content: completion,
|
||||||
role: "assistant",
|
role: "assistant",
|
||||||
id: nanoid(),
|
},
|
||||||
}),
|
],
|
||||||
};
|
};
|
||||||
await db
|
const chat = await db
|
||||||
.insert(chats)
|
.insert(chats)
|
||||||
.values(payload)
|
.values(payload)
|
||||||
.onConflictDoUpdate({
|
// .onConflictDoUpdate({
|
||||||
target: json.id,
|
// target: payload.id,
|
||||||
set: payload,
|
// set: payload,
|
||||||
})
|
// })
|
||||||
.execute();
|
.returning();
|
||||||
|
console.log(chat);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,9 @@
|
||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { type Message } from "@prisma/client";
|
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { ChatList } from "./chat-list";
|
import { ChatList } from "./chat-list";
|
||||||
import { Prompt } from "./prompt";
|
import { Prompt } from "./prompt";
|
||||||
import { useChat } from "ai-connector";
|
import { useChat, type Message } from "ai-connector";
|
||||||
|
|
||||||
export interface ChatProps {
|
export interface ChatProps {
|
||||||
// create?: (input: string) => Chat | undefined;
|
// create?: (input: string) => Chat | undefined;
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
import { Sidebar } from "@/app/sidebar";
|
import { Sidebar } from "@/app/sidebar";
|
||||||
import { prisma } from "@/lib/prisma";
|
|
||||||
|
|
||||||
import { Chat } from "../../chat";
|
|
||||||
import { type Metadata } from "next";
|
import { type Metadata } from "next";
|
||||||
import { auth } from "@/auth";
|
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 {
|
export interface ChatPageProps {
|
||||||
params: {
|
params: {
|
||||||
id: string;
|
id: string;
|
||||||
|
|
@ -14,10 +15,8 @@ export interface ChatPageProps {
|
||||||
export async function generateMetadata({
|
export async function generateMetadata({
|
||||||
params,
|
params,
|
||||||
}: ChatPageProps): Promise<Metadata> {
|
}: ChatPageProps): Promise<Metadata> {
|
||||||
const chat = await prisma.chat.findFirst({
|
const chat = await db.query.chats.findFirst({
|
||||||
where: {
|
where: eq(chats.id, params.id),
|
||||||
id: params.id,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
title: chat?.title.slice(0, 50) ?? "Chat",
|
title: chat?.title.slice(0, 50) ?? "Chat",
|
||||||
|
|
@ -30,14 +29,10 @@ export const preferredRegion = "home";
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
export default async function ChatPage({ params }: ChatPageProps) {
|
export default async function ChatPage({ params }: ChatPageProps) {
|
||||||
const session = await auth();
|
const session = await auth();
|
||||||
const chat = await prisma.chat.findFirst({
|
const chat = await db.query.chats.findFirst({
|
||||||
where: {
|
where: eq(chats.id, params.id),
|
||||||
id: params.id,
|
|
||||||
},
|
|
||||||
include: {
|
|
||||||
messages: true,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!chat) {
|
if (!chat) {
|
||||||
throw new Error("Chat not found");
|
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">
|
<div className="relative flex h-full w-full overflow-hidden">
|
||||||
<Sidebar session={session} />
|
<Sidebar session={session} />
|
||||||
<div className="flex h-full min-w-0 flex-1 flex-col">
|
<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>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,8 @@ import { Chat } from "./chat";
|
||||||
import { Sidebar } from "./sidebar";
|
import { Sidebar } from "./sidebar";
|
||||||
import { auth } from "@/auth";
|
import { auth } from "@/auth";
|
||||||
|
|
||||||
// Prisma does not support Edge without the Data Proxy currently
|
export const runtime = "edge"; // default
|
||||||
export const runtime = "nodejs"; // default
|
|
||||||
export const preferredRegion = "home";
|
export const preferredRegion = "home";
|
||||||
export const dynamic = "force-dynamic";
|
|
||||||
|
|
||||||
export default async function IndexPage() {
|
export default async function IndexPage() {
|
||||||
const session = await auth();
|
const session = await auth();
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,14 @@
|
||||||
import { NextChatLogo } from "@/components/ui/nextchat-logo";
|
|
||||||
import { Login } from "@/components/ui/login";
|
import { Login } from "@/components/ui/login";
|
||||||
|
import { NextChatLogo } from "@/components/ui/nextchat-logo";
|
||||||
import { UserMenu } from "@/components/ui/user-menu";
|
import { UserMenu } from "@/components/ui/user-menu";
|
||||||
import { prisma } from "@/lib/prisma";
|
import { db, chats } from "@/lib/db/schema";
|
||||||
import { type Session } from "@auth/nextjs/types";
|
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
|
import { type Session } from "@auth/nextjs/types";
|
||||||
|
import { eq } from "drizzle-orm";
|
||||||
import { Plus } from "lucide-react";
|
import { Plus } from "lucide-react";
|
||||||
import Link from "next/link";
|
|
||||||
import { unstable_cache } from "next/cache";
|
import { unstable_cache } from "next/cache";
|
||||||
|
import Link from "next/link";
|
||||||
import { SidebarItem } from "./sidebar-item";
|
import { SidebarItem } from "./sidebar-item";
|
||||||
import { db } from "@/lib/db/schema";
|
|
||||||
|
|
||||||
export interface SidebarProps {
|
export interface SidebarProps {
|
||||||
session?: Session;
|
session?: Session;
|
||||||
|
|
@ -59,9 +59,16 @@ export function Sidebar({ session, newChat }: SidebarProps) {
|
||||||
Sidebar.displayName = "Sidebar";
|
Sidebar.displayName = "Sidebar";
|
||||||
|
|
||||||
async function SidebarList({ session }: { session?: Session }) {
|
async function SidebarList({ session }: { session?: Session }) {
|
||||||
const chats = await (
|
const results: any[] = await (
|
||||||
await unstable_cache(
|
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
|
// @ts-ignore
|
||||||
[session?.user?.id || ""],
|
[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} />
|
<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();
|
dotenv.config();
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
migrate(db, {
|
migrate(db, {
|
||||||
migrationsFolder: "lib/db/migrations",
|
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,
|
"when": 1685719963202,
|
||||||
"tag": "0001_naive_gunslinger",
|
"tag": "0001_naive_gunslinger",
|
||||||
"breakpoints": false
|
"breakpoints": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 2,
|
||||||
|
"version": "5",
|
||||||
|
"when": 1685720130302,
|
||||||
|
"tag": "0002_striped_purifiers",
|
||||||
|
"breakpoints": false
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { sql } from "@vercel/postgres";
|
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";
|
import { drizzle } from "drizzle-orm/vercel-postgres";
|
||||||
|
|
||||||
// const connection = createPool({ connectionString: process.env.POSTGRES_URL });
|
// const connection = createPool({ connectionString: process.env.POSTGRES_URL });
|
||||||
|
|
@ -8,7 +8,7 @@ export const chats = pgTable("chats", {
|
||||||
id: text("id").notNull().primaryKey(),
|
id: text("id").notNull().primaryKey(),
|
||||||
title: text("role").notNull(),
|
title: text("role").notNull(),
|
||||||
userId: text("userId").notNull(),
|
userId: text("userId").notNull(),
|
||||||
// messages: json("messages").notNull().default({}),
|
messages: json("messages").notNull().default({}),
|
||||||
// .references(() => users.id),
|
// .references(() => users.id),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue