├── .eslintrc.json ├── prettier.config.js ├── src ├── app │ ├── ThemeProvider.tsx │ ├── favicon.ico │ ├── notes │ │ ├── layout.tsx │ │ ├── page.tsx │ │ └── NavBar.tsx │ ├── sign-in │ │ └── [[...sign-in]] │ │ │ └── page.tsx │ ├── sign-up │ │ └── [[...sign-up]] │ │ │ └── page.tsx │ ├── layout.tsx │ ├── page.tsx │ ├── globals.css │ └── api │ │ ├── chat │ │ └── route.ts │ │ └── notes │ │ └── route.ts ├── assets │ └── logo.png ├── lib │ ├── utils.ts │ ├── db │ │ ├── pinecone.ts │ │ └── prisma.ts │ ├── validation │ │ └── note.ts │ └── openai.ts ├── middleware.ts └── components │ ├── ui │ ├── loading-button.tsx │ ├── label.tsx │ ├── textarea.tsx │ ├── input.tsx │ ├── button.tsx │ ├── card.tsx │ ├── dialog.tsx │ └── form.tsx │ ├── AIChatButton.tsx │ ├── ThemeToggleButton.tsx │ ├── Note.tsx │ ├── AIChatBox.tsx │ └── AddEditNoteDialog.tsx ├── postcss.config.js ├── next.config.js ├── components.json ├── .gitignore ├── prisma └── schema.prisma ├── public ├── vercel.svg └── next.svg ├── tsconfig.json ├── README.md ├── package.json └── tailwind.config.js /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["next/core-web-vitals", "prettier"] 3 | } 4 | -------------------------------------------------------------------------------- /prettier.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: ["prettier-plugin-tailwindcss"], 3 | }; 4 | -------------------------------------------------------------------------------- /src/app/ThemeProvider.tsx: -------------------------------------------------------------------------------- 1 | "use client"; 2 | 3 | export { ThemeProvider } from "next-themes"; 4 | -------------------------------------------------------------------------------- /src/app/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codinginflow/nextjs-ai-note-app/HEAD/src/app/favicon.ico -------------------------------------------------------------------------------- /src/assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codinginflow/nextjs-ai-note-app/HEAD/src/assets/logo.png -------------------------------------------------------------------------------- /postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | } 7 | -------------------------------------------------------------------------------- /src/lib/utils.ts: -------------------------------------------------------------------------------- 1 | import { type ClassValue, clsx } from "clsx" 2 | import { twMerge } from "tailwind-merge" 3 | 4 | export function cn(...inputs: ClassValue[]) { 5 | return twMerge(clsx(inputs)) 6 | } 7 | -------------------------------------------------------------------------------- /next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = { 3 | images: { 4 | remotePatterns: [ 5 | { 6 | hostname: "img.clerk.com", 7 | }, 8 | ], 9 | }, 10 | }; 11 | 12 | module.exports = nextConfig; 13 | -------------------------------------------------------------------------------- /src/app/notes/layout.tsx: -------------------------------------------------------------------------------- 1 | import NavBar from "./NavBar"; 2 | 3 | export default function Layout({ children }: { children: React.ReactNode }) { 4 | return ( 5 | <> 6 | 7 |
{children}
8 | 9 | ); 10 | } 11 | -------------------------------------------------------------------------------- /src/lib/db/pinecone.ts: -------------------------------------------------------------------------------- 1 | import { Pinecone } from "@pinecone-database/pinecone"; 2 | 3 | const apiKey = process.env.PINECONE_API_KEY; 4 | 5 | if (!apiKey) { 6 | throw Error("PINECONE_API_KEY is not set"); 7 | } 8 | 9 | const pinecone = new Pinecone({ 10 | environment: "gcp-starter", 11 | apiKey, 12 | }); 13 | 14 | export const notesIndex = pinecone.Index("nextjs-ai-note-app"); 15 | -------------------------------------------------------------------------------- /components.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://ui.shadcn.com/schema.json", 3 | "style": "default", 4 | "rsc": true, 5 | "tsx": true, 6 | "tailwind": { 7 | "config": "tailwind.config.js", 8 | "css": "src/app/globals.css", 9 | "baseColor": "slate", 10 | "cssVariables": true 11 | }, 12 | "aliases": { 13 | "components": "@/components", 14 | "utils": "@/lib/utils" 15 | } 16 | } -------------------------------------------------------------------------------- /src/app/sign-in/[[...sign-in]]/page.tsx: -------------------------------------------------------------------------------- 1 | import { SignIn } from "@clerk/nextjs"; 2 | import { Metadata } from "next"; 3 | 4 | export const metadata: Metadata = { 5 | title: "FlowBrain - Sign In", 6 | }; 7 | 8 | export default function SignInPage() { 9 | return ( 10 |
11 | 12 |
13 | ); 14 | } 15 | -------------------------------------------------------------------------------- /src/app/sign-up/[[...sign-up]]/page.tsx: -------------------------------------------------------------------------------- 1 | import { SignUp } from "@clerk/nextjs"; 2 | import { Metadata } from "next"; 3 | 4 | export const metadata: Metadata = { 5 | title: "FlowBrain - Sign Up", 6 | }; 7 | 8 | export default function SignUpPage() { 9 | return ( 10 |
11 | 12 |
13 | ); 14 | } 15 | -------------------------------------------------------------------------------- /src/lib/validation/note.ts: -------------------------------------------------------------------------------- 1 | import { z } from "zod"; 2 | 3 | export const createNoteSchema = z.object({ 4 | title: z.string().min(1, { message: "Title is required" }), 5 | content: z.string().optional(), 6 | }); 7 | 8 | export type CreateNoteSchema = z.infer; 9 | 10 | export const updateNoteSchema = createNoteSchema.extend({ 11 | id: z.string().min(1), 12 | }); 13 | 14 | export const deleteNoteSchema = z.object({ 15 | id: z.string().min(1), 16 | }); 17 | -------------------------------------------------------------------------------- /src/middleware.ts: -------------------------------------------------------------------------------- 1 | import { authMiddleware } from "@clerk/nextjs"; 2 | 3 | // This example protects all routes including api/trpc routes 4 | // Please edit this to allow other routes to be public as needed. 5 | // See https://clerk.com/docs/references/nextjs/auth-middleware for more information about configuring your Middleware 6 | export default authMiddleware({ 7 | publicRoutes: ["/"], 8 | }); 9 | 10 | export const config = { 11 | matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"], 12 | }; 13 | -------------------------------------------------------------------------------- /src/components/ui/loading-button.tsx: -------------------------------------------------------------------------------- 1 | import { Loader2 } from "lucide-react"; 2 | import { Button, ButtonProps } from "./button"; 3 | 4 | type LoadingButtonProps = { 5 | loading: boolean; 6 | } & ButtonProps; 7 | 8 | export default function LoadingButton({ 9 | children, 10 | loading, 11 | ...props 12 | }: LoadingButtonProps) { 13 | return ( 14 | 18 | ); 19 | } 20 | -------------------------------------------------------------------------------- /src/lib/db/prisma.ts: -------------------------------------------------------------------------------- 1 | import { PrismaClient } from "@prisma/client"; 2 | 3 | const prismaClientSingleton = () => { 4 | return new PrismaClient(); 5 | }; 6 | 7 | type PrismaClientSingleton = ReturnType; 8 | 9 | const globalForPrisma = globalThis as unknown as { 10 | prisma: PrismaClientSingleton | undefined; 11 | }; 12 | 13 | const prisma = globalForPrisma.prisma ?? prismaClientSingleton(); 14 | 15 | export default prisma; 16 | 17 | if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma; 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | .yarn/install-state.gz 8 | 9 | # testing 10 | /coverage 11 | 12 | # next.js 13 | /.next/ 14 | /out/ 15 | 16 | # production 17 | /build 18 | 19 | # misc 20 | .DS_Store 21 | *.pem 22 | 23 | # debug 24 | npm-debug.log* 25 | yarn-debug.log* 26 | yarn-error.log* 27 | 28 | # local env files 29 | .env*.local 30 | 31 | # vercel 32 | .vercel 33 | 34 | # typescript 35 | *.tsbuildinfo 36 | next-env.d.ts 37 | 38 | .env -------------------------------------------------------------------------------- /prisma/schema.prisma: -------------------------------------------------------------------------------- 1 | // This is your Prisma schema file, 2 | // learn more about it in the docs: https://pris.ly/d/prisma-schema 3 | 4 | generator client { 5 | provider = "prisma-client-js" 6 | } 7 | 8 | datasource db { 9 | provider = "mongodb" 10 | url = env("DATABASE_URL") 11 | } 12 | 13 | model Note { 14 | id String @id @default(auto()) @map("_id") @db.ObjectId 15 | title String 16 | content String? 17 | userId String 18 | createdAt DateTime @default(now()) 19 | updatedAt DateTime @updatedAt 20 | 21 | @@map("notes") 22 | } 23 | -------------------------------------------------------------------------------- /src/components/AIChatButton.tsx: -------------------------------------------------------------------------------- 1 | import { Bot } from "lucide-react"; 2 | import { useState } from "react"; 3 | import AIChatBox from "./AIChatBox"; 4 | import { Button } from "./ui/button"; 5 | 6 | export default function AIChatButton() { 7 | const [chatBoxOpen, setChatBoxOpen] = useState(false); 8 | 9 | return ( 10 | <> 11 | 15 | setChatBoxOpen(false)} /> 16 | 17 | ); 18 | } 19 | -------------------------------------------------------------------------------- /public/vercel.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/lib/openai.ts: -------------------------------------------------------------------------------- 1 | import OpenAI from "openai"; 2 | 3 | const apiKey = process.env.OPENAI_API_KEY; 4 | 5 | if (!apiKey) { 6 | throw Error("OPENAI_API_KEY is not set"); 7 | } 8 | 9 | const openai = new OpenAI({ apiKey }); 10 | 11 | export default openai; 12 | 13 | export async function getEmbedding(text: string) { 14 | const response = await openai.embeddings.create({ 15 | model: "text-embedding-ada-002", 16 | input: text, 17 | }); 18 | 19 | const embedding = response.data[0].embedding; 20 | 21 | if (!embedding) throw Error("Error generating embedding."); 22 | 23 | console.log(embedding); 24 | 25 | return embedding; 26 | } 27 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": ["dom", "dom.iterable", "esnext"], 5 | "allowJs": true, 6 | "skipLibCheck": true, 7 | "strict": true, 8 | "noEmit": true, 9 | "esModuleInterop": true, 10 | "module": "esnext", 11 | "moduleResolution": "bundler", 12 | "resolveJsonModule": true, 13 | "isolatedModules": true, 14 | "jsx": "preserve", 15 | "incremental": true, 16 | "plugins": [ 17 | { 18 | "name": "next" 19 | } 20 | ], 21 | "paths": { 22 | "@/*": ["./src/*"] 23 | } 24 | }, 25 | "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], 26 | "exclude": ["node_modules"] 27 | } 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Next.js 14 AI Note App 2 | 3 | This is a note-taking app with an integrated **AI chatbot**. By using the **ChatGPT API**, **vector embeddings**, and **Pinecone**, the chatbot knows about all notes stored in your user account and can retrieve relevant information to answer your questions and summarize information. 4 | 5 | **Response streaming** is implemented via the **Vercel AI SDK**. 6 | 7 | The app is built with Next.js 14's app router, TailwindCSS, Shadcn UI, and TypeScript. It has a light/dark theme toggle and a fully mobile-responsive layout. 8 | 9 | Learn how to build this app in my tutorial: https://www.youtube.com/watch?v=mkJbEP5GeRA 10 | 11 | ![thumbnail](https://github.com/codinginflow/nextjs-ai-note-app/assets/52977034/cefc69f2-a486-4072-bf69-d0738f7336af) 12 | -------------------------------------------------------------------------------- /src/app/layout.tsx: -------------------------------------------------------------------------------- 1 | import { ClerkProvider } from "@clerk/nextjs"; 2 | import type { Metadata } from "next"; 3 | import { Inter } from "next/font/google"; 4 | import { ThemeProvider } from "./ThemeProvider"; 5 | import "./globals.css"; 6 | 7 | const inter = Inter({ subsets: ["latin"] }); 8 | 9 | export const metadata: Metadata = { 10 | title: "FlowBrain", 11 | description: "The intelligent note-taking app", 12 | }; 13 | 14 | export default function RootLayout({ 15 | children, 16 | }: { 17 | children: React.ReactNode; 18 | }) { 19 | return ( 20 | 21 | 22 | 23 | {children} 24 | 25 | 26 | 27 | ); 28 | } 29 | -------------------------------------------------------------------------------- /src/components/ui/label.tsx: -------------------------------------------------------------------------------- 1 | "use client" 2 | 3 | import * as React from "react" 4 | import * as LabelPrimitive from "@radix-ui/react-label" 5 | import { cva, type VariantProps } from "class-variance-authority" 6 | 7 | import { cn } from "@/lib/utils" 8 | 9 | const labelVariants = cva( 10 | "text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70" 11 | ) 12 | 13 | const Label = React.forwardRef< 14 | React.ElementRef, 15 | React.ComponentPropsWithoutRef & 16 | VariantProps 17 | >(({ className, ...props }, ref) => ( 18 | 23 | )) 24 | Label.displayName = LabelPrimitive.Root.displayName 25 | 26 | export { Label } 27 | -------------------------------------------------------------------------------- /src/components/ui/textarea.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react" 2 | 3 | import { cn } from "@/lib/utils" 4 | 5 | export interface TextareaProps 6 | extends React.TextareaHTMLAttributes {} 7 | 8 | const Textarea = React.forwardRef( 9 | ({ className, ...props }, ref) => { 10 | return ( 11 |