shadcn-chatbot-kit

repository·main·Indexed 21 days ago

https://github.com/blazity/shadcn-chatbot-kit

A specialized toolkit for building AI-powered chatbot interfaces using shadcn/ui components and modern web technologies. It provides high-level components like Chat and DemoChat, as well as primitives such as ChatMessage, AudioVisualizer, and various copy-to-clipboard utilities. The kit is designed to integrate seamlessly with the @ai-sdk/react useChat hook.

Tokens
15.1K
Snippets
50
Records
70
Agent score
73%

What's inside shadcn-chatbot-kit

  1. Core features of the chatbot kit

    main

    The kit provides several built-in features for a complete chat experience:

    • Message Area: Auto-scroll with smart scrolling behavior.
    • Input: Auto-resize message input with file upload support.
    • Suggestions: Built-in prompt suggestions.
    • Actions: Message actions such as copy and rate response.
    • UI/UX: Elegant loading states, transitions, and Markdown support in messages.
    • Media: File attachments and image previews.
    • Support: Dark mode and TypeScript support.
  2. How double-enter interrupt behavior works in MessageInput

    main

    The enableInterrupt feature provides a safety mechanism for stopping AI generation.

    When enableInterrupt is enabled (default) and isGenerating is true:

    1. The user presses Enter once.
    2. Instead of submitting, the component shows a prompt asking the user to press Enter again to interrupt.
    3. The interruption occurs if the user presses Enter a second time (triggering the stop function).
    4. The prompt disappears if the generation completes naturally.

    To disable this behavior, set enableInterrupt={false}.

  3. Install the MarkdownRenderer component

    main

    You can install the MarkdownRenderer component using the shadcn CLI or by manual installation.

    Using CLI

    Run the following command to add the component directly to your project:

    npx shadcn@latest add https://shadcn-chatbot-kit.vercel.app/r/markdown-renderer.json

    Manual Installation

    1. Install dependencies:
      npm install react-markdown remark-gfm shiki
    2. Add CopyButton: Ensure the CopyButton component is present in your project, as MarkdownRenderer depends on it.
    3. Copy Source: Copy the component source code into your project.
    4. Configure Tailwind: Add the shiki color configuration to your tailwind.config.js (see Configure Shiki colors in Tailwind).
    5. Update Imports: Adjust the import paths within the component to match your project's directory structure.
    npx shadcn@latest add https://shadcn-chatbot-kit.vercel.app/r/markdown-renderer.json
  4. Install the PromptSuggestions component

    main

    You can install the PromptSuggestions component using the shadcn CLI or by manually copying the source code into your project.

    Using CLI

    Run the following command to add the component directly via the registry:

    npx shadcn@latest add https://shadcn-chatbot-kit.vercel.app/r/prompt-suggestions.json

    Manual Installation

    1. Copy the component source code into your project.
    2. Update the import paths within the file to match your project's directory structure.
  5. Use the MessageInput component

    main

    The MessageInput component is a rich textarea designed for AI chat interfaces. It supports auto-resizing, file attachments, and voice input.

    Basic Usage

    import { MessageInput } from "@/components/ui/message-input"
    
    export function BasicMessageInput() {
      return (
        <MessageInput
          value={input}
          onChange={handleInputChange}
          isGenerating={false}
        />
      )
    }

    With Interrupt Behavior

    When enableInterrupt is true and isGenerating is true, pressing Enter once will prompt the user to press Enter again to interrupt the generation. This is useful for preventing accidental interruptions.

    export function MessageInputWithInterrupt() {
      return (
        <MessageInput
          value={input}
          onChange={handleInputChange}
          isGenerating={isGenerating}
          stop={handleStop}
          enableInterrupt={true}
        />
      )
    }

    With File Attachments

    To enable file attachments and drag-and-drop support, set allowAttachments to true and manage the files state.

    import { MessageInput } from "@/components/ui/message-input"
    
    export function MessageInputWithAttachments() {
      const [files, setFiles] = useState<File[] | null>(null)
    
      return (
        <MessageInput
          value={input}
          onChange={handleInputChange}
          isGenerating={false}
          allowAttachments
          files={files}
          setFiles={setFiles}
        />
      )
    }

    Voice Input

    Providing the transcribeAudio prop will automatically display the voice input button. This function handles the conversion of audio blobs to text.

    <MessageInput
      value={input}
      onChange={handleInputChange}
      isGenerating={false}
      transcribeAudio={transcribeAudio}
    />
    import { MessageInput } from "@/components/ui/message-input"
    
    export function BasicMessageInput() {
      return (
        <MessageInput
          value={input}
          onChange={handleInputChange}
          isGenerating={false}
        />
      )
    }
  6. Integrate with Vercel AI SDK

    main

    The kit is designed to work seamlessly with the Vercel AI SDK. To integrate, import the useChat hook from the Vercel AI SDK and pass its returned properties to the Chat component provided by the kit.

    // Example pattern for Vercel AI SDK integration
    import { useChat } from 'ai/react';
    import { Chat } from '@blazity/shadcn-chatbot-kit'; // Hypothetical import path
    
    export function MyChatComponent() {
      const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat();
    
      return (
        <Chat 
          messages={messages} 
          input={input} 
          handleInputChange={handleInputChange} 
          handleSubmit={handleSubmit} 
          isLoading={isLoading} 
        />
      );
    }
  7. Use the MessageList component

    main

    The MessageList component renders a vertical list of chat messages. It supports displaying a typing indicator and can be customized using messageOptions to pass additional props to individual ChatMessage components.

    import { MessageList } from "@/components/ui/message-list"
    
    export function ChatDemo() {
      const messages = [
        {
          id: "1",
          role: "user",
          content: "Hello, how are you?",
        },
        {
          id: "2",
          role: "assistant",
          content: "I'm doing well, thank you for asking!",
        },
      ]
    
      return <MessageList messages={messages} isTyping={false} />
    }
  8. Configure Tailwind CSS for Typing Indicator

    main

    If installing manually, you must add the typing-dot-bounce keyframes and animation to your tailwind.config.js file to enable the bouncing dot effect.

    // tailwind.config.js
    module.exports = {
      theme: {
        extend: {
          keyframes: {
            "typing-dot-bounce": {
              "0%,40%": { transform: "translateY(0)" },
              "20%": { transform: "translateY(-0.25rem)" },
            },
          },
          animation: {
            "typing-dot-bounce": "typing-dot-bounce 1.25s ease-out infinite",
          },
        },
      },
    }
  9. Install the Chat component

    main

    You can install the Chat component using the shadcn CLI or by manually adding the necessary files and dependencies to your project.

    CLI Installation

    Run the following command to add the component directly via the registry:

    npx shadcn@latest add https://shadcn-chatbot-kit.vercel.app/r/chat.json

    Manual Installation

    If you prefer manual installation, follow these steps:

    1. Install Dependencies: The Chat component requires PromptSuggestions, MessageInput, MessageList, and ChatMessage. Ensure these are installed in your project first.
    2. Copy Files: Copy the following files into your project:
      • components/ui/chat.tsx
      • hooks/use-auto-scroll.ts
    3. Update Imports: Adjust the import paths in the copied files to match your project's directory structure.
  10. Install the MessageInput component

    main

    You can install the MessageInput component using the shadcn CLI or by manual installation.

    Via CLI

    npx shadcn@latest add https://shadcn-chatbot-kit.vercel.app/r/message-input.json

    Via Manual Installation

    1. Install required dependencies:
    npm install framer-motion@11 remeda@2
    1. Ensure the FilePreview component is installed in your project (as MessageInput depends on it).
    2. Copy the following files into your project:
      • components/ui/message-input.tsx
      • hooks/use-autosize-textarea.ts
      • hooks/use-audio-recording.ts
    3. Update the import paths in the copied files to match your project structure.
    npx shadcn@latest add https://shadcn-chatbot-kit.vercel.app/r/message-input.json