qq-bot-roost 1.0.15 → 1.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/docs//345/256/277/344/270/273/345/215/217/350/256/256v1.md +33 -0
- package/package.json +58 -58
- package/src/api.js +32 -3
- package/src/bot.js +2 -0
- package/src/host/ext-bridge.js +140 -0
- package/src/host/index.js +12 -0
- package/src/host/media.js +25 -1
- package/src/host/methods.js +970 -756
- package/src/onebot/voice.js +53 -0
- package/src/outbound.js +32 -12
- package/src/plugin/context.js +23 -1
- package/src/plugin/loader.js +9 -5
- package/src/semver.js +19 -0
package/README.md
CHANGED
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
|
|
9
9
|
<img src="https://img.shields.io/badge/Node.js-22.13%2B-339933?logo=node.js&logoColor=white" alt="Node.js 22.13+">
|
|
10
10
|
<img src="https://img.shields.io/badge/dependencies-0-brightgreen" alt="零依赖">
|
|
11
|
-
<img src="https://img.shields.io/badge/tests-
|
|
11
|
+
<img src="https://img.shields.io/badge/tests-1079%20passed-brightgreen" alt="测试 1079 项">
|
|
12
12
|
<img src="https://img.shields.io/badge/plugins-JS%20%7C%20Python%20%7C%20Go%20%7C%20any-blue" alt="多语言插件">
|
|
13
|
-
<img src="https://img.shields.io/badge/version-1.0.
|
|
13
|
+
<img src="https://img.shields.io/badge/version-1.0.16-blueviolet" alt="版本 1.0.16">
|
|
14
14
|
<img src="https://img.shields.io/badge/license-AGPL--3.0--only-blue" alt="AGPL-3.0-only">
|
|
15
15
|
<a href="https://github.com/tdcq9594/qq-bot-roost/actions"><img src="https://img.shields.io/github/actions/workflow/status/tdcq9594/qq-bot-roost/ci.yml?branch=master" alt="CI"></a>
|
|
16
16
|
|
|
@@ -230,6 +230,9 @@ data: {"at":1731400000000}
|
|
|
230
230
|
| | `plugin.data.*` / `plugin.groups.*` | read ~ manage | 插件存档、按群开关 |
|
|
231
231
|
| | `plugin.market` / `plugin.versions` / `plugin.updates` | read | 市场、本地版本、可更新 |
|
|
232
232
|
| | `plugin.install/remove/update/update.all/rescan/load/unload/forget` | manage | 插件全生命周期 |
|
|
233
|
+
| 扩展联动 | `ext.list` | read | 列出 DSH 已装扩展暴露的动作 |
|
|
234
|
+
| | `ext.call` | message | 调一个扩展动作(SSE 请求 + RPC 回执) |
|
|
235
|
+
| | `ext.register` / `ext.call.reply` | message | DSH 上报清单 / 回传结果(内部) |
|
|
233
236
|
| 配置 | `config.get` | read | 脱敏后的机器人配置 |
|
|
234
237
|
| | `config.set` | manage | 改运行时设置(见下) |
|
|
235
238
|
|
|
@@ -400,3 +403,33 @@ data: {"at":1731400000000}
|
|
|
400
403
|
|
|
401
404
|
两个出口**共用同一份 `ws` 依赖与同一张「依赖」卡片**:装一次,两边都能用。
|
|
402
405
|
外接页上半部分是宿主协议,下半部分仍是 OneBot。
|
|
406
|
+
|
|
407
|
+
## 7. 反向联动:框架插件 → DSH 扩展
|
|
408
|
+
|
|
409
|
+
`tool.*` 解决的是「DSH 调框架插件工具」,方向是客户端到服务端。反过来
|
|
410
|
+
「框架插件调 DSH 已装扩展」需要一条回程,但不新开端口、不给 DSH 发第二套
|
|
411
|
+
令牌——复用协议已有的两条通道:
|
|
412
|
+
|
|
413
|
+
```
|
|
414
|
+
框架插件 ctx.ext.call('bili', 'refresh_hot')
|
|
415
|
+
│
|
|
416
|
+
├─ ext.call 方法 → 生成 requestId → SSE 事件 ext.call.request ──► DSH
|
|
417
|
+
│ │
|
|
418
|
+
│ DSH 按 ui.actions 白名单执行进程内动作
|
|
419
|
+
│ │
|
|
420
|
+
◄──────────── RPC ext.call.reply {requestId, result} ──────────┘
|
|
421
|
+
│
|
|
422
|
+
└─ pending Promise resolve → 结果回到插件
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
- 能力清单由 DSH 连上后调 `ext.register` 上报,内容直接取自扩展
|
|
426
|
+
`plugin.json` 的 `ui.actions`,**不新增声明格式**;只有
|
|
427
|
+
`ui.runtime: "inprocess"` 的扩展能被同步调用(MCP 子进程的 stdin 管道
|
|
428
|
+
不属于宿主)。
|
|
429
|
+
- 每个请求带 30 秒超时;认不出的 `requestId` 一律拒绝,防止旧回执解掉新
|
|
430
|
+
请求;协议停止时 `clear()` 把还在等的调用全部解掉,插件不会挂在 `await` 上。
|
|
431
|
+
- 框架插件侧入口是 `ctx.ext.list()` / `ctx.ext.call(extId, action, payload)`;
|
|
432
|
+
没接外接协议时 `available === false`,`call()` 明确抛错,不静默失败。
|
|
433
|
+
- DSH 侧对外同样提供 `GET /v1/roost.capabilities` 与 `POST /v1/roost.call`:
|
|
434
|
+
扩展可经宿主代理调用只读能力、框架插件的 `tool.*` 与 `msg.send`,
|
|
435
|
+
框架地址与令牌不出宿主,写/管理面不开放。
|
package/package.json
CHANGED
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "qq-bot-roost",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"type": "module",
|
|
5
|
-
"description": "栖 Roost:零依赖的 QQ 官方机器人框架,填 AppID/AppSecret 即连,插件可写任意语言",
|
|
6
|
-
"license": "AGPL-3.0-only",
|
|
7
|
-
"repository": {
|
|
8
|
-
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/tdcq9594/qq-bot-roost.git"
|
|
10
|
-
},
|
|
11
|
-
"bugs": {
|
|
12
|
-
"url": "https://github.com/tdcq9594/qq-bot-roost/issues"
|
|
13
|
-
},
|
|
14
|
-
"homepage": "https://github.com/tdcq9594/qq-bot-roost#readme",
|
|
15
|
-
"keywords": [
|
|
16
|
-
"qq-bot",
|
|
17
|
-
"qqbot",
|
|
18
|
-
"bot-framework",
|
|
19
|
-
"qq-official-bot",
|
|
20
|
-
"roost",
|
|
21
|
-
"multi-bot",
|
|
22
|
-
"plugin",
|
|
23
|
-
"zero-dependency"
|
|
24
|
-
],
|
|
25
|
-
"engines": {
|
|
26
|
-
"node": ">=22.13"
|
|
27
|
-
},
|
|
28
|
-
"scripts": {
|
|
29
|
-
"start": "node src/main.js",
|
|
30
|
-
"mock": "node src/main.js --mock",
|
|
31
|
-
"check": "node src/main.js --check",
|
|
32
|
-
"test": "node test/selftest.mjs && node test/qrlogin.mjs && node test/deps.mjs && node --test test/trust.mjs && node --test test/manifest.mjs && node --test test/market-cache.mjs && node --test test/market-kind.mjs && node --test test/instance-proc.mjs && node --test test/proxy.mjs && node --test test/trace.mjs && node --test test/fuzz-tar.mjs && node --test test/group-name.mjs && node --test test/identity.mjs && node --test test/identity-commands.mjs && node --test test/mail.mjs && node --test test/ledger-outbound.mjs && node --test test/api-record.mjs && node --test test/reply-auto.mjs && node --test test/plugin-observability.mjs && node --test test/outbound-dry.mjs && node --test test/ext-deps-migrate.mjs && node --test test/onebot-deps.mjs && node --test test/onebot-config.mjs && node --test test/onebot-segments.mjs && node --test test/onebot-outbound-at.mjs && node --test test/onebot-translate.mjs && node --test test/onebot-voice.mjs && node --test test/onebot-loopback.mjs && node --test test/host-protocol.mjs && node --test test/host-config.mjs && node --test test/host-tool-methods.mjs && node --test test/web-token-echo.mjs",
|
|
33
|
-
"test:e2e": "node test/e2e.mjs",
|
|
34
|
-
"test:all": "node test/selftest.mjs && node test/qrlogin.mjs && node test/deps.mjs && node --test test/trust.mjs && node --test test/manifest.mjs && node --test test/market-cache.mjs && node --test test/market-kind.mjs && node --test test/instance-proc.mjs && node --test test/proxy.mjs && node --test test/trace.mjs && node --test test/fuzz-tar.mjs && node --test test/group-name.mjs && node --test test/identity.mjs && node --test test/identity-commands.mjs && node --test test/mail.mjs && node --test test/ledger-outbound.mjs && node --test test/api-record.mjs && node --test test/reply-auto.mjs && node test/e2e.mjs && node test/live-driver.mjs && node --test test/ext-deps-migrate.mjs && node --test test/onebot-deps.mjs && node --test test/onebot-config.mjs && node --test test/onebot-segments.mjs && node --test test/onebot-outbound-at.mjs && node --test test/onebot-translate.mjs && node --test test/onebot-voice.mjs && node --test test/onebot-loopback.mjs && node --test test/host-protocol.mjs && node --test test/host-config.mjs && node --test test/host-tool-methods.mjs && node --test test/web-token-echo.mjs",
|
|
35
|
-
"check:secrets": "node scripts/check-secrets.mjs",
|
|
36
|
-
"create": "node scripts/create-plugin.mjs",
|
|
37
|
-
"test:plugin": "node scripts/test-plugin.mjs",
|
|
38
|
-
"live": "node scripts/live-test.mjs",
|
|
39
|
-
"market:index": "node scripts/build-market-index.mjs",
|
|
40
|
-
"sums": "node scripts/build-sums.mjs",
|
|
41
|
-
"sums:check": "node scripts/build-sums.mjs --check",
|
|
42
|
-
"sums:verify": "node scripts/build-sums.mjs --verify",
|
|
43
|
-
"test:coverage": "node --experimental-test-coverage --test-coverage-lines=95 --test-coverage-branches=80 --test-coverage-include=src/trust.js --test-coverage-include=src/plugin/manifest.js --test-coverage-include=src/trace.js --test test/trust.mjs test/manifest.mjs test/trace.mjs"
|
|
44
|
-
},
|
|
45
|
-
"author": {
|
|
46
|
-
"name": "倦飞之鸟"
|
|
47
|
-
},
|
|
48
|
-
"files": [
|
|
49
|
-
"src",
|
|
50
|
-
"web",
|
|
51
|
-
"sdk",
|
|
52
|
-
"types",
|
|
53
|
-
"docs",
|
|
54
|
-
"config/*.example.json",
|
|
55
|
-
"README.md",
|
|
56
|
-
"LICENSE"
|
|
57
|
-
]
|
|
58
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "qq-bot-roost",
|
|
3
|
+
"version": "1.0.16",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "栖 Roost:零依赖的 QQ 官方机器人框架,填 AppID/AppSecret 即连,插件可写任意语言",
|
|
6
|
+
"license": "AGPL-3.0-only",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/tdcq9594/qq-bot-roost.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/tdcq9594/qq-bot-roost/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/tdcq9594/qq-bot-roost#readme",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"qq-bot",
|
|
17
|
+
"qqbot",
|
|
18
|
+
"bot-framework",
|
|
19
|
+
"qq-official-bot",
|
|
20
|
+
"roost",
|
|
21
|
+
"multi-bot",
|
|
22
|
+
"plugin",
|
|
23
|
+
"zero-dependency"
|
|
24
|
+
],
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=22.13"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"start": "node src/main.js",
|
|
30
|
+
"mock": "node src/main.js --mock",
|
|
31
|
+
"check": "node src/main.js --check",
|
|
32
|
+
"test": "node test/selftest.mjs && node test/qrlogin.mjs && node test/deps.mjs && node --test test/trust.mjs && node --test test/manifest.mjs && node --test test/market-cache.mjs && node --test test/market-kind.mjs && node --test test/instance-proc.mjs && node --test test/proxy.mjs && node --test test/trace.mjs && node --test test/fuzz-tar.mjs && node --test test/group-name.mjs && node --test test/identity.mjs && node --test test/identity-commands.mjs && node --test test/mail.mjs && node --test test/ledger-outbound.mjs && node --test test/api-record.mjs && node --test test/reply-auto.mjs && node --test test/plugin-observability.mjs && node --test test/outbound-dry.mjs && node --test test/ext-deps-migrate.mjs && node --test test/onebot-deps.mjs && node --test test/onebot-config.mjs && node --test test/onebot-segments.mjs && node --test test/onebot-outbound-at.mjs && node --test test/onebot-translate.mjs && node --test test/onebot-voice.mjs && node --test test/onebot-loopback.mjs && node --test test/host-protocol.mjs && node --test test/host-config.mjs && node --test test/host-tool-methods.mjs && node --test test/host-ext-bridge.mjs && node --test test/web-token-echo.mjs && node --test test/group-manage.mjs && node --test test/wakeup-payload.mjs && node --test test/typing.mjs && node --test test/media-convert.mjs",
|
|
33
|
+
"test:e2e": "node test/e2e.mjs",
|
|
34
|
+
"test:all": "node test/selftest.mjs && node test/qrlogin.mjs && node test/deps.mjs && node --test test/trust.mjs && node --test test/manifest.mjs && node --test test/market-cache.mjs && node --test test/market-kind.mjs && node --test test/instance-proc.mjs && node --test test/proxy.mjs && node --test test/trace.mjs && node --test test/fuzz-tar.mjs && node --test test/group-name.mjs && node --test test/identity.mjs && node --test test/identity-commands.mjs && node --test test/mail.mjs && node --test test/ledger-outbound.mjs && node --test test/api-record.mjs && node --test test/reply-auto.mjs && node test/e2e.mjs && node test/live-driver.mjs && node --test test/ext-deps-migrate.mjs && node --test test/onebot-deps.mjs && node --test test/onebot-config.mjs && node --test test/onebot-segments.mjs && node --test test/onebot-outbound-at.mjs && node --test test/onebot-translate.mjs && node --test test/onebot-voice.mjs && node --test test/onebot-loopback.mjs && node --test test/host-protocol.mjs && node --test test/host-config.mjs && node --test test/host-tool-methods.mjs && node --test test/host-ext-bridge.mjs && node --test test/web-token-echo.mjs && node --test test/group-manage.mjs && node --test test/wakeup-payload.mjs && node --test test/typing.mjs && node --test test/media-convert.mjs",
|
|
35
|
+
"check:secrets": "node scripts/check-secrets.mjs",
|
|
36
|
+
"create": "node scripts/create-plugin.mjs",
|
|
37
|
+
"test:plugin": "node scripts/test-plugin.mjs",
|
|
38
|
+
"live": "node scripts/live-test.mjs",
|
|
39
|
+
"market:index": "node scripts/build-market-index.mjs",
|
|
40
|
+
"sums": "node scripts/build-sums.mjs",
|
|
41
|
+
"sums:check": "node scripts/build-sums.mjs --check",
|
|
42
|
+
"sums:verify": "node scripts/build-sums.mjs --verify",
|
|
43
|
+
"test:coverage": "node --experimental-test-coverage --test-coverage-lines=95 --test-coverage-branches=80 --test-coverage-include=src/trust.js --test-coverage-include=src/plugin/manifest.js --test-coverage-include=src/trace.js --test test/trust.mjs test/manifest.mjs test/trace.mjs"
|
|
44
|
+
},
|
|
45
|
+
"author": {
|
|
46
|
+
"name": "倦飞之鸟"
|
|
47
|
+
},
|
|
48
|
+
"files": [
|
|
49
|
+
"src",
|
|
50
|
+
"web",
|
|
51
|
+
"sdk",
|
|
52
|
+
"types",
|
|
53
|
+
"docs",
|
|
54
|
+
"config/*.example.json",
|
|
55
|
+
"README.md",
|
|
56
|
+
"LICENSE"
|
|
57
|
+
]
|
|
58
|
+
}
|
package/src/api.js
CHANGED
|
@@ -122,7 +122,8 @@ const PASSIVE_MAX_SEQ = 5;
|
|
|
122
122
|
* 构造发送报文。抽成纯函数是为了能单测——`#payload` 是私有方法,测试摸不到。
|
|
123
123
|
*
|
|
124
124
|
* @param {object} opts 驼峰字段:content / msgType / msgId / msgSeq / eventId /
|
|
125
|
-
* markdown / keyboard / media / card / ark / messageReference
|
|
125
|
+
* markdown / keyboard / media / card / ark / messageReference /
|
|
126
|
+
* isWakeup(互动召回,官方独立额度)
|
|
126
127
|
* @param {(msgId:string)=>number} nextSeq 取下一个 msg_seq
|
|
127
128
|
* @returns {object} 官方要的 snake_case 报文
|
|
128
129
|
*/
|
|
@@ -133,6 +134,7 @@ export function buildSendPayload(opts = {}, nextSeq = () => 1) {
|
|
|
133
134
|
msgId,
|
|
134
135
|
msgSeq,
|
|
135
136
|
eventId,
|
|
137
|
+
isWakeup,
|
|
136
138
|
markdown,
|
|
137
139
|
keyboard,
|
|
138
140
|
media,
|
|
@@ -153,6 +155,13 @@ export function buildSendPayload(opts = {}, nextSeq = () => 1) {
|
|
|
153
155
|
* 与 msg_id 二选一。
|
|
154
156
|
*/
|
|
155
157
|
if (eventId) payload.event_id = eventId;
|
|
158
|
+
/**
|
|
159
|
+
* 4.6 互动召回(官方口径):用户主动对话后 30 天内,当天 / 1–3 天 /
|
|
160
|
+
* 3–7 天 / 7–30 天四个窗口各 1 条。带上它服务端不校验
|
|
161
|
+
* msg_id / event_id 有效期,也不算普通主动推送的额度。
|
|
162
|
+
* 上游(dsh-roost)按窗口记账,这里只负责把声明透传出去。
|
|
163
|
+
*/
|
|
164
|
+
if (isWakeup === true) payload.is_wakeup = true;
|
|
156
165
|
if (markdown) {
|
|
157
166
|
// 官方:填了 markdown 后 content 必须为空,否则 40034008 / 304061
|
|
158
167
|
payload.content = '';
|
|
@@ -176,7 +185,27 @@ export function buildSendPayload(opts = {}, nextSeq = () => 1) {
|
|
|
176
185
|
* 留着空串跟「类型与内容不匹配」(22006)只差一步。
|
|
177
186
|
*/
|
|
178
187
|
if (card) {
|
|
179
|
-
|
|
188
|
+
/**
|
|
189
|
+
* 官方 msg_type=8 的 `card` 必须是
|
|
190
|
+
* `{type:'tuwen', content:{title, description, url, pic_url}}`。
|
|
191
|
+
*
|
|
192
|
+
* 但协议 / 扩展路径(`/v1/roost.call` → `msg.send`、`/v1/send`)给的
|
|
193
|
+
* 一直是扁平四字段 `{title, description, url, picUrl}`;直接透传官方回
|
|
194
|
+
* `card msg is nil`,消息根本发不出去,面板上就是「点了没反应」。
|
|
195
|
+
* `msg.replyTuwen()` 早已构造好嵌套结构,这里只补扁平那一种。
|
|
196
|
+
*/
|
|
197
|
+
const normalized = card?.type === 'tuwen' && card?.content && typeof card.content === 'object'
|
|
198
|
+
? card
|
|
199
|
+
: {
|
|
200
|
+
type: 'tuwen',
|
|
201
|
+
content: {
|
|
202
|
+
title: String(card?.title ?? ''),
|
|
203
|
+
description: String(card?.description ?? ''),
|
|
204
|
+
url: String(card?.url ?? ''),
|
|
205
|
+
pic_url: String(card?.picUrl ?? card?.pic_url ?? ''),
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
payload.card = normalized;
|
|
180
209
|
payload.msg_type = 8;
|
|
181
210
|
payload.content = '';
|
|
182
211
|
}
|
|
@@ -1092,7 +1121,7 @@ export class QQApi {
|
|
|
1092
1121
|
if (!this.groupInvited) {
|
|
1093
1122
|
throw new Error(
|
|
1094
1123
|
'群管内邀功能未开启。群成员列表 / 踢人 / 群黑名单需要官方白名单,' +
|
|
1095
|
-
'
|
|
1124
|
+
'申请通过后到 Roost 面板(浏览器打开的那个)「配置 → 群管能力」打开',
|
|
1096
1125
|
);
|
|
1097
1126
|
}
|
|
1098
1127
|
}
|
package/src/bot.js
CHANGED
|
@@ -303,6 +303,8 @@ export class BotRuntime {
|
|
|
303
303
|
/** 身份对照表:插件通过 ctx.identity 只读查询(QQ号 → openid) */
|
|
304
304
|
identity: this.identity,
|
|
305
305
|
isOwner: (uid) => cfg.owners.includes(uid),
|
|
306
|
+
/** 外接协议桥:给插件 `ctx.ext.*`,让它能调 DSH 侧已装扩展的能力 */
|
|
307
|
+
host: this.host,
|
|
306
308
|
// 插件声明了 frameworkVersion 就做兼容校验;作者本地调试可以关掉
|
|
307
309
|
ignoreCompat: this.globalCfg.plugins?.ignoreFrameworkVersion === true,
|
|
308
310
|
});
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 框架插件 ↔ DSH 扩展:反向调用桥
|
|
3
|
+
* ================================
|
|
4
|
+
*
|
|
5
|
+
* 直连协议(roost-host/1)里 DSH 是客户端、框架是服务端,所以「扩展调框架
|
|
6
|
+
* 插件工具」天然走得通(`tool.*`);反过来「框架插件调扩展」需要一条回程。
|
|
7
|
+
*
|
|
8
|
+
* 不新开端口、不给 DSH 发令牌:复用协议已有的两条通道——
|
|
9
|
+
*
|
|
10
|
+
* 框架 --SSE 事件 `ext.call.request`--> DSH
|
|
11
|
+
* 框架 <--HTTP RPC `ext.call.reply`-- DSH
|
|
12
|
+
*
|
|
13
|
+
* 这一层只管「请求 → 等回执」的配对,是纯逻辑,不碰网络:
|
|
14
|
+
*
|
|
15
|
+
* · 每个请求一个 id,带超时(DSH 没连上 / 扩展卡住都要能失败,不能挂死);
|
|
16
|
+
* · 回执只能认自己发出去的 id,杜绝别的客户端伪造结果;
|
|
17
|
+
* · 能力清单由 DSH 连上后上报(`ext.register`),框架不主动扫盘。
|
|
18
|
+
*
|
|
19
|
+
* 为什么扩展能力不投影成 `tool.*`:`tool.*` 的 scope 是 `message`,谁拿到
|
|
20
|
+
* 协议令牌都能调;而扩展动作可能带写操作(发消息、评论),应当只在框架
|
|
21
|
+
* 插件明确调用时才发生,且由 DSH 自己按 `ui.actions` 白名单把着。
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { HostError } from './methods.js';
|
|
25
|
+
|
|
26
|
+
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
27
|
+
const MAX_PENDING = 32;
|
|
28
|
+
const MAX_PAYLOAD_KEYS = 40;
|
|
29
|
+
|
|
30
|
+
export function createExtBridge({ pushEvent = () => {}, timeoutMs = DEFAULT_TIMEOUT_MS, log = () => {} } = {}) {
|
|
31
|
+
/** requestId -> { resolve, reject, timer, at } */
|
|
32
|
+
const pending = new Map();
|
|
33
|
+
/** DSH 上报的扩展能力清单:{ extId, actions:[{id,label}] }[] */
|
|
34
|
+
let catalog = [];
|
|
35
|
+
let catalogAt = 0;
|
|
36
|
+
let seq = 0;
|
|
37
|
+
|
|
38
|
+
function fail(p, e) {
|
|
39
|
+
clearTimeout(p.timer);
|
|
40
|
+
pending.delete(p.id);
|
|
41
|
+
p.reject(e);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 发起一次反向调用。
|
|
46
|
+
*
|
|
47
|
+
* @returns {Promise<any>} DSH 回执里的 result
|
|
48
|
+
*/
|
|
49
|
+
async function call({ extId = '', action = '', payload = null, chat = '' } = {}) {
|
|
50
|
+
const id = String(extId ?? '').trim();
|
|
51
|
+
const act = String(action ?? '').trim();
|
|
52
|
+
if (!id || !act) throw new HostError('BAD_PARAMS', '要给 extId 与 action', 400);
|
|
53
|
+
if (pending.size >= MAX_PENDING) {
|
|
54
|
+
throw new HostError('BUSY', `同时最多 ${MAX_PENDING} 个扩展调用,稍后再试`, 429);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const requestId = `exc-${Date.now().toString(36)}-${++seq}`;
|
|
58
|
+
/** payload 只收一层对象,超量的键直接截断,避免把整份配置塞进事件流 */
|
|
59
|
+
let args = null;
|
|
60
|
+
if (payload && typeof payload === 'object' && !Array.isArray(payload)) {
|
|
61
|
+
args = Object.fromEntries(Object.entries(payload).slice(0, MAX_PAYLOAD_KEYS));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const promise = new Promise((resolve, reject) => {
|
|
65
|
+
/**
|
|
66
|
+
* **不要 unref**:这个定时器是请求生命周期本身的一部分。unref 之后
|
|
67
|
+
* 若进程里暂时没有别的活跃 handle(例如框架正在停机、或单测环境),
|
|
68
|
+
* 超时永远不触发,调用方会永久挂在 await 上。停机路径由
|
|
69
|
+
* `HostBridge.#stopNow()` 调 `clear()` 统一解掉,不会拖住退出。
|
|
70
|
+
*/
|
|
71
|
+
const timer = setTimeout(() => {
|
|
72
|
+
const p = pending.get(requestId);
|
|
73
|
+
if (p) fail(p, new HostError('TIMEOUT', `扩展 ${id}.${act} 没在 ${timeoutMs}ms 内回执`, 504));
|
|
74
|
+
}, timeoutMs);
|
|
75
|
+
pending.set(requestId, { id: requestId, resolve, reject, timer, at: Date.now() });
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
try {
|
|
79
|
+
pushEvent('ext.call.request', {
|
|
80
|
+
requestId, extId: id, action: act, payload: args,
|
|
81
|
+
/** 调用方所在会话,DSH 侧要按它派生扩展的上下文;可为空 */
|
|
82
|
+
chat: String(chat ?? '').slice(0, 120),
|
|
83
|
+
});
|
|
84
|
+
} catch (e) {
|
|
85
|
+
const p = pending.get(requestId);
|
|
86
|
+
if (p) fail(p, new HostError('INTERNAL', `推不出扩展调用事件:${e?.message ?? e}`, 500));
|
|
87
|
+
}
|
|
88
|
+
return await promise;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** DSH 通过 RPC 回执。认不出 id 一律拒绝,别让旧回执解掉新请求 */
|
|
92
|
+
function reply({ requestId = '', ok = true, result = null, error = '' } = {}) {
|
|
93
|
+
const key = String(requestId ?? '').trim();
|
|
94
|
+
const p = pending.get(key);
|
|
95
|
+
if (!p) throw new HostError('NOT_FOUND', '没有这个待回执的扩展调用(可能已超时)', 404);
|
|
96
|
+
clearTimeout(p.timer);
|
|
97
|
+
pending.delete(key);
|
|
98
|
+
if (ok === false) {
|
|
99
|
+
p.reject(new HostError('EXT_FAILED', String(error ?? '扩展调用失败').slice(0, 300), 502));
|
|
100
|
+
return { ok: true, settled: key, failed: true };
|
|
101
|
+
}
|
|
102
|
+
p.resolve(result ?? null);
|
|
103
|
+
return { ok: true, settled: key };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** DSH 连上 / 扩展装卸后上报能力清单 */
|
|
107
|
+
function register({ extensions = [] } = {}) {
|
|
108
|
+
const list = Array.isArray(extensions) ? extensions : [];
|
|
109
|
+
catalog = list
|
|
110
|
+
.filter((e) => e && typeof e.id === 'string' && e.id)
|
|
111
|
+
.map((e) => ({
|
|
112
|
+
extId: e.id.slice(0, 64),
|
|
113
|
+
actions: (Array.isArray(e.actions) ? e.actions : [])
|
|
114
|
+
.filter((a) => a && typeof a.id === 'string' && a.id)
|
|
115
|
+
.slice(0, 60)
|
|
116
|
+
.map((a) => ({ id: a.id.slice(0, 80), label: String(a.label ?? a.id).slice(0, 120) })),
|
|
117
|
+
}));
|
|
118
|
+
catalogAt = Date.now();
|
|
119
|
+
log(`扩展能力清单已更新:${catalog.length} 个扩展 / ${catalog.reduce((n, e) => n + e.actions.length, 0)} 个动作`);
|
|
120
|
+
return { ok: true, count: catalog.length };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
call,
|
|
125
|
+
reply,
|
|
126
|
+
register,
|
|
127
|
+
list: () => ({
|
|
128
|
+
extensions: catalog.map((e) => ({ extId: e.extId, actions: e.actions.map((a) => ({ ...a })) })),
|
|
129
|
+
updatedAt: catalogAt,
|
|
130
|
+
pending: pending.size,
|
|
131
|
+
}),
|
|
132
|
+
/** 断线时把还在等的请求全部解掉,别让框架插件永远挂在 await 上 */
|
|
133
|
+
clear: (why = '链路断开') => {
|
|
134
|
+
for (const p of [...pending.values()]) {
|
|
135
|
+
fail(p, new HostError('DISCONNECTED', `${why},扩展调用已取消`, 503));
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
pendingCount: () => pending.size,
|
|
139
|
+
};
|
|
140
|
+
}
|
package/src/host/index.js
CHANGED
|
@@ -26,6 +26,7 @@ import { createLogger } from '../logger.js';
|
|
|
26
26
|
import * as deps from '../ext-deps.js';
|
|
27
27
|
import { createHostServer } from './server.js';
|
|
28
28
|
import { createMethods, effectiveScopes, HostError, HOST_PROTOCOL } from './methods.js';
|
|
29
|
+
import { createExtBridge } from './ext-bridge.js';
|
|
29
30
|
import { serializeMessage, serializeNotice } from './serialize.js';
|
|
30
31
|
|
|
31
32
|
const log = createLogger('host');
|
|
@@ -70,10 +71,19 @@ export class HostBridge {
|
|
|
70
71
|
* 才知道这个协议能干什么;createMethods 只捕获 runtime 引用,
|
|
71
72
|
* 里面的 R.api / R.plugins 都是调用时现读,早建不会拿到过期对象。
|
|
72
73
|
*/
|
|
74
|
+
/**
|
|
75
|
+
* 反向调用桥:框架插件调 DSH 扩展。事件走协议自己的 SSE 流,
|
|
76
|
+
* 回执走 RPC,所以不新开监听、不给 DSH 发第二套令牌。
|
|
77
|
+
*/
|
|
78
|
+
this.extBridge = createExtBridge({
|
|
79
|
+
pushEvent: (event, data) => this.#push(event, data),
|
|
80
|
+
log: (m) => log.info(`[ext-bridge] ${m}`),
|
|
81
|
+
});
|
|
73
82
|
this.methods = createMethods({
|
|
74
83
|
runtime: this.runtime,
|
|
75
84
|
getState: () => this.#state(),
|
|
76
85
|
log: (m) => log.info(m),
|
|
86
|
+
extBridge: this.extBridge,
|
|
77
87
|
});
|
|
78
88
|
/** 插件工具集签名:变了就往事件流推一条 capabilities.changed */
|
|
79
89
|
this.toolSig = this.methods?.toolSignature?.() ?? '';
|
|
@@ -219,6 +229,8 @@ export class HostBridge {
|
|
|
219
229
|
await this.server.stop().catch((e) => log.warn(`关监听失败:${e.message}`));
|
|
220
230
|
this.server = null;
|
|
221
231
|
}
|
|
232
|
+
/** 监听关了就没人能回执了,把还在等的扩展调用全部解掉,别挂死插件 */
|
|
233
|
+
this.extBridge?.clear?.('外接协议已停止');
|
|
222
234
|
if (this.state === 'running') this.state = 'stopped';
|
|
223
235
|
this.port = 0;
|
|
224
236
|
return this;
|
package/src/host/media.js
CHANGED
|
@@ -26,7 +26,7 @@ import path from 'node:path';
|
|
|
26
26
|
import { FILE_TYPE, IMAGE_CONTENT_TYPES } from '../api.js';
|
|
27
27
|
import { netFetch } from '../proxy.js';
|
|
28
28
|
import * as deps from '../ext-deps.js';
|
|
29
|
-
import { toSilk, uploadVoice, MAX_VOICE_BYTES } from '../onebot/voice.js';
|
|
29
|
+
import { toSilk, toSilkDry, uploadVoice, MAX_VOICE_BYTES } from '../onebot/voice.js';
|
|
30
30
|
import { sendOut, receivedAtOf } from './dispatch.js';
|
|
31
31
|
|
|
32
32
|
/** 官方所有富媒体类型的硬上限;会话累计超过就拒,别等 API 报 850031 */
|
|
@@ -288,6 +288,30 @@ export function registerMedia(def, { R, err, log = () => {} }) {
|
|
|
288
288
|
return { id: data.id, msgId: data.id, ...(data.pending ? { pending: true } : {}), ...(data.requestId ? { requestId: data.requestId } : {}), upload: { via: up.via, size: up.size } };
|
|
289
289
|
});
|
|
290
290
|
|
|
291
|
+
/**
|
|
292
|
+
* 5.3 语音试听:把「即将发出的那一份字节」过一遍 SILK 编码校验。
|
|
293
|
+
*
|
|
294
|
+
* 与 `media.send` 用同一个 `onebot/voice.js` 的编码实现,但**到编码为止**:
|
|
295
|
+
* 不 `uploadVoice`、不 `sendOut`、不进投递账本、不占主动额度、不写任何盘。
|
|
296
|
+
* 也刻意不调 `ensureSilk()`:试听是「听一下」,不该偷偷开始装 62MB 依赖。
|
|
297
|
+
*
|
|
298
|
+
* 老框架(没有这个方法)时宿主侧会拿到 NOT_FOUND,按「未校验」处理,
|
|
299
|
+
* 试听本身照样能播——不因为框架没升级就把试听按钮弄成灰的。
|
|
300
|
+
*/
|
|
301
|
+
def('media.convert', 'message', '只校验语音能否转成 QQ 的 SILK(不上传、不发送、不装依赖、不落盘)', async (p) => {
|
|
302
|
+
const buf = decodeB64(p.dataBase64, err);
|
|
303
|
+
/** `cached()` / `load()` 都只取已装好的模块,不触发安装 */
|
|
304
|
+
const silkMod = deps.cached('silk-wasm') || await deps.load('silk-wasm');
|
|
305
|
+
if (!silkMod) {
|
|
306
|
+
return { ok: false, reason: 'silk_missing', message: '没装 silk-wasm:真正发语音时框架会自动安装,试听不装' };
|
|
307
|
+
}
|
|
308
|
+
try {
|
|
309
|
+
return await toSilkDry(buf, silkMod);
|
|
310
|
+
} catch (e) {
|
|
311
|
+
return { ok: false, reason: 'convert_failed', message: String(e?.message || e) };
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
|
|
291
315
|
def('media.markdownImage', 'message', '上传 Markdown 素材图,返回可复用的 qqbot-media:// 引用(默认保留)', async (p) => {
|
|
292
316
|
return await uploadInput(p, {
|
|
293
317
|
scene: p.scene === 'group' ? 'group' : 'c2c',
|