npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, πŸ‘‹, I’m Ryan HefnerΒ  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you πŸ™

Β© 2024 – Pkg Stats / Ryan Hefner

@embedapi/react

v1.0.5

Published

πŸš€ Build stunning AI chat interfaces in minutes! Production-ready React components with real-time streaming, Material-UI design, and zero configuration required. Transform your app with powerful, customizable AI chat features.

Downloads

407

Readme

@embedapi/react

πŸš€ Build Production-Ready AI Chat Interfaces in Minutes!

Transform your React app with stunning AI chat interfaces powered by EmbedAPI. Get a fully customizable Material-UI chat component that works out of the box, or build your own with our powerful hooks.

Why Choose @embedapi/react?

✨ Zero Configuration Required

  • Drop in our <AgentChat /> component and you're ready to go
  • Beautiful Material-UI design works instantly

🎨 Fully Customizable

  • Light/Dark themes included
  • Style every component to match your brand
  • Build custom interfaces with our hooks

⚑ Built for Performance

  • Real-time streaming responses
  • Optimized for production
  • Built on modern React 18

πŸ› οΈ Developer Friendly

  • TypeScript definitions included
  • Extensive documentation
  • Active community support

Prerequisites

  1. Create a free account at EmbedAPI
  2. Create an AI agent in your dashboard
  3. Copy your agent ID (starts with agent_...)

Quick Start

npm install @embedapi/react

Basic Usage

import React from 'react';
import { AgentChat } from '@embedapi/react';

function App() {
  return (
    <AgentChat
      agentId="your-agent-id"
      theme="light"
      placeholder="Type a message..."
      customStyles={{
        container: {
          maxWidth: '800px',
          margin: '0 auto'
        }
      }}
    />
  );
}

export default App;

That's it! You now have a professional AI chat interface in your app. πŸŽ‰

View Demo | Read Docs | Join Discord

Features

  • πŸ€– Pre-built AgentChat component with Material-UI interface
  • 🎣 useChat hook for custom chat implementations
  • πŸ”„ Real-time streaming support
  • ⚑ Easy integration with EmbedAPI services
  • 🎨 Fully customizable components

Installation

npm install @embedapi/react

That's it! All required dependencies are included.

Components

AgentChat

A fully-featured chat interface built with Material-UI, ready for production use.

<AgentChat
  // Required
  agentId="agent_..."              // Your EmbedAPI agent ID

  // Optional
  theme="light"                    // 'light' or 'dark'
  initialMessages={[               // Initial messages to display
    {
      role: 'assistant',
      content: 'How can I help?'
    }
  ]}
  placeholder="Type a message..."  // Input placeholder text
  className=""                     // Additional CSS class
  containerWidth="100%"           // Width of the chat container
  maxHeight="600px"               // Maximum height of message area
  onError={(error) => {}}         // Error handling callback
  style={{}}                      // Additional inline styles
  customStyles={{                 // Custom styling for components
    container: {},               // Container styles
    messageContainer: {},        // Message area styles
    userMessage: {},            // User message bubble styles
    assistantMessage: {},       // Assistant message bubble styles
    inputContainer: {}          // Input area styles
  }}
/>

Styling Guide

The component supports comprehensive styling through the customStyles prop:

<AgentChat
  customStyles={{
    container: {
      // Styles for the outer container
      maxWidth: '800px',
      margin: '0 auto'
    },
    messageContainer: {
      // Styles for the messages area
      padding: '20px',
      backgroundColor: '#f5f5f5'
    },
    userMessage: {
      // Styles for user message bubbles
      backgroundColor: '#e3f2fd',
      borderRadius: '10px'
    },
    assistantMessage: {
      // Styles for assistant message bubbles
      backgroundColor: '#fff',
      borderRadius: '10px'
    },
    inputContainer: {
      // Styles for the input area
      borderTop: '1px solid #eee',
      padding: '20px'
    }
  }}
  onError={(error) => {}}         // Optional error handling
/>

Hooks

useChat

Build custom chat interfaces with our low-level hook:

const {
  messages,        // Current chat messages
  isLoading,       // Loading state
  error,           // Error state
  currentMessage,  // Current message being processed
  sendMessage,     // Send a message without streaming
  streamMessage,   // Send a message with streaming
  reset,           // Reset the chat
  clearCache,      // Clear cached messages
  messageCount     // Number of messages currently stored
} = useChat({
  agentId: 'agent_...',
  enableCache: true,
  messageLimit: 10, // Limit the number of cached messages
  initialMessages: []
});

Message Caching and Limits

The useChat hook includes built-in message caching with automatic limits:

  • Messages are automatically cached in sessionStorage
  • Conversations persist between page reloads
  • Each agent has its own cache
  • Cache can be disabled with enableCache: false
  • Clear cache manually with clearCache()
  • Limit the number of cached messages with messageLimit (default: 10)

Advanced Example

Build a custom chat interface using the useChat hook with caching:

import React, { useState } from 'react';
import { useChat } from '@embedapi/react';

function CustomChat() {
  const [input, setInput] = useState('');
  const {
    messages,
    isLoading,
    currentMessage,
    streamMessage,
    reset,
    clearCache,
    messageCount
  } = useChat({
    agentId: 'agent_...',
    enableCache: true,
    messageLimit: 20, // Custom message limit
    initialMessages: [
      { role: 'assistant', content: 'How can I help you today?' }
    ]
  });

  const handleSend = async (e) => {
    e.preventDefault();
    if (!input.trim() || isLoading) return;
    
    const message = input.trim();
    setInput('');
    await streamMessage(message);
  };

  const handleReset = () => {
    reset();        // Reset conversation
    clearCache();   // Clear cached messages
  };

  return (
    <div>
      <div>Messages: {messageCount}/20</div>
      {messages.map((msg, i) => (
        <div key={i} className={msg.role}>
          {msg.content}
        </div>
      ))}
      
      {isLoading && (
        <div className="loading">
          Typing: {currentMessage.content}
        </div>
      )}

      <form onSubmit={handleSend}>
        <input
          value={input}
          onChange={(e) => setInput(e.target.value)}
          placeholder="Type a message..."
          disabled={isLoading}
        />
        <button type="submit" disabled={isLoading || !input.trim()}>
          Send
        </button>
        <button type="button" onClick={handleReset}>
          Reset Chat
        </button>
      </form>
    </div>
  );
}

export default CustomChat;

Customization

Themes

The AgentChat component supports both light and dark themes:

<AgentChat theme="dark" />

Custom Styling

Customize any part of the chat interface:

<AgentChat
  customStyles={{
    container: {
      boxShadow: '0 0 10px rgba(0,0,0,0.1)',
      borderRadius: '12px'
    },
    messageContainer: {
      padding: '20px'
    },
    userMessage: {
      background: '#e3f2fd'
    },
    assistantMessage: {
      background: '#f5f5f5'
    },
    inputContainer: {
      borderTop: '1px solid #eee'
    }
  }}
/>

Coming Soon

  • πŸ“Ž File attachments support
  • πŸ” Message search functionality
  • πŸ’Ύ Message persistence
  • πŸ”„ Context management
  • 🎨 More UI components
  • πŸ› οΈ Additional utility hooks

Contributing

We welcome contributions! Please see our Contributing Guide for details.

License

MIT License - see LICENSE file for details.

Support