├── .eslintrc.json ├── .example.env ├── .gitignore ├── Dockerfile ├── LICENSE ├── README.md ├── components.json ├── next.config.mjs ├── ollama-nextjs-ui.gif ├── package-lock.json ├── package.json ├── postcss.config.js ├── public ├── next.svg ├── ollama.png ├── user.jpg └── vercel.svg ├── src ├── app │ ├── (chat) │ │ ├── c │ │ │ └── [id] │ │ │ │ └── page.tsx │ │ ├── layout.tsx │ │ └── page.tsx │ ├── api │ │ ├── chat │ │ │ └── route.ts │ │ ├── model │ │ │ └── route.ts │ │ └── tags │ │ │ └── route.ts │ ├── favicon.ico │ ├── globals.css │ └── hooks │ │ ├── useChatStore.ts │ │ └── useSpeechRecognition.ts ├── components │ ├── button-with-tooltip.tsx │ ├── chat │ │ ├── chat-bottombar.tsx │ │ ├── chat-layout.tsx │ │ ├── chat-list.tsx │ │ ├── chat-message.tsx │ │ ├── chat-topbar.tsx │ │ └── chat.tsx │ ├── code-display-block.tsx │ ├── edit-username-form.tsx │ ├── emoji-picker.tsx │ ├── image-embedder.tsx │ ├── mode-toggle.tsx │ ├── pull-model-form.tsx │ ├── pull-model.tsx │ ├── sidebar-skeleton.tsx │ ├── sidebar.tsx │ ├── ui │ │ ├── avatar.tsx │ │ ├── button.tsx │ │ ├── card.tsx │ │ ├── chat │ │ │ ├── chat-bubble.tsx │ │ │ ├── chat-input.tsx │ │ │ ├── chat-message-list.tsx │ │ │ ├── expandable-chat.tsx │ │ │ ├── hooks │ │ │ │ └── useAutoScroll.tsx │ │ │ └── message-loading.tsx │ │ ├── dialog.tsx │ │ ├── dropdown-menu.tsx │ │ ├── form.tsx │ │ ├── input.tsx │ │ ├── label.tsx │ │ ├── popover.tsx │ │ ├── resizable.tsx │ │ ├── select.tsx │ │ ├── sheet.tsx │ │ ├── skeleton.tsx │ │ ├── sonner.tsx │ │ ├── textarea.tsx │ │ └── tooltip.tsx │ ├── user-settings.tsx │ └── username-form.tsx ├── lib │ ├── model-helper.ts │ └── utils.ts ├── providers │ └── theme-provider.tsx └── utils │ └── initial-questions.ts ├── tailwind.config.ts └── tsconfig.json /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "next/core-web-vitals" 3 | } 4 | -------------------------------------------------------------------------------- /.example.env: -------------------------------------------------------------------------------- 1 | OLLAMA_URL="http://localhost:11434" 2 | -------------------------------------------------------------------------------- /.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 39 | .env -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Use Node.js as the base image 2 | FROM node:20-alpine AS builder 3 | 4 | WORKDIR /app 5 | 6 | COPY package.json package-lock.json ./ 7 | 8 | RUN npm ci 9 | 10 | # Set a build-time argument for OLLAMA_URL with a default value 11 | ARG OLLAMA_URL=http://127.0.0.1:11434 12 | ENV OLLAMA_URL=${OLLAMA_URL} 13 | 14 | COPY . . 15 | 16 | RUN npm run build 17 | 18 | FROM node:20-alpine 19 | 20 | WORKDIR /app 21 | 22 | # Copy built files from the builder stage 23 | COPY --from=builder /app/.next ./.next 24 | COPY --from=builder /app/public ./public 25 | COPY --from=builder /app/package.json ./package.json 26 | COPY --from=builder /app/package-lock.json ./package-lock.json 27 | COPY --from=builder /app/node_modules ./node_modules 28 | 29 | # Set environment variable with a default value that can be overridden at runtime 30 | ENV OLLAMA_URL=http://127.0.0.1:11434 31 | ENV PORT=3000 32 | 33 | EXPOSE 3000 34 | 35 | CMD ["npm", "start"] -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Jakob Hoeg Mørk 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 | 3 |
4 | 5 |

6 | Fully-featured web interface for Ollama LLMs 7 |

8 | 9 |
10 | 11 | ![GitHub Repo stars](https://img.shields.io/github/stars/jakobhoeg/nextjs-ollama-llm-ui) 12 | 13 |
14 | 15 | Get up and running with Large Language Models **quickly**, **locally** and even **offline**. 16 | This project aims to be the easiest way for you to get started with LLMs. No tedious and annoying setup required! 17 | 18 | > This is a hobby project. If you want a more complete experience, I suggest taking a look at [this](https://github.com/open-webui/open-webui) instead. 19 | 20 | # Features ✨ 21 | 22 | - **Beautiful & intuitive UI:** Inspired by ChatGPT, to enhance similarity in the user experience. 23 | - **Fully local:** Stores chats in localstorage for convenience. No need to run a database. 24 | - **Fully responsive:** Use your phone to chat, with the same ease as on desktop. 25 | - **Easy setup:** No tedious and annoying setup required. Just clone the repo and you're good to go! 26 | - **Code syntax highligting:** Messages that include code, will be highlighted for easy access. 27 | - **Copy codeblocks easily:** Easily copy the highlighted code with one click. 28 | - **Download/Pull & Delete models:** Easily download and delete models directly from the interface. 29 | - **Switch between models:** Switch between models fast with a click. 30 | - **Chat history:** Chats are saved and easily accessed. 31 | - **Light & Dark mode:** Switch between light & dark mode. 32 | 33 | # Preview 34 | 35 | https://github.com/jakobhoeg/nextjs-ollama-llm-ui/assets/114422072/08eaed4f-9deb-4e1b-b87a-ba17d81b9a02 36 | 37 | # Requisites ⚙️ 38 | 39 | To use the web interface, these requisites must be met: 40 | 41 | 1. Download [Ollama](https://ollama.com/download) and have it running. Or run it in a Docker container. Check the [docs](https://github.com/ollama/ollama) for instructions. 42 | 2. Node.js (18+) and npm is required. [Download](https://nodejs.org/en/download) 43 | 44 | # Quick start with Docker 45 | 46 | ## Installation with prebuilt Docker image 47 | 48 | - **If Ollama is running on your pc**: 49 | 50 | ``` 51 | docker run -d -p 8080:3000 --add-host=host.docker.internal:host-gateway -e OLLAMA_URL=http://host.docker.internal:11434 --name nextjs-ollama-ui --restart always jakobhoeg/nextjs-ollama-ui:latest 52 | ``` 53 | 54 | - **If Ollama is on a different server than the Web UI**: 55 | 56 | ``` 57 | docker run -d -p 8080:3000 --add-host=host.docker.internal:host-gateway -e OLLAMA_URL=http://example.com:11434 --name nextjs-ollama-ui --restart always jakobhoeg/nextjs-ollama-ui:latest 58 | ``` 59 | 60 | > You can also change the default 8080 port if you wish. 61 | 62 | # Installation locally 📖 63 | 64 | [![Packaging status](https://repology.org/badge/vertical-allrepos/nextjs-ollama-llm-ui.svg?columns=3)](https://repology.org/project/nextjs-ollama-llm-ui/versions) 65 | 66 | Use a pre-build package from one of the supported package managers to run a local environment of the web interface. 67 | Alternatively you can install from source with the instructions below. 68 | 69 | > [!NOTE] 70 | > If your frontend runs on something other than `http://localhost` or `http://127.0.0.1`, you'll need to set the OLLAMA_ORIGINS to your frontend url. 71 | > 72 | > This is also stated in the [documentation](https://github.com/ollama/ollama/blob/main/docs/faq.md#how-do-i-configure-ollama-server): 73 | > 74 | > `Ollama allows cross-origin requests from 127.0.0.1 and 0.0.0.0 by default. Additional origins can be configured with OLLAMA_ORIGINS` 75 | 76 | ## Install from source 77 | 78 | **1. Clone the repository to a directory on your pc via command prompt:** 79 | 80 | ``` 81 | git clone https://github.com/jakobhoeg/nextjs-ollama-llm-ui 82 | ``` 83 | 84 | **2. Open the folder:** 85 | 86 | ``` 87 | cd nextjs-ollama-llm-ui 88 | ``` 89 | 90 | **3. Rename the `.example.env` to `.env`:** 91 | 92 | ``` 93 | mv .example.env .env 94 | ``` 95 | 96 | **4. If your instance of Ollama is NOT running on the default ip-address and port, change the variable in the .env file to fit your usecase:** 97 | 98 | ``` 99 | OLLAMA_URL="http://localhost:11434" 100 | ``` 101 | 102 | **5. Install dependencies:** 103 | 104 | ``` 105 | npm install 106 | ``` 107 | 108 | **6. Start the development server:** 109 | 110 | ``` 111 | npm run dev 112 | ``` 113 | 114 | **5. Go to [localhost:3000](http://localhost:3000) and start chatting with your favourite model!** 115 | 116 | # Upcoming features 117 | 118 | This is a to-do list consisting of upcoming features. 119 | 120 | - ✅ Voice input support 121 | - ✅ Code syntax highlighting 122 | - ✅ Ability to send an image in the prompt to utilize vision language models. 123 | - ✅ Ability to regenerate responses 124 | - ⬜️ Import and export chats 125 | 126 | # Tech stack 127 | 128 | [NextJS](https://nextjs.org/) - React Framework for the Web 129 | 130 | [TailwindCSS](https://tailwindcss.com/) - Utility-first CSS framework 131 | 132 | [shadcn-ui](https://ui.shadcn.com/) - UI component built using Radix UI and Tailwind CSS 133 | 134 | [shadcn-chat](https://github.com/jakobhoeg/shadcn-chat) - Chat components for NextJS/React projects 135 | 136 | [Framer Motion](https://www.framer.com/motion/) - Motion/animation library for React 137 | 138 | [Lucide Icons](https://lucide.dev/) - Icon library 139 | 140 | # Helpful links 141 | 142 | [Medium Article](https://medium.com/@bartek.lewicz/launch-your-own-chatgpt-clone-for-free-on-colab-shareable-and-online-in-less-than-10-minutes-da19e44be5eb) - How to launch your own ChatGPT clone for free on Google Colab. By Bartek Lewicz. 143 | 144 | [Lobehub mention](https://lobehub.com/blog/5-ollama-web-ui-recommendation#5-next-js-ollama-llm-ui) - Five Excellent Free Ollama WebUI Client Recommendations 145 | -------------------------------------------------------------------------------- /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.ts", 8 | "css": "src/app/globals.css", 9 | "baseColor": "zinc", 10 | "cssVariables": true, 11 | "prefix": "" 12 | }, 13 | "aliases": { 14 | "components": "@/components", 15 | "utils": "@/lib/utils" 16 | } 17 | } -------------------------------------------------------------------------------- /next.config.mjs: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = { 3 | output: 'standalone', 4 | webpack: (config, { isServer }) => { 5 | // Fixes npm packages that depend on `fs` module 6 | if (!isServer) { 7 | config.resolve.fallback = { 8 | ...config.resolve.fallback, // if you miss it, all the other options in fallback, specified 9 | // by next.js will be dropped. Doesn't make much sense, but how it is 10 | fs: false, // the solution 11 | module: false, 12 | perf_hooks: false, 13 | }; 14 | } 15 | 16 | return config 17 | }, 18 | typescript: { 19 | // !! WARN !! 20 | // Dangerously allow production builds to successfully complete even if 21 | // your project has type errors. 22 | // !! WARN !! 23 | ignoreBuildErrors: true, 24 | }, 25 | }; 26 | 27 | 28 | 29 | export default nextConfig; 30 | -------------------------------------------------------------------------------- /ollama-nextjs-ui.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jakobhoeg/nextjs-ollama-llm-ui/79c53f4ab0db3ebe18e9389e56eb639e1371c75f/ollama-nextjs-ui.gif -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nextjs-ollama-local-ai", 3 | "version": "0.1.0", 4 | "private": true, 5 | "scripts": { 6 | "dev": "next dev", 7 | "build": "next build", 8 | "start": "next start", 9 | "lint": "next lint" 10 | }, 11 | "dependencies": { 12 | "@emoji-mart/data": "^1.2.1", 13 | "@emoji-mart/react": "^1.1.1", 14 | "@hookform/resolvers": "^3.9.0", 15 | "@langchain/community": "^0.3.1", 16 | "@langchain/core": "^0.3.3", 17 | "@radix-ui/react-avatar": "^1.1.0", 18 | "@radix-ui/react-dialog": "^1.1.1", 19 | "@radix-ui/react-dropdown-menu": "^2.1.1", 20 | "@radix-ui/react-icons": "^1.3.0", 21 | "@radix-ui/react-label": "^2.1.0", 22 | "@radix-ui/react-popover": "^1.1.1", 23 | "@radix-ui/react-scroll-area": "^1.1.0", 24 | "@radix-ui/react-select": "^2.1.1", 25 | "@radix-ui/react-slot": "^1.1.0", 26 | "@radix-ui/react-tooltip": "^1.1.2", 27 | "@tanstack/react-query": "^5.62.15", 28 | "@types/dom-speech-recognition": "^0.0.4", 29 | "ai": "^4.0.33", 30 | "class-variance-authority": "^0.7.0", 31 | "clsx": "^2.1.1", 32 | "emoji-mart": "^5.6.0", 33 | "framer-motion": "^11.5.6", 34 | "langchain": "^0.3.2", 35 | "lodash": "^4.17.21", 36 | "lucide-react": "^0.445.0", 37 | "next": "^14.2.13", 38 | "next-themes": "^0.3.0", 39 | "ollama-ai-provider": "^0.15.0", 40 | "react": "^18.3.1", 41 | "react-code-blocks": "^0.1.6", 42 | "react-dom": "^18.3.1", 43 | "react-dropzone": "^14.2.9", 44 | "react-hook-form": "^7.53.0", 45 | "react-markdown": "^9.0.1", 46 | "react-resizable-panels": "^2.1.3", 47 | "react-textarea-autosize": "^8.5.3", 48 | "remark-gfm": "^4.0.0", 49 | "sharp": "^0.33.5", 50 | "sonner": "^1.5.0", 51 | "tailwind-merge": "^2.5.2", 52 | "tailwindcss-animate": "^1.0.7", 53 | "uuid": "^10.0.0", 54 | "zod": "^3.23.8", 55 | "zustand": "^5.0.0-rc.2" 56 | }, 57 | "devDependencies": { 58 | "@types/lodash": "^4.17.14", 59 | "@types/node": "^22.5.5", 60 | "@types/react": "^18.3.8", 61 | "@types/react-dom": "^18.3.0", 62 | "@types/uuid": "^10.0.0", 63 | "autoprefixer": "^10.4.20", 64 | "eslint": "^8.0.0", 65 | "eslint-config-next": "14.2.13", 66 | "postcss": "^8.4.47", 67 | "tailwindcss": "^3.4.12", 68 | "typescript": "^5.6.2" 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | }; 7 | -------------------------------------------------------------------------------- /public/next.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /public/ollama.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jakobhoeg/nextjs-ollama-llm-ui/79c53f4ab0db3ebe18e9389e56eb639e1371c75f/public/ollama.png -------------------------------------------------------------------------------- /public/user.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jakobhoeg/nextjs-ollama-llm-ui/79c53f4ab0db3ebe18e9389e56eb639e1371c75f/public/user.jpg -------------------------------------------------------------------------------- /public/vercel.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/app/(chat)/c/[id]/page.tsx: -------------------------------------------------------------------------------- 1 | "use client"; 2 | 3 | import { ChatLayout } from "@/components/chat/chat-layout"; 4 | import React, { Suspense } from "react"; 5 | import { notFound } from "next/navigation"; 6 | import useChatStore from "@/app/hooks/useChatStore"; 7 | 8 | export default function Page({ params }: { params: { id: string } }) { 9 | const id = params.id; 10 | 11 | const getChatById = useChatStore((state) => state.getChatById); 12 | const chat = getChatById(id); 13 | 14 | if (!chat) { 15 | return notFound(); 16 | } 17 | 18 | return ( 19 |
20 | 27 |
28 | ); 29 | } 30 | -------------------------------------------------------------------------------- /src/app/(chat)/layout.tsx: -------------------------------------------------------------------------------- 1 | import type { Metadata } from "next"; 2 | import { Inter } from "next/font/google"; 3 | import "../globals.css"; 4 | import { ThemeProvider } from "@/providers/theme-provider"; 5 | import { Toaster } from "@/components/ui/sonner"; 6 | 7 | const inter = Inter({ subsets: ["latin"] }); 8 | 9 | export const metadata: Metadata = { 10 | title: "Ollama UI", 11 | description: "Ollama chatbot web interface", 12 | }; 13 | 14 | export const viewport = { 15 | width: "device-width", 16 | initialScale: 1, 17 | maximumScale: 1, 18 | userScalable: 1, 19 | }; 20 | 21 | export default function RootLayout({ 22 | children, 23 | }: Readonly<{ 24 | children: React.ReactNode; 25 | }>) { 26 | return ( 27 | 28 | 29 | 30 | {children} 31 | 32 | 33 | 34 | 35 | ); 36 | } 37 | -------------------------------------------------------------------------------- /src/app/(chat)/page.tsx: -------------------------------------------------------------------------------- 1 | "use client"; 2 | 3 | import { ChatLayout } from "@/components/chat/chat-layout"; 4 | import { 5 | Dialog, 6 | DialogDescription, 7 | DialogHeader, 8 | DialogTitle, 9 | DialogContent, 10 | } from "@/components/ui/dialog"; 11 | import UsernameForm from "@/components/username-form"; 12 | import { generateUUID } from "@/lib/utils"; 13 | import React from "react"; 14 | import useChatStore from "../hooks/useChatStore"; 15 | 16 | export default function Home() { 17 | const id = generateUUID(); 18 | const [open, setOpen] = React.useState(false); 19 | const userName = useChatStore((state) => state.userName); 20 | const setUserName = useChatStore((state) => state.setUserName); 21 | 22 | const onOpenChange = (isOpen: boolean) => { 23 | if (userName) return setOpen(isOpen); 24 | 25 | setUserName("Anonymous"); 26 | setOpen(isOpen); 27 | }; 28 | 29 | return ( 30 |
31 | 32 | 39 | 40 | 41 | Welcome to Ollama! 42 | 43 | Enter your name to get started. This is just to personalize your 44 | experience. 45 | 46 | 47 | 48 | 49 | 50 |
51 | ); 52 | } 53 | -------------------------------------------------------------------------------- /src/app/api/chat/route.ts: -------------------------------------------------------------------------------- 1 | import { createOllama } from 'ollama-ai-provider'; 2 | import { streamText, convertToCoreMessages, CoreMessage, UserContent } from 'ai'; 3 | 4 | export const runtime = "edge"; 5 | export const dynamic = "force-dynamic"; 6 | 7 | export async function POST(req: Request) { 8 | // Destructure request data 9 | const { messages, selectedModel, data } = await req.json(); 10 | 11 | const ollamaUrl = process.env.OLLAMA_URL; 12 | 13 | const initialMessages = messages.slice(0, -1); 14 | const currentMessage = messages[messages.length - 1]; 15 | 16 | const ollama = createOllama({baseURL: ollamaUrl + "/api"}); 17 | 18 | // Build message content array directly 19 | const messageContent: UserContent = [{ type: 'text', text: currentMessage.content }]; 20 | 21 | // Add images if they exist 22 | data?.images?.forEach((imageUrl: string) => { 23 | const image = new URL(imageUrl); 24 | messageContent.push({ type: 'image', image }); 25 | }); 26 | 27 | // Stream text using the ollama model 28 | const result = await streamText({ 29 | model: ollama(selectedModel), 30 | messages: [ 31 | ...convertToCoreMessages(initialMessages), 32 | { role: 'user', content: messageContent }, 33 | ], 34 | }); 35 | 36 | return result.toDataStreamResponse(); 37 | } 38 | -------------------------------------------------------------------------------- /src/app/api/model/route.ts: -------------------------------------------------------------------------------- 1 | // app/api/model/route.ts 2 | export async function POST(req: Request) { 3 | const { name } = await req.json(); 4 | 5 | const ollamaUrl = process.env.OLLAMA_URL; 6 | 7 | const response = await fetch(ollamaUrl + "/api/pull", { 8 | method: "POST", 9 | body: JSON.stringify({ name }), 10 | }); 11 | 12 | if (!response.ok) { 13 | throw new Error("Failed to pull model"); 14 | } 15 | 16 | const contentLength = response.headers.get("content-length"); 17 | const totalBytes = contentLength ? parseInt(contentLength, 10) : null; 18 | 19 | const stream = createProgressStream(response.body, totalBytes); 20 | 21 | const headers = new Headers(response.headers); 22 | headers.set("Content-Type", "application/json"); 23 | return new Response(stream, { headers }); 24 | } 25 | 26 | function createProgressStream( 27 | body: ReadableStream | null, 28 | totalBytes: number | null 29 | ): ReadableStream { 30 | return new ReadableStream({ 31 | async start(controller) { 32 | const reader = body?.getReader(); 33 | if (!reader) { 34 | controller.close(); 35 | return; 36 | } 37 | 38 | let receivedBytes = 0; 39 | 40 | while (true) { 41 | const { done, value } = await reader.read(); 42 | if (done) { 43 | const progressMessage = JSON.stringify({ progress: 100 }); 44 | controller.enqueue(new TextEncoder().encode(progressMessage + "\n")); 45 | controller.close(); 46 | return; 47 | } 48 | 49 | receivedBytes += value.length; 50 | const progress = totalBytes ? (receivedBytes / totalBytes) * 100 : null; 51 | 52 | const progressMessage = JSON.stringify({ progress }); 53 | controller.enqueue(new TextEncoder().encode(progressMessage + "\n")); 54 | 55 | controller.enqueue(value); 56 | } 57 | }, 58 | }); 59 | } -------------------------------------------------------------------------------- /src/app/api/tags/route.ts: -------------------------------------------------------------------------------- 1 | export const dynamic = "force-dynamic"; 2 | export const revalidate = 0; 3 | 4 | export async function GET(req: Request) { 5 | const OLLAMA_URL = process.env.OLLAMA_URL; 6 | console.log('OLLAMA_URL:', process.env.OLLAMA_URL); 7 | const res = await fetch( 8 | OLLAMA_URL + "/api/tags" 9 | ); 10 | return new Response(res.body, res); 11 | } 12 | -------------------------------------------------------------------------------- /src/app/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jakobhoeg/nextjs-ollama-llm-ui/79c53f4ab0db3ebe18e9389e56eb639e1371c75f/src/app/favicon.ico -------------------------------------------------------------------------------- /src/app/globals.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | 5 | @layer base { 6 | :root { 7 | --background: 0 0% 100%; 8 | --foreground: 240 10% 3.9%; 9 | --card: 0 0% 100%; 10 | --card-foreground: 240 10% 3.9%; 11 | --popover: 0 0% 100%; 12 | --popover-foreground: 240 10% 3.9%; 13 | --primary: 240 5.9% 10%; 14 | --primary-foreground: 0 0% 98%; 15 | --secondary: 240 4.8% 95.9%; 16 | --secondary-foreground: 240 5.9% 10%; 17 | --muted: 240 4.8% 95.9%; 18 | --muted-foreground: 240 3.8% 46.1%; 19 | --accent: 240 4.8% 95.9%; 20 | --accent-foreground: 240 5.9% 10%; 21 | --destructive: 0 84.2% 60.2%; 22 | --destructive-foreground: 0 0% 98%; 23 | --border: 240 5.9% 90%; 24 | --input: 240 5.9% 90%; 25 | --ring: 240 5.9% 10%; 26 | --radius: 1rem; 27 | } 28 | 29 | .dark { 30 | --background: 0 0% 9%; 31 | --foreground: 0 0% 98%; 32 | --card: 0 0% 12%; 33 | --card-foreground: 0 0% 98%; 34 | --popover: 0 0% 12%; 35 | --popover-foreground: 0 0% 98%; 36 | --primary: 0 0% 98%; 37 | --primary-foreground: 240 5.9% 10%; 38 | --secondary: 240 3.7% 15.9%; 39 | --secondary-foreground: 0 0% 98%; 40 | --muted: 240 3.7% 15.9%; 41 | --muted-foreground: 240 5% 64.9%; 42 | --accent: 240 3.7% 15.9%; 43 | --accent-foreground: 0 0% 98%; 44 | --destructive: 0 62.8% 30.6%; 45 | --destructive-foreground: 0 0% 98%; 46 | --border: 240 3.7% 15.9%; 47 | --input: 240 3.7% 15.9%; 48 | --ring: 240 4.9% 83.9%; 49 | } 50 | } 51 | 52 | @layer base { 53 | * { 54 | @apply border-border; 55 | } 56 | body { 57 | @apply bg-background text-foreground; 58 | } 59 | } 60 | 61 | #scroller * { 62 | overflow-anchor: none; 63 | } 64 | 65 | #anchor { 66 | overflow-anchor: auto; 67 | height: 1px; 68 | } 69 | 70 | :root { 71 | --scrollbar-thumb-color: #ccc; 72 | --scrollbar-thumb-hover-color: #aaa; 73 | } 74 | 75 | ::-webkit-scrollbar { 76 | width: 6px; 77 | height: 6px; 78 | } 79 | 80 | ::-webkit-scrollbar-thumb { 81 | background-color: var(--scrollbar-thumb-color); 82 | border-radius: 999px; 83 | transition: width 0.3s, height 0.3s, visibility 0.3s; 84 | } 85 | 86 | ::-webkit-scrollbar-thumb:hover { 87 | background-color: var(--scrollbar-thumb-hover-color); 88 | } 89 | 90 | ::-webkit-scrollbar-thumb:not(:hover) { 91 | width: 0; 92 | height: 0; 93 | visibility: hidden; 94 | } 95 | -------------------------------------------------------------------------------- /src/app/hooks/useChatStore.ts: -------------------------------------------------------------------------------- 1 | import { CoreMessage, generateId, Message } from "ai"; 2 | import { create } from "zustand"; 3 | import { createJSONStorage, persist } from "zustand/middleware"; 4 | 5 | interface ChatSession { 6 | messages: Message[]; 7 | createdAt: string; 8 | } 9 | 10 | interface State { 11 | base64Images: string[] | null; 12 | chats: Record; 13 | currentChatId: string | null; 14 | selectedModel: string | null; 15 | userName: string | "Anonymous"; 16 | isDownloading: boolean; 17 | downloadProgress: number; 18 | downloadingModel: string | null; 19 | } 20 | 21 | interface Actions { 22 | setBase64Images: (base64Images: string[] | null) => void; 23 | setCurrentChatId: (chatId: string) => void; 24 | setSelectedModel: (selectedModel: string) => void; 25 | getChatById: (chatId: string) => ChatSession | undefined; 26 | getMessagesById: (chatId: string) => Message[]; 27 | saveMessages: (chatId: string, messages: Message[]) => void; 28 | handleDelete: (chatId: string, messageId?: string) => void; 29 | setUserName: (userName: string) => void; 30 | startDownload: (modelName: string) => void; 31 | stopDownload: () => void; 32 | setDownloadProgress: (progress: number) => void; 33 | } 34 | 35 | const useChatStore = create()( 36 | persist( 37 | (set, get) => ({ 38 | base64Images: null, 39 | chats: {}, 40 | currentChatId: null, 41 | selectedModel: null, 42 | userName: "Anonymous", 43 | isDownloading: false, 44 | downloadProgress: 0, 45 | downloadingModel: null, 46 | 47 | setBase64Images: (base64Images) => set({ base64Images }), 48 | setUserName: (userName) => set({ userName }), 49 | 50 | setCurrentChatId: (chatId) => set({ currentChatId: chatId }), 51 | setSelectedModel: (selectedModel) => set({ selectedModel }), 52 | getChatById: (chatId) => { 53 | const state = get(); 54 | return state.chats[chatId]; 55 | }, 56 | getMessagesById: (chatId) => { 57 | const state = get(); 58 | return state.chats[chatId]?.messages || []; 59 | }, 60 | saveMessages: (chatId, messages) => { 61 | set((state) => { 62 | const existingChat = state.chats[chatId]; 63 | 64 | return { 65 | chats: { 66 | ...state.chats, 67 | [chatId]: { 68 | messages: [...messages], 69 | createdAt: existingChat?.createdAt || new Date().toISOString(), 70 | }, 71 | }, 72 | }; 73 | }); 74 | }, 75 | handleDelete: (chatId, messageId) => { 76 | set((state) => { 77 | const chat = state.chats[chatId]; 78 | if (!chat) return state; 79 | 80 | // If messageId is provided, delete specific message 81 | if (messageId) { 82 | const updatedMessages = chat.messages.filter( 83 | (message) => message.id !== messageId 84 | ); 85 | return { 86 | chats: { 87 | ...state.chats, 88 | [chatId]: { 89 | ...chat, 90 | messages: updatedMessages, 91 | }, 92 | }, 93 | }; 94 | } 95 | 96 | // If no messageId, delete the entire chat 97 | const { [chatId]: _, ...remainingChats } = state.chats; 98 | return { 99 | chats: remainingChats, 100 | }; 101 | }); 102 | }, 103 | 104 | startDownload: (modelName) => 105 | set({ isDownloading: true, downloadingModel: modelName, downloadProgress: 0 }), 106 | stopDownload: () => 107 | set({ isDownloading: false, downloadingModel: null, downloadProgress: 0 }), 108 | setDownloadProgress: (progress) => set({ downloadProgress: progress }), 109 | }), 110 | { 111 | name: "nextjs-ollama-ui-state", 112 | partialize: (state) => ({ 113 | chats: state.chats, 114 | currentChatId: state.currentChatId, 115 | selectedModel: state.selectedModel, 116 | userName: state.userName, 117 | }), 118 | } 119 | ) 120 | ); 121 | 122 | export default useChatStore; -------------------------------------------------------------------------------- /src/app/hooks/useSpeechRecognition.ts: -------------------------------------------------------------------------------- 1 | import { useState, useRef, useEffect } from "react"; 2 | 3 | interface SpeechRecognitionOptions { 4 | interimResults?: boolean; 5 | lang?: string; 6 | continuous?: boolean; 7 | } 8 | 9 | const useSpeechToText = (options: SpeechRecognitionOptions = {}) => { 10 | const [isListening, setIsListening] = useState(false); 11 | const [transcript, setTranscript] = useState(""); 12 | const recognitionRef = useRef(null); 13 | 14 | useEffect(() => { 15 | if (!("webkitSpeechRecognition" in window)) { 16 | console.error("Web Speech API is not supported"); 17 | return; 18 | } 19 | 20 | const recognition = new window.webkitSpeechRecognition(); 21 | recognitionRef.current = recognition; 22 | 23 | recognition.interimResults = options.interimResults || true; 24 | recognition.lang = options.lang || "en-US"; 25 | recognition.continuous = options.continuous || false; 26 | 27 | if ("webkitSpeechGrammarList" in window) { 28 | const grammar = 29 | "#JSGF V1.0; grammar punctuation; public = . | , | ! | ; | : ;"; 30 | const speechRecognitionList = new window.webkitSpeechGrammarList(); 31 | speechRecognitionList.addFromString(grammar, 1); 32 | recognition.grammars = speechRecognitionList; 33 | } 34 | 35 | recognition.onresult = (event: SpeechRecognitionEvent) => { 36 | let text = ""; 37 | 38 | for (let i = 0; i < event.results.length; i++) { 39 | text += event.results[i][0].transcript; 40 | } 41 | 42 | // Always capitalize the first letter 43 | setTranscript(text.charAt(0).toUpperCase() + text.slice(1)); 44 | }; 45 | 46 | recognition.onerror = (event) => { 47 | console.error(event.error); 48 | }; 49 | 50 | recognition.onend = () => { 51 | setIsListening(false); 52 | setTranscript(""); 53 | }; 54 | 55 | return () => { 56 | if (recognitionRef.current) { 57 | recognitionRef.current.stop(); 58 | } 59 | }; 60 | }, []); 61 | 62 | const startListening = () => { 63 | if (recognitionRef.current && !isListening) { 64 | recognitionRef.current.start(); 65 | setIsListening(true); 66 | } 67 | }; 68 | 69 | const stopListening = () => { 70 | if (recognitionRef.current && isListening) { 71 | recognitionRef.current.stop(); 72 | setIsListening(false); 73 | } 74 | }; 75 | 76 | return { 77 | isListening, 78 | transcript, 79 | startListening, 80 | stopListening, 81 | }; 82 | }; 83 | 84 | export default useSpeechToText; 85 | -------------------------------------------------------------------------------- /src/components/button-with-tooltip.tsx: -------------------------------------------------------------------------------- 1 | import React, { forwardRef } from "react"; 2 | import { Button } from "./ui/button"; 3 | import { 4 | Tooltip, 5 | TooltipContent, 6 | TooltipProvider, 7 | TooltipTrigger, 8 | } from "@/components/ui/tooltip"; 9 | 10 | interface ButtonWithTooltipProps { 11 | children: React.ReactElement; 12 | side: "top" | "bottom" | "left" | "right"; 13 | toolTipText: string; 14 | } 15 | 16 | const ButtonWithTooltip = forwardRef( 17 | ({ children, side, toolTipText }, ref) => { 18 | return ( 19 | 20 | 21 | 22 | {React.cloneElement(children, { ref })} 23 | 24 | 25 |
{toolTipText}
26 |
27 |
28 |
29 | ); 30 | } 31 | ); 32 | 33 | ButtonWithTooltip.displayName = "ButtonWithTooltip"; 34 | 35 | export default ButtonWithTooltip; 36 | -------------------------------------------------------------------------------- /src/components/chat/chat-bottombar.tsx: -------------------------------------------------------------------------------- 1 | "use client"; 2 | 3 | import React, { useEffect } from "react"; 4 | import { ChatProps } from "./chat"; 5 | import Link from "next/link"; 6 | import { cn } from "@/lib/utils"; 7 | import { Button, buttonVariants } from "../ui/button"; 8 | import TextareaAutosize from "react-textarea-autosize"; 9 | import { motion, AnimatePresence } from "framer-motion"; 10 | import { 11 | Cross2Icon, 12 | ImageIcon, 13 | PaperPlaneIcon, 14 | StopIcon, 15 | } from "@radix-ui/react-icons"; 16 | import { Mic, SendHorizonal } from "lucide-react"; 17 | import useSpeechToText from "@/app/hooks/useSpeechRecognition"; 18 | import MultiImagePicker from "../image-embedder"; 19 | import useChatStore from "@/app/hooks/useChatStore"; 20 | import Image from "next/image"; 21 | import { ChatRequestOptions, Message } from "ai"; 22 | import { ChatInput } from "../ui/chat/chat-input"; 23 | 24 | interface ChatBottombarProps { 25 | handleInputChange: (e: React.ChangeEvent) => void; 26 | handleSubmit: ( 27 | e: React.FormEvent, 28 | chatRequestOptions?: ChatRequestOptions 29 | ) => void; 30 | isLoading: boolean; 31 | stop: () => void; 32 | setInput?: React.Dispatch>; 33 | input: string; 34 | } 35 | 36 | export default function ChatBottombar({ 37 | input, 38 | handleInputChange, 39 | handleSubmit, 40 | isLoading, 41 | stop, 42 | setInput, 43 | }: ChatBottombarProps) { 44 | const inputRef = React.useRef(null); 45 | const base64Images = useChatStore((state) => state.base64Images); 46 | const setBase64Images = useChatStore((state) => state.setBase64Images); 47 | const selectedModel = useChatStore((state) => state.selectedModel); 48 | 49 | const handleKeyPress = (e: React.KeyboardEvent) => { 50 | if (e.key === "Enter" && !e.shiftKey && !e.nativeEvent.isComposing) { 51 | e.preventDefault(); 52 | handleSubmit(e as unknown as React.FormEvent); 53 | } 54 | }; 55 | 56 | const { isListening, transcript, startListening, stopListening } = 57 | useSpeechToText({ continuous: true }); 58 | 59 | const listen = () => { 60 | isListening ? stopVoiceInput() : startListening(); 61 | }; 62 | 63 | const stopVoiceInput = () => { 64 | setInput && setInput(transcript.length ? transcript : ""); 65 | stopListening(); 66 | }; 67 | 68 | const handleListenClick = () => { 69 | listen(); 70 | }; 71 | 72 | useEffect(() => { 73 | if (inputRef.current) { 74 | inputRef.current.focus(); 75 | console.log("Input focused"); 76 | } 77 | }, [inputRef]); 78 | 79 | return ( 80 |
81 | 82 |
86 | 95 | 96 |
97 | {isLoading ? ( 98 | // Loading state 99 |
100 | 101 |
102 | 111 | 123 |
124 |
125 | ) : ( 126 | // Default state 127 |
128 | 132 |
133 | {/* Microphone button with animation when listening */} 134 | 151 | 152 | {/* Send button */} 153 | 167 |
168 |
169 | )} 170 |
171 | {base64Images && ( 172 |
173 | {base64Images.map((image, index) => { 174 | return ( 175 |
179 |
180 | {""} 187 |
188 | 199 |
200 | ); 201 | })} 202 |
203 | )} 204 | 205 |
206 |
207 | ); 208 | } 209 | -------------------------------------------------------------------------------- /src/components/chat/chat-layout.tsx: -------------------------------------------------------------------------------- 1 | "use client"; 2 | 3 | import React, { useEffect, useState } from "react"; 4 | import { 5 | ResizableHandle, 6 | ResizablePanel, 7 | ResizablePanelGroup, 8 | } from "@/components/ui/resizable"; 9 | import { cn } from "@/lib/utils"; 10 | import { Sidebar } from "../sidebar"; 11 | import { Message, useChat } from "ai/react"; 12 | import Chat, { ChatProps } from "./chat"; 13 | import ChatList from "./chat-list"; 14 | import { HamburgerMenuIcon } from "@radix-ui/react-icons"; 15 | 16 | interface ChatLayoutProps { 17 | defaultLayout: number[] | undefined; 18 | defaultCollapsed?: boolean; 19 | navCollapsedSize: number; 20 | } 21 | 22 | type MergedProps = ChatLayoutProps & ChatProps; 23 | 24 | export function ChatLayout({ 25 | defaultLayout = [30, 160], 26 | defaultCollapsed = false, 27 | navCollapsedSize, 28 | initialMessages, 29 | id, 30 | }: MergedProps) { 31 | const [isCollapsed, setIsCollapsed] = React.useState(defaultCollapsed); 32 | const [isMobile, setIsMobile] = useState(false); 33 | 34 | useEffect(() => { 35 | const checkScreenWidth = () => { 36 | setIsMobile(window.innerWidth <= 1023); 37 | }; 38 | 39 | // Initial check 40 | checkScreenWidth(); 41 | 42 | // Event listener for screen width changes 43 | window.addEventListener("resize", checkScreenWidth); 44 | 45 | // Cleanup the event listener on component unmount 46 | return () => { 47 | window.removeEventListener("resize", checkScreenWidth); 48 | }; 49 | }, []); 50 | 51 | return ( 52 | { 55 | document.cookie = `react-resizable-panels:layout=${JSON.stringify( 56 | sizes 57 | )}`; 58 | }} 59 | className="h-screen items-stretch" 60 | > 61 | { 68 | setIsCollapsed(true); 69 | document.cookie = `react-resizable-panels:collapsed=${JSON.stringify( 70 | true 71 | )}`; 72 | }} 73 | onExpand={() => { 74 | setIsCollapsed(false); 75 | document.cookie = `react-resizable-panels:collapsed=${JSON.stringify( 76 | false 77 | )}`; 78 | }} 79 | className={cn( 80 | isCollapsed 81 | ? "min-w-[50px] md:min-w-[70px] transition-all duration-300 ease-in-out" 82 | : "hidden md:block" 83 | )} 84 | > 85 | 91 | 92 | 93 | 97 | 98 | 99 | 100 | ); 101 | } 102 | -------------------------------------------------------------------------------- /src/components/chat/chat-list.tsx: -------------------------------------------------------------------------------- 1 | import { Message } from "ai/react"; 2 | import React from "react"; 3 | import ChatMessage from "./chat-message"; 4 | import { ChatMessageList } from "../ui/chat/chat-message-list"; 5 | import { 6 | ChatBubble, 7 | ChatBubbleAvatar, 8 | ChatBubbleMessage, 9 | } from "../ui/chat/chat-bubble"; 10 | import { ChatRequestOptions } from "ai"; 11 | 12 | interface ChatListProps { 13 | messages: Message[]; 14 | isLoading: boolean; 15 | loadingSubmit?: boolean; 16 | reload: ( 17 | chatRequestOptions?: ChatRequestOptions 18 | ) => Promise; 19 | } 20 | 21 | export default function ChatList({ 22 | messages, 23 | isLoading, 24 | loadingSubmit, 25 | reload, 26 | }: ChatListProps) { 27 | return ( 28 |
29 | 30 | {messages.map((message, index) => ( 31 | 38 | ))} 39 | {loadingSubmit && ( 40 | 41 | 47 | 48 | 49 | )} 50 | 51 |
52 | ); 53 | } 54 | -------------------------------------------------------------------------------- /src/components/chat/chat-message.tsx: -------------------------------------------------------------------------------- 1 | import React, { memo, useMemo, useState } from "react"; 2 | import { motion } from "framer-motion"; 3 | import Markdown from "react-markdown"; 4 | import remarkGfm from "remark-gfm"; 5 | import { Message } from "ai/react"; 6 | import { ChatRequestOptions } from "ai"; 7 | import { CheckIcon, CopyIcon } from "@radix-ui/react-icons"; 8 | import { RefreshCcw } from "lucide-react"; 9 | import Image from "next/image"; 10 | import { 11 | ChatBubble, 12 | ChatBubbleAvatar, 13 | ChatBubbleMessage, 14 | } from "../ui/chat/chat-bubble"; 15 | import ButtonWithTooltip from "../button-with-tooltip"; 16 | import { Button } from "../ui/button"; 17 | import CodeDisplayBlock from "../code-display-block"; 18 | 19 | export type ChatMessageProps = { 20 | message: Message; 21 | isLast: boolean; 22 | isLoading: boolean | undefined; 23 | reload: (chatRequestOptions?: ChatRequestOptions) => Promise; 24 | }; 25 | 26 | const MOTION_CONFIG = { 27 | initial: { opacity: 0, scale: 1, y: 20, x: 0 }, 28 | animate: { opacity: 1, scale: 1, y: 0, x: 0 }, 29 | exit: { opacity: 0, scale: 1, y: 20, x: 0 }, 30 | transition: { 31 | opacity: { duration: 0.1 }, 32 | layout: { 33 | type: "spring", 34 | bounce: 0.3, 35 | duration: 0.2, 36 | }, 37 | }, 38 | }; 39 | 40 | function ChatMessage({ message, isLast, isLoading, reload }: ChatMessageProps) { 41 | const [isCopied, setIsCopied] = useState(false); 42 | 43 | // Extract "think" content from Deepseek R1 models and clean message (rest) content 44 | const { thinkContent, cleanContent } = useMemo(() => { 45 | const getThinkContent = (content: string) => { 46 | const match = content.match(/([\s\S]*?)(?:<\/think>|$)/); 47 | return match ? match[1].trim() : null; 48 | }; 49 | 50 | return { 51 | thinkContent: message.role === "assistant" ? getThinkContent(message.content) : null, 52 | cleanContent: message.content.replace(/[\s\S]*?(?:<\/think>|$)/g, '').trim(), 53 | }; 54 | }, [message.content, message.role]); 55 | 56 | const contentParts = useMemo(() => cleanContent.split("```"), [cleanContent]); 57 | 58 | const handleCopy = () => { 59 | navigator.clipboard.writeText(message.content); 60 | setIsCopied(true); 61 | setTimeout(() => setIsCopied(false), 1500); 62 | }; 63 | 64 | const renderAttachments = () => ( 65 |
66 | {message.experimental_attachments 67 | ?.filter((attachment) => attachment.contentType?.startsWith("image/")) 68 | .map((attachment, index) => ( 69 | attached image 77 | ))} 78 |
79 | ); 80 | 81 | const renderThinkingProcess = () => ( 82 | thinkContent && message.role === "assistant" && ( 83 |
84 | 85 | Thinking process 86 | 87 |
88 | {thinkContent} 89 |
90 |
91 | ) 92 | ); 93 | 94 | const renderContent = () => ( 95 | contentParts.map((part, index) => ( 96 | index % 2 === 0 ? ( 97 | {part} 98 | ) : ( 99 |
100 |           
101 |         
102 | ) 103 | )) 104 | ); 105 | 106 | const renderActionButtons = () => ( 107 | message.role === "assistant" && ( 108 |
109 | {!isLoading && ( 110 | 111 | 123 | 124 | )} 125 | {!isLoading && isLast && ( 126 | 127 | 135 | 136 | )} 137 |
138 | ) 139 | ); 140 | 141 | return ( 142 | 143 | 144 | 151 | 152 | {renderThinkingProcess()} 153 | {renderAttachments()} 154 | {renderContent()} 155 | {renderActionButtons()} 156 | 157 | 158 | 159 | ); 160 | } 161 | 162 | export default memo(ChatMessage, (prevProps, nextProps) => { 163 | if (nextProps.isLast) return false; 164 | return prevProps.isLast === nextProps.isLast && prevProps.message === nextProps.message; 165 | }); -------------------------------------------------------------------------------- /src/components/chat/chat-topbar.tsx: -------------------------------------------------------------------------------- 1 | "use client"; 2 | 3 | import React, { useEffect } from "react"; 4 | import { 5 | Popover, 6 | PopoverContent, 7 | PopoverTrigger, 8 | } from "@/components/ui/popover"; 9 | import { 10 | Sheet, 11 | SheetContent, 12 | SheetDescription, 13 | SheetHeader, 14 | SheetTitle, 15 | SheetTrigger, 16 | } from "@/components/ui/sheet"; 17 | 18 | import { Button } from "../ui/button"; 19 | import { CaretSortIcon, HamburgerMenuIcon } from "@radix-ui/react-icons"; 20 | import { Sidebar } from "../sidebar"; 21 | import { Message } from "ai/react"; 22 | import { getSelectedModel } from "@/lib/model-helper"; 23 | import useChatStore from "@/app/hooks/useChatStore"; 24 | 25 | interface ChatTopbarProps { 26 | isLoading: boolean; 27 | chatId?: string; 28 | messages: Message[]; 29 | setMessages: (messages: Message[]) => void; 30 | } 31 | 32 | export default function ChatTopbar({ 33 | isLoading, 34 | chatId, 35 | messages, 36 | setMessages, 37 | }: ChatTopbarProps) { 38 | const [models, setModels] = React.useState([]); 39 | const [open, setOpen] = React.useState(false); 40 | const [sheetOpen, setSheetOpen] = React.useState(false); 41 | const selectedModel = useChatStore((state) => state.selectedModel); 42 | const setSelectedModel = useChatStore((state) => state.setSelectedModel); 43 | 44 | useEffect(() => { 45 | (async () => { 46 | try { 47 | const res = await fetch("/api/tags"); 48 | if (!res.ok) throw new Error(`HTTP Error: ${res.status}`); 49 | 50 | const data = await res.json().catch(() => null); 51 | if (!data?.models?.length) return; 52 | 53 | setModels(data.models.map(({ name }: { name: string }) => name)); 54 | } catch (error) { 55 | console.error("Error fetching models:", error); 56 | } 57 | })(); 58 | }, []); 59 | 60 | 61 | const handleModelChange = (model: string) => { 62 | setSelectedModel(model); 63 | setOpen(false); 64 | }; 65 | 66 | const handleCloseSidebar = () => { 67 | setSheetOpen(false); 68 | }; 69 | 70 | return ( 71 |
72 | 73 | 74 | 75 | 76 | 77 | 84 | 85 | 86 | 87 | 88 | 89 | 99 | 100 | 101 | {models.length > 0 ? ( 102 | models.map((model) => ( 103 | 113 | )) 114 | ) : ( 115 | 118 | )} 119 | 120 | 121 |
122 | ); 123 | } 124 | -------------------------------------------------------------------------------- /src/components/chat/chat.tsx: -------------------------------------------------------------------------------- 1 | "use client"; 2 | 3 | import ChatTopbar from "./chat-topbar"; 4 | import ChatList from "./chat-list"; 5 | import ChatBottombar from "./chat-bottombar"; 6 | import { AIMessage, HumanMessage } from "@langchain/core/messages"; 7 | import { BytesOutputParser } from "@langchain/core/output_parsers"; 8 | import { Attachment, ChatRequestOptions, generateId } from "ai"; 9 | import { Message, useChat } from "ai/react"; 10 | import React, { useEffect, useRef, useState } from "react"; 11 | import { toast } from "sonner"; 12 | import { v4 as uuidv4 } from "uuid"; 13 | import useChatStore from "@/app/hooks/useChatStore"; 14 | import { useRouter } from "next/navigation"; 15 | import Image from "next/image"; 16 | 17 | export interface ChatProps { 18 | id: string; 19 | initialMessages: Message[] | []; 20 | isMobile?: boolean; 21 | } 22 | 23 | export default function Chat({ initialMessages, id, isMobile }: ChatProps) { 24 | const { 25 | messages, 26 | input, 27 | handleInputChange, 28 | handleSubmit, 29 | isLoading, 30 | stop, 31 | setMessages, 32 | setInput, 33 | reload, 34 | } = useChat({ 35 | id, 36 | initialMessages, 37 | onResponse: (response) => { 38 | if (response) { 39 | setLoadingSubmit(false); 40 | } 41 | }, 42 | onFinish: (message) => { 43 | const savedMessages = getMessagesById(id); 44 | saveMessages(id, [...savedMessages, message]); 45 | setLoadingSubmit(false); 46 | router.replace(`/c/${id}`); 47 | }, 48 | onError: (error) => { 49 | setLoadingSubmit(false); 50 | router.replace("/"); 51 | console.error(error.message); 52 | console.error(error.cause); 53 | }, 54 | }); 55 | const [loadingSubmit, setLoadingSubmit] = React.useState(false); 56 | const formRef = useRef(null); 57 | const base64Images = useChatStore((state) => state.base64Images); 58 | const setBase64Images = useChatStore((state) => state.setBase64Images); 59 | const selectedModel = useChatStore((state) => state.selectedModel); 60 | const saveMessages = useChatStore((state) => state.saveMessages); 61 | const getMessagesById = useChatStore((state) => state.getMessagesById); 62 | const router = useRouter(); 63 | 64 | const onSubmit = (e: React.FormEvent) => { 65 | e.preventDefault(); 66 | window.history.replaceState({}, "", `/c/${id}`); 67 | 68 | if (!selectedModel) { 69 | toast.error("Please select a model"); 70 | return; 71 | } 72 | 73 | const userMessage: Message = { 74 | id: generateId(), 75 | role: "user", 76 | content: input, 77 | }; 78 | 79 | setLoadingSubmit(true); 80 | 81 | const attachments: Attachment[] = base64Images 82 | ? base64Images.map((image) => ({ 83 | contentType: "image/base64", 84 | url: image, 85 | })) 86 | : []; 87 | 88 | const requestOptions: ChatRequestOptions = { 89 | body: { 90 | selectedModel: selectedModel, 91 | }, 92 | ...(base64Images && { 93 | data: { 94 | images: base64Images, 95 | }, 96 | experimental_attachments: attachments, 97 | }), 98 | }; 99 | 100 | handleSubmit(e, requestOptions); 101 | saveMessages(id, [...messages, userMessage]); 102 | setBase64Images(null); 103 | }; 104 | 105 | const removeLatestMessage = () => { 106 | const updatedMessages = messages.slice(0, -1); 107 | setMessages(updatedMessages); 108 | saveMessages(id, updatedMessages); 109 | return updatedMessages; 110 | }; 111 | 112 | const handleStop = () => { 113 | stop(); 114 | saveMessages(id, [...messages]); 115 | setLoadingSubmit(false); 116 | }; 117 | 118 | return ( 119 |
120 | 126 | 127 | {messages.length === 0 ? ( 128 |
129 | AI 136 |

137 | How can I help you today? 138 |

139 | 147 |
148 | ) : ( 149 | <> 150 | { 155 | removeLatestMessage(); 156 | 157 | const requestOptions: ChatRequestOptions = { 158 | body: { 159 | selectedModel: selectedModel, 160 | }, 161 | }; 162 | 163 | setLoadingSubmit(true); 164 | return reload(requestOptions); 165 | }} 166 | /> 167 | 175 | 176 | )} 177 |
178 | ); 179 | } 180 | -------------------------------------------------------------------------------- /src/components/code-display-block.tsx: -------------------------------------------------------------------------------- 1 | "use client"; 2 | import { CheckIcon, CopyIcon } from "@radix-ui/react-icons"; 3 | import React, { useMemo, useRef, useState } from "react"; 4 | import { CodeBlock, dracula, github } from "react-code-blocks"; 5 | import { Button } from "./ui/button"; 6 | import { toast } from "sonner"; 7 | import { useTheme } from "next-themes"; 8 | 9 | interface ButtonCodeblockProps { 10 | code: string; 11 | } 12 | 13 | export default function CodeDisplayBlock({ code }: ButtonCodeblockProps) { 14 | const [isCopied, setIsCopied] = useState(false); 15 | const isCopiedRef = useRef(false); 16 | const { resolvedTheme } = useTheme(); 17 | 18 | const filteredCode = useMemo( 19 | () => code.split("\n").slice(1).join("\n") || code, 20 | [code] 21 | ); 22 | const trimmedCode = useMemo(() => filteredCode.trim(), [filteredCode]); 23 | const language = useMemo( 24 | () => 25 | ["tsx", "js", "python", "css", "html", "cs"].includes(code.split("\n")[0]) 26 | ? code.split("\n")[0] 27 | : "tsx", 28 | [code] 29 | ); 30 | 31 | const customStyle = useMemo( 32 | () => 33 | resolvedTheme === "dark" 34 | ? { background: "#303033" } 35 | : { background: "#fcfcfc" }, 36 | [resolvedTheme] 37 | ); 38 | const codeTheme = useMemo( 39 | () => (resolvedTheme === "dark" ? dracula : github), 40 | [resolvedTheme] 41 | ); 42 | 43 | const copyToClipboard = () => { 44 | if (isCopiedRef.current) return; // Prevent multiple triggers 45 | navigator.clipboard.writeText(trimmedCode); 46 | isCopiedRef.current = true; 47 | setIsCopied(true); 48 | toast.success("Code copied to clipboard!"); 49 | 50 | setTimeout(() => { 51 | isCopiedRef.current = false; 52 | setIsCopied(false); 53 | }, 1500); 54 | }; 55 | 56 | return ( 57 |
58 | 70 | 77 |
78 | ); 79 | } 80 | -------------------------------------------------------------------------------- /src/components/edit-username-form.tsx: -------------------------------------------------------------------------------- 1 | "use client"; 2 | 3 | import { z } from "zod"; 4 | import { zodResolver } from "@hookform/resolvers/zod"; 5 | import { useForm } from "react-hook-form"; 6 | import { Button } from "@/components/ui/button"; 7 | import { 8 | Form, 9 | FormControl, 10 | FormField, 11 | FormItem, 12 | FormLabel, 13 | FormMessage, 14 | } from "@/components/ui/form"; 15 | import { Input } from "@/components/ui/input"; 16 | import React from "react"; 17 | import { ModeToggle } from "./mode-toggle"; 18 | import { toast } from "sonner"; 19 | import useChatStore from "@/app/hooks/useChatStore"; 20 | 21 | const formSchema = z.object({ 22 | username: z.string().min(2, { 23 | message: "Name must be at least 2 characters.", 24 | }), 25 | }); 26 | 27 | interface EditUsernameFormProps { 28 | setOpen: React.Dispatch>; 29 | } 30 | 31 | export default function EditUsernameForm({ setOpen }: EditUsernameFormProps) { 32 | const userName = useChatStore((state) => state.userName); 33 | const setUserName = useChatStore((state) => state.setUserName); 34 | 35 | const form = useForm>({ 36 | resolver: zodResolver(formSchema), 37 | defaultValues: { 38 | username: userName, 39 | }, 40 | }); 41 | 42 | function onSubmit(values: z.infer) { 43 | setUserName(values.username); // Update the userName in the store 44 | toast.success("Name updated successfully"); 45 | } 46 | 47 | return ( 48 |
49 |
50 | Theme 51 | 52 |
53 | 54 | ( 58 | 59 | Name 60 | 61 |
62 | 63 | 64 |
65 |
66 | 67 |
68 | )} 69 | /> 70 | 71 | 72 | ); 73 | } 74 | -------------------------------------------------------------------------------- /src/components/emoji-picker.tsx: -------------------------------------------------------------------------------- 1 | 'use client' 2 | 3 | import { 4 | Popover, 5 | PopoverContent, 6 | PopoverTrigger, 7 | } from "@/components/ui/popover" 8 | import { SmileIcon } from "lucide-react"; 9 | import Picker from '@emoji-mart/react'; 10 | import data from "@emoji-mart/data" 11 | 12 | interface EmojiPickerProps { 13 | onChange: (value: string) => void; 14 | } 15 | 16 | 17 | export const EmojiPicker = ({ 18 | onChange 19 | }: EmojiPickerProps) => { 20 | 21 | return ( 22 | 23 | 24 | 25 | 26 | 28 | onChange(emoji.native)} 34 | /> 35 | 36 | 37 | ) 38 | } 39 | -------------------------------------------------------------------------------- /src/components/image-embedder.tsx: -------------------------------------------------------------------------------- 1 | "use client"; 2 | 3 | import React, { useCallback } from "react"; 4 | import { useDropzone } from "react-dropzone"; 5 | import { Button } from "./ui/button"; 6 | import { ImageIcon } from "lucide-react"; 7 | 8 | interface MultiImagePickerProps { 9 | onImagesPick: (base64Images: string[]) => void; 10 | disabled: boolean 11 | } 12 | 13 | const MultiImagePicker: React.FC = ({ onImagesPick, disabled }) => { 14 | const convertToBase64 = (file: File): Promise => { 15 | return new Promise((resolve, reject) => { 16 | const reader = new FileReader(); 17 | reader.readAsDataURL(file); 18 | reader.onload = () => resolve(reader.result as string); 19 | reader.onerror = error => reject(error); 20 | }); 21 | }; 22 | 23 | const onDrop = useCallback( 24 | async (acceptedFiles: File[]) => { 25 | try { 26 | const base64Images = await Promise.all(acceptedFiles.map(convertToBase64)); 27 | onImagesPick(base64Images); 28 | } catch (error) { 29 | console.error("Error converting images to base64:", error); 30 | } 31 | }, 32 | [onImagesPick] 33 | ); 34 | 35 | const { getRootProps, getInputProps, isDragActive } = useDropzone({ 36 | onDrop, 37 | accept: { 38 | 'image/*': ['.jpeg', '.jpg', '.png', '.gif', '.webp'] 39 | }, 40 | multiple: true, // Allow multiple file selection 41 | maxSize: 10485760, // 10 MB per file 42 | }); 43 | 44 | return ( 45 |
46 | 47 | 51 |
52 | ); 53 | }; 54 | 55 | export default MultiImagePicker; -------------------------------------------------------------------------------- /src/components/mode-toggle.tsx: -------------------------------------------------------------------------------- 1 | "use client"; 2 | 3 | import { Monitor, Moon, Sun } from "lucide-react"; 4 | import { useTheme } from "next-themes"; 5 | import { Button } from "./ui/button"; 6 | import clsx from "clsx"; 7 | 8 | export function ModeToggle() { 9 | const { setTheme, theme } = useTheme(); 10 | 11 | return ( 12 |
16 | 25 | 33 | 42 |
43 | ); 44 | } 45 | -------------------------------------------------------------------------------- /src/components/pull-model-form.tsx: -------------------------------------------------------------------------------- 1 | "use client"; 2 | 3 | import React from "react"; 4 | import { 5 | Form, 6 | FormField, 7 | FormItem, 8 | FormLabel, 9 | FormMessage, 10 | FormControl, // Add FormControl 11 | } from "@/components/ui/form"; 12 | import { Button } from "./ui/button"; 13 | import { z } from "zod"; 14 | import { useForm } from "react-hook-form"; 15 | import { zodResolver } from "@hookform/resolvers/zod"; 16 | import { toast } from "sonner"; 17 | import { Loader2Icon } from "lucide-react"; 18 | import { Input } from "./ui/input"; 19 | import { throttle } from "lodash"; 20 | import useChatStore from "@/app/hooks/useChatStore"; 21 | import { useRouter } from "next/navigation"; 22 | 23 | const formSchema = z.object({ 24 | name: z.string().min(1, { 25 | message: "Please select a model to pull", 26 | }), 27 | }); 28 | 29 | export default function PullModelForm() { 30 | const { 31 | isDownloading, 32 | downloadProgress, 33 | downloadingModel, 34 | startDownload, 35 | stopDownload, 36 | setDownloadProgress, 37 | } = useChatStore(); 38 | 39 | const router = useRouter(); 40 | 41 | const form = useForm>({ 42 | resolver: zodResolver(formSchema), 43 | defaultValues: { 44 | name: "", 45 | }, 46 | }); 47 | 48 | const handlePullModel = async (data: z.infer) => { 49 | const modelName = data.name.trim(); 50 | startDownload(modelName); 51 | 52 | const throttledSetProgress = throttle((progress: number) => { 53 | setDownloadProgress(progress); 54 | }, 200); 55 | 56 | let lastStatus: string | null = null; 57 | 58 | try { 59 | const response = await fetch("/api/model", { 60 | method: "POST", 61 | headers: { 62 | "Content-Type": "application/json", 63 | }, 64 | body: JSON.stringify({ name: modelName }), 65 | }); 66 | 67 | if (!response.ok) { 68 | throw new Error("Network response was not ok"); 69 | } 70 | 71 | if (!response.body) { 72 | throw new Error("Something went wrong"); 73 | } 74 | 75 | await processStream(response.body, throttledSetProgress, lastStatus); 76 | 77 | toast.success("Model pulled successfully"); 78 | router.refresh(); 79 | } catch (error) { 80 | toast.error( 81 | `Error: ${ 82 | error instanceof Error ? error.message : "Failed to pull model" 83 | }` 84 | ); 85 | } finally { 86 | stopDownload(); 87 | throttledSetProgress.cancel(); 88 | } 89 | }; 90 | 91 | const processStream = async ( 92 | body: ReadableStream, 93 | throttledSetProgress: (progress: number) => void, 94 | lastStatus: string | null 95 | ) => { 96 | const reader = body.getReader(); 97 | const decoder = new TextDecoder(); 98 | 99 | while (true) { 100 | const { done, value } = await reader.read(); 101 | if (done) break; 102 | 103 | const text = decoder.decode(value); 104 | const jsonObjects = text.trim().split("\n"); 105 | 106 | for (const jsonObject of jsonObjects) { 107 | try { 108 | const responseJson = JSON.parse(jsonObject); 109 | 110 | if (responseJson.error) { 111 | throw new Error(responseJson.error); 112 | } 113 | 114 | if ( 115 | responseJson.completed !== undefined && 116 | responseJson.total !== undefined 117 | ) { 118 | const progress = 119 | (responseJson.completed / responseJson.total) * 100; 120 | throttledSetProgress(progress); 121 | } 122 | 123 | if (responseJson.status && responseJson.status !== lastStatus) { 124 | toast.info(`Status: ${responseJson.status}`); 125 | lastStatus = responseJson.status; 126 | } 127 | } catch (error) { 128 | throw new Error("Error parsing JSON"); 129 | } 130 | } 131 | } 132 | }; 133 | 134 | const onSubmit = (data: z.infer) => { 135 | handlePullModel(data); 136 | }; 137 | 138 | return ( 139 |
140 | 141 | ( 145 | 146 | Model name 147 | 148 | 154 | 155 |

156 | Check the{" "} 157 | 162 | library 163 | {" "} 164 | for a list of available models. 165 |

166 | 167 |
168 | 185 |

186 | {isDownloading 187 | ? "This may take a while. You can safely close this modal and continue using the app." 188 | : "Pressing the button will download the specified model to your device."} 189 |

190 |
191 |
192 | )} 193 | /> 194 | 195 | 196 | ); 197 | } 198 | -------------------------------------------------------------------------------- /src/components/pull-model.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Dialog, DialogContent, DialogTitle, DialogTrigger } from "./ui/dialog"; 3 | 4 | import { DownloadIcon } from "@radix-ui/react-icons"; 5 | import PullModelForm from "./pull-model-form"; 6 | 7 | export default function PullModel() { 8 | return ( 9 | 10 | 11 |
12 | 13 |

Pull model

14 |
15 |
16 | 17 | Pull Model 18 | 19 | 20 |
21 | ); 22 | } 23 | -------------------------------------------------------------------------------- /src/components/sidebar-skeleton.tsx: -------------------------------------------------------------------------------- 1 | import { Skeleton } from "@/components/ui/skeleton"; 2 | 3 | export default function SidebarSkeleton() { 4 | return ( 5 |
6 |
7 | 8 | 9 |
10 | 11 |
12 | 13 | 14 |
15 | 16 |
17 | 18 | 19 |
20 | 21 |
22 | 23 | 24 |
25 | 26 |
27 | 28 | 29 |
30 | 31 |
32 | ); 33 | } 34 | -------------------------------------------------------------------------------- /src/components/sidebar.tsx: -------------------------------------------------------------------------------- 1 | "use client"; 2 | 3 | import Link from "next/link"; 4 | import { MoreHorizontal, SquarePen, Trash2 } from "lucide-react"; 5 | import { cn } from "@/lib/utils"; 6 | import { Button, buttonVariants } from "@/components/ui/button"; 7 | import { Message } from "ai/react"; 8 | import Image from "next/image"; 9 | import { Suspense, useEffect, useState } from "react"; 10 | import SidebarSkeleton from "./sidebar-skeleton"; 11 | import { Avatar, AvatarFallback, AvatarImage } from "./ui/avatar"; 12 | import UserSettings from "./user-settings"; 13 | import { ScrollArea, Scrollbar } from "@radix-ui/react-scroll-area"; 14 | import PullModel from "./pull-model"; 15 | import { 16 | Dialog, 17 | DialogContent, 18 | DialogDescription, 19 | DialogHeader, 20 | DialogTitle, 21 | DialogTrigger, 22 | } from "./ui/dialog"; 23 | import { 24 | DropdownMenu, 25 | DropdownMenuContent, 26 | DropdownMenuTrigger, 27 | } from "./ui/dropdown-menu"; 28 | import { TrashIcon } from "@radix-ui/react-icons"; 29 | import { useRouter } from "next/navigation"; 30 | import useChatStore from "@/app/hooks/useChatStore"; 31 | 32 | interface SidebarProps { 33 | isCollapsed: boolean; 34 | messages: Message[]; 35 | onClick?: () => void; 36 | isMobile: boolean; 37 | chatId: string; 38 | closeSidebar?: () => void; 39 | } 40 | 41 | export function Sidebar({ 42 | messages, 43 | isCollapsed, 44 | isMobile, 45 | chatId, 46 | closeSidebar, 47 | }: SidebarProps) { 48 | const router = useRouter(); 49 | 50 | const chats = useChatStore((state) => state.chats); 51 | const handleDelete = useChatStore((state) => state.handleDelete); 52 | 53 | return ( 54 |
58 |
59 | 83 | 84 |
85 |

Your chats

86 | 87 | {chats && 88 | Object.entries(chats) 89 | .sort( 90 | ([, a], [, b]) => 91 | new Date(b.createdAt).getTime() - 92 | new Date(a.createdAt).getTime() 93 | ) 94 | .map(([id, chat]) => ( 95 | 107 |
108 |
109 | 110 | {chat.messages.length > 0 111 | ? chat.messages[0].content 112 | : ""} 113 | 114 |
115 |
116 | 117 | 118 | 125 | 126 | 127 | 128 | 129 | 137 | 138 | 139 | 140 | Delete chat? 141 | 142 | Are you sure you want to delete this chat? This 143 | action cannot be undone. 144 | 145 |
146 | 147 | 157 |
158 |
159 |
160 |
161 |
162 |
163 | 164 | ))} 165 |
166 |
167 |
168 | 169 |
170 | 171 |
172 |
173 | ); 174 | } 175 | -------------------------------------------------------------------------------- /src/components/ui/avatar.tsx: -------------------------------------------------------------------------------- 1 | "use client" 2 | 3 | import * as React from "react" 4 | import * as AvatarPrimitive from "@radix-ui/react-avatar" 5 | 6 | import { cn } from "@/lib/utils" 7 | 8 | const Avatar = React.forwardRef< 9 | React.ElementRef, 10 | React.ComponentPropsWithoutRef 11 | >(({ className, ...props }, ref) => ( 12 | 20 | )) 21 | Avatar.displayName = AvatarPrimitive.Root.displayName 22 | 23 | const AvatarImage = React.forwardRef< 24 | React.ElementRef, 25 | React.ComponentPropsWithoutRef 26 | >(({ className, ...props }, ref) => ( 27 | 32 | )) 33 | AvatarImage.displayName = AvatarPrimitive.Image.displayName 34 | 35 | const AvatarFallback = React.forwardRef< 36 | React.ElementRef, 37 | React.ComponentPropsWithoutRef 38 | >(({ className, ...props }, ref) => ( 39 | 47 | )) 48 | AvatarFallback.displayName = AvatarPrimitive.Fallback.displayName 49 | 50 | export { Avatar, AvatarImage, AvatarFallback } 51 | -------------------------------------------------------------------------------- /src/components/ui/button.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react"; 2 | import { Slot } from "@radix-ui/react-slot"; 3 | import { cva, type VariantProps } from "class-variance-authority"; 4 | 5 | import { cn } from "@/lib/utils"; 6 | 7 | const buttonVariants = cva( 8 | "inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50", 9 | { 10 | variants: { 11 | variant: { 12 | default: 13 | "bg-primary text-primary-foreground shadow hover:bg-primary/90", 14 | destructive: 15 | "bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90", 16 | outline: 17 | "border border-input bg-background shadow-sm hover:bg-accent hover:text-accent-foreground", 18 | secondary: 19 | "dark:bg-card/60 bg-accent/20 text-secondary-foreground shadow-sm hover:bg-secondary/60 hover:dark:bg-card/40", 20 | ghost: "hover:bg-accent hover:text-accent-foreground", 21 | link: "text-primary underline-offset-4 hover:underline", 22 | secondaryLink: 23 | "bg-accent/90 dark:bg-secondary/80 text-secondary-foreground shadow-sm dark:hover:bg-secondary hover:bg-accent", 24 | }, 25 | size: { 26 | default: "h-9 px-4 py-2", 27 | sm: "h-8 rounded-md px-3 text-xs", 28 | lg: "h-10 rounded-md px-8", 29 | icon: "h-10 w-10", 30 | iconSm: "h-8 w-8", 31 | }, 32 | }, 33 | defaultVariants: { 34 | variant: "default", 35 | size: "default", 36 | }, 37 | } 38 | ); 39 | 40 | export interface ButtonProps 41 | extends React.ButtonHTMLAttributes, 42 | VariantProps { 43 | asChild?: boolean; 44 | } 45 | 46 | const Button = React.forwardRef( 47 | ({ className, variant, size, asChild = false, ...props }, ref) => { 48 | const Comp = asChild ? Slot : "button"; 49 | return ( 50 | 55 | ); 56 | } 57 | ); 58 | Button.displayName = "Button"; 59 | 60 | export { Button, buttonVariants }; 61 | -------------------------------------------------------------------------------- /src/components/ui/card.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react" 2 | 3 | import { cn } from "@/lib/utils" 4 | 5 | const Card = React.forwardRef< 6 | HTMLDivElement, 7 | React.HTMLAttributes 8 | >(({ className, ...props }, ref) => ( 9 |
17 | )) 18 | Card.displayName = "Card" 19 | 20 | const CardHeader = React.forwardRef< 21 | HTMLDivElement, 22 | React.HTMLAttributes 23 | >(({ className, ...props }, ref) => ( 24 |
29 | )) 30 | CardHeader.displayName = "CardHeader" 31 | 32 | const CardTitle = React.forwardRef< 33 | HTMLParagraphElement, 34 | React.HTMLAttributes 35 | >(({ className, ...props }, ref) => ( 36 |

41 | )) 42 | CardTitle.displayName = "CardTitle" 43 | 44 | const CardDescription = React.forwardRef< 45 | HTMLParagraphElement, 46 | React.HTMLAttributes 47 | >(({ className, ...props }, ref) => ( 48 |

53 | )) 54 | CardDescription.displayName = "CardDescription" 55 | 56 | const CardContent = React.forwardRef< 57 | HTMLDivElement, 58 | React.HTMLAttributes 59 | >(({ className, ...props }, ref) => ( 60 |

61 | )) 62 | CardContent.displayName = "CardContent" 63 | 64 | const CardFooter = React.forwardRef< 65 | HTMLDivElement, 66 | React.HTMLAttributes 67 | >(({ className, ...props }, ref) => ( 68 |
73 | )) 74 | CardFooter.displayName = "CardFooter" 75 | 76 | export { Card, CardHeader, CardFooter, CardTitle, CardDescription, CardContent } 77 | -------------------------------------------------------------------------------- /src/components/ui/chat/chat-bubble.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react"; 2 | import { cva, type VariantProps } from "class-variance-authority"; 3 | import { cn } from "@/lib/utils"; 4 | import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/avatar"; 5 | import MessageLoading from "./message-loading"; 6 | import { Button, ButtonProps } from "../button"; 7 | 8 | // ChatBubble 9 | const chatBubbleVariant = cva( 10 | "flex gap-2 max-w-[80%] items-end relative group", 11 | { 12 | variants: { 13 | variant: { 14 | received: "self-start", 15 | sent: "self-end flex-row-reverse", 16 | }, 17 | layout: { 18 | default: "", 19 | ai: "max-w-full w-full items-center", 20 | }, 21 | }, 22 | defaultVariants: { 23 | variant: "received", 24 | layout: "default", 25 | }, 26 | } 27 | ); 28 | 29 | interface ChatBubbleProps 30 | extends React.HTMLAttributes, 31 | VariantProps {} 32 | 33 | const ChatBubble = React.forwardRef( 34 | ({ className, variant, layout, children, ...props }, ref) => ( 35 |
43 | {React.Children.map(children, (child) => 44 | React.isValidElement(child) && typeof child.type !== "string" 45 | ? React.cloneElement(child, { 46 | variant, 47 | layout, 48 | } as React.ComponentProps) 49 | : child 50 | )} 51 |
52 | ) 53 | ); 54 | ChatBubble.displayName = "ChatBubble"; 55 | 56 | // ChatBubbleAvatar 57 | interface ChatBubbleAvatarProps { 58 | src?: string; 59 | fallback?: string; 60 | className?: string; 61 | width?: number; 62 | height?: number; 63 | } 64 | 65 | const ChatBubbleAvatar: React.FC = ({ 66 | src, 67 | fallback, 68 | className, 69 | width, 70 | height, 71 | }) => ( 72 | 73 | 80 | {fallback} 81 | 82 | ); 83 | 84 | // ChatBubbleMessage 85 | const chatBubbleMessageVariants = cva("p-4", { 86 | variants: { 87 | variant: { 88 | received: 89 | "bg-secondary text-secondary-foreground rounded-r-lg rounded-tl-lg", 90 | sent: "bg-primary text-primary-foreground rounded-l-lg rounded-tr-lg", 91 | }, 92 | layout: { 93 | default: "", 94 | ai: "border-t w-full rounded-none bg-transparent", 95 | }, 96 | }, 97 | defaultVariants: { 98 | variant: "received", 99 | layout: "default", 100 | }, 101 | }); 102 | 103 | interface ChatBubbleMessageProps 104 | extends React.HTMLAttributes, 105 | VariantProps { 106 | isLoading?: boolean; 107 | } 108 | 109 | const ChatBubbleMessage = React.forwardRef< 110 | HTMLDivElement, 111 | ChatBubbleMessageProps 112 | >( 113 | ( 114 | { className, variant, layout, isLoading = false, children, ...props }, 115 | ref 116 | ) => ( 117 |
125 | {isLoading ? ( 126 |
127 | 128 |
129 | ) : ( 130 | children 131 | )} 132 |
133 | ) 134 | ); 135 | ChatBubbleMessage.displayName = "ChatBubbleMessage"; 136 | 137 | // ChatBubbleTimestamp 138 | interface ChatBubbleTimestampProps 139 | extends React.HTMLAttributes { 140 | timestamp: string; 141 | } 142 | 143 | const ChatBubbleTimestamp: React.FC = ({ 144 | timestamp, 145 | className, 146 | ...props 147 | }) => ( 148 |
149 | {timestamp} 150 |
151 | ); 152 | 153 | // ChatBubbleAction 154 | type ChatBubbleActionProps = ButtonProps & { 155 | icon: React.ReactNode; 156 | }; 157 | 158 | const ChatBubbleAction: React.FC = ({ 159 | icon, 160 | onClick, 161 | className, 162 | variant = "ghost", 163 | size = "icon", 164 | ...props 165 | }) => ( 166 | 175 | ); 176 | 177 | interface ChatBubbleActionWrapperProps 178 | extends React.HTMLAttributes { 179 | variant?: "sent" | "received"; 180 | className?: string; 181 | } 182 | 183 | const ChatBubbleActionWrapper = React.forwardRef< 184 | HTMLDivElement, 185 | ChatBubbleActionWrapperProps 186 | >(({ variant, className, children, ...props }, ref) => ( 187 |
198 | {children} 199 |
200 | )); 201 | ChatBubbleActionWrapper.displayName = "ChatBubbleActionWrapper"; 202 | 203 | export { 204 | ChatBubble, 205 | ChatBubbleAvatar, 206 | ChatBubbleMessage, 207 | ChatBubbleTimestamp, 208 | chatBubbleVariant, 209 | chatBubbleMessageVariants, 210 | ChatBubbleAction, 211 | ChatBubbleActionWrapper, 212 | }; 213 | -------------------------------------------------------------------------------- /src/components/ui/chat/chat-input.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react"; 2 | import { Textarea } from "@/components/ui/textarea"; 3 | import { cn } from "@/lib/utils"; 4 | 5 | interface ChatInputProps 6 | extends React.TextareaHTMLAttributes {} 7 | 8 | const ChatInput = React.forwardRef( 9 | ({ className, ...props }, forwardedRef) => { 10 | const handleRef = (node: HTMLTextAreaElement | null) => { 11 | if (node) { 12 | // Apply auto-resize logic 13 | node.style.height = "0px"; 14 | node.style.height = node.scrollHeight + "px"; 15 | 16 | if (typeof forwardedRef === "function") { 17 | forwardedRef(node); 18 | } else if (forwardedRef) { 19 | forwardedRef.current = node; 20 | } 21 | } 22 | }; 23 | 24 | return ( 25 |