@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 响应处理钩子
