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 🙏

© 2025 – Pkg Stats / Ryan Hefner

agui-chat-widget

v0.0.1

Published

A beautiful React chat widget with SSE streaming, dark mode, and structured data rendering support

Readme

agui-chat-widget

一个现代化的 React 聊天组件,支持 SSE 流式响应、深色模式和结构化数据渲染。

npm version license

✨ 特性

  • 🎨 深色/浅色主题 - 内置主题切换,自动适配
  • 🌊 流式响应 - 完整支持 Server-Sent Events (SSE)
  • 💰 结构化数据渲染 - 支持虚拟货币卡片、图表等多种内容类型
  • 📱 响应式设计 - 适配各种屏幕尺寸
  • 🎯 TypeScript - 完整的类型定义
  • 🚀 零配置 - 开箱即用
  • 🎭 可定制 - 通过 CSS 变量轻松定制样式

📦 安装

npm install agui-chat-widget

或使用其他包管理器:

yarn add agui-chat-widget
pnpm add agui-chat-widget

🚀 快速开始

基础使用

import { ChatWidget } from 'agui-chat-widget';
import 'agui-chat-widget/dist/index.css';

export default function App() {
  return (
    <div>
      <h1>我的应用</h1>
      <ChatWidget 
        apiEndpoint="https://your-api.com/stream"
        title="AI 助手"
        theme="light"
      />
    </div>
  );
}

深色模式

import { useState } from 'react';
import { ChatWidget } from 'agui-chat-widget';
import 'agui-chat-widget/dist/index.css';

export default function App() {
  const [theme, setTheme] = useState<'light' | 'dark'>('light');

  return (
    <div>
      <button onClick={() => setTheme(t => t === 'light' ? 'dark' : 'light')}>
        切换主题
      </button>
      
      <ChatWidget 
        apiEndpoint="https://your-api.com/stream"
        theme={theme}
      />
    </div>
  );
}

📖 API 文档

Props

| 属性 | 类型 | 默认值 | 描述 | |------|------|--------|------| | apiEndpoint | string | - | SSE 接口地址(必填) | | title | string | 'Assistant' | 聊天窗口标题 | | theme | 'light' \| 'dark' | 'light' | 主题模式 |

SSE 响应格式

您的后端接口需要返回以下格式的 SSE 事件:

1. 开始事件

data: {"event": "start", "data": {"message": null, "type": null}}

2. 文本消息(流式)

data: {"event": "message", "data": {"message": "你好", "type": "text"}}
data: {"event": "message", "data": {"message": ",", "type": "text"}}
data: {"event": "message", "data": {"message": "很高兴", "type": "text"}}

3. 结构化数据 - 虚拟货币卡片

data: {"event": "message", "data": {
  "message": null, 
  "type": "coin", 
  "data": [
    {"coin": "BTC", "price": 90000},
    {"coin": "ETH", "price": 10000}
  ]
}}

4. 结束事件

data: {"event": "end", "data": {"message": null, "type": null}}

完整示例

data: {"event": "start", "data": {"message": null, "type": null}}

data: {"event": "message", "data": {"message": "当前", "type": "text"}}
data: {"event": "message", "data": {"message": "加密", "type": "text"}}
data: {"event": "message", "data": {"message": "货币", "type": "text"}}
data: {"event": "message", "data": {"message": "价格", "type": "text"}}
data: {"event": "message", "data": {"message": ":", "type": "text"}}

data: {"event": "message", "data": {"message": null, "type": "coin", "data": [
  {"coin": "BTC", "price": 90000},
  {"coin": "ETH", "price": 10000},
  {"coin": "SOL", "price": 180}
]}}

data: {"event": "end", "data": {"message": null, "type": null}}

🎨 样式定制

组件使用 CSS 变量,您可以通过覆盖变量来自定义样式:

.agui-chat-widget-container {
  --agui-brand-color: #your-brand-color;
  --agui-bg-primary: #ffffff;
  --agui-text-primary: #171717;
  /* ... 更多变量 */
}

/* 深色模式 */
.agui-chat-widget-container[data-theme="dark"] {
  --agui-bg-primary: #0f0f11;
  --agui-text-primary: #ffffff;
  /* ... */
}

🛠️ 后端实现示例

Next.js App Router

// app/api/stream/route.ts
import { NextRequest, NextResponse } from 'next/server';

export async function POST(req: NextRequest) {
  const encoder = new TextEncoder();

  const stream = new ReadableStream({
    async start(controller) {
      // 开始事件
      controller.enqueue(encoder.encode(
        `data: ${JSON.stringify({ event: "start", data: { message: null, type: null } })}\n\n`
      ));

      // 文本消息
      const text = "你好!这是一条测试消息。";
      for (const char of text) {
        controller.enqueue(encoder.encode(
          `data: ${JSON.stringify({ event: "message", data: { message: char, type: "text" } })}\n\n`
        ));
        await new Promise(resolve => setTimeout(resolve, 50));
      }

      // 虚拟货币数据
      controller.enqueue(encoder.encode(
        `data: ${JSON.stringify({ 
          event: "message", 
          data: { 
            message: null, 
            type: "coin", 
            data: [
              { coin: "BTC", price: 90000 },
              { coin: "ETH", price: 10000 }
            ]
          } 
        })}\n\n`
      ));

      // 结束事件
      controller.enqueue(encoder.encode(
        `data: ${JSON.stringify({ event: "end", data: { message: null, type: null } })}\n\n`
      ));

      controller.close();
    },
  });

  return new NextResponse(stream, {
    headers: {
      'Content-Type': 'text/event-stream',
      'Cache-Control': 'no-cache',
      'Connection': 'keep-alive',
    },
  });
}

🌟 支持的内容类型

目前支持以下内容类型:

  • text: 纯文本消息(流式拼接)
  • coin: 虚拟货币卡片(水平滚动)
  • 🔜 chart: 图表数据
  • 🔜 table: 表格数据
  • 🔜 image: 图片

📝 开发计划

  • [ ] 支持更多结构化数据类型(图表、表格等)
  • [ ] 添加消息搜索功能
  • [ ] 支持文件上传
  • [ ] 添加语音输入
  • [ ] 国际化支持

🤝 贡献

欢迎提交 Issue 和 Pull Request!

📄 License

MIT © Agui Team

🔗 相关链接