├── .env.example ├── .eslintrc.json ├── .github └── FUNDING.yml ├── .gitignore ├── .prettierignore ├── LICENSE ├── README.md ├── app ├── (auth) │ ├── auth-code-error │ │ └── page.tsx │ ├── layout.tsx │ ├── signin │ │ └── page.tsx │ └── signup │ │ └── page.tsx ├── api │ ├── auth │ │ ├── callback │ │ │ └── route.ts │ │ └── logout │ │ │ └── route.ts │ └── chat │ │ └── route.ts ├── apps │ ├── chat │ │ ├── [id] │ │ │ └── page.tsx │ │ ├── loading.tsx │ │ └── page.tsx │ ├── layout.tsx │ └── page.tsx ├── docs │ └── page.tsx ├── favicon.ico ├── globals.css ├── layout.tsx ├── page.tsx ├── profile │ ├── layout.tsx │ └── page.tsx └── styles │ └── custom.css ├── components.json ├── components ├── modules │ ├── apps │ │ ├── app-side-bar │ │ │ ├── AppSideBar.tsx │ │ │ ├── AppSideBarItem.tsx │ │ │ ├── AppSideBarList.tsx │ │ │ ├── AppSidebarSection.tsx │ │ │ └── index.ts │ │ └── chat │ │ │ ├── ChatForm.tsx │ │ │ ├── ChatHistory.tsx │ │ │ ├── ChatHistoryDrawer.tsx │ │ │ ├── ChatHistoryItem.tsx │ │ │ ├── ChatLayout.tsx │ │ │ ├── ChatPanel.tsx │ │ │ ├── CodeBlock.tsx │ │ │ ├── DeleteChatAction.tsx │ │ │ ├── EditChatAction.tsx │ │ │ ├── Header.tsx │ │ │ ├── MobileDrawerControls.tsx │ │ │ ├── NewChatButton.tsx │ │ │ ├── SystemPromptControl.tsx │ │ │ ├── action.ts │ │ │ ├── chat-members │ │ │ ├── AddMembersForm.tsx │ │ │ ├── ChatMemberItem.tsx │ │ │ ├── ChatMembers.tsx │ │ │ ├── DeleteMemberAction.tsx │ │ │ ├── action.ts │ │ │ ├── index.ts │ │ │ └── schema.ts │ │ │ ├── control-side-bar │ │ │ ├── ControlSidebar.tsx │ │ │ ├── ControlSidebarSheet.tsx │ │ │ ├── FrequencyPenaltySelector.tsx │ │ │ ├── MaxLengthSelector.tsx │ │ │ ├── ModelSelector.tsx │ │ │ ├── PresencePenaltySelector.tsx │ │ │ ├── TemperatureSelector.tsx │ │ │ ├── TopPSelector.tsx │ │ │ ├── action.ts │ │ │ ├── data │ │ │ │ └── models.ts │ │ │ └── index.ts │ │ │ ├── schema.ts │ │ │ ├── types.ts │ │ │ └── utils.ts │ ├── auth │ │ ├── LogoutButton.tsx │ │ ├── SocialLoginButton.tsx │ │ ├── SocialLoginOptions.tsx │ │ ├── UserAuthForm.tsx │ │ ├── UserSignupForm.tsx │ │ └── schema.ts │ ├── home │ │ ├── DescriptionHeadingText.tsx │ │ ├── FeatureItems.tsx │ │ └── HeroBannerImage.tsx │ └── profile │ │ ├── AccountDropdownMenu.tsx │ │ ├── Header.tsx │ │ ├── ProfileForm.tsx │ │ ├── action.ts │ │ ├── schema.ts │ │ └── type.ts ├── navigation │ ├── NavigationBar.tsx │ ├── NavigationMainMenu.tsx │ └── SideBar.tsx ├── theme │ ├── ThemeToggle.tsx │ └── index.ts └── ui │ ├── Accordion.tsx │ ├── AlertDialog.tsx │ ├── Avatar.tsx │ ├── Badge.tsx │ ├── Button.tsx │ ├── Card.tsx │ ├── Command.tsx │ ├── CustomIcon.tsx │ ├── Dialog.tsx │ ├── DropdownMenu.tsx │ ├── Flex.tsx │ ├── HoverCard.tsx │ ├── Input.tsx │ ├── Label.tsx │ ├── NavigationMenu.tsx │ ├── Popover.tsx │ ├── Resizable.tsx │ ├── ScrollArea.tsx │ ├── Section.tsx │ ├── Select.tsx │ ├── Separator.tsx │ ├── Sheet.tsx │ ├── Skeleton.tsx │ ├── Slider.tsx │ ├── Switch.tsx │ ├── Tabs.tsx │ ├── TextArea.tsx │ ├── Toast.tsx │ ├── Toaster.tsx │ ├── Tooltip.tsx │ ├── chat │ ├── ChatBubble.tsx │ ├── ChatInput.tsx │ ├── ChatList.tsx │ ├── ChatProfileHoverCard.tsx │ ├── Markdown.tsx │ ├── index.ts │ └── mention-input-default-style.ts │ ├── common │ ├── AppLogo.tsx │ ├── ChatScrollAnchor.tsx │ ├── MainLayout.tsx │ └── UserAvatar.tsx │ ├── form │ └── form-fields │ │ ├── InputField │ │ ├── InputField.tsx │ │ └── index.ts │ │ ├── SliderField │ │ ├── SliderField.tsx │ │ └── index.ts │ │ ├── TextAreaField │ │ ├── TextAreaField.tsx │ │ └── index.ts │ │ ├── index.ts │ │ └── types.ts │ ├── typography │ ├── Blockquote.tsx │ ├── Heading1.tsx │ ├── Heading2.tsx │ ├── Heading3.tsx │ ├── Heading4.tsx │ ├── Heading5.tsx │ ├── Paragraph.tsx │ ├── Subtle.tsx │ ├── index.ts │ └── types.ts │ └── use-toast.ts ├── config └── site.ts ├── env.mjs ├── hooks ├── useActiveTheme.tsx ├── useAtBottom.tsx ├── useChatIdFromPathName.tsx ├── useCopyToClipboard.tsx ├── useEnterSubmit.tsx ├── useMutationObserver.ts ├── usePrevious.tsx └── useSubscribeChatMessages.ts ├── lib ├── cache.ts ├── chat-input.ts ├── contants.ts ├── db │ ├── apps.ts │ ├── chat-members.ts │ ├── chats.ts │ ├── database.types.ts │ ├── index.ts │ ├── message.ts │ └── profile.ts ├── session.ts ├── stores │ └── profile.ts ├── supabase │ ├── client.ts │ ├── middleware.ts │ └── server.ts └── utils.ts ├── middleware.ts ├── next.config.js ├── package.json ├── postcss.config.js ├── prettier.config.js ├── public ├── avatar.png ├── chat-gpt.jpeg ├── featured-dark.jpg ├── featured-mobile.png ├── featured.jpg ├── logo.png ├── next.svg ├── screenshot.png └── vercel.svg ├── supabase ├── .gitignore ├── config.toml ├── migrations │ ├── 20240402103717_init_schema.sql │ ├── 20240403013936_rls.sql │ ├── 20240405151156_default_profile_id.sql │ ├── 20240420162835_chat_members.sql │ ├── 20240504083818_chat_members.sql │ ├── 20240609070425_handle_new_user_update.sql │ ├── 20240626065103_migrate_username_from_email.sql │ └── 20240626065226_update_handle_new_user.sql └── seed.sql ├── tailwind.config.js ├── tsconfig.json └── yarn.lock /.env.example: -------------------------------------------------------------------------------- 1 | NEXT_PUBLIC_APP_URL=YOUR_VALUE 2 | OPENAI_API_KEY=YOUR_VALUE 3 | NEXT_PUBLIC_SUPABASE_URL=YOUR_VALUE 4 | NEXT_PUBLIC_SUPABASE_ANON_KEY=YOUR_VALUE -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/eslintrc", 3 | "root": true, 4 | "extends": [ 5 | "next/core-web-vitals", 6 | "plugin:tailwindcss/recommended", 7 | "plugin:@typescript-eslint/recommended", 8 | "prettier" 9 | ], 10 | "plugins": ["tailwindcss", "prettier"], 11 | "rules": { 12 | "@next/next/no-html-link-for-pages": "off", 13 | "react/jsx-key": "off", 14 | "tailwindcss/no-custom-classname": "off", 15 | "@typescript-eslint/no-unused-vars": "error", 16 | "@typescript-eslint/ban-ts-comment": "off", 17 | "@typescript-eslint/no-empty-function": "off", 18 | "prettier/prettier": "error" 19 | }, 20 | "settings": { 21 | "tailwindcss": { 22 | "callees": ["cn"], 23 | "config": "tailwind.config.js" 24 | }, 25 | "next": { 26 | "rootDir": ["./"] 27 | } 28 | }, 29 | "overrides": [ 30 | { 31 | "files": ["*.ts", "*.tsx"], 32 | "parser": "@typescript-eslint/parser" 33 | } 34 | ] 35 | } 36 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [nphivu414] 4 | -------------------------------------------------------------------------------- /.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 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env.* 29 | 30 | # vercel 31 | .vercel 32 | 33 | # typescript 34 | *.tsbuildinfo 35 | next-env.d.ts 36 | 37 | .env -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | cache 2 | .cache 3 | package.json 4 | package-lock.json 5 | public 6 | CHANGELOG.md 7 | .yarn 8 | dist 9 | node_modules 10 | .next 11 | build 12 | .contentlayer -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Vu Nguyen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | AI Fusion Kit 3 |

AI Fusion Kit

4 |
5 | 6 |

7 | A feature-rich, highly customizable AI Web App Template, empowered by Next.js. 8 |

9 | 10 |

11 | Tech stacks · 12 | Installation · 13 | Run Locally · 14 | Authors 15 |

16 |
17 | 18 | ## Tech stacks 19 | - [Typescript](https://www.typescriptlang.org/) 20 | - [ReactJS](https://reactjs.org/) 21 | - [NextJS](https://nextjs.org/) 22 | - [Supabase](https://supabase.com/) 23 | - [Open AI API](https://platform.openai.com/docs/api-reference) 24 | - [Vercel AI SDK](https://github.com/vercel/ai) 25 | - [TailwindCSS](https://tailwindcss.com/) 26 | - [Shadcn UI](https://ui.shadcn.com/) 27 | - [Aceternity UI](https://ui.aceternity.com/) 28 | - [Next.js AI Chatbot](https://github.com/vercel-labs/ai-chatbot) 29 | 30 | 31 | ## Installation 32 | 33 | 1. Clone the repo 34 | ```sh 35 | git clone https://github.com/nphivu414/ai-fusion-kit 36 | ``` 37 | 2. Install dependencies 38 | ```sh 39 | yarn install 40 | ``` 41 | 3. Setup Supabase local development 42 | - Install [Docker](https://www.docker.com/get-started/) 43 | - The start command uses Docker to start the Supabase services. This command may take a while to run if this is the first time using the CLI. 44 | ```sh 45 | supabase start 46 | ``` 47 | - Once all of the Supabase services are running, you'll see output containing your local Supabase credentials. It should look like this, with urls and keys that you'll use in your local project: 48 | ```sh 49 | Started supabase local development setup. 50 | 51 | API URL: http://localhost:54321 52 | DB URL: postgresql://postgres:postgres@localhost:54322/postgres 53 | Studio URL: http://localhost:54323 54 | Inbucket URL: http://localhost:54324 55 | anon key: eyJh...... 56 | service_role key: eyJh...... 57 | ``` 58 | - The API URL will be used as the `NEXT_PUBLIC_SUPABASE_URL` in `.env.local` 59 | - For more information about how to use Supabase on your local development machine: https://supabase.com/docs/guides/cli/local-development 60 | 61 | 4. Get an account from OpenAI and generate your own API key 62 | 63 | 5. Rename `.env.example` to `.env.local` and populate with your values 64 | > Note: You should not commit your `.env` file or it will expose secrets that will allow others to control access to your various OpenAI and authentication provider accounts. 65 | 66 | ## Run Locally 67 | 68 | 1. Go to the project directory 69 | 70 | ```bash 71 | cd ai-fusion-kit 72 | ``` 73 | 74 | 2. Start the web app 75 | 76 | ```bash 77 | yarn dev 78 | ``` 79 | 80 | ## Authors 81 | - [@nphivu414](https://github.com/nphivu414) 82 | - [@toproad1407](https://github.com/toproad1407) 83 | -------------------------------------------------------------------------------- /app/(auth)/auth-code-error/page.tsx: -------------------------------------------------------------------------------- 1 | import { Metadata } from "next"; 2 | 3 | import { Heading3 } from "@/components/ui/typography/Heading3"; 4 | 5 | export const metadata: Metadata = { 6 | title: "Error", 7 | description: "Failed to sign in", 8 | }; 9 | 10 | export default async function AuthCodeError() { 11 | return ( 12 | <> 13 |
14 | Error 15 |

Failed to sign in

16 |
17 | 18 | ); 19 | } 20 | -------------------------------------------------------------------------------- /app/(auth)/layout.tsx: -------------------------------------------------------------------------------- 1 | import { AppLogo } from "@/components/ui/common/AppLogo"; 2 | 3 | type AuthLayoutProps = { 4 | children: React.ReactNode; 5 | }; 6 | 7 | export default function AuthLayout({ children }: AuthLayoutProps) { 8 | return ( 9 |
10 |
11 |
12 |
19 |
20 | 21 |
22 |
23 |
24 |
25 | {children} 26 |
27 |
28 |
29 |
30 | ); 31 | } 32 | -------------------------------------------------------------------------------- /app/(auth)/signin/page.tsx: -------------------------------------------------------------------------------- 1 | import { Metadata } from "next"; 2 | import { cookies } from "next/headers"; 3 | import { redirect } from "next/navigation"; 4 | 5 | import { siteConfig } from "@/config/site"; 6 | import { getCurrentUser } from "@/lib/session"; 7 | import { createClient } from "@/lib/supabase/server"; 8 | import { Heading3 } from "@/components/ui/typography"; 9 | import { UserAuthForm } from "@/components/modules/auth/UserAuthForm"; 10 | 11 | export const metadata: Metadata = { 12 | title: "Sigin", 13 | description: "Sigin to your account", 14 | }; 15 | 16 | export const runtime = "edge"; 17 | export const dynamic = "force-dynamic"; 18 | 19 | export default async function LoginPage() { 20 | const cookieStore = cookies(); 21 | const supabase = createClient(cookieStore); 22 | const user = await getCurrentUser(supabase); 23 | 24 | if (user) { 25 | redirect(`/apps/chat`); 26 | } 27 | 28 | return ( 29 | <> 30 |
31 | {siteConfig.name} 32 |

33 | Empowering Your Imagination with AI Services 34 |

35 |
36 | 37 | 38 | ); 39 | } 40 | -------------------------------------------------------------------------------- /app/(auth)/signup/page.tsx: -------------------------------------------------------------------------------- 1 | import { Metadata } from "next"; 2 | import { cookies } from "next/headers"; 3 | import { redirect } from "next/navigation"; 4 | 5 | import { siteConfig } from "@/config/site"; 6 | import { getCurrentUser } from "@/lib/session"; 7 | import { createClient } from "@/lib/supabase/server"; 8 | import { Heading3 } from "@/components/ui/typography"; 9 | import { UserSignupForm } from "@/components/modules/auth/UserSignupForm"; 10 | 11 | export const runtime = "edge"; 12 | export const metadata: Metadata = { 13 | title: "Signup", 14 | description: "Signup a new account", 15 | }; 16 | 17 | export const dynamic = "force-dynamic"; 18 | 19 | export default async function LoginPage() { 20 | const cookieStore = cookies(); 21 | const supabase = createClient(cookieStore); 22 | const user = await getCurrentUser(supabase); 23 | 24 | if (user) { 25 | redirect(`/apps/chat`); 26 | } 27 | 28 | return ( 29 | <> 30 |
31 | {siteConfig.name} 32 |

33 | Empowering Your Imagination with AI Services 34 |

35 |
36 | 37 | 38 | ); 39 | } 40 | -------------------------------------------------------------------------------- /app/api/auth/callback/route.ts: -------------------------------------------------------------------------------- 1 | import { cookies } from "next/headers"; 2 | import { NextResponse } from "next/server"; 3 | 4 | import { createClient } from "@/lib/supabase/server"; 5 | 6 | export async function GET(request: Request) { 7 | const { searchParams, origin } = new URL(request.url); 8 | const code = searchParams.get("code"); 9 | // if "next" is in param, use it as the redirect URL 10 | const next = searchParams.get("next") ?? "/"; 11 | 12 | if (code) { 13 | const cookieStore = cookies(); 14 | const supabase = createClient(cookieStore); 15 | 16 | const { error } = await supabase.auth.exchangeCodeForSession(code); 17 | if (!error) { 18 | return NextResponse.redirect(`${origin}${next}`); 19 | } 20 | } 21 | 22 | // return the user to an error page with instructions 23 | return NextResponse.redirect(`${origin}/auth-code-error`); 24 | } 25 | -------------------------------------------------------------------------------- /app/api/auth/logout/route.ts: -------------------------------------------------------------------------------- 1 | import { cookies } from "next/headers"; 2 | import { NextResponse, type NextRequest } from "next/server"; 3 | 4 | import { createClient } from "@/lib/supabase/server"; 5 | 6 | export const dynamic = "force-dynamic"; 7 | 8 | export async function POST(req: NextRequest) { 9 | const cookieStore = cookies(); 10 | const supabase = createClient(cookieStore); 11 | 12 | // Check if we have a session 13 | const { 14 | data: { user }, 15 | } = await supabase.auth.getUser(); 16 | 17 | if (user) { 18 | await supabase.auth.signOut(); 19 | } 20 | 21 | return NextResponse.redirect(new URL("/signin", req.url), { 22 | status: 302, 23 | }); 24 | } 25 | -------------------------------------------------------------------------------- /app/api/chat/route.ts: -------------------------------------------------------------------------------- 1 | import { cookies } from "next/headers"; 2 | import { env } from "@/env.mjs"; 3 | import { createOpenAI } from "@ai-sdk/openai"; 4 | import { Message, streamText } from "ai"; 5 | import { pick } from "lodash"; 6 | import { AxiomRequest, withAxiom } from "next-axiom"; 7 | 8 | import { getAppBySlug } from "@/lib/db/apps"; 9 | import { createNewChatMember } from "@/lib/db/chat-members"; 10 | import { createNewChat } from "@/lib/db/chats"; 11 | import { 12 | createNewMessage, 13 | deleteMessagesFrom, 14 | getMessageById, 15 | } from "@/lib/db/message"; 16 | import { getCurrentUser } from "@/lib/session"; 17 | import { createClient } from "@/lib/supabase/server"; 18 | 19 | export const dynamic = "force-dynamic"; 20 | export const runtime = "edge"; 21 | export const preferredRegion = "home"; 22 | 23 | const openai = createOpenAI({ 24 | apiKey: env.OPENAI_API_KEY, 25 | }); 26 | 27 | export const POST = withAxiom(async (req: AxiomRequest) => { 28 | const log = req.log.with({ 29 | route: "api/chat", 30 | }); 31 | 32 | const cookieStore = cookies(); 33 | const supabase = createClient(cookieStore); 34 | const params = await req.json(); 35 | const { 36 | messages, 37 | temperature, 38 | model, 39 | maxTokens, 40 | topP, 41 | frequencyPenalty, 42 | presencePenalty, 43 | chatId, 44 | isRegenerate, 45 | regenerateMessageId, 46 | isNewChat, 47 | enableChatAssistant = true, 48 | } = params; 49 | 50 | const user = await getCurrentUser(supabase); 51 | const currentApp = await getAppBySlug(supabase, "/apps/chat"); 52 | 53 | if (!user) { 54 | return new Response("Unauthorized", { status: 401 }); 55 | } 56 | 57 | const lastMessage = messages[messages.length - 1]; 58 | 59 | if (!isRegenerate) { 60 | if (isNewChat && currentApp) { 61 | await createNewChat(supabase, { 62 | id: chatId, 63 | app_id: currentApp.id, 64 | name: lastMessage.content, 65 | }); 66 | await createNewChatMember(supabase, { 67 | chat_id: chatId, 68 | member_id: user.id, 69 | }); 70 | } 71 | await createNewMessage(supabase, { 72 | chat_id: chatId, 73 | content: lastMessage.content, 74 | role: "user", 75 | id: lastMessage.id, 76 | }); 77 | } else if (regenerateMessageId) { 78 | const fromMessage = await getMessageById(supabase, regenerateMessageId); 79 | if (fromMessage?.created_at) { 80 | await deleteMessagesFrom(supabase, chatId, fromMessage.created_at); 81 | } 82 | } 83 | 84 | if (!enableChatAssistant) { 85 | return new Response(null, { 86 | status: 200, 87 | headers: { 88 | "Content-Type": "application/json", 89 | "should-redirect-to-new-chat": "true", 90 | }, 91 | }); 92 | } 93 | 94 | log.debug("Start stream text"); 95 | const response = await streamText({ 96 | model: openai(model), 97 | temperature, 98 | messages: messages.map((message: Message) => 99 | pick(message, "content", "role") 100 | ), 101 | maxTokens, 102 | topP, 103 | frequencyPenalty, 104 | presencePenalty, 105 | onFinish: async ({ text }) => { 106 | await createNewMessage(supabase, { 107 | chat_id: chatId, 108 | content: text, 109 | role: "assistant", 110 | }); 111 | }, 112 | }); 113 | log.debug("End stream text"); 114 | 115 | return response.toAIStreamResponse(); 116 | }); 117 | -------------------------------------------------------------------------------- /app/apps/chat/[id]/page.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Metadata } from "next"; 3 | import { cookies } from "next/headers"; 4 | import { redirect } from "next/navigation"; 5 | import { Message } from "ai"; 6 | 7 | import { 8 | CHAT_MEMBER_SIDEBAR_LAYOUT_COOKIE, 9 | DEFAULT_CHAT_MEMBER_SIDEBAR_LAYOUT, 10 | } from "@/lib/contants"; 11 | import { getAppBySlug } from "@/lib/db/apps"; 12 | import { getChatMembers } from "@/lib/db/chat-members"; 13 | import { getChatById, getChats } from "@/lib/db/chats"; 14 | import { getMessages } from "@/lib/db/message"; 15 | import { getCurrentUser } from "@/lib/session"; 16 | import { createClient } from "@/lib/supabase/server"; 17 | import { ChatPanel } from "@/components/modules/apps/chat/ChatPanel"; 18 | import { ChatParams } from "@/components/modules/apps/chat/types"; 19 | 20 | export const runtime = "edge"; 21 | export const preferredRegion = "home"; 22 | 23 | export const metadata: Metadata = { 24 | title: "Chat", 25 | description: 26 | "Chat with your AI assistant to generate new ideas and get inspired.", 27 | }; 28 | 29 | export default async function ChatPage({ params }: { params: { id: string } }) { 30 | const chatId = params.id; 31 | 32 | const cookieStore = cookies(); 33 | const supabase = createClient(cookieStore); 34 | const user = await getCurrentUser(supabase); 35 | const currentApp = await getAppBySlug(supabase, "/apps/chat"); 36 | 37 | if (!currentApp || !user) { 38 | return
No app found
; 39 | } 40 | 41 | const chats = await getChats(supabase, currentApp.id); 42 | 43 | const dbMessages = await getMessages(supabase, chatId); 44 | 45 | const chatDetails = await getChatById(supabase, chatId); 46 | if (!chatDetails) { 47 | redirect("/apps/chat"); 48 | } 49 | const chatParams = chatDetails?.settings as ChatParams | undefined; 50 | const isChatHost = chatDetails?.profile_id === user.id; 51 | 52 | const initialChatMessages: Message[] = dbMessages?.length 53 | ? dbMessages.map((message) => { 54 | return { 55 | id: message.id, 56 | role: message.role || "system", 57 | content: message.content || "", 58 | data: { 59 | profile_id: message.profile_id, 60 | chat_id: message.chat_id, 61 | chatBubleDirection: 62 | message.role === "user" && message.profile_id === user.id 63 | ? "end" 64 | : "start", 65 | }, 66 | }; 67 | }) 68 | : []; 69 | 70 | if (chatParams?.description) { 71 | initialChatMessages.unshift({ 72 | id: "description", 73 | role: "system", 74 | content: chatParams.description, 75 | }); 76 | } 77 | 78 | const chatMembers = await getChatMembers(supabase, chatId); 79 | 80 | const memberSidebarLayout = cookies().get(CHAT_MEMBER_SIDEBAR_LAYOUT_COOKIE); 81 | 82 | let defaultMemberSidebarLayout = DEFAULT_CHAT_MEMBER_SIDEBAR_LAYOUT; 83 | if (memberSidebarLayout) { 84 | defaultMemberSidebarLayout = JSON.parse(memberSidebarLayout.value); 85 | } 86 | 87 | return ( 88 | 97 | ); 98 | } 99 | -------------------------------------------------------------------------------- /app/apps/chat/loading.tsx: -------------------------------------------------------------------------------- 1 | import { Loader } from "lucide-react"; 2 | 3 | import { Separator } from "@/components/ui/Separator"; 4 | import { Skeleton } from "@/components/ui/Skeleton"; 5 | import { Heading2 } from "@/components/ui/typography"; 6 | 7 | export default function Page() { 8 | return ( 9 |
10 |
11 |
12 |
13 | GPT AI Assistant 14 |
15 | 16 |
17 |
18 | 19 |
20 |
21 |
22 |
23 |
24 | 25 |
26 | 27 | 28 | 29 | 30 |
31 | 32 |
33 | 34 | 35 | 36 |
37 |
38 | 39 | 40 | 41 |
42 |
43 | 44 | 45 | 46 |
47 |
48 | 49 | 50 | 51 |
52 |
53 |
54 | ); 55 | } 56 | -------------------------------------------------------------------------------- /app/apps/chat/page.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Metadata } from "next"; 3 | import { cookies } from "next/headers"; 4 | import { v4 as uuidv4 } from "uuid"; 5 | 6 | import { getAppBySlug } from "@/lib/db/apps"; 7 | import { getChats } from "@/lib/db/chats"; 8 | import { getCurrentUser } from "@/lib/session"; 9 | import { createClient } from "@/lib/supabase/server"; 10 | import { ChatPanel } from "@/components/modules/apps/chat/ChatPanel"; 11 | 12 | export const metadata: Metadata = { 13 | title: "Create a New Chat", 14 | }; 15 | 16 | export default async function NewChatPage() { 17 | const chatId = uuidv4(); 18 | 19 | const cookieStore = cookies(); 20 | const supabase = createClient(cookieStore); 21 | const user = await getCurrentUser(supabase); 22 | const currentApp = await getAppBySlug(supabase, "/apps/chat"); 23 | 24 | if (!currentApp || !user) { 25 | return
No app found
; 26 | } 27 | 28 | const chats = await getChats(supabase, currentApp.id); 29 | 30 | return ( 31 | 39 | ); 40 | } 41 | -------------------------------------------------------------------------------- /app/apps/layout.tsx: -------------------------------------------------------------------------------- 1 | import { cookies } from "next/headers"; 2 | 3 | import { getAppBySlug } from "@/lib/db/apps"; 4 | import { getChats } from "@/lib/db/chats"; 5 | import { getCurrentUser } from "@/lib/session"; 6 | import { createClient } from "@/lib/supabase/server"; 7 | import { MainLayout } from "@/components/ui/common/MainLayout"; 8 | import { ChatHistory } from "@/components/modules/apps/chat/ChatHistory"; 9 | 10 | interface AppLayoutProps { 11 | children: React.ReactNode; 12 | } 13 | 14 | export default async function AppLayout({ children }: AppLayoutProps) { 15 | const cookieStore = cookies(); 16 | const supabase = createClient(cookieStore); 17 | const user = await getCurrentUser(supabase); 18 | const currentApp = await getAppBySlug(supabase, "/apps/chat"); 19 | 20 | if (!currentApp || !user) { 21 | return
No app found
; 22 | } 23 | 24 | const chats = await getChats(supabase, currentApp.id); 25 | 26 | return ( 27 | 28 |
29 |
30 |
31 |
32 |
33 | 34 |
35 | {children} 36 |
37 |
38 |
39 |
40 |
41 | ); 42 | } 43 | -------------------------------------------------------------------------------- /app/apps/page.tsx: -------------------------------------------------------------------------------- 1 | import { Heading1 } from "@/components/ui/typography"; 2 | 3 | export const runtime = "edge"; 4 | 5 | export default function Apps() { 6 | return Apps; 7 | } 8 | -------------------------------------------------------------------------------- /app/docs/page.tsx: -------------------------------------------------------------------------------- 1 | export default async function Docs() { 2 | return
docs
; 3 | } 4 | -------------------------------------------------------------------------------- /app/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nphivu414/ai-fusion-kit/1d250ee29b7291f9b69b09c3a427957bc0d0fbb0/app/favicon.ico -------------------------------------------------------------------------------- /app/globals.css: -------------------------------------------------------------------------------- 1 | @import url('./styles/custom.css'); 2 | 3 | @tailwind base; 4 | @tailwind components; 5 | @tailwind utilities; 6 | 7 | .drawer-toggle:checked ~ .drawer-side { 8 | backdrop-filter: blur(5px); 9 | } 10 | 11 | @media screen and (-webkit-min-device-pixel-ratio:0) { 12 | select, 13 | textarea, 14 | input { 15 | font-size: 16px !important; 16 | } 17 | } 18 | 19 | @layer base { 20 | :root { 21 | --background: 0 0% 100%; 22 | --foreground: 240 10% 3.9%; 23 | --card: 0 0% 100%; 24 | --card-foreground: 240 10% 3.9%; 25 | --popover: 0 0% 100%; 26 | --popover-foreground: 240 10% 3.9%; 27 | --primary: 262.1 83.3% 57.8%; 28 | --primary-foreground: 210 20% 98%; 29 | --secondary: 240 4.8% 95.9%; 30 | --secondary-foreground: 240 5.9% 10%; 31 | --muted: 240 4.8% 95.9%; 32 | --muted-foreground: 240 3.8% 46.1%; 33 | --accent: 240 4.8% 95.9%; 34 | --accent-foreground: 240 5.9% 10%; 35 | --destructive: 0 84.2% 60.2%; 36 | --destructive-foreground: 0 0% 98%; 37 | --border: 240 5.9% 90%; 38 | --input: 240 5.9% 90%; 39 | --ring: 262.1 83.3% 57.8%; 40 | --radius: 0.5rem; 41 | } 42 | 43 | .dark { 44 | --background: 20 14.3% 4.1%; 45 | --foreground: 0 0% 95%; 46 | --card: 24 9.8% 10%; 47 | --card-foreground: 0 0% 95%; 48 | --popover: 0 0% 9%; 49 | --popover-foreground: 0 0% 95%; 50 | --primary: 263.4 70% 50.4%; 51 | --primary-foreground: 210 20% 98%; 52 | --secondary: 240 3.7% 15.9%; 53 | --secondary-foreground: 0 0% 98%; 54 | --muted: 0 0% 15%; 55 | --muted-foreground: 240 5% 64.9%; 56 | --accent: 12 6.5% 15.1%; 57 | --accent-foreground: 0 0% 98%; 58 | --destructive: 0 62.8% 30.6%; 59 | --destructive-foreground: 0 85.7% 97.3%; 60 | --border: 240 3.7% 15.9%; 61 | --input: 240 3.7% 15.9%; 62 | --ring: 263.4 70% 50.4%; 63 | } 64 | } 65 | 66 | @layer base { 67 | * { 68 | @apply border-border; 69 | } 70 | body { 71 | @apply bg-background text-foreground; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /app/layout.tsx: -------------------------------------------------------------------------------- 1 | import { AxiomWebVitals } from "next-axiom"; 2 | 3 | import "./globals.css"; 4 | 5 | import { Metadata, Viewport } from "next"; 6 | import { Analytics } from "@vercel/analytics/react"; 7 | import { GeistMono } from "geist/font/mono"; 8 | import { GeistSans } from "geist/font/sans"; 9 | import { ThemeProvider } from "next-themes"; 10 | 11 | import { siteConfig } from "@/config/site"; 12 | import { Toaster } from "@/components/ui/Toaster"; 13 | 14 | export const metadata: Metadata = { 15 | title: { 16 | default: siteConfig.name, 17 | template: `%s - ${siteConfig.name}`, 18 | }, 19 | description: siteConfig.description, 20 | icons: { 21 | icon: "/favicon.ico", 22 | shortcut: "/favicon-16x16.png", 23 | apple: "/apple-touch-icon.png", 24 | }, 25 | }; 26 | 27 | export const viewport: Viewport = { 28 | themeColor: [ 29 | { media: "(prefers-color-scheme: light)", color: "white" }, 30 | { media: "(prefers-color-scheme: dark)", color: "black" }, 31 | ], 32 | }; 33 | 34 | interface RootLayoutProps { 35 | children: React.ReactNode; 36 | } 37 | 38 | export default function RootLayout({ children }: RootLayoutProps) { 39 | return ( 40 | <> 41 | 46 | 50 | 51 | 52 | 53 | {children} 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | ); 62 | } 63 | -------------------------------------------------------------------------------- /app/page.tsx: -------------------------------------------------------------------------------- 1 | import { Metadata } from "next"; 2 | import Link from "next/link"; 3 | import { Play } from "lucide-react"; 4 | 5 | import { siteConfig } from "@/config/site"; 6 | import { cn } from "@/lib/utils"; 7 | import { buttonVariants } from "@/components/ui/Button"; 8 | import { MainLayout } from "@/components/ui/common/MainLayout"; 9 | import { Heading1 } from "@/components/ui/typography"; 10 | import { DescriptionHeadingText } from "@/components/modules/home/DescriptionHeadingText"; 11 | import { FeatureItems } from "@/components/modules/home/FeatureItems"; 12 | import { HeroBannerImage } from "@/components/modules/home/HeroBannerImage"; 13 | 14 | export const metadata: Metadata = { 15 | title: siteConfig.name, 16 | description: siteConfig.description, 17 | }; 18 | 19 | export const runtime = "edge"; 20 | 21 | export default async function Home() { 22 | return ( 23 | 24 |
25 |
26 |
27 |
28 |
29 | 30 | {siteConfig.name} 31 | 32 | 33 |
34 | 43 | Demo 44 | 45 | 46 | Get Started 47 | 48 |
49 |
50 |
51 |
52 |
53 | 54 | 55 |
56 |
57 |
58 | ); 59 | } 60 | -------------------------------------------------------------------------------- /app/profile/layout.tsx: -------------------------------------------------------------------------------- 1 | import { MainLayout } from "@/components/ui/common/MainLayout"; 2 | 3 | interface AppLayoutProps { 4 | children: React.ReactNode; 5 | } 6 | 7 | export default function AppLayout({ children }: AppLayoutProps) { 8 | return ( 9 | 10 |
{children}
11 |
12 | ); 13 | } 14 | -------------------------------------------------------------------------------- /app/profile/page.tsx: -------------------------------------------------------------------------------- 1 | import { cookies } from "next/headers"; 2 | 3 | import { getCurrentProfile } from "@/lib/db/profile"; 4 | import { getCurrentUser } from "@/lib/session"; 5 | import { createClient } from "@/lib/supabase/server"; 6 | import { Header } from "@/components/modules/profile/Header"; 7 | import { ProfileForm } from "@/components/modules/profile/ProfileForm"; 8 | import { ProfileFormValues } from "@/components/modules/profile/type"; 9 | 10 | export const dynamic = "force-dynamic"; 11 | 12 | export const runtime = "edge"; 13 | 14 | export default async function Profile() { 15 | const cookieStore = cookies(); 16 | const supabase = createClient(cookieStore); 17 | const profile = await getCurrentProfile(supabase); 18 | const user = await getCurrentUser(supabase); 19 | 20 | if (!profile) { 21 | return null; 22 | } 23 | 24 | const { avatar_url, full_name, username, website } = profile; 25 | const profileFormValues: ProfileFormValues = { 26 | fullName: full_name || undefined, 27 | username: username || undefined, 28 | website: website || undefined, 29 | }; 30 | 31 | return ( 32 |
33 |
40 | 41 |
42 | ); 43 | } 44 | -------------------------------------------------------------------------------- /components.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://ui.shadcn.com/schema.json", 3 | "style": "new-york", 4 | "rsc": true, 5 | "tsx": true, 6 | "tailwind": { 7 | "config": "tailwind.config.js", 8 | "css": "app/globals.css", 9 | "baseColor": "zinc", 10 | "cssVariables": true 11 | }, 12 | "aliases": { 13 | "components": "@/components", 14 | "utils": "@/lib/utils" 15 | } 16 | } -------------------------------------------------------------------------------- /components/modules/apps/app-side-bar/AppSideBar.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { cookies } from "next/headers"; 3 | 4 | import { getApps } from "@/lib/db/apps"; 5 | import { createClient } from "@/lib/supabase/server"; 6 | 7 | import { AppSideBarList } from "./AppSideBarList"; 8 | 9 | export const AppSideBar = async () => { 10 | const cookieStore = cookies(); 11 | const supabase = await createClient(cookieStore); 12 | const apps = await getApps(supabase); 13 | return ( 14 | 20 | ); 21 | }; 22 | -------------------------------------------------------------------------------- /components/modules/apps/app-side-bar/AppSideBarItem.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import Link from "next/link"; 3 | 4 | import { App } from "@/lib/db"; 5 | import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/Avatar"; 6 | 7 | type AppSideBarItemProps = Pick< 8 | App, 9 | "name" | "slug" | "description" | "logo_url" 10 | >; 11 | 12 | export const AppSideBarItem = ({ 13 | name, 14 | slug, 15 | description, 16 | logo_url: logoUrl, 17 | }: AppSideBarItemProps) => { 18 | return ( 19 |
  • 20 | 21 |
    22 |
    23 | 24 | {logoUrl ? ( 25 | 26 | ) : null} 27 | 28 |
    29 | 30 | 31 |
    32 |
    33 |

    {name}

    34 | {description ? ( 35 |

    {description}

    36 | ) : null} 37 |
    38 |
    39 | 40 |
  • 41 | ); 42 | }; 43 | -------------------------------------------------------------------------------- /components/modules/apps/app-side-bar/AppSideBarList.tsx: -------------------------------------------------------------------------------- 1 | import { App } from "@/lib/db"; 2 | 3 | import { AppSideBarItem } from "./AppSideBarItem"; 4 | 5 | type AppSideBarListProps = { 6 | apps: App[] | null; 7 | }; 8 | 9 | export const AppSideBarList = ({ apps }: AppSideBarListProps) => { 10 | if (!apps?.length) { 11 | return
    No apps found
    ; 12 | } 13 | 14 | return ( 15 |
      16 | {apps.map((app) => { 17 | const { id, name, description, slug, logo_url } = app; 18 | return ( 19 | 26 | ); 27 | })} 28 |
    29 | ); 30 | }; 31 | -------------------------------------------------------------------------------- /components/modules/apps/app-side-bar/AppSidebarSection.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | 3 | import { Heading5 } from "@/components/ui/typography"; 4 | 5 | type AppSidebarSectionProps = { 6 | title: string; 7 | children: React.ReactNode; 8 | }; 9 | 10 | export const AppSidebarSection = ({ 11 | title, 12 | children, 13 | }: AppSidebarSectionProps) => { 14 | return ( 15 |
    16 | {title} 17 | {children} 18 |
    19 | ); 20 | }; 21 | -------------------------------------------------------------------------------- /components/modules/apps/app-side-bar/index.ts: -------------------------------------------------------------------------------- 1 | export * from "./AppSideBar"; 2 | -------------------------------------------------------------------------------- /components/modules/apps/chat/ChatForm.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { SendHorizonal } from "lucide-react"; 3 | import { MentionsInputProps, SuggestionDataItem } from "react-mentions"; 4 | 5 | import { Chat, ChatMemberProfile } from "@/lib/db"; 6 | import { useProfileStore } from "@/lib/stores/profile"; 7 | import { useEnterSubmit } from "@/hooks/useEnterSubmit"; 8 | import { Button } from "@/components/ui/Button"; 9 | import { ChatInput } from "@/components/ui/chat"; 10 | 11 | import { MobileDrawerControl } from "./MobileDrawerControls"; 12 | 13 | type ChatFormProps = { 14 | chatInput: string; 15 | chats: Chat[] | null; 16 | isChatStreamming: boolean; 17 | chatMembers: ChatMemberProfile[] | null; 18 | onSubmit: (e: React.FormEvent) => void; 19 | onInputChange: ( 20 | e: 21 | | React.ChangeEvent 22 | | React.ChangeEvent 23 | ) => void; 24 | }; 25 | 26 | export const ChatForm = ({ 27 | chats, 28 | isChatStreamming, 29 | chatMembers, 30 | chatInput, 31 | onSubmit, 32 | onInputChange, 33 | }: ChatFormProps) => { 34 | const { formRef, onKeyDown } = useEnterSubmit(); 35 | const currentProfile = useProfileStore((state) => state.profile); 36 | 37 | const mentionData: SuggestionDataItem[] = React.useMemo(() => { 38 | const mentionData = [{ id: "assistant", display: "Assistant" }]; 39 | 40 | if (!chatMembers) return mentionData; 41 | 42 | chatMembers.forEach((member) => { 43 | if (!member.profiles) return; 44 | mentionData.push({ 45 | id: member.profiles.id, 46 | display: member.profiles.username || "", 47 | }); 48 | }); 49 | 50 | return mentionData.filter((mention) => mention.id !== currentProfile?.id); 51 | }, [chatMembers, currentProfile?.id]); 52 | 53 | const handleOnChange: MentionsInputProps["onChange"] = (e) => { 54 | onInputChange({ 55 | target: { value: e.target.value }, 56 | } as React.ChangeEvent); 57 | }; 58 | 59 | return ( 60 |
    61 |
    62 | 68 | 69 |
    70 | 74 |
    75 | 76 |
    77 | ); 78 | }; 79 | -------------------------------------------------------------------------------- /components/modules/apps/chat/ChatHistory.tsx: -------------------------------------------------------------------------------- 1 | "use client"; 2 | 3 | import React from "react"; 4 | 5 | import { Chat } from "@/lib/db"; 6 | import { useChatIdFromPathName } from "@/hooks/useChatIdFromPathName"; 7 | import { Separator } from "@/components/ui/Separator"; 8 | import { Paragraph } from "@/components/ui/typography"; 9 | 10 | import { ChatHistoryItem } from "./ChatHistoryItem"; 11 | import { NewChatButton } from "./NewChatButton"; 12 | 13 | type ChatHistoryProps = { 14 | data: Chat[] | null; 15 | closeDrawer?: () => void; 16 | }; 17 | 18 | export const ChatHistory = ({ data, closeDrawer }: ChatHistoryProps) => { 19 | const chatId = useChatIdFromPathName(); 20 | 21 | return ( 22 | 50 | ); 51 | }; 52 | -------------------------------------------------------------------------------- /components/modules/apps/chat/ChatHistoryDrawer.tsx: -------------------------------------------------------------------------------- 1 | "use client"; 2 | 3 | import React from "react"; 4 | import { History } from "lucide-react"; 5 | import { Drawer } from "vaul"; 6 | 7 | import { Chat } from "@/lib/db"; 8 | import { cn } from "@/lib/utils"; 9 | import { Button } from "@/components/ui/Button"; 10 | 11 | import { ChatHistory } from "./ChatHistory"; 12 | 13 | type ChatHistoryDrawerProps = { 14 | data: Chat[] | null; 15 | }; 16 | 17 | export const ChatHistoryDrawer = ({ data }: ChatHistoryDrawerProps) => { 18 | const [drawerOpen, setDrawerOpen] = React.useState(false); 19 | 20 | const onHistoryButtonClick = () => { 21 | setDrawerOpen(true); 22 | }; 23 | 24 | const closeDrawer = React.useCallback(() => { 25 | setDrawerOpen(false); 26 | }, []); 27 | 28 | return ( 29 | 30 | 33 | 34 | 35 | 40 |
    41 |
    42 | 43 |
    44 | 45 | 46 | 47 | ); 48 | }; 49 | -------------------------------------------------------------------------------- /components/modules/apps/chat/ChatHistoryItem.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import Link from "next/link"; 3 | import { MessageCircle } from "lucide-react"; 4 | 5 | import { Chat } from "@/lib/db"; 6 | import { cn } from "@/lib/utils"; 7 | 8 | import { DeleteChatAction } from "./DeleteChatAction"; 9 | import { EditChatAction } from "./EditChatAction"; 10 | 11 | type ChatHistoryItemProps = { 12 | chat: Chat; 13 | isActive: boolean; 14 | closeDrawer?: () => void; 15 | }; 16 | 17 | export const ChatHistoryItem = ({ 18 | chat, 19 | isActive, 20 | closeDrawer, 21 | }: ChatHistoryItemProps) => { 22 | const renderActionButtons = () => { 23 | return ( 24 |
    29 |
    34 | 35 | 36 |
    37 |
    38 | ); 39 | }; 40 | 41 | return ( 42 |
  • 43 |
    49 |
    50 |
    51 | 61 |
    62 | 63 |
    64 |

    65 | {chat.name} 66 |

    67 | 68 | {renderActionButtons()} 69 |
    70 |
    71 |
    72 |
  • 73 | ); 74 | }; 75 | -------------------------------------------------------------------------------- /components/modules/apps/chat/ChatLayout.tsx: -------------------------------------------------------------------------------- 1 | import { MainLayout } from "@/components/ui/common/MainLayout"; 2 | 3 | interface ChatLayoutProps { 4 | children: React.ReactNode; 5 | leftSidebarElement: React.ReactNode; 6 | } 7 | 8 | export const ChatLayout = ({ 9 | children, 10 | leftSidebarElement, 11 | }: ChatLayoutProps) => { 12 | return ( 13 | 14 |
    15 |
    16 |
    17 |
    18 |
    19 | {leftSidebarElement} 20 |
    21 | {children} 22 |
    23 |
    24 |
    25 |
    26 |
    27 | ); 28 | }; 29 | -------------------------------------------------------------------------------- /components/modules/apps/chat/DeleteChatAction.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { useRouter } from "next/navigation"; 3 | import { Loader, Trash2 } from "lucide-react"; 4 | 5 | import { cn } from "@/lib/utils"; 6 | import { useChatIdFromPathName } from "@/hooks/useChatIdFromPathName"; 7 | import { 8 | AlertDialog, 9 | AlertDialogAction, 10 | AlertDialogCancel, 11 | AlertDialogContent, 12 | AlertDialogDescription, 13 | AlertDialogFooter, 14 | AlertDialogHeader, 15 | AlertDialogTitle, 16 | AlertDialogTrigger, 17 | } from "@/components/ui/AlertDialog"; 18 | import { Button, buttonVariants } from "@/components/ui/Button"; 19 | import { toast } from "@/components/ui/use-toast"; 20 | 21 | import { deleteChat } from "./action"; 22 | import { ChatActionProps } from "./types"; 23 | 24 | export const DeleteChatAction = ({ chat, ...rest }: ChatActionProps) => { 25 | const [isAlertOpen, setIsAlertOpen] = React.useState(false); 26 | const [pendingDeleteChat, startDeleteChat] = React.useTransition(); 27 | const { replace } = useRouter(); 28 | const chatIdFromPathName = useChatIdFromPathName(); 29 | 30 | const onDelete = (e: React.MouseEvent) => { 31 | e.preventDefault(); 32 | startDeleteChat(async () => { 33 | try { 34 | await deleteChat(chat.id); 35 | toast({ 36 | title: "Success", 37 | description: "Your chat has been deleted.", 38 | }); 39 | if (chatIdFromPathName === chat.id) { 40 | replace("/apps/chat"); 41 | } 42 | setIsAlertOpen(false); 43 | } catch (error) { 44 | toast({ 45 | title: "Error", 46 | description: "Failed to delete chat. Please try again.", 47 | variant: "destructive", 48 | }); 49 | } 50 | }); 51 | }; 52 | 53 | return ( 54 | 55 | 56 | 59 | 60 | 61 | 62 | 63 | Are you sure you want to delete this chat? 64 | 65 | 66 | This action cannot be undone. This will permanently delete your 67 | chat. 68 | 69 | 70 | 71 | Cancel 72 | 80 | {pendingDeleteChat ? ( 81 | 82 | ) : ( 83 | "Delete" 84 | )} 85 | 86 | 87 | 88 | 89 | ); 90 | }; 91 | -------------------------------------------------------------------------------- /components/modules/apps/chat/EditChatAction.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Edit, Loader } from "lucide-react"; 3 | 4 | import { 5 | AlertDialog, 6 | AlertDialogAction, 7 | AlertDialogCancel, 8 | AlertDialogContent, 9 | AlertDialogDescription, 10 | AlertDialogFooter, 11 | AlertDialogHeader, 12 | AlertDialogTitle, 13 | AlertDialogTrigger, 14 | } from "@/components/ui/AlertDialog"; 15 | import { Button } from "@/components/ui/Button"; 16 | import { Input } from "@/components/ui/Input"; 17 | import { toast } from "@/components/ui/use-toast"; 18 | 19 | import { updateChat } from "./action"; 20 | import { ChatActionProps } from "./types"; 21 | 22 | export const EditChatAction = ({ chat, ...rest }: ChatActionProps) => { 23 | const [isAlertOpen, setIsAlertOpen] = React.useState(false); 24 | const [pendingUpdateChat, startUpdateChat] = React.useTransition(); 25 | const [inputValue, setInputValue] = React.useState(chat.name || ""); 26 | 27 | const handleDelete = () => { 28 | startUpdateChat(async () => { 29 | try { 30 | await updateChat({ 31 | id: chat.id, 32 | name: inputValue, 33 | }); 34 | toast({ 35 | title: "Success", 36 | description: "Your chat has been updated.", 37 | }); 38 | setIsAlertOpen(false); 39 | } catch (error) { 40 | toast({ 41 | title: "Error", 42 | description: "Failed to update chat. Please try again.", 43 | variant: "destructive", 44 | }); 45 | } 46 | }); 47 | }; 48 | 49 | const onChange = (e: React.ChangeEvent) => { 50 | setInputValue(e.target.value); 51 | }; 52 | 53 | const onKeyDown = (e: React.KeyboardEvent) => { 54 | if (e.key === "Enter") { 55 | e.preventDefault(); 56 | handleDelete(); 57 | } 58 | }; 59 | 60 | const onEdit = (e: React.MouseEvent) => { 61 | e.stopPropagation(); 62 | }; 63 | 64 | const onDelete = (e: React.MouseEvent) => { 65 | e.preventDefault(); 66 | handleDelete(); 67 | }; 68 | 69 | return ( 70 | 71 | 72 | 75 | 76 | 77 | 78 | Edit your chat title 79 | 80 | 81 |
    82 | 89 |
    90 | 91 | Cancel 92 | 93 | {pendingUpdateChat ? ( 94 | 95 | ) : ( 96 | "Update" 97 | )} 98 | 99 | 100 |
    101 |
    102 | ); 103 | }; 104 | -------------------------------------------------------------------------------- /components/modules/apps/chat/Header.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | 3 | import { Heading2 } from "@/components/ui/typography"; 4 | 5 | export const Header = () => { 6 | return ( 7 |
    8 | GPT AI Assistant 9 |
    10 | ); 11 | }; 12 | -------------------------------------------------------------------------------- /components/modules/apps/chat/MobileDrawerControls.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { PanelRight } from "lucide-react"; 3 | 4 | import { Chat } from "@/lib/db"; 5 | import { Button } from "@/components/ui/Button"; 6 | import { SheetTrigger } from "@/components/ui/Sheet"; 7 | 8 | import { ChatHistoryDrawer } from "./ChatHistoryDrawer"; 9 | 10 | type MobileDrawerControlProps = { 11 | chats: Chat[] | null; 12 | }; 13 | 14 | export const MobileDrawerControl = React.memo(function MobileDrawerControl({ 15 | chats, 16 | }: MobileDrawerControlProps) { 17 | return ( 18 | <> 19 |
    20 | 21 |
    22 |
    23 | 24 | 27 | 28 |
    29 | 30 | ); 31 | }); 32 | -------------------------------------------------------------------------------- /components/modules/apps/chat/NewChatButton.tsx: -------------------------------------------------------------------------------- 1 | "use client"; 2 | 3 | import React from "react"; 4 | import Link from "next/link"; 5 | import { Plus } from "lucide-react"; 6 | 7 | import { cn } from "@/lib/utils"; 8 | import { buttonVariants } from "@/components/ui/Button"; 9 | 10 | type NewChatButtonProps = { 11 | closeDrawer?: () => void; 12 | }; 13 | 14 | export const NewChatButton = ({ closeDrawer }: NewChatButtonProps) => { 15 | return ( 16 | 26 | 27 | 28 | ); 29 | }; 30 | -------------------------------------------------------------------------------- /components/modules/apps/chat/SystemPromptControl.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Message, UseChatHelpers } from "ai/react"; 3 | import { useFormContext } from "react-hook-form"; 4 | 5 | import { Button } from "@/components/ui/Button"; 6 | import { Label } from "@/components/ui/Label"; 7 | import { 8 | Popover, 9 | PopoverContent, 10 | PopoverTrigger, 11 | } from "@/components/ui/Popover"; 12 | import { TextArea } from "@/components/ui/TextArea"; 13 | import { Subtle } from "@/components/ui/typography"; 14 | 15 | import { ChatParams } from "./types"; 16 | 17 | type SystemPromptControlProps = Pick< 18 | UseChatHelpers, 19 | "setMessages" | "messages" 20 | >; 21 | 22 | export const SystemPromptControl = ({ 23 | setMessages, 24 | messages, 25 | }: SystemPromptControlProps) => { 26 | const [isPopoverOpen, setIsPopoverOpen] = React.useState(false); 27 | const { getValues, setValue } = useFormContext(); 28 | const formValues = getValues(); 29 | const { description } = formValues; 30 | const [systemPromptInputValue, setSystemPromptInputValue] = React.useState< 31 | string | undefined 32 | >(description); 33 | 34 | const handlePopoverOpenChange = (isOpen: boolean) => { 35 | if (isOpen) { 36 | if (description !== systemPromptInputValue) { 37 | setSystemPromptInputValue(description); 38 | } 39 | } 40 | 41 | setIsPopoverOpen(isOpen); 42 | }; 43 | 44 | const handleSystemPromptInputChange = ( 45 | e: React.ChangeEvent 46 | ) => { 47 | setSystemPromptInputValue(e.target.value); 48 | }; 49 | 50 | const handleSave = () => { 51 | if (!systemPromptInputValue) { 52 | return; 53 | } 54 | 55 | const systemMessage: Message = { 56 | role: "system", 57 | content: systemPromptInputValue, 58 | id: "system-prompt", 59 | }; 60 | 61 | setMessages([systemMessage, ...messages]); 62 | setValue("description", systemPromptInputValue); 63 | 64 | setIsPopoverOpen(false); 65 | }; 66 | 67 | return ( 68 | 69 |
    70 |
    71 | 72 | 73 | 76 | 77 |
    78 | {description} 79 |
    80 | 81 |
    82 |
    83 |

    Set system prompt

    84 |

    85 | {`Set a custom system prompt to be prepended to the user's input. This is useful for giving the AI some context about the conversation.`} 86 |

    87 |
    88 |
    89 |