├── app ├── (auth) │ ├── api │ │ └── auth │ │ │ └── [...nextauth] │ │ │ └── route.ts │ ├── auth.config.ts │ ├── auth.ts │ ├── actions.ts │ ├── login │ │ └── page.tsx │ └── register │ │ └── page.tsx ├── favicon.ico ├── (chat) │ ├── twitter-image.png │ ├── opengraph-image.png │ ├── api │ │ ├── history │ │ │ └── route.ts │ │ ├── suggestions │ │ │ └── route.ts │ │ ├── vote │ │ │ └── route.ts │ │ ├── files │ │ │ └── upload │ │ │ │ └── route.ts │ │ └── document │ │ │ └── route.ts │ ├── page.tsx │ ├── layout.tsx │ ├── actions.ts │ └── chat │ │ └── [id] │ │ └── page.tsx ├── layout.tsx └── globals.css ├── public ├── fonts │ ├── geist.woff2 │ └── geist-mono.woff2 └── images │ └── demo-thumbnail.png ├── ai ├── custom-middleware.ts ├── index.ts ├── models.ts └── prompts.ts ├── postcss.config.mjs ├── prettier.config.cjs ├── next-env.d.ts ├── middleware.ts ├── components ├── ui │ ├── skeleton.tsx │ ├── label.tsx │ ├── textarea.tsx │ ├── separator.tsx │ ├── input.tsx │ ├── tooltip.tsx │ ├── button.tsx │ ├── card.tsx │ ├── sheet.tsx │ ├── alert-dialog.tsx │ ├── select.tsx │ └── dropdown-menu.tsx └── custom │ ├── theme-provider.tsx │ ├── sign-out-form.tsx │ ├── document-skeleton.tsx │ ├── sidebar-toggle.tsx │ ├── use-scroll-to-bottom.ts │ ├── submit-button.tsx │ ├── block-stream-handler.tsx │ ├── preview-attachment.tsx │ ├── auth-form.tsx │ ├── overview.tsx │ ├── sidebar-user-nav.tsx │ ├── suggestion.tsx │ ├── app-sidebar.tsx │ ├── chat-header.tsx │ ├── model-selector.tsx │ ├── use-block-stream.tsx │ ├── document.tsx │ ├── diffview.tsx │ ├── version-footer.tsx │ ├── markdown.tsx │ ├── chat.tsx │ ├── editor.tsx │ ├── message-actions.tsx │ ├── message.tsx │ ├── weather.tsx │ └── multimodal-input.tsx ├── lib ├── editor │ ├── react-renderer.tsx │ ├── config.ts │ ├── functions.tsx │ └── suggestions.tsx ├── drizzle │ ├── meta │ │ ├── _journal.json │ │ ├── 0000_snapshot.json │ │ ├── 0001_snapshot.json │ │ └── 0002_snapshot.json │ ├── 0000_keen_devos.sql │ ├── 0002_wandering_riptide.sql │ └── 0001_sparkling_blue_marvel.sql └── utils.ts ├── next.config.ts ├── drizzle.config.ts ├── components.json ├── .gitignore ├── LICENSE ├── .env.example ├── hooks └── use-mobile.tsx ├── tsconfig.json ├── db ├── migrate.ts ├── schema.ts └── queries.ts ├── .eslintrc.json ├── tailwind.config.ts ├── package.json └── README.md /app/(auth)/api/auth/[...nextauth]/route.ts: -------------------------------------------------------------------------------- 1 | export { GET, POST } from '@/app/(auth)/auth'; 2 | -------------------------------------------------------------------------------- /app/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MuhamedHabib/ai-chatbot/main/app/favicon.ico -------------------------------------------------------------------------------- /public/fonts/geist.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MuhamedHabib/ai-chatbot/main/public/fonts/geist.woff2 -------------------------------------------------------------------------------- /app/(chat)/twitter-image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MuhamedHabib/ai-chatbot/main/app/(chat)/twitter-image.png -------------------------------------------------------------------------------- /public/fonts/geist-mono.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MuhamedHabib/ai-chatbot/main/public/fonts/geist-mono.woff2 -------------------------------------------------------------------------------- /app/(chat)/opengraph-image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MuhamedHabib/ai-chatbot/main/app/(chat)/opengraph-image.png -------------------------------------------------------------------------------- /public/images/demo-thumbnail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MuhamedHabib/ai-chatbot/main/public/images/demo-thumbnail.png -------------------------------------------------------------------------------- /ai/custom-middleware.ts: -------------------------------------------------------------------------------- 1 | import { Experimental_LanguageModelV1Middleware } from 'ai'; 2 | 3 | export const customMiddleware: Experimental_LanguageModelV1Middleware = {}; 4 | -------------------------------------------------------------------------------- /postcss.config.mjs: -------------------------------------------------------------------------------- 1 | /** @type {import('postcss-load-config').Config} */ 2 | const config = { 3 | plugins: { 4 | tailwindcss: {}, 5 | }, 6 | }; 7 | 8 | export default config; 9 | -------------------------------------------------------------------------------- /prettier.config.cjs: -------------------------------------------------------------------------------- 1 | /** @type {import('prettier').Config} */ 2 | module.exports = { 3 | endOfLine: 'lf', 4 | semi: true, 5 | useTabs: false, 6 | singleQuote: true, 7 | tabWidth: 2, 8 | trailingComma: 'es5', 9 | }; 10 | -------------------------------------------------------------------------------- /next-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | 4 | // NOTE: This file should not be edited 5 | // see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information. 6 | -------------------------------------------------------------------------------- /middleware.ts: -------------------------------------------------------------------------------- 1 | import NextAuth from "next-auth"; 2 | 3 | import { authConfig } from "@/app/(auth)/auth.config"; 4 | 5 | export default NextAuth(authConfig).auth; 6 | 7 | export const config = { 8 | matcher: ["/", "/:id", "/api/:path*", "/login", "/register"], 9 | }; 10 | -------------------------------------------------------------------------------- /components/ui/skeleton.tsx: -------------------------------------------------------------------------------- 1 | import { cn } from "@/lib/utils" 2 | 3 | function Skeleton({ 4 | className, 5 | ...props 6 | }: React.HTMLAttributes) { 7 | return ( 8 |
12 | ) 13 | } 14 | 15 | export { Skeleton } 16 | -------------------------------------------------------------------------------- /lib/editor/react-renderer.tsx: -------------------------------------------------------------------------------- 1 | import { createRoot } from 'react-dom/client'; 2 | 3 | export class ReactRenderer { 4 | static render(component: React.ReactElement, dom: HTMLElement) { 5 | const root = createRoot(dom); 6 | root.render(component); 7 | 8 | return { 9 | destroy: () => root.unmount(), 10 | }; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /components/custom/theme-provider.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { ThemeProvider as NextThemesProvider } from 'next-themes'; 4 | import { type ThemeProviderProps } from 'next-themes/dist/types'; 5 | 6 | export function ThemeProvider({ children, ...props }: ThemeProviderProps) { 7 | return {children}; 8 | } 9 | -------------------------------------------------------------------------------- /next.config.ts: -------------------------------------------------------------------------------- 1 | import type { NextConfig } from 'next'; 2 | 3 | const nextConfig: NextConfig = { 4 | /* config options here */ 5 | experimental: { 6 | ppr: true, 7 | }, 8 | images: { 9 | remotePatterns: [ 10 | { 11 | hostname: 'avatar.vercel.sh', 12 | }, 13 | ], 14 | }, 15 | }; 16 | 17 | export default nextConfig; 18 | -------------------------------------------------------------------------------- /drizzle.config.ts: -------------------------------------------------------------------------------- 1 | import { config } from "dotenv"; 2 | import { defineConfig } from "drizzle-kit"; 3 | 4 | config({ 5 | path: ".env.local", 6 | }); 7 | 8 | export default defineConfig({ 9 | schema: "./db/schema.ts", 10 | out: "./lib/drizzle", 11 | dialect: "postgresql", 12 | dbCredentials: { 13 | url: process.env.POSTGRES_URL!, 14 | }, 15 | }); 16 | -------------------------------------------------------------------------------- /ai/index.ts: -------------------------------------------------------------------------------- 1 | import { openai } from '@ai-sdk/openai'; 2 | import { experimental_wrapLanguageModel as wrapLanguageModel } from 'ai'; 3 | 4 | import { customMiddleware } from './custom-middleware'; 5 | 6 | export const customModel = (apiIdentifier: string) => { 7 | return wrapLanguageModel({ 8 | model: openai(apiIdentifier), 9 | middleware: customMiddleware, 10 | }); 11 | }; 12 | -------------------------------------------------------------------------------- /app/(chat)/api/history/route.ts: -------------------------------------------------------------------------------- 1 | import { auth } from "@/app/(auth)/auth"; 2 | import { getChatsByUserId } from "@/db/queries"; 3 | 4 | export async function GET() { 5 | const session = await auth(); 6 | 7 | if (!session || !session.user) { 8 | return Response.json("Unauthorized!", { status: 401 }); 9 | } 10 | 11 | const chats = await getChatsByUserId({ id: session.user.id! }); 12 | return Response.json(chats); 13 | } 14 | -------------------------------------------------------------------------------- /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.ts", 8 | "css": "app/globals.css", 9 | "baseColor": "zinc", 10 | "cssVariables": true, 11 | "prefix": "" 12 | }, 13 | "aliases": { 14 | "components": "@/components", 15 | "utils": "@/lib/utils", 16 | "ui": "@/components/ui", 17 | "lib": "@/lib", 18 | "hooks": "@/hooks" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /components/custom/sign-out-form.tsx: -------------------------------------------------------------------------------- 1 | import Form from 'next/form'; 2 | 3 | import { signOut } from '@/app/(auth)/auth'; 4 | 5 | export const SignOutForm = () => { 6 | return ( 7 |
{ 10 | 'use server'; 11 | 12 | await signOut({ 13 | redirectTo: '/', 14 | }); 15 | }} 16 | > 17 | 23 |
24 | ); 25 | }; 26 | -------------------------------------------------------------------------------- /.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 | 8 | # testing 9 | coverage 10 | 11 | # next.js 12 | .next/ 13 | out/ 14 | build 15 | 16 | # misc 17 | .DS_Store 18 | *.pem 19 | 20 | # debug 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | .pnpm-debug.log* 25 | 26 | # local env files 27 | .env.local 28 | .env.development.local 29 | .env.test.local 30 | .env.production.local 31 | 32 | # turbo 33 | .turbo 34 | 35 | .env 36 | .vercel 37 | .vscode 38 | .env*.local 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2024 Vercel, Inc. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /.env.example: -------------------------------------------------------------------------------- 1 | # Get your OpenAI API Key here: https://platform.openai.com/account/api-keys 2 | OPENAI_API_KEY=**** 3 | 4 | # Generate a random secret: https://generate-secret.vercel.app/32 or `openssl rand -base64 32` 5 | AUTH_SECRET=**** 6 | 7 | # The following keys below are automatically created and 8 | # added to your environment when you deploy on vercel 9 | 10 | # Instructions to create kv database here: https://vercel.com/docs/storage/vercel-blob 11 | BLOB_READ_WRITE_TOKEN=**** 12 | 13 | # Instructions to create a database here: https://vercel.com/docs/storage/vercel-postgres/quickstart 14 | POSTGRES_URL=**** 15 | -------------------------------------------------------------------------------- /ai/models.ts: -------------------------------------------------------------------------------- 1 | // Define your models here. 2 | 3 | export interface Model { 4 | id: string; 5 | label: string; 6 | apiIdentifier: string; 7 | description: string; 8 | } 9 | 10 | export const models: Array = [ 11 | { 12 | id: 'gpt-4o-mini', 13 | label: 'GPT 4o mini', 14 | apiIdentifier: 'gpt-4o-mini', 15 | description: 'Small model for fast, lightweight tasks', 16 | }, 17 | { 18 | id: 'gpt-4o', 19 | label: 'GPT 4o', 20 | apiIdentifier: 'gpt-4o', 21 | description: 'For complex, multi-step tasks', 22 | }, 23 | ] as const; 24 | 25 | export const DEFAULT_MODEL_NAME: string = 'gpt-4o-mini'; 26 | -------------------------------------------------------------------------------- /lib/drizzle/meta/_journal.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "7", 3 | "dialect": "postgresql", 4 | "entries": [ 5 | { 6 | "idx": 0, 7 | "version": "7", 8 | "when": 1728598022383, 9 | "tag": "0000_keen_devos", 10 | "breakpoints": true 11 | }, 12 | { 13 | "idx": 1, 14 | "version": "7", 15 | "when": 1730207363999, 16 | "tag": "0001_sparkling_blue_marvel", 17 | "breakpoints": true 18 | }, 19 | { 20 | "idx": 2, 21 | "version": "7", 22 | "when": 1730725226313, 23 | "tag": "0002_wandering_riptide", 24 | "breakpoints": true 25 | } 26 | ] 27 | } -------------------------------------------------------------------------------- /hooks/use-mobile.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react" 2 | 3 | const MOBILE_BREAKPOINT = 768 4 | 5 | export function useIsMobile() { 6 | const [isMobile, setIsMobile] = React.useState(undefined) 7 | 8 | React.useEffect(() => { 9 | const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`) 10 | const onChange = () => { 11 | setIsMobile(window.innerWidth < MOBILE_BREAKPOINT) 12 | } 13 | mql.addEventListener("change", onChange) 14 | setIsMobile(window.innerWidth < MOBILE_BREAKPOINT) 15 | return () => mql.removeEventListener("change", onChange) 16 | }, []) 17 | 18 | return !!isMobile 19 | } 20 | -------------------------------------------------------------------------------- /lib/drizzle/0000_keen_devos.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS "Chat" ( 2 | "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, 3 | "createdAt" timestamp NOT NULL, 4 | "messages" json NOT NULL, 5 | "userId" uuid NOT NULL 6 | ); 7 | --> statement-breakpoint 8 | CREATE TABLE IF NOT EXISTS "User" ( 9 | "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, 10 | "email" varchar(64) NOT NULL, 11 | "password" varchar(64) 12 | ); 13 | --> statement-breakpoint 14 | DO $$ BEGIN 15 | ALTER TABLE "Chat" ADD CONSTRAINT "Chat_userId_User_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."User"("id") ON DELETE no action ON UPDATE no action; 16 | EXCEPTION 17 | WHEN duplicate_object THEN null; 18 | END $$; 19 | -------------------------------------------------------------------------------- /app/(chat)/page.tsx: -------------------------------------------------------------------------------- 1 | import { cookies } from 'next/headers'; 2 | 3 | import { DEFAULT_MODEL_NAME, models } from '@/ai/models'; 4 | import { Chat } from '@/components/custom/chat'; 5 | import { generateUUID } from '@/lib/utils'; 6 | 7 | export default async function Page() { 8 | const id = generateUUID(); 9 | 10 | const cookieStore = await cookies(); 11 | const modelIdFromCookie = cookieStore.get('model-id')?.value; 12 | 13 | const selectedModelId = 14 | models.find((model) => model.id === modelIdFromCookie)?.id || 15 | DEFAULT_MODEL_NAME; 16 | 17 | return ( 18 | 24 | ); 25 | } 26 | -------------------------------------------------------------------------------- /app/(chat)/layout.tsx: -------------------------------------------------------------------------------- 1 | import { cookies } from 'next/headers'; 2 | 3 | import { AppSidebar } from '@/components/custom/app-sidebar'; 4 | import { SidebarInset, SidebarProvider } from '@/components/ui/sidebar'; 5 | 6 | import { auth } from '../(auth)/auth'; 7 | 8 | export const experimental_ppr = true; 9 | 10 | export default async function Layout({ 11 | children, 12 | }: { 13 | children: React.ReactNode; 14 | }) { 15 | const [session, cookieStore] = await Promise.all([auth(), cookies()]); 16 | const isCollapsed = cookieStore.get('sidebar:state')?.value !== 'true'; 17 | 18 | return ( 19 | 20 | 21 | {children} 22 | 23 | ); 24 | } 25 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ESNext", 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 | "@/*": ["./*"] 23 | } 24 | }, 25 | "include": [ 26 | "next-env.d.ts", 27 | "**/*.ts", 28 | "**/*.tsx", 29 | ".next/types/**/*.ts", 30 | "next.config.js" 31 | ], 32 | "exclude": ["node_modules"] 33 | } 34 | -------------------------------------------------------------------------------- /components/custom/document-skeleton.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | export const DocumentSkeleton = () => { 4 | return ( 5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | ); 15 | }; 16 | -------------------------------------------------------------------------------- /components/custom/sidebar-toggle.tsx: -------------------------------------------------------------------------------- 1 | import { ComponentProps } from 'react'; 2 | 3 | import { SidebarTrigger, useSidebar } from '@/components/ui/sidebar'; 4 | import { BetterTooltip } from '@/components/ui/tooltip'; 5 | import { cn } from '@/lib/utils'; 6 | 7 | import { SidebarLeftIcon } from './icons'; 8 | import { Button } from '../ui/button'; 9 | 10 | export function SidebarToggle({ 11 | className, 12 | }: ComponentProps) { 13 | const { toggleSidebar } = useSidebar(); 14 | 15 | return ( 16 | 17 | 24 | 25 | ); 26 | } 27 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 |