@cmach_socket/opencode-qq-bot 0.6.7 → 0.6.8
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 +71 -65
- package/chat-overrides.example.json +7 -0
- package/package.json +3 -1
- package/src/config.ts +20 -4
- package/src/index.ts +34 -7
- package/src/overrides.ts +3 -3
- package/src/seed.ts +31 -0
- package/src/session/manager.ts +43 -1
- package/src/session/soul.ts +4 -10
- package/src/tools/policy-guard.ts +3 -0
- package/src/tools/policy.ts +3 -3
- package/src/tools/qq.ts +72 -0
- package/tool-profiles.example.json +21 -0
package/README.md
CHANGED
|
@@ -71,65 +71,66 @@ bun install
|
|
|
71
71
|
插件不会读取项目 `.env`。如果选择用环境变量配置,必须把变量注入 OpenCode 服务进程本身,
|
|
72
72
|
可参考 `.env.example`;`cp .env.example .env` 不会生效。
|
|
73
73
|
|
|
74
|
-
| 选项 | 环境变量 | 默认值
|
|
75
|
-
| ----------------------------------- | ---------------------------------- |
|
|
76
|
-
| `napcat.url` | `NAPCAT_WS_URL` | 无(必填)
|
|
77
|
-
| `napcat.token` | `NAPCAT_WS_TOKEN` | 无
|
|
78
|
-
| `access.allow.groups` | `QQ_ALLOW_GROUPS` | `[]`
|
|
79
|
-
| `access.allow.users` | `QQ_ALLOW_USERS` | `[]`
|
|
80
|
-
| `access.deny.groups` | `QQ_DENY_GROUPS` | `[]`
|
|
81
|
-
| `access.deny.users` | `QQ_DENY_USERS` | `[]`
|
|
82
|
-
| `access.allowAllWhenEmpty` | `QQ_ALLOW_ALL_WHEN_EMPTY` | `false`
|
|
83
|
-
| `ownerQQ` | `QQ_OWNER_QQ` | 无
|
|
84
|
-
| `admins` | `QQ_ADMINS` | `[]`
|
|
85
|
-
| `toolProfilesPath` | `QQ_TOOL_PROFILES` | `tool-profiles.json` |
|
|
86
|
-
| `chatOverridesPath` | `QQ_CHAT_OVERRIDES` | `chat-overrides.json` |
|
|
87
|
-
| `soulPath` | `QQ_BOT_SOUL_PATH` | 工作目录 `souls/`
|
|
88
|
-
| `soulName` | `QQ_BOT_SOUL_NAME` | `default.txt`
|
|
89
|
-
| `home` | `QQ_BOT_HOME` | XDG 工作区
|
|
90
|
-
| `agent` | `QQ_BOT_AGENT` | OpenCode 默认
|
|
91
|
-
| `model` | `QQ_BOT_MODEL` | OpenCode 默认
|
|
92
|
-
| `sendDelayMs` | `QQ_SEND_DELAY_MS` | `1000`
|
|
93
|
-
| `warnLength` | `QQ_WARN_LENGTH` | `500`
|
|
94
|
-
| `reuseSession` | `QQ_REUSE_SESSION` | `false`
|
|
95
|
-
| `proactive.enabled` | `QQ_PROACTIVE_ENABLED` | `true`
|
|
96
|
-
| `proactive.silenceThresholdSeconds` | `QQ_PROACTIVE_SILENCE_SECONDS` | `10800`
|
|
97
|
-
| `proactive.startProbability` | `QQ_PROACTIVE_P_START` | `0.01`
|
|
98
|
-
| `proactive.probabilityPerHour` | `QQ_PROACTIVE_P_SLOPE_PER_HOUR` | `0.05`
|
|
99
|
-
| `proactive.tickSeconds` | `QQ_PROACTIVE_TICK_SECONDS` | `3600`
|
|
100
|
-
| `proactive.allowGroups` | `QQ_PROACTIVE_ALLOW_GROUPS` | `[]`
|
|
101
|
-
| `proactive.allowUsers` | `QQ_PROACTIVE_ALLOW_USERS` | `[]`
|
|
102
|
-
| `wake.prefixes` | `QQ_WAKE_PREFIXES` | `["/"]`
|
|
103
|
-
| `wake.ignoreAtAll` | `QQ_WAKE_IGNORE_AT_ALL` | `true`
|
|
104
|
-
| `wake.replyToSelf` | `QQ_WAKE_REPLY_TO_SELF` | `true`
|
|
105
|
-
| `wake.ignoreAtOthers` | `QQ_WAKE_IGNORE_AT_OTHERS` | `true`
|
|
106
|
-
| `reply.minDelayMs` | `QQ_REPLY_MIN_DELAY_MS` | `0`
|
|
107
|
-
| `reply.maxDelayMs` | `QQ_REPLY_MAX_DELAY_MS` | `0`
|
|
108
|
-
| `reply.segmentChars` | `QQ_REPLY_SEGMENT_CHARS` | `150`
|
|
109
|
-
| `reply.forwardThresholdChars` | `QQ_REPLY_FORWARD_THRESHOLD_CHARS` | `1500`
|
|
110
|
-
| `
|
|
111
|
-
| `trace.
|
|
112
|
-
| `trace.
|
|
113
|
-
| `trace.
|
|
114
|
-
| `
|
|
115
|
-
| `reminder.
|
|
116
|
-
| `
|
|
117
|
-
| `keepAlive.
|
|
118
|
-
| `
|
|
119
|
-
| `
|
|
120
|
-
| `rateLimit.
|
|
121
|
-
| `rateLimit.
|
|
122
|
-
| `rateLimit.
|
|
123
|
-
| `rateLimit.
|
|
124
|
-
| `
|
|
125
|
-
| `
|
|
126
|
-
| `events.
|
|
127
|
-
| `events.
|
|
128
|
-
| `
|
|
129
|
-
| `log.
|
|
130
|
-
| `log.
|
|
131
|
-
| `log.
|
|
132
|
-
| `
|
|
74
|
+
| 选项 | 环境变量 | 默认值 | 说明 |
|
|
75
|
+
| ----------------------------------- | ---------------------------------- | ------------------------------ | --------------------------------------------------- |
|
|
76
|
+
| `napcat.url` | `NAPCAT_WS_URL` | 无(必填) | NapCat 正向 WebSocket 地址 |
|
|
77
|
+
| `napcat.token` | `NAPCAT_WS_TOKEN` | 无 | NapCat 访问令牌 |
|
|
78
|
+
| `access.allow.groups` | `QQ_ALLOW_GROUPS` | `[]` | 放行的群号,逗号分隔;为空时看 `allowAllWhenEmpty` |
|
|
79
|
+
| `access.allow.users` | `QQ_ALLOW_USERS` | `[]` | 放行的私聊 QQ 号,逗号分隔 |
|
|
80
|
+
| `access.deny.groups` | `QQ_DENY_GROUPS` | `[]` | 拒绝的群号,优先于放行名单 |
|
|
81
|
+
| `access.deny.users` | `QQ_DENY_USERS` | `[]` | 拒绝的私聊 QQ 号,优先于放行名单 |
|
|
82
|
+
| `access.allowAllWhenEmpty` | `QQ_ALLOW_ALL_WHEN_EMPTY` | `false` | 放行名单为空时是否放行全部目标 |
|
|
83
|
+
| `ownerQQ` | `QQ_OWNER_QQ` | 无 | 所有者 QQ:管理命令只在它的私聊生效,该私聊始终放行 |
|
|
84
|
+
| `admins` | `QQ_ADMINS` | `[]` | 管理员 QQ 号:拥有全部工具权限,私聊还需在放行名单 |
|
|
85
|
+
| `toolProfilesPath` | `QQ_TOOL_PROFILES` | 工作目录 `tool-profiles.json` | 工具配置文件路径,相对工作目录 |
|
|
86
|
+
| `chatOverridesPath` | `QQ_CHAT_OVERRIDES` | 工作目录 `chat-overrides.json` | 会话行为覆盖文件路径,相对工作目录 |
|
|
87
|
+
| `soulPath` | `QQ_BOT_SOUL_PATH` | 工作目录 `souls/` | 角色设定文件或目录路径,相对工作目录 |
|
|
88
|
+
| `soulName` | `QQ_BOT_SOUL_NAME` | `default.txt` | 工作目录 `souls` 里的角色设定文件名 |
|
|
89
|
+
| `home` | `QQ_BOT_HOME` | XDG 工作区 | 机器人会话的工作目录,必须是绝对路径或 `~/` |
|
|
90
|
+
| `agent` | `QQ_BOT_AGENT` | OpenCode 默认 | 会话使用的 agent |
|
|
91
|
+
| `model` | `QQ_BOT_MODEL` | OpenCode 默认 | 会话使用的模型,格式 `provider/model[/variant]` |
|
|
92
|
+
| `sendDelayMs` | `QQ_SEND_DELAY_MS` | `1000` | 超长消息分段发送时每段之间的间隔 |
|
|
93
|
+
| `warnLength` | `QQ_WARN_LENGTH` | `500` | 单次发送的内容长度超过该值时告警 |
|
|
94
|
+
| `reuseSession` | `QQ_REUSE_SESSION` | `false` | 没有保存的绑定时,按会话标记找回上一次的会话 |
|
|
95
|
+
| `proactive.enabled` | `QQ_PROACTIVE_ENABLED` | `true` | 是否启用主动说话 |
|
|
96
|
+
| `proactive.silenceThresholdSeconds` | `QQ_PROACTIVE_SILENCE_SECONDS` | `10800` | 沉默多久后才开始计算主动说话概率 |
|
|
97
|
+
| `proactive.startProbability` | `QQ_PROACTIVE_P_START` | `0.01` | 刚跨过阈值时的概率 |
|
|
98
|
+
| `proactive.probabilityPerHour` | `QQ_PROACTIVE_P_SLOPE_PER_HOUR` | `0.05` | 每多沉默一小时概率的增量 |
|
|
99
|
+
| `proactive.tickSeconds` | `QQ_PROACTIVE_TICK_SECONDS` | `3600` | 心跳间隔,必须大于 0 |
|
|
100
|
+
| `proactive.allowGroups` | `QQ_PROACTIVE_ALLOW_GROUPS` | `[]` | 只在这些群里主动说话,逗号分隔;留空表示不限制 |
|
|
101
|
+
| `proactive.allowUsers` | `QQ_PROACTIVE_ALLOW_USERS` | `[]` | 只在这些私聊里主动说话,逗号分隔;留空表示不限制 |
|
|
102
|
+
| `wake.prefixes` | `QQ_WAKE_PREFIXES` | `["/"]` | 全局唤醒前缀,逗号分隔;命中就必须回复 |
|
|
103
|
+
| `wake.ignoreAtAll` | `QQ_WAKE_IGNORE_AT_ALL` | `true` | 是否忽略 @全体成员 |
|
|
104
|
+
| `wake.replyToSelf` | `QQ_WAKE_REPLY_TO_SELF` | `true` | 回复机器人自己的消息是否必须回复 |
|
|
105
|
+
| `wake.ignoreAtOthers` | `QQ_WAKE_IGNORE_AT_OTHERS` | `true` | 群里第一个 @ 指向别人时是否不唤醒机器人 |
|
|
106
|
+
| `reply.minDelayMs` | `QQ_REPLY_MIN_DELAY_MS` | `0` | 分段之间的随机等待下限(毫秒) |
|
|
107
|
+
| `reply.maxDelayMs` | `QQ_REPLY_MAX_DELAY_MS` | `0` | 分段之间的随机等待上限(毫秒),`0` 表示用固定间隔 |
|
|
108
|
+
| `reply.segmentChars` | `QQ_REPLY_SEGMENT_CHARS` | `150` | 单段最多多少字,超出就拆成多段发送 |
|
|
109
|
+
| `reply.forwardThresholdChars` | `QQ_REPLY_FORWARD_THRESHOLD_CHARS` | `1500` | 超过多少字改用合并转发,`0` 表示不转换 |
|
|
110
|
+
| `reply.requirePermission` | `QQ_REPLY_REQUIRE_PERMISSION` | `true` | 群里发消息前是否要求模型先说明理由(私聊始终开放) |
|
|
111
|
+
| `trace.enabled` | `QQ_TRACE_ENABLED` | `true` | 是否记录调用链追踪 |
|
|
112
|
+
| `trace.dir` | `QQ_TRACE_DIR` | 与日志同目录 | 追踪文件目录 |
|
|
113
|
+
| `trace.maxSizeMb` | `QQ_TRACE_MAX_SIZE_MB` | `80` | 追踪文件总容量上限(MB) |
|
|
114
|
+
| `trace.retentionDays` | `QQ_TRACE_RETENTION_DAYS` | `7` | 追踪文件保留天数 |
|
|
115
|
+
| `reminder.enabled` | `QQ_REMINDER_ENABLED` | `true` | 模型一轮没调用发送工具时是否提醒 |
|
|
116
|
+
| `reminder.max` | `QQ_REMINDER_MAX` | `2` | 连续提醒次数上限 |
|
|
117
|
+
| `keepAlive.enabled` | `QQ_KEEP_ALIVE_ENABLED` | `true` | 是否启用保活心跳 |
|
|
118
|
+
| `keepAlive.seconds` | `QQ_KEEP_ALIVE_SECONDS` | `1200` | 保活心跳间隔(秒),必须大于 0 |
|
|
119
|
+
| `history.count` | `QQ_HISTORY_COUNT` | `20` | 新建或恢复会话时补多少条历史,`0` 表示关闭 |
|
|
120
|
+
| `rateLimit.maxPerMinute` | `QQ_RATE_MAX_PER_MINUTE` | `8` | 每分钟最多发多少条消息,`0` 表示不限制 |
|
|
121
|
+
| `rateLimit.maxPerHour` | `QQ_RATE_MAX_PER_HOUR` | `60` | 每小时最多发多少条消息,`0` 表示不限制 |
|
|
122
|
+
| `rateLimit.minGapMs` | `QQ_RATE_MIN_GAP_MS` | `1500` | 相邻两次发送之间的最小间隔(毫秒) |
|
|
123
|
+
| `rateLimit.stall` | `QQ_RATE_STALL` | `false` | 触顶时等窗口结束再发,而不是直接拒绝 |
|
|
124
|
+
| `rateLimit.stallMaxWaitMs` | `QQ_RATE_STALL_MAX_WAIT_MS` | `60000` | 最多等多久(毫秒),超过就直接拒绝 |
|
|
125
|
+
| `sensitive.enabled` | `QQ_SENSITIVE_ENABLED` | `true` | 拦截含凭证、令牌、私钥的出站消息 |
|
|
126
|
+
| `events.subscribe` | `QQ_EVENT_SUBSCRIBE` | `message` | 订阅的事件类别:`message` / `notice` |
|
|
127
|
+
| `events.notices` | `QQ_EVENT_NOTICES` | `[]` | `notice` 里关心哪些通知,留空表示全收 |
|
|
128
|
+
| `events.inject` | `QQ_EVENT_INJECT` | `true` | 通知是否注入会话上下文 |
|
|
129
|
+
| `log.level` | `QQ_LOG_LEVEL` | `info` | 日志级别:`debug` / `info` / `warn` / `error` |
|
|
130
|
+
| `log.dir` | `QQ_LOG_DIR` | XDG 日志目录 | 日志文件目录,不存在时自动创建 |
|
|
131
|
+
| `log.maxSize` | `QQ_LOG_MAX_SIZE` | `5m` | 单个日志文件的大小上限 |
|
|
132
|
+
| `log.maxFiles` | `QQ_LOG_MAX_FILES` | `5` | 保留多少个历史日志文件 |
|
|
133
|
+
| `media.allowedDirs` | `QQ_MEDIA_ALLOWED_DIRS` | `[]` | 额外允许发送图片、语音的目录 |
|
|
133
134
|
|
|
134
135
|
没配置 `access.allow` 时插件不会响应任何消息(默认全部关闭);拒绝名单 `access.deny`
|
|
135
136
|
里的目标一律不响应,且优先于放行名单。详见下面「准入判定」。
|
|
@@ -228,6 +229,7 @@ OpenCode 按 location 统计会话活动,默认空闲 60 分钟后卸载该 lo
|
|
|
228
229
|
|
|
229
230
|
| 工具 | 说明 |
|
|
230
231
|
| -------------------------- | ------------------------------------------------------------------------------------------- |
|
|
232
|
+
| `qq_request_reply` | 申请本轮的回复权限,必须说明为什么要回复;权限只在本轮有效 |
|
|
231
233
|
| `qq_send_message` | 发消息。`content` 文本,`reply=false` 表示已读不回,`unlimit_length=true` 解除长度告警 |
|
|
232
234
|
| `qq_send_image` | 发图片,`file` 传路径或 URL,`base64` 传 `base64://` 数据 |
|
|
233
235
|
| `qq_send_record` | 发语音 |
|
|
@@ -246,8 +248,9 @@ OpenCode 按 location 统计会话活动,默认空闲 60 分钟后卸载该 lo
|
|
|
246
248
|
|
|
247
249
|
### 工具权限与配置档
|
|
248
250
|
|
|
249
|
-
工具按「发送者角色 +
|
|
250
|
-
(`toolProfilesPath`
|
|
251
|
+
工具按「发送者角色 + 聊天」分配,配置写在机器人工作目录的 `tool-profiles.json`
|
|
252
|
+
(`toolProfilesPath` 可改路径)。文件不存在时启动会把随包的示例复制过去,
|
|
253
|
+
里面的 `group:100` / `private:200` 是占位目标,改成自己的群和私聊才生效:
|
|
251
254
|
|
|
252
255
|
| 角色 | 使用的配置档 |
|
|
253
256
|
| ------------------ | ---------------------------------------------- |
|
|
@@ -280,8 +283,8 @@ OpenCode 按 location 统计会话活动,默认空闲 60 分钟后卸载该 lo
|
|
|
280
283
|
### 会话覆盖
|
|
281
284
|
|
|
282
285
|
每个群 / 私聊可以单独改唤醒方式、要不要主动说话、用哪个 agent / 模型 / 角色设定,
|
|
283
|
-
|
|
284
|
-
(`chatOverridesPath`
|
|
286
|
+
配置写在机器人工作目录的 `chat-overrides.json`
|
|
287
|
+
(`chatOverridesPath` 可改路径,文件不存在时启动会把随包示例复制过去,里面的聊天号同样是占位):
|
|
285
288
|
|
|
286
289
|
```json
|
|
287
290
|
{
|
|
@@ -430,6 +433,9 @@ QQ 私聊(owner / 管理员)用 `/emoji-label`,TUI 用加前缀的 `/qq-em
|
|
|
430
433
|
## 行为
|
|
431
434
|
|
|
432
435
|
- **回复是强制的**:系统提示要求模型每轮至少调用一个发送类工具,直接输出的文字会被丢弃。
|
|
436
|
+
- **发消息前先说明理由**:群里默认不给模型发送权限,要回复必须先调用 `qq_request_reply`
|
|
437
|
+
说明为什么要回复,拿到权限后才能发送;权限只在本轮有效。私聊默认开放,不用申请。
|
|
438
|
+
关闭 `reply.requirePermission` 后模型可以直接发消息。
|
|
433
439
|
- **被 @ 才必须回**:群里没被 @ 时模型可以调用 `qq_send_message` 并设 `reply=false` 选择已读不回;私聊消息必须回复。
|
|
434
440
|
每个群可以用会话覆盖改这条规则(每条必回 / 前缀必回 / 完全不理),详见「会话覆盖」。
|
|
435
441
|
- **消息合并**:会话正在跑模型时新到的消息进缓存,会话空闲后合并成一轮再投进去。
|
|
@@ -487,8 +493,8 @@ GitHub Release。版本号策略、发布清单与回滚步骤见 [docs/release.
|
|
|
487
493
|
|
|
488
494
|
```
|
|
489
495
|
souls/ 随包分发的初始角色设定,首次启动复制到机器人工作目录
|
|
490
|
-
tool-profiles.
|
|
491
|
-
chat-overrides.
|
|
496
|
+
tool-profiles.example.json 工具权限配置档示例,首次启动复制到工作目录
|
|
497
|
+
chat-overrides.example.json 每会话行为覆盖示例,首次启动复制到工作目录
|
|
492
498
|
src/
|
|
493
499
|
access.ts 准入判定:放行 / 拒绝名单与放行规则
|
|
494
500
|
config.ts 配置合并与校验
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cmach_socket/opencode-qq-bot",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.8",
|
|
4
4
|
"description": "OpenCode V2 plugin bridging a QQ group chat (NapCat via NapLink) to an OpenCode session.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -22,6 +22,8 @@
|
|
|
22
22
|
"files": [
|
|
23
23
|
"src",
|
|
24
24
|
"souls",
|
|
25
|
+
"tool-profiles.example.json",
|
|
26
|
+
"chat-overrides.example.json",
|
|
25
27
|
"NOTICE",
|
|
26
28
|
"README.md"
|
|
27
29
|
],
|
package/src/config.ts
CHANGED
|
@@ -65,6 +65,8 @@ export interface ReplyConfig {
|
|
|
65
65
|
segmentChars: number
|
|
66
66
|
/** 单条回复超过该字数就改用合并转发,0 表示关闭。 */
|
|
67
67
|
forwardThresholdChars: number
|
|
68
|
+
/** 是否要求模型先说明理由才能发送:默认开启。 */
|
|
69
|
+
requirePermission: boolean
|
|
68
70
|
}
|
|
69
71
|
|
|
70
72
|
/** 调用链追踪配置。 */
|
|
@@ -158,9 +160,9 @@ export interface QQBotConfig {
|
|
|
158
160
|
ownerQQ?: number
|
|
159
161
|
/** 管理员 QQ 号:拥有全部工具权限。 */
|
|
160
162
|
admins: number[]
|
|
161
|
-
/**
|
|
163
|
+
/** 工具配置文件路径;相对路径按机器人工作目录解析,空表示工作目录下的 `tool-profiles.json`。 */
|
|
162
164
|
toolProfilesPath?: string
|
|
163
|
-
/**
|
|
165
|
+
/** 会话行为覆盖文件路径;相对路径按机器人工作目录解析,空表示工作目录下的 `chat-overrides.json`。 */
|
|
164
166
|
chatOverridesPath?: string
|
|
165
167
|
/** 角色设定文件路径;相对路径按机器人工作目录解析,默认取工作目录里的 souls/。 */
|
|
166
168
|
soulPath?: string
|
|
@@ -215,7 +217,13 @@ export const DEFAULT_CONFIG: Omit<QQBotConfig, "napcat"> = {
|
|
|
215
217
|
allowUsers: [],
|
|
216
218
|
},
|
|
217
219
|
wake: { prefixes: ["/"], ignoreAtAll: true, replyToSelf: true, ignoreAtOthers: true },
|
|
218
|
-
reply: {
|
|
220
|
+
reply: {
|
|
221
|
+
minDelayMs: 0,
|
|
222
|
+
maxDelayMs: 0,
|
|
223
|
+
segmentChars: 150,
|
|
224
|
+
forwardThresholdChars: 1500,
|
|
225
|
+
requirePermission: true,
|
|
226
|
+
},
|
|
219
227
|
trace: { enabled: true, dir: "", maxSizeMb: 80, retentionDays: 7 },
|
|
220
228
|
reminder: { enabled: true, max: 2 },
|
|
221
229
|
keepAlive: { enabled: true, seconds: 20 * 60 },
|
|
@@ -603,6 +611,7 @@ function createConfigSchema(
|
|
|
603
611
|
"reply.forwardThresholdChars",
|
|
604
612
|
defaults.reply.forwardThresholdChars,
|
|
605
613
|
),
|
|
614
|
+
requirePermission: boolean("reply.requirePermission", defaults.reply.requirePermission),
|
|
606
615
|
})
|
|
607
616
|
.prefault({})
|
|
608
617
|
.catch(defaults.reply),
|
|
@@ -716,6 +725,7 @@ export const ENV_VARS = {
|
|
|
716
725
|
"reply.maxDelayMs": "QQ_REPLY_MAX_DELAY_MS",
|
|
717
726
|
"reply.segmentChars": "QQ_REPLY_SEGMENT_CHARS",
|
|
718
727
|
"reply.forwardThresholdChars": "QQ_REPLY_FORWARD_THRESHOLD_CHARS",
|
|
728
|
+
"reply.requirePermission": "QQ_REPLY_REQUIRE_PERMISSION",
|
|
719
729
|
"trace.enabled": "QQ_TRACE_ENABLED",
|
|
720
730
|
"trace.dir": "QQ_TRACE_DIR",
|
|
721
731
|
"trace.maxSizeMb": "QQ_TRACE_MAX_SIZE_MB",
|
|
@@ -785,7 +795,13 @@ const NESTED_OPTION_KEYS: Record<string, ReadonlySet<string>> = {
|
|
|
785
795
|
"allowUsers",
|
|
786
796
|
]),
|
|
787
797
|
wake: new Set(["prefixes", "ignoreAtAll", "replyToSelf", "ignoreAtOthers"]),
|
|
788
|
-
reply: new Set([
|
|
798
|
+
reply: new Set([
|
|
799
|
+
"minDelayMs",
|
|
800
|
+
"maxDelayMs",
|
|
801
|
+
"segmentChars",
|
|
802
|
+
"forwardThresholdChars",
|
|
803
|
+
"requirePermission",
|
|
804
|
+
]),
|
|
789
805
|
trace: new Set(["enabled", "dir", "maxSizeMb", "retentionDays"]),
|
|
790
806
|
reminder: new Set(["enabled", "max"]),
|
|
791
807
|
keepAlive: new Set(["enabled", "seconds"]),
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { join, resolve } from "node:path"
|
|
1
2
|
import { Plugin } from "@opencode/plugin"
|
|
2
3
|
import type { GroupMessageEvent, MessageEvent, NoticeEvent } from "@naplink/naplink"
|
|
3
4
|
import { Mutex } from "async-mutex"
|
|
@@ -17,6 +18,7 @@ import { createHistoryProvider } from "./naplink/history"
|
|
|
17
18
|
import { createMediaStore } from "./naplink/media"
|
|
18
19
|
import { describeMentions, renderMessage, resolveUserName, shouldProcess } from "./naplink/render"
|
|
19
20
|
import { SendRateLimiter } from "./rate-limit"
|
|
21
|
+
import { packageRoot, seedFile } from "./seed"
|
|
20
22
|
import { collectEnvSecrets, redactUrl, secretsFromUrl, SensitiveChecker } from "./sensitive"
|
|
21
23
|
import { SessionManager } from "./session/manager"
|
|
22
24
|
import { currentTimeText, PROACTIVE_HINT } from "./session/prompt"
|
|
@@ -37,7 +39,13 @@ import { registerAskGuard } from "./tools/ask-guard"
|
|
|
37
39
|
import { defaultToolProfilesPath, roleOf, ToolPolicy } from "./tools/policy"
|
|
38
40
|
import { registerToolPolicy } from "./tools/policy-guard"
|
|
39
41
|
import { registerSendGuard } from "./tools/send-guard"
|
|
40
|
-
import {
|
|
42
|
+
import {
|
|
43
|
+
createPlainSender,
|
|
44
|
+
QQ_REQUEST_REPLY_TOOL,
|
|
45
|
+
registerTools,
|
|
46
|
+
sendPlainMessage,
|
|
47
|
+
TOOL_NAMESPACE,
|
|
48
|
+
} from "./tools/qq"
|
|
41
49
|
import { createEmojiRunner, emojiCommand } from "./emoji/command"
|
|
42
50
|
import { describeNotice, noticeMatches, noticeTarget } from "./events/notice"
|
|
43
51
|
import { NoticeStore } from "./events/store"
|
|
@@ -319,12 +327,22 @@ export default Plugin.define({
|
|
|
319
327
|
// 通知缓存:通知不自己起一轮,只在下一次该聊天的上下文里附带出现。
|
|
320
328
|
const noticeStore = new NoticeStore()
|
|
321
329
|
// 工具配置:按聊天与角色决定模型能看到、能调用哪些工具。
|
|
322
|
-
|
|
330
|
+
// 走默认位置时把随包的示例复制过去,占位目标得自己改成真实的群 / 私聊。
|
|
331
|
+
const profilesPath = config.toolProfilesPath
|
|
332
|
+
? resolve(home.directory, config.toolProfilesPath)
|
|
333
|
+
: defaultToolProfilesPath(home.directory)
|
|
334
|
+
if (!config.toolProfilesPath) {
|
|
335
|
+
await seedFile(join(packageRoot, "tool-profiles.example.json"), profilesPath, logger)
|
|
336
|
+
}
|
|
337
|
+
const policy = new ToolPolicy(profilesPath, logger)
|
|
323
338
|
// 会话行为覆盖:按聊天决定怎么唤醒、要不要主动说话。
|
|
324
|
-
const
|
|
325
|
-
config.chatOverridesPath
|
|
326
|
-
|
|
327
|
-
)
|
|
339
|
+
const overridesPath = config.chatOverridesPath
|
|
340
|
+
? resolve(home.directory, config.chatOverridesPath)
|
|
341
|
+
: defaultChatOverridesPath(home.directory)
|
|
342
|
+
if (!config.chatOverridesPath) {
|
|
343
|
+
await seedFile(join(packageRoot, "chat-overrides.example.json"), overridesPath, logger)
|
|
344
|
+
}
|
|
345
|
+
const overrides = new ChatOverrides(overridesPath, logger)
|
|
328
346
|
// 调用链追踪:一条入站消息拿一个 trace_id,事后能把这轮串起来。
|
|
329
347
|
// 追踪文件与日志放在一起,容量与保留天数都有上限。
|
|
330
348
|
const tracer = config.trace.enabled
|
|
@@ -504,7 +522,7 @@ export default Plugin.define({
|
|
|
504
522
|
event.system.push({
|
|
505
523
|
type: "text",
|
|
506
524
|
text: turn?.proactive
|
|
507
|
-
? `这是一次主动发起话题,必须调用 qq_send_message 把话题发到${destination}
|
|
525
|
+
? `这是一次主动发起话题,必须调用 qq_send_message 把话题发到${destination}。${target.kind === "group" && config.reply.requirePermission ? "群里要先调用 qq_request_reply 说明为什么要回复,拿到权限后才能发送。" : ""}`
|
|
508
526
|
: turn?.atMe
|
|
509
527
|
? wakeHint(turn.wake)
|
|
510
528
|
: turn?.suppress
|
|
@@ -519,12 +537,21 @@ export default Plugin.define({
|
|
|
519
537
|
text: `聊天里发生过的通知(不需要回复):\n${notices.map((item) => `- ${item}`).join("\n")}`,
|
|
520
538
|
})
|
|
521
539
|
}
|
|
540
|
+
// 群里默认没有发送权限:模型得先想清楚该不该回复,说明理由才拿得到权限。
|
|
541
|
+
if (target.kind === "group" && config.reply.requirePermission) {
|
|
542
|
+
event.system.push({
|
|
543
|
+
type: "text",
|
|
544
|
+
text: `默认你没有向用户发消息的权限。要回复,先判断该不该回复,再调用 ${QQ_REQUEST_REPLY_TOOL} 说明为什么要回复;拿到权限后才能调用 qq_send_message,权限只在本轮有效。`,
|
|
545
|
+
})
|
|
546
|
+
}
|
|
522
547
|
// 本轮发送者的角色决定工具档:owner / admin 全开,其他按聊天配置。
|
|
523
548
|
// 角色按会话保留,没有在途轮次(重启、TUI 驱动)时也照样收窄。
|
|
524
549
|
{
|
|
525
550
|
const allowed = await policy.allow(role, targetKey(target), Object.keys(event.tools))
|
|
526
551
|
const removed: string[] = []
|
|
527
552
|
for (const name of Object.keys(event.tools)) {
|
|
553
|
+
// 申请回复权限的工具必须一直留着:不然模型永远拿不到发送权限。
|
|
554
|
+
if (name === QQ_REQUEST_REPLY_TOOL) continue
|
|
528
555
|
if (!allowed.has(name)) {
|
|
529
556
|
removed.push(name)
|
|
530
557
|
delete event.tools[name]
|
package/src/overrides.ts
CHANGED
|
@@ -83,9 +83,9 @@ export function defaultChatOverrides(): ChatOverridesFile {
|
|
|
83
83
|
return { chats: {} }
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
/**
|
|
87
|
-
export function defaultChatOverridesPath(): string {
|
|
88
|
-
return join(
|
|
86
|
+
/** 默认的配置文件位置:工作目录下的 `chat-overrides.json`。 */
|
|
87
|
+
export function defaultChatOverridesPath(home: string): string {
|
|
88
|
+
return join(home, "chat-overrides.json")
|
|
89
89
|
}
|
|
90
90
|
|
|
91
91
|
/**
|
package/src/seed.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { existsSync } from "node:fs"
|
|
2
|
+
import { copyFile, mkdir } from "node:fs/promises"
|
|
3
|
+
import { dirname, resolve } from "node:path"
|
|
4
|
+
import type { Logger } from "./logger"
|
|
5
|
+
|
|
6
|
+
/** 随包分发文件所在目录:种子文件只从这里取。 */
|
|
7
|
+
export const packageRoot = resolve(import.meta.dirname, "..")
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 把随包分发的模板复制到工作目录。
|
|
11
|
+
*
|
|
12
|
+
* 目标已存在时原样保留:用户的改动不会被升级或重启覆盖。
|
|
13
|
+
*
|
|
14
|
+
* @returns 是否真的复制了。
|
|
15
|
+
*/
|
|
16
|
+
export async function seedFile(
|
|
17
|
+
source: string,
|
|
18
|
+
destination: string,
|
|
19
|
+
logger: Logger,
|
|
20
|
+
): Promise<boolean> {
|
|
21
|
+
if (existsSync(destination)) return false
|
|
22
|
+
try {
|
|
23
|
+
await mkdir(dirname(destination), { recursive: true, mode: 0o700 })
|
|
24
|
+
await copyFile(source, destination)
|
|
25
|
+
logger.info(`已放到工作目录:${destination}`)
|
|
26
|
+
return true
|
|
27
|
+
} catch (error) {
|
|
28
|
+
logger.warn(`复制 ${source} 失败`, error)
|
|
29
|
+
return false
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/session/manager.ts
CHANGED
|
@@ -172,6 +172,10 @@ interface ChatState {
|
|
|
172
172
|
lastRetireAt: number
|
|
173
173
|
/** 最近一次清空当前轮次的原因与时间,仅供排查。 */
|
|
174
174
|
lastClear?: { reason: string; at: number; role?: Role; traceId?: string }
|
|
175
|
+
/** 本轮是否已批准发送:模型先说明理由才拿到这个权限,轮次结束即失效。 */
|
|
176
|
+
replyAllowed: boolean
|
|
177
|
+
/** 批准发送的理由,仅供日志与排查。 */
|
|
178
|
+
replyReason?: string
|
|
175
179
|
}
|
|
176
180
|
|
|
177
181
|
/** 插件存储接受的值类型。 */
|
|
@@ -605,6 +609,7 @@ export class SessionManager {
|
|
|
605
609
|
reminders: 0,
|
|
606
610
|
lastUserMessageAt: this.activity[key] ?? Date.now(),
|
|
607
611
|
lastRetireAt: 0,
|
|
612
|
+
replyAllowed: false,
|
|
608
613
|
}
|
|
609
614
|
this.states.set(key, state)
|
|
610
615
|
}
|
|
@@ -875,6 +880,9 @@ export class SessionManager {
|
|
|
875
880
|
...(state.lastClear.traceId ? { trace_id: state.lastClear.traceId } : {}),
|
|
876
881
|
})
|
|
877
882
|
state.current = undefined
|
|
883
|
+
// 发送权限只在本轮有效:轮次结束就收回,下一轮要重新说明理由。
|
|
884
|
+
state.replyAllowed = false
|
|
885
|
+
state.replyReason = undefined
|
|
878
886
|
// 在途轮次落盘,重启后不会把已经结束的一轮又恢复出来。
|
|
879
887
|
this.scheduleSave()
|
|
880
888
|
}
|
|
@@ -909,6 +917,8 @@ export class SessionManager {
|
|
|
909
917
|
lastDispatchAt: state.lastDispatchAt,
|
|
910
918
|
idleAt: state.idleAt,
|
|
911
919
|
lastClear: state.lastClear,
|
|
920
|
+
replyAllowed: state.replyAllowed,
|
|
921
|
+
replyReason: state.replyReason,
|
|
912
922
|
now: Date.now(),
|
|
913
923
|
}
|
|
914
924
|
}
|
|
@@ -922,6 +932,33 @@ export class SessionManager {
|
|
|
922
932
|
this.scheduleSave()
|
|
923
933
|
}
|
|
924
934
|
|
|
935
|
+
/**
|
|
936
|
+
* 模型申请本轮的发送权限。
|
|
937
|
+
*
|
|
938
|
+
* 默认不给发送权限:模型得先说明为什么回复,拿到权限后才能调用发送工具。
|
|
939
|
+
* 权限只在本轮有效,轮次结束就失效。
|
|
940
|
+
*
|
|
941
|
+
* @returns 是否批准。会话没有绑定或已经有权限时不算失败。
|
|
942
|
+
*/
|
|
943
|
+
requestReply(sessionID: string, reason: string): { granted: boolean; reason: string } {
|
|
944
|
+
const state = this.sessions.get(sessionID)
|
|
945
|
+
if (!state) return { granted: false, reason }
|
|
946
|
+
// 权限只在本轮有效:没有进行中的轮次就不给。
|
|
947
|
+
if (!state.current) {
|
|
948
|
+
this.logger.warn(`${targetLabel(state.target)} 没有进行中的轮次,不给发送权限`)
|
|
949
|
+
return { granted: false, reason }
|
|
950
|
+
}
|
|
951
|
+
state.replyAllowed = true
|
|
952
|
+
state.replyReason = reason
|
|
953
|
+
this.logger.info(`${targetLabel(state.target)} 模型申请本轮发送权限:${reason}`)
|
|
954
|
+
return { granted: true, reason }
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
/** 本轮是否已经拿到发送权限。 */
|
|
958
|
+
canReply(sessionID: string): boolean {
|
|
959
|
+
return this.sessions.get(sessionID)?.replyAllowed === true
|
|
960
|
+
}
|
|
961
|
+
|
|
925
962
|
/** 记录一条机器人发出的消息,撤回工具只能操作这些消息。 */
|
|
926
963
|
rememberOutgoing(target: ChatTarget, messageId: string): void {
|
|
927
964
|
this.track(this.outgoing, target, messageId)
|
|
@@ -1266,10 +1303,15 @@ export class SessionManager {
|
|
|
1266
1303
|
...(reminderRole ? { role: reminderRole } : {}),
|
|
1267
1304
|
}
|
|
1268
1305
|
this.logger.warn(`${label} 模型未调用发送工具,提醒第 ${state.reminders} 次`)
|
|
1306
|
+
// 群里默认没有发送权限,提醒里得说清楚先申请再发。
|
|
1307
|
+
const permissionHint =
|
|
1308
|
+
this.config.reply.requirePermission && state.target.kind === "group"
|
|
1309
|
+
? "群里要先调用 qq_request_reply 说明为什么要回复,拿到权限后才能发送。"
|
|
1310
|
+
: ""
|
|
1269
1311
|
try {
|
|
1270
1312
|
await this.ctx.session.synthetic({
|
|
1271
1313
|
sessionID,
|
|
1272
|
-
text: `[提醒] 你这一轮没有调用任何发送工具,直接输出的文字会被丢弃。请调用 qq_send_message 工具:想回复就 reply=true 并给出 content,不想回复就 reply=false
|
|
1314
|
+
text: `[提醒] 你这一轮没有调用任何发送工具,直接输出的文字会被丢弃。请调用 qq_send_message 工具:想回复就 reply=true 并给出 content,不想回复就 reply=false;发送图片、语音、转发或戳一戳也视为已回复。${permissionHint}`,
|
|
1273
1315
|
})
|
|
1274
1316
|
} catch (error) {
|
|
1275
1317
|
state.awaitingSend = false
|
package/src/session/soul.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { mkdir, readdir, readFile } from "node:fs/promises"
|
|
2
2
|
import { existsSync, statSync, watch, type FSWatcher } from "node:fs"
|
|
3
3
|
import { dirname, isAbsolute, join, resolve } from "node:path"
|
|
4
4
|
import { unrefTimer } from "../async"
|
|
5
|
+
import { seedFile } from "../seed"
|
|
5
6
|
import type { Logger } from "../logger"
|
|
6
7
|
import { buildSystemPrompt } from "./prompt"
|
|
7
8
|
|
|
@@ -65,15 +66,8 @@ export async function seedSouls(
|
|
|
65
66
|
}
|
|
66
67
|
for (const entry of entries) {
|
|
67
68
|
if (!entry.isFile()) continue
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if (existsSync(destination)) continue
|
|
71
|
-
try {
|
|
72
|
-
await copyFile(join(template, entry.name), destination)
|
|
73
|
-
logger.info(`角色设定已就位:${destination}`)
|
|
74
|
-
} catch (error) {
|
|
75
|
-
logger.warn(`复制角色设定失败:${entry.name}`, error)
|
|
76
|
-
}
|
|
69
|
+
// 已有同名文件说明用户改过或换过,seedFile 不会动它。
|
|
70
|
+
await seedFile(join(template, entry.name), join(target, entry.name), logger)
|
|
77
71
|
}
|
|
78
72
|
}
|
|
79
73
|
|
|
@@ -3,6 +3,7 @@ import { targetKey, targetLabel } from "../chat"
|
|
|
3
3
|
import type { Logger } from "../logger"
|
|
4
4
|
import type { SessionManager } from "../session/manager"
|
|
5
5
|
import type { ToolPolicy } from "./policy"
|
|
6
|
+
import { QQ_REQUEST_REPLY_TOOL } from "./qq"
|
|
6
7
|
|
|
7
8
|
/** 会开启独立会话的工具名;不同版本分别叫 subagent 与 task。 */
|
|
8
9
|
const SUBAGENT_TOOLS = new Set(["subagent", "task"])
|
|
@@ -35,6 +36,8 @@ export async function registerToolPolicy(
|
|
|
35
36
|
logger.warn(`${targetLabel(target)} 会话里拒绝创建子代理 ${event.tool}`)
|
|
36
37
|
throw new Error("QQ 聊天会话里不能创建子代理:子会话不会继承按聊天的工具限制。")
|
|
37
38
|
}
|
|
39
|
+
// 申请回复权限的工具必须一直放行:不然模型永远拿不到发送权限。
|
|
40
|
+
if (event.tool === QQ_REQUEST_REPLY_TOOL) return
|
|
38
41
|
const allowed = await policy.allow(role, targetKey(target), [event.tool])
|
|
39
42
|
logger.debug(`${targetLabel(target)} 工具策略检查`, {
|
|
40
43
|
role,
|
package/src/tools/policy.ts
CHANGED
|
@@ -56,9 +56,9 @@ export function defaultToolPolicy(): ToolPolicyFile {
|
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
/**
|
|
60
|
-
export function defaultToolProfilesPath(): string {
|
|
61
|
-
return join(
|
|
59
|
+
/** 默认的配置文件位置:工作目录下的 `tool-profiles.json`。 */
|
|
60
|
+
export function defaultToolProfilesPath(home: string): string {
|
|
61
|
+
return join(home, "tool-profiles.json")
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
/**
|
package/src/tools/qq.ts
CHANGED
|
@@ -23,6 +23,14 @@ import type { Tracer } from "../trace"
|
|
|
23
23
|
/** 工具命名空间,最终工具名为 qq_send_message 等。 */
|
|
24
24
|
export const TOOL_NAMESPACE = "qq"
|
|
25
25
|
|
|
26
|
+
/**
|
|
27
|
+
* 申请回复权限的工具名。
|
|
28
|
+
*
|
|
29
|
+
* 群里默认没有发送权限:模型得先说明为什么要回复,拿到权限才能发送;
|
|
30
|
+
* 权限只在本轮有效。私聊默认开放,不需要申请。
|
|
31
|
+
*/
|
|
32
|
+
export const QQ_REQUEST_REPLY_TOOL = "qq_request_reply"
|
|
33
|
+
|
|
26
34
|
/**
|
|
27
35
|
* 暴露给模型的 QQ 工具名。
|
|
28
36
|
*
|
|
@@ -30,6 +38,7 @@ export const TOOL_NAMESPACE = "qq"
|
|
|
30
38
|
* `qq_send_message` 是模型发消息的唯一入口。
|
|
31
39
|
*/
|
|
32
40
|
export const QQ_TOOL_NAMES = [
|
|
41
|
+
"qq_request_reply",
|
|
33
42
|
"qq_send_message",
|
|
34
43
|
"qq_send_image",
|
|
35
44
|
"qq_send_sticker",
|
|
@@ -241,6 +250,22 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
241
250
|
return undefined
|
|
242
251
|
}
|
|
243
252
|
|
|
253
|
+
/**
|
|
254
|
+
* 群里发消息前的权限检查。
|
|
255
|
+
*
|
|
256
|
+
* 默认没有发送权限:模型先说明理由才拿得到,权限只在本轮有效。
|
|
257
|
+
* 私聊默认开放,不用申请;选择不回复(`reply=false`)也不需要权限。
|
|
258
|
+
*
|
|
259
|
+
* @returns 可以发送时返回 undefined,否则返回给模型的错误文案。
|
|
260
|
+
*/
|
|
261
|
+
const checkReplyPermission = (sessionID: string, target: ChatTarget): string | undefined => {
|
|
262
|
+
if (!config.reply.requirePermission) return undefined
|
|
263
|
+
if (target.kind === "private") return undefined
|
|
264
|
+
if (manager.canReply(sessionID)) return undefined
|
|
265
|
+
logger.warn(`${targetLabel(target)} 本轮还没有回复权限,拒绝发送`, traceMeta(sessionID))
|
|
266
|
+
return `error: 本轮还没有回复权限。要回复用户,先调用 ${QQ_REQUEST_REPLY_TOOL} 说明为什么要回复;拿到权限后才能调用 qq_send_message。`
|
|
267
|
+
}
|
|
268
|
+
|
|
244
269
|
// 同一次工具调用拆出的多段共用一个操作 id:段与段之间不再额外等间隔。
|
|
245
270
|
let operationSeq = 0
|
|
246
271
|
const nextOperation = (): string => `send-${++operationSeq}`
|
|
@@ -432,6 +457,41 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
432
457
|
// QQ 工具必须作为独立工具暴露。Code Mode 只给模型一个 execute,
|
|
433
458
|
// 按聊天配置的 qq_* 规则永远命不中,普通成员会连发消息的能力都没有。
|
|
434
459
|
|
|
460
|
+
editor.add({
|
|
461
|
+
name: "request_reply",
|
|
462
|
+
description:
|
|
463
|
+
"申请本轮的回复权限。群里默认没有发送权限:要回复用户,先调用这个工具并说明为什么要回复,拿到权限后才能调用 qq_send_message;权限只在本轮有效,轮次结束就收回。私聊默认就有权限,不用申请。",
|
|
464
|
+
input: {
|
|
465
|
+
type: "object",
|
|
466
|
+
properties: {
|
|
467
|
+
reason: {
|
|
468
|
+
type: "string",
|
|
469
|
+
description: "为什么要回复这条消息(必填):说清楚回复的理由,不要只写「需要回复」",
|
|
470
|
+
},
|
|
471
|
+
},
|
|
472
|
+
required: ["reason"],
|
|
473
|
+
additionalProperties: false,
|
|
474
|
+
},
|
|
475
|
+
options: { namespace: TOOL_NAMESPACE, codemode: false },
|
|
476
|
+
execute: async (raw, context) => {
|
|
477
|
+
const input = (raw ?? {}) as { reason?: unknown }
|
|
478
|
+
const reason = typeof input.reason === "string" ? input.reason.trim() : ""
|
|
479
|
+
if (!reason) return text("error: 必须说明为什么回复(reason 不能为空)")
|
|
480
|
+
const target = manager.targetForSession(context.sessionID)
|
|
481
|
+
if (!target) return text("error: 当前会话没有绑定 QQ 群聊或私聊")
|
|
482
|
+
const granted = manager.requestReply(context.sessionID, reason)
|
|
483
|
+
if (!granted.granted) {
|
|
484
|
+
return text("error: 当前没有进行中的 QQ 消息轮次,拿不到回复权限")
|
|
485
|
+
}
|
|
486
|
+
if (target.kind === "private") {
|
|
487
|
+
return text("私聊默认就有回复权限,直接调用 qq_send_message 即可。")
|
|
488
|
+
}
|
|
489
|
+
return text(
|
|
490
|
+
`已获得本轮的回复权限(理由:${reason})。现在可以调用 qq_send_message 回复;权限只在本轮有效。`,
|
|
491
|
+
)
|
|
492
|
+
},
|
|
493
|
+
})
|
|
494
|
+
|
|
435
495
|
editor.add({
|
|
436
496
|
name: "send_message",
|
|
437
497
|
description:
|
|
@@ -472,6 +532,8 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
472
532
|
manager.markMessageSent(context.sessionID)
|
|
473
533
|
return text("skipped")
|
|
474
534
|
}
|
|
535
|
+
const denied = checkReplyPermission(context.sessionID, target)
|
|
536
|
+
if (denied) return text(denied)
|
|
475
537
|
guardOutbound(target, plain)
|
|
476
538
|
|
|
477
539
|
// 长度按字素簇算,和拆段的口径一致:一个 emoji 只算一个。
|
|
@@ -542,6 +604,8 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
542
604
|
const prepared = await prepareSend(context.sessionID)
|
|
543
605
|
if (prepared.error) return prepared.error
|
|
544
606
|
const target = prepared.target
|
|
607
|
+
const denied = checkReplyPermission(context.sessionID, target)
|
|
608
|
+
if (denied) return text(denied)
|
|
545
609
|
const file = input.file ?? (input.base64 ? `base64://${input.base64}` : undefined)
|
|
546
610
|
if (!file) return text("error: 缺少 file 或 base64")
|
|
547
611
|
const tooLarge = checkInlineSize(file, "图片")
|
|
@@ -593,6 +657,8 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
593
657
|
const prepared = await prepareSend(context.sessionID)
|
|
594
658
|
if (prepared.error) return prepared.error
|
|
595
659
|
const target = prepared.target
|
|
660
|
+
const denied = checkReplyPermission(context.sessionID, target)
|
|
661
|
+
if (denied) return text(denied)
|
|
596
662
|
const id = input.id
|
|
597
663
|
if (typeof id !== "number" || !Number.isInteger(id)) {
|
|
598
664
|
return text("error: 缺少 id(收藏表情编号)")
|
|
@@ -639,6 +705,8 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
639
705
|
const prepared = await prepareSend(context.sessionID)
|
|
640
706
|
if (prepared.error) return prepared.error
|
|
641
707
|
const target = prepared.target
|
|
708
|
+
const denied = checkReplyPermission(context.sessionID, target)
|
|
709
|
+
if (denied) return text(denied)
|
|
642
710
|
if (!input.file) return text("error: 缺少 file")
|
|
643
711
|
const tooLarge = checkInlineSize(input.file, "语音")
|
|
644
712
|
if (tooLarge) return text(tooLarge)
|
|
@@ -685,6 +753,8 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
685
753
|
const prepared = await prepareSend(context.sessionID)
|
|
686
754
|
if (prepared.error) return prepared.error
|
|
687
755
|
const target = prepared.target
|
|
756
|
+
const denied = checkReplyPermission(context.sessionID, target)
|
|
757
|
+
if (denied) return text(denied)
|
|
688
758
|
const nodes: {
|
|
689
759
|
user_id: number
|
|
690
760
|
nickname: string
|
|
@@ -733,6 +803,8 @@ export async function registerTools(deps: ToolDeps): Promise<{ dispose(): Promis
|
|
|
733
803
|
const prepared = await prepareSend(context.sessionID)
|
|
734
804
|
if (prepared.error) return prepared.error
|
|
735
805
|
const target = prepared.target
|
|
806
|
+
const denied = checkReplyPermission(context.sessionID, target)
|
|
807
|
+
if (denied) return text(denied)
|
|
736
808
|
if (!input.user_id) return text("error: 缺少 user_id")
|
|
737
809
|
const verdict = await permit(target, nextOperation())
|
|
738
810
|
if (!verdict.allowed) return refuse(context.sessionID, verdict)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"defaultProfile": "chat",
|
|
3
|
+
"profiles": {
|
|
4
|
+
"chat": {
|
|
5
|
+
"allow": ["websearch", "webfetch", "qq_*"],
|
|
6
|
+
"deny": []
|
|
7
|
+
},
|
|
8
|
+
"readonly": {
|
|
9
|
+
"allow": ["read", "glob", "grep", "qq_*"],
|
|
10
|
+
"deny": []
|
|
11
|
+
},
|
|
12
|
+
"admin": {
|
|
13
|
+
"allow": ["*"],
|
|
14
|
+
"deny": []
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"chats": {
|
|
18
|
+
"group:100": "readonly",
|
|
19
|
+
"private:200": "chat"
|
|
20
|
+
}
|
|
21
|
+
}
|