Skip to content

Commit c040ad8

Browse files
author
linyuan.yang
committed
channel.yuanbao
1 parent b105a1b commit c040ad8

23 files changed

Lines changed: 1192 additions & 10 deletions

‎.vscode/launch.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"${workspaceFolder}/packages/channel.xiaoai/dist/**/*.js",
2424
"${workspaceFolder}/packages/channel.dingtalk/dist/**/*.js",
2525
"${workspaceFolder}/packages/channel.qq/dist/**/*.js",
26+
"${workspaceFolder}/packages/channel.yuanbao/dist/**/*.js",
2627
"${workspaceFolder}/packages/channel.remote-agent/dist/**/*.js",
2728
"${workspaceFolder}/packages/wiki.base/dist/**/*.js",
2829
"${workspaceFolder}/packages/wiki.local/dist/**/*.js",

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ docker compose pull && docker compose up -d # upgrade to latest image
146146
- **Agenda** — Conversation-driven reminders, schedules, and routines with absolute / interval / cron triggers; optionally synced from the conversation after every turn and delivered to any session or channel
147147
- **Heartbeat** — Configurable periodic self-activation lets agents run scheduled prompts proactively across any channel
148148
- **MCP tools** — Standard MCP protocol (stdio/SSE), connect to any MCP tool ecosystem; per-agent and global servers with auto-restart
149-
- **Multiple channels** — Web UI, CLI, Lark/Feishu, Slack, WeCom, WeChat, DingTalk, QQ (official bot), OneBot (v11 reverse WebSocket), XiaoAI, REST API, WebSocket
149+
- **Multiple channels** — Web UI, CLI, Lark/Feishu, Slack, WeCom, WeChat, DingTalk, QQ (official bot), Tencent Yuanbao, OneBot (v11 reverse WebSocket), XiaoAI, REST API, WebSocket
150150
- **Built-in tools** — Shell execution, file system, archive operations, media file read, Python/PowerShell inline execution, web fetch/download, cron scheduler, todo, ask
151151
- **Skills** — Installable prompt modules with remote install from Clawhub, skills.sh, and skillhub.cn
152152
- **Agent Store** — Browse and install pre-packaged agents (model + prompt + tools + skills + MCP servers) from configurable sources
@@ -166,7 +166,7 @@ Full guide, with step-by-step setup and per-feature reference, lives at
166166
- **Storage & knowledge** — [Savers](https://while-coder.github.io/sbot/guide/savers) · [Notes](https://while-coder.github.io/sbot/guide/note) · [Wiki](https://while-coder.github.io/sbot/guide/wiki)
167167
- **Automation** — [Memory](https://while-coder.github.io/sbot/guide/memory) · [Agenda](https://while-coder.github.io/sbot/guide/agenda) · [Heartbeat](https://while-coder.github.io/sbot/guide/heartbeat)
168168
- **Tools & skills** — [Built-in Tools](https://while-coder.github.io/sbot/guide/tools) · [MCP Tools](https://while-coder.github.io/sbot/guide/mcp) · [Skills](https://while-coder.github.io/sbot/guide/skills)
169-
- **Channels** — [Channels](https://while-coder.github.io/sbot/guide/channels) (Lark/Feishu · Slack · WeCom · WeChat · DingTalk · QQ · OneBot · XiaoAI)
169+
- **Channels** — [Channels](https://while-coder.github.io/sbot/guide/channels) (Lark/Feishu · Slack · WeCom · WeChat · DingTalk · QQ · Yuanbao · OneBot · XiaoAI)
170170

171171
---
172172

‎README.zh.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ docker compose pull && docker compose up -d # 升级到最新镜像
145145
- **日程(Agenda)** — 由对话驱动的提醒、日程与周期任务,支持 absolute / interval / cron 触发器;可每轮对话后从对话自动同步,并投递到任意会话或渠道
146146
- **心跳唤醒** — 可配置定时提示词,让 Agent 在任意渠道周期性主动发起任务
147147
- **MCP 工具** — 标准 MCP 协议(stdio/SSE),接入任意 MCP 工具生态;支持全局与 Agent 级独立配置,故障自动重启
148-
- **多渠道接入** — Web UI、CLI、飞书/Lark、Slack、企业微信、微信、钉钉、QQ(官方机器人)、OneBot(v11 反向 WebSocket)、小爱音箱、REST API、WebSocket
148+
- **多渠道接入** — Web UI、CLI、飞书/Lark、Slack、企业微信、微信、钉钉、QQ(官方机器人)、腾讯元宝、OneBot(v11 反向 WebSocket)、小爱音箱、REST API、WebSocket
149149
- **内置工具** — Shell 执行、文件系统、归档操作、媒体文件读取、Python/PowerShell 内联执行、Web 抓取/下载、Cron 调度、待办事项
150150
- **技能系统** — 可安装的 Prompt 模块,支持从 Clawhub、skills.sh、skillhub.cn 远程安装
151151
- **Agent 商店** — 浏览并一键安装预打包 Agent(含模型、Prompt、工具、技能、MCP 服务器配置),支持自定义源
@@ -165,7 +165,7 @@ docker compose pull && docker compose up -d # 升级到最新镜像
165165
- **存储与知识** — [存储](https://while-coder.github.io/sbot/zh/guide/savers) · [笔记](https://while-coder.github.io/sbot/zh/guide/note) · [知识库](https://while-coder.github.io/sbot/zh/guide/wiki)
166166
- **自动化** — [记忆](https://while-coder.github.io/sbot/zh/guide/memory) · [日程](https://while-coder.github.io/sbot/zh/guide/agenda) · [心跳唤醒](https://while-coder.github.io/sbot/zh/guide/heartbeat)
167167
- **工具与技能** — [内置工具](https://while-coder.github.io/sbot/zh/guide/tools) · [MCP 工具](https://while-coder.github.io/sbot/zh/guide/mcp) · [技能](https://while-coder.github.io/sbot/zh/guide/skills)
168-
- **渠道** — [渠道配置](https://while-coder.github.io/sbot/zh/guide/channels)(飞书/Lark · Slack · 企业微信 · 微信 · 钉钉 · QQ · OneBot · 小爱)
168+
- **渠道** — [渠道配置](https://while-coder.github.io/sbot/zh/guide/channels)(飞书/Lark · Slack · 企业微信 · 微信 · 钉钉 · QQ · 元宝 · OneBot · 小爱)
169169

170170
---
171171

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"test": "pnpm -r --if-present run test",
77
"build:core": "pnpm --filter scorpio.di run build && pnpm --filter scorpio.ai run build && pnpm --filter sbot.plugin run build && pnpm --filter sbot.commons run build",
88
"build:wiki": "pnpm --filter wiki.base run build && pnpm --filter wiki.local run build && pnpm --filter wiki.gdrive run build",
9-
"build:channel": "pnpm --filter channel.base run build && pnpm --filter channel.lark run build && pnpm --filter channel.slack run build && pnpm --filter channel.wecom run build && pnpm --filter channel.wechat run build && pnpm --filter channel.onebot run build && pnpm --filter channel.xiaoai run build && pnpm --filter channel.dingtalk run build && pnpm --filter channel.qq run build && pnpm --filter channel.remote-agent run build",
9+
"build:channel": "pnpm --filter channel.base run build && pnpm --filter channel.lark run build && pnpm --filter channel.slack run build && pnpm --filter channel.wecom run build && pnpm --filter channel.wechat run build && pnpm --filter channel.onebot run build && pnpm --filter channel.xiaoai run build && pnpm --filter channel.dingtalk run build && pnpm --filter channel.qq run build && pnpm --filter channel.yuanbao run build && pnpm --filter channel.remote-agent run build",
1010
"build:deps": "pnpm run build:core && pnpm run build:wiki && pnpm run build:channel",
1111
"assets": "pnpm --filter shared-assets run distribute",
1212

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/node_modules
2+
/dist
3+
/package-lock.json
4+
/tsconfig.tsbuildinfo
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "channel.yuanbao",
3+
"version": "0.0.1",
4+
"description": "Tencent Yuanbao Bot channel integration (protobuf WebSocket)",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
7+
"scripts": {
8+
"build": "rimraf dist && tsc -b --force"
9+
},
10+
"dependencies": {
11+
"axios": "catalog:",
12+
"channel.base": "workspace:*",
13+
"protobufjs": "^7.5.4",
14+
"ws": "catalog:"
15+
},
16+
"private": true,
17+
"devDependencies": {
18+
"@types/node": "catalog:",
19+
"@types/ws": "catalog:",
20+
"rimraf": "catalog:",
21+
"typescript": "catalog:"
22+
}
23+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { AbstractChatProvider, GlobalLoggerService, parseMessages2Text } from 'channel.base';
2+
import type { YuanbaoService } from './YuanbaoService';
3+
4+
const getLogger = () => GlobalLoggerService.getLogger('YuanbaoChatProvider.ts');
5+
6+
export class YuanbaoChatProvider extends AbstractChatProvider {
7+
private sessionId = '';
8+
private sent = false;
9+
10+
constructor(private readonly service: YuanbaoService) {
11+
super();
12+
}
13+
14+
init(sessionId: string): this {
15+
this.sessionId = sessionId;
16+
return this;
17+
}
18+
19+
async flush(): Promise<void> {
20+
if (this.sent || !this.sessionId) return;
21+
this.sent = true;
22+
try {
23+
const text = parseMessages2Text(this.getDisplayMessages()).trim();
24+
if (text) await this.service.sendTextToSession(this.sessionId, text);
25+
} catch (error: any) {
26+
getLogger()?.error(`flush exception: ${error?.message ?? error}`, error?.stack);
27+
}
28+
}
29+
30+
protected onMessagesUpdated(): void {
31+
// 元宝不支持原地编辑;完成后一次性发送。
32+
}
33+
}
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
import { randomBytes, randomUUID } from 'node:crypto';
2+
import { Field, Root, Type } from 'protobufjs';
3+
4+
export interface ConnHead {
5+
cmdType: number;
6+
cmd: string;
7+
seqNo: number;
8+
msgId: string;
9+
module: string;
10+
needAck?: boolean;
11+
status?: number;
12+
}
13+
14+
export interface ConnMessage {
15+
head: ConnHead;
16+
data: Buffer;
17+
}
18+
19+
export interface YuanbaoMessageElement {
20+
msgType: string;
21+
msgContent: {
22+
text?: string;
23+
uuid?: string;
24+
imageFormat?: number;
25+
data?: string;
26+
desc?: string;
27+
imageInfoArray?: Array<{ type: number; size: number; width: number; height: number; url: string }>;
28+
url?: string;
29+
fileSize?: number;
30+
fileName?: string;
31+
};
32+
}
33+
34+
const root = new Root();
35+
36+
function type(name: string, fields: Array<[string, number, string, 'repeated'?]>): Type {
37+
const value = new Type(name);
38+
for (const [fieldName, id, fieldType, rule] of fields) {
39+
value.add(new Field(fieldName, id, fieldType, rule));
40+
}
41+
return value;
42+
}
43+
44+
const conn = root.define('trpc.yuanbao.conn_common');
45+
conn.add(type('Head', [
46+
['cmdType', 1, 'uint32'], ['cmd', 2, 'string'], ['seqNo', 3, 'uint32'],
47+
['msgId', 4, 'string'], ['module', 5, 'string'], ['needAck', 6, 'bool'],
48+
['status', 10, 'int32'],
49+
]));
50+
conn.add(type('ConnMsg', [['head', 1, 'Head'], ['data', 2, 'bytes']]));
51+
conn.add(type('AuthInfo', [['uid', 1, 'string'], ['source', 2, 'string'], ['token', 3, 'string']]));
52+
conn.add(type('DeviceInfo', [['instanceId', 10, 'string']]));
53+
conn.add(type('AuthBindReq', [
54+
['bizId', 1, 'string'], ['authInfo', 2, 'AuthInfo'], ['deviceInfo', 3, 'DeviceInfo'],
55+
['envName', 5, 'string'], ['bindMode', 6, 'uint32'], ['forceToken', 7, 'string'],
56+
]));
57+
conn.add(type('AuthBindRsp', [
58+
['code', 1, 'int32'], ['message', 2, 'string'], ['connectId', 3, 'string'],
59+
['timestamp', 4, 'uint64'], ['clientIp', 5, 'string'],
60+
]));
61+
conn.add(type('PingReq', []));
62+
conn.add(type('PingRsp', [['heartInterval', 1, 'uint32'], ['timestamp', 2, 'uint64']]));
63+
conn.add(type('KickoutMsg', [['status', 1, 'int32'], ['reason', 2, 'string'], ['otherDeviceName', 3, 'string']]));
64+
65+
const biz = root.define('trpc.yuanbao.yuanbao_conn.yuanbao_openclaw_proxy');
66+
biz.add(type('ImImageInfoArray', [
67+
['type', 1, 'uint32'], ['size', 2, 'uint32'], ['width', 3, 'uint32'],
68+
['height', 4, 'uint32'], ['url', 5, 'string'],
69+
]));
70+
biz.add(type('MsgContent', [
71+
['text', 1, 'string'], ['uuid', 2, 'string'], ['imageFormat', 3, 'uint32'],
72+
['data', 4, 'string'], ['desc', 5, 'string'], ['imageInfoArray', 8, 'ImImageInfoArray', 'repeated'],
73+
['url', 10, 'string'], ['fileSize', 11, 'uint32'], ['fileName', 12, 'string'],
74+
]));
75+
biz.add(type('MsgBodyElement', [['msgType', 1, 'string'], ['msgContent', 2, 'MsgContent']]));
76+
biz.add(type('SendC2CMessageReq', [
77+
['msgId', 1, 'string'], ['toAccount', 2, 'string'], ['fromAccount', 3, 'string'],
78+
['msgRandom', 4, 'uint32'], ['msgBody', 5, 'MsgBodyElement', 'repeated'],
79+
['groupCode', 6, 'string'], ['msgSeq', 7, 'uint64'],
80+
]));
81+
biz.add(type('SendGroupMessageReq', [
82+
['msgId', 1, 'string'], ['groupCode', 2, 'string'], ['fromAccount', 3, 'string'],
83+
['toAccount', 4, 'string'], ['random', 5, 'string'],
84+
['msgBody', 6, 'MsgBodyElement', 'repeated'], ['refMsgId', 7, 'string'], ['msgSeq', 8, 'uint64'],
85+
]));
86+
biz.add(type('SendPrivateHeartbeatReq', [
87+
['fromAccount', 1, 'string'], ['toAccount', 2, 'string'], ['heartbeat', 3, 'int32'],
88+
]));
89+
biz.add(type('SendGroupHeartbeatReq', [
90+
['fromAccount', 1, 'string'], ['toAccount', 2, 'string'], ['groupCode', 3, 'string'],
91+
['sendTime', 4, 'int64'], ['heartbeat', 5, 'int32'],
92+
]));
93+
root.resolveAll();
94+
95+
const CONN_MSG = 'trpc.yuanbao.conn_common.ConnMsg';
96+
const AUTH_BIND_REQ = 'trpc.yuanbao.conn_common.AuthBindReq';
97+
const AUTH_BIND_RSP = 'trpc.yuanbao.conn_common.AuthBindRsp';
98+
const PING_REQ = 'trpc.yuanbao.conn_common.PingReq';
99+
const PING_RSP = 'trpc.yuanbao.conn_common.PingRsp';
100+
const KICKOUT_MSG = 'trpc.yuanbao.conn_common.KickoutMsg';
101+
const BIZ = 'trpc.yuanbao.yuanbao_conn.yuanbao_openclaw_proxy';
102+
103+
export const CMD_TYPE_REQUEST = 0;
104+
export const CMD_TYPE_RESPONSE = 1;
105+
export const CMD_TYPE_PUSH = 2;
106+
export const CMD_TYPE_PUSH_ACK = 3;
107+
export const CMD_AUTH_BIND = 'auth-bind';
108+
export const CMD_PING = 'ping';
109+
export const CMD_KICKOUT = 'kickout';
110+
111+
let seq = 0;
112+
113+
function nextSeq(): number {
114+
seq = (seq + 1) % 0x80000000;
115+
return seq;
116+
}
117+
118+
function messageId(): string {
119+
return randomUUID().replace(/-/g, '');
120+
}
121+
122+
function randomUint32(): number {
123+
return randomBytes(4).readUInt32BE(0);
124+
}
125+
126+
function encode(typeName: string, value: Record<string, unknown>): Buffer {
127+
const messageType = root.lookupType(typeName);
128+
const error = messageType.verify(value);
129+
if (error) throw new Error(`${typeName}: ${error}`);
130+
return Buffer.from(messageType.encode(messageType.create(value)).finish());
131+
}
132+
133+
function decode(typeName: string, data: Uint8Array): any {
134+
const messageType = root.lookupType(typeName);
135+
return messageType.toObject(messageType.decode(data), {
136+
longs: String,
137+
bytes: Buffer,
138+
defaults: false,
139+
});
140+
}
141+
142+
function encodeConn(head: ConnHead, data?: Buffer): Buffer {
143+
return encode(CONN_MSG, data?.length ? { head, data } : { head });
144+
}
145+
146+
export function decodeConn(raw: Uint8Array): ConnMessage {
147+
const decoded = decode(CONN_MSG, raw);
148+
return {
149+
head: decoded.head as ConnHead,
150+
data: decoded.data ? Buffer.from(decoded.data) : Buffer.alloc(0),
151+
};
152+
}
153+
154+
export function buildAuthBindMessage(uid: string, source: string, token: string): Buffer {
155+
const data = encode(AUTH_BIND_REQ, {
156+
bizId: 'ybBot',
157+
authInfo: { uid, source, token },
158+
deviceInfo: { instanceId: '16' },
159+
});
160+
return encodeConn({
161+
cmdType: CMD_TYPE_REQUEST, cmd: CMD_AUTH_BIND, seqNo: nextSeq(),
162+
msgId: messageId(), module: 'conn_access',
163+
}, data);
164+
}
165+
166+
export function buildPingMessage(): Buffer {
167+
return encodeConn({
168+
cmdType: CMD_TYPE_REQUEST, cmd: CMD_PING, seqNo: nextSeq(),
169+
msgId: messageId(), module: 'conn_access',
170+
}, encode(PING_REQ, {}));
171+
}
172+
173+
export function buildPushAck(head: ConnHead): Buffer {
174+
return encodeConn({
175+
cmdType: CMD_TYPE_PUSH_ACK,
176+
cmd: head.cmd,
177+
seqNo: nextSeq(),
178+
msgId: head.msgId,
179+
module: head.module,
180+
});
181+
}
182+
183+
function buildBusinessMessage(cmd: string, data: Buffer): Buffer {
184+
return encodeConn({
185+
cmdType: CMD_TYPE_REQUEST, cmd, seqNo: nextSeq(),
186+
msgId: messageId(), module: 'yuanbao_openclaw_proxy',
187+
}, data);
188+
}
189+
190+
export function buildSendMessage(
191+
chatType: 'c2c' | 'group',
192+
targetId: string,
193+
fromAccount: string,
194+
elements: YuanbaoMessageElement[],
195+
): Buffer {
196+
if (chatType === 'group') {
197+
return buildBusinessMessage('send_group_message', encode(`${BIZ}.SendGroupMessageReq`, {
198+
groupCode: targetId,
199+
fromAccount,
200+
random: String(randomUint32()),
201+
msgBody: elements,
202+
}));
203+
}
204+
return buildBusinessMessage('send_c2c_message', encode(`${BIZ}.SendC2CMessageReq`, {
205+
toAccount: targetId,
206+
fromAccount,
207+
msgRandom: randomUint32(),
208+
msgBody: elements,
209+
}));
210+
}
211+
212+
export function buildTypingMessage(
213+
chatType: 'c2c' | 'group',
214+
targetId: string,
215+
fromAccount: string,
216+
senderId: string,
217+
heartbeat: 1 | 2,
218+
): Buffer {
219+
if (chatType === 'group') {
220+
return buildBusinessMessage('send_group_heartbeat', encode(`${BIZ}.SendGroupHeartbeatReq`, {
221+
fromAccount, toAccount: senderId, groupCode: targetId, sendTime: 0, heartbeat,
222+
}));
223+
}
224+
return buildBusinessMessage('send_private_heartbeat', encode(`${BIZ}.SendPrivateHeartbeatReq`, {
225+
fromAccount, toAccount: targetId, heartbeat,
226+
}));
227+
}
228+
229+
export const decodeAuthBindResponse = (data: Uint8Array): any => decode(AUTH_BIND_RSP, data);
230+
export const decodePingResponse = (data: Uint8Array): any => decode(PING_RSP, data);
231+
export const decodeKickoutMessage = (data: Uint8Array): any => decode(KICKOUT_MSG, data);

0 commit comments

Comments
 (0)