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

@baiducloud/qianfan-mutil-agent

v0.1.0

Published

Qianfan SSE message protocol SDK - parse and handle streaming AI responses

Readme

@baiducloud/qianfan-mutil-agent

千帆 SSE 消息协议 SDK,用于解析和处理 AI 流式响应。

安装

npm install @baiducloud/qianfan-mutil-agent

快速开始

基础用法

import {
    Executor,
    FetchSSETransport,
    TreeView,
    ChatEvent,
    ThoughtEvent,
    ToolCallEvent
} from '@baiducloud/qianfan-mutil-agent';

// 创建传输层
const transport = new FetchSSETransport({
    endpoint: new URL('https://api.example.com/chat'),
    header: {
        'Authorization': 'Bearer your-token'
    },
    timeout: 60000
});

// 创建执行器
const executor = new Executor({
    transport,
    views: [TreeView],
    events: [ChatEvent, ThoughtEvent, ToolCallEvent]
});

// 执行请求
const response = executor.invoke({
    query: 'Hello, AI!'
});

// 获取视图并迭代结果
const treeView = response.getView(TreeView);

for await (const node of treeView) {
    console.log(node);
}

使用 XHR 传输层

import {
    Executor,
    XHRSSETransport,
    TreeView
} from '@baiducloud/qianfan-mutil-agent';

const transport = new XHRSSETransport({
    endpoint: new URL('https://api.example.com/chat')
});

const executor = new Executor({
    transport,
    views: [TreeView]
});

const response = executor.invoke({ query: 'Hello' });

处理事件

import {
    Executor,
    FetchSSETransport,
    TreeView,
    ChatEvent,
    ToolCallEvent,
    RAGEvent
} from '@baiducloud/qianfan-mutil-agent';

const executor = new Executor({
    transport: new FetchSSETransport({
        endpoint: new URL('https://api.example.com/chat')
    }),
    views: [TreeView],
    events: [ChatEvent, ToolCallEvent, RAGEvent]
});

const response = executor.invoke({ query: 'Search something' });

// 监听消息
response.eventTarget.addEventListener('message', () => {
    for (const event of response.events) {
        if (event instanceof ChatEvent) {
            console.log('Chat:', event.content);
        } else if (event instanceof ToolCallEvent) {
            console.log('Tool:', event);
        }
    }
});

取消请求

const abortController = new AbortController();

const response = executor.invoke(
    { query: 'Hello' },
    { abortController }
);

// 取消请求
setTimeout(() => {
    abortController.abort();
}, 5000);

API

核心类

| 导出 | 说明 | |------|------| | Executor | 执行器,管理请求和响应 | | ExecutionResponse | 执行响应,包含事件和视图 | | ExecutionEvent | 执行事件基类 | | EventMessage | SSE 消息封装 | | ExecutionError | 错误类型 |

传输层

| 导出 | 说明 | |------|------| | FetchSSETransport | 基于 Fetch API 的 SSE 传输层 | | XHRSSETransport | 基于 XMLHttpRequest 的 SSE 传输层 | | ITransport | 传输层接口 (类型) |

事件插件

| 插件 | action | resource | 说明 | |------|--------|----------|------| | ChatEvent | chat | - | 聊天对话事件 | | ThoughtEvent | thought | - | 思考过程事件 | | ToolCallEvent | toolcall | - | 工具调用事件 | | RAGEvent | tools | knowledge | 知识库检索事件 | | DatabaseEvent | tools | database | 数据库查询事件 | | DataSheetEvent | tools | database_sheet | 数据表事件 | | FileParserEvent | tools | file_parser | 文件解析事件 | | DeepSearchStepEvent | deep_search | - | 深度搜索步骤事件 |

视图层

| 导出 | 说明 | |------|------| | BaseView | 视图基类 | | TreeView | 树形结构视图 | | ToolUseView | 工具使用视图 | | IView | 视图接口 (类型) |

聚合规则

| 导出 | 说明 | |------|------| | AggregateRule | 聚合规则基类 | | defaultAggregatePresets | 默认聚合规则预设 | | multiAgentAggregatePresets | 多 Agent 聚合规则预设 | | assistantAgentAggregatePresets | Assistant Agent 聚合规则预设 | | IAggregateRule | 聚合规则接口 (类型) |

内容聚合策略

| contentType | 聚合策略 | |-------------|---------| | text | 字符串拼接 | | code | 字符串拼接 | | json | 对象替换 | | audio | 对象替换 | | chart | 对象替换 | | files | 数组合并 | | image | 数组合并 | | references | 数组合并 | | urls | 数组合并 |

依赖

该包依赖以下库(会自动安装):

  • ky - HTTP 客户端
  • @baidu/ky-sse-hook - SSE 响应处理钩子