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 🙏

© 2026 – Pkg Stats / Ryan Hefner

qagent-hub

v0.1.0

Published

A lightweight and powerful AI Agent hub built on Bun runtime with tool calling support

Readme

Agent Hub

一个轻量级、强大的 AI Agent 平台服务库,基于 Bun 运行时构建,支持工具调用和流式响应。

✨ 特性

  • 🚀 流式响应 - 实时接收 AI 模型输出
  • 🎯 事件驱动 - 基于 EventEmitter 的消息分发
  • 🔄 会话隔离 - 独立的 Session 和消息队列
  • 📝 历史管理 - 灵活的历史记录接口
  • ⏸️ 停止控制 - 支持优雅地中断 Agent
  • 🔒 类型安全 - 完整的 TypeScript 类型定义
  • 高性能 - 基于 Bun 运行时,性能卓越

📦 安装

bun add agent-hub

🚀 快速开始

import * as agentHub from 'agent-hub';
import type { AgentConfig, Message } from 'agent-hub';

// 1. 配置 Agent
const config: AgentConfig = {
  id: 'assistant',
  name: 'My Assistant',
  model: {
    modelId: 'gpt-4',
    baseURL: 'https://api.openai.com/v1',
    apiKey: process.env.OPENAI_API_KEY!,
    provider: 'openai-chat',
  },
  systemPrompt: 'You are a helpful assistant.',
};

// 2. 初始化
agentHub.setup({
  getAgentConfig: (id) => (id === 'assistant' ? config : null),
});

// 3. 发送消息
const response = agentHub.sendMessage({
  agentId: 'assistant',
  type: 'user_input',
  content: 'Hello, how are you?',
});

// 4. 监听响应
if (response.success) {
  agentHub.addMessageListener({
    sessionId: response.result.sessionId,
    onMessage: (message: Message) => {
      if (message.type === 'assistant' && message.chunk.type === 'text-delta') {
        process.stdout.write(message.chunk.text);
      }
    },
  });
}

📚 文档

完整文档请查看 docs 目录:

🎯 核心概念

Session 管理

每次对话都会创建一个独立的 Session,包含:

  • 唯一的 Session ID
  • 独立的消息队列
  • 自动的生命周期管理

消息类型

支持多种消息类型:

  • user - 用户消息
  • assistant - AI 助手消息
  • loop_start / loop_end - 对话循环标记
  • step_start / step_end - 处理步骤标记

流式输出

支持细粒度的流式控制:

  • text-start - 开始输出
  • text-delta - 流式增量
  • text-end - 输出完成

🔧 系统要求

  • Bun >= 1.0
  • TypeScript >= 5.0

🌟 使用场景

Web 应用集成

Bun.serve({
  port: 3000,
  websocket: {
    message(ws, message) {
      const data = JSON.parse(message);
      const response = agentHub.sendMessage({
        agentId: data.agentId,
        type: 'user_input',
        content: data.message,
      });

      if (response.success) {
        agentHub.addMessageListener({
          sessionId: response.result.sessionId,
          onMessage: (msg) => ws.send(JSON.stringify(msg)),
        });
      }
    },
  },
});

历史记录持久化

agentHub.setup({
  getAgentConfig: (id) => agents[id],
  getAgentHistory: async (id) => {
    // 从数据库加载历史
    return await db.loadHistory(id);
  },
  onAgentLoopEnd: async (id, history) => {
    // 保存到数据库
    await db.saveHistory(id, history);
  },
});

多 Agent 管理

const agents = {
  'assistant': { ... },
  'coder': { ... },
  'designer': { ... },
};

agentHub.setup({
  getAgentConfig: (id) => agents[id] || null,
});

// 根据意图路由到不同 Agent
const agentId = await routeToAgent(userMessage);
agentHub.sendMessage({ agentId, ... });

🧪 运行示例

查看完整示例:

# 配置环境变量
cp .env.example .env
# 编辑 .env 填入你的 API 密钥

# 运行示例
bun demo.ts

📄 许可证

MIT License

🤝 贡献

欢迎提交 Issue 和 Pull Request!

🔗 相关项目

📊 项目状态

  • ✅ 核心功能已完成
  • ✅ 流式响应支持
  • ✅ 历史记录管理
  • ✅ 停止控制
  • ✅ 完整文档
  • 🚧 工具调用支持(开发中)
  • 🚧 更多示例(开发中)

Made with ❤️ using Bun