@tencent-connect/openclaw-qqbot 1.7.0 → 1.7.1

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.
@@ -0,0 +1,505 @@
1
+ /**
2
+ * QQBot Approval Handler
3
+ *
4
+ * 监听 Gateway 的 exec/plugin approval 事件,
5
+ * 直接调用 QQ API 发送带 Inline Keyboard 的审批消息。
6
+ * 参考 DiscordExecApprovalHandler 的实现模式。
7
+ *
8
+ * 兼容性:gateway-runtime / approval-runtime 模块在 openclaw < 3.22 上不存在,
9
+ * 使用动态 import 避免插件整体加载失败,旧版框架上审批功能自动降级(不可用)。
10
+ */
11
+
12
+ import type { OpenClawConfig } from "openclaw/plugin-sdk";
13
+ import { createRequire } from "node:module";
14
+ import path from "node:path";
15
+ import { fileURLToPath } from "node:url";
16
+ import {
17
+ getAccessToken,
18
+ sendC2CMessageWithInlineKeyboard,
19
+ sendGroupMessageWithInlineKeyboard,
20
+ } from "./api.js";
21
+ import type { InlineKeyboard, KeyboardButton } from "./types.js";
22
+
23
+ // ─── 动态加载 gateway-runtime(兼容不同安装环境) ────────
24
+
25
+ function loadGatewayRuntime(): { createOperatorApprovalsGatewayClient: (...args: any[]) => Promise<GatewayClient> } {
26
+ const req = createRequire(import.meta.url);
27
+ const currentFile = fileURLToPath(import.meta.url);
28
+ const pluginRoot = path.resolve(path.dirname(currentFile), "..", "..");
29
+ const fs = req("node:fs") as typeof import("node:fs");
30
+
31
+ // 尝试从找到的 openclaw 根目录加载 gateway-runtime.js
32
+ const tryLoadFromRoot = (root: string) => {
33
+ for (const rel of ["dist/plugin-sdk/gateway-runtime.js", "plugin-sdk/gateway-runtime.js"]) {
34
+ const p = path.join(root, rel);
35
+ try {
36
+ if (fs.existsSync(p)) return req(p);
37
+ } catch { /* try next */ }
38
+ }
39
+ return null;
40
+ };
41
+
42
+ // 策略 1: link-sdk-core.cjs findOpenclawRoot
43
+ try {
44
+ const { findOpenclawRoot } = req(path.join(pluginRoot, "scripts", "link-sdk-core.cjs")) as {
45
+ findOpenclawRoot: (root: string) => string | null;
46
+ };
47
+ const root = findOpenclawRoot(pluginRoot);
48
+ if (root) {
49
+ const mod = tryLoadFromRoot(root);
50
+ if (mod) return mod;
51
+ }
52
+ } catch { /* fallback */ }
53
+
54
+ // 策略 2: process.argv[1] 反推(当前进程就是 openclaw)
55
+ try {
56
+ const entry = process.argv[1];
57
+ if (entry) {
58
+ const realEntry = fs.realpathSync(entry);
59
+ let dir = path.dirname(realEntry);
60
+ for (let i = 0; i < 6; i++) {
61
+ const mod = tryLoadFromRoot(dir);
62
+ if (mod) return mod;
63
+ const parent = path.dirname(dir);
64
+ if (parent === dir) break;
65
+ dir = parent;
66
+ }
67
+ }
68
+ } catch { /* fallback */ }
69
+
70
+ throw new Error("Cannot find openclaw/plugin-sdk/gateway-runtime (all strategies failed)");
71
+ }
72
+
73
+ // ─── 动态加载的模块类型(兼容旧版框架) ───────────────────────
74
+
75
+ /** gateway-runtime 模块的接口(动态 import) */
76
+ type GatewayClient = {
77
+ start: () => void | Promise<void>;
78
+ stop: () => void | Promise<void>;
79
+ request: (method: string, params: unknown) => Promise<unknown>;
80
+ };
81
+
82
+ type EventFrame = {
83
+ event: string;
84
+ payload: unknown;
85
+ };
86
+
87
+ /** approval-runtime 中的审批请求/结果类型(内联定义,避免顶层 import) */
88
+ interface ExecApprovalRequest {
89
+ id: string;
90
+ expiresAtMs: number;
91
+ request: {
92
+ commandPreview?: string;
93
+ command?: string;
94
+ cwd?: string;
95
+ agentId?: string;
96
+ turnSourceAccountId?: string;
97
+ sessionKey?: string;
98
+ turnSourceTo?: string;
99
+ [key: string]: unknown;
100
+ };
101
+ }
102
+
103
+ interface ExecApprovalResolved {
104
+ id: string;
105
+ decision: string;
106
+ resolvedBy?: string;
107
+ [key: string]: unknown;
108
+ }
109
+
110
+ interface PluginApprovalRequest {
111
+ id: string;
112
+ request: {
113
+ timeoutMs?: number;
114
+ severity?: "critical" | "info" | string;
115
+ title: string;
116
+ description?: string;
117
+ toolName?: string;
118
+ pluginId?: string;
119
+ agentId?: string;
120
+ turnSourceAccountId?: string;
121
+ sessionKey?: string;
122
+ turnSourceTo?: string;
123
+ [key: string]: unknown;
124
+ };
125
+ }
126
+
127
+ interface PluginApprovalResolved {
128
+ id: string;
129
+ decision: string;
130
+ resolvedBy?: string;
131
+ [key: string]: unknown;
132
+ }
133
+
134
+ // ─── 类型 ───────────────────────────────────────────────────
135
+
136
+ export interface QQBotApprovalHandlerOpts {
137
+ accountId: string;
138
+ appId: string;
139
+ clientSecret: string;
140
+ cfg: OpenClawConfig;
141
+ gatewayUrl?: string;
142
+ log?: {
143
+ info: (msg: string) => void;
144
+ error: (msg: string) => void;
145
+ debug?: (msg: string) => void;
146
+ };
147
+ }
148
+
149
+ type ApprovalKind = "exec" | "plugin";
150
+
151
+ type CachedApprovalRequest =
152
+ | { kind: "exec"; request: ExecApprovalRequest }
153
+ | { kind: "plugin"; request: PluginApprovalRequest };
154
+
155
+ type PendingEntry = {
156
+ targets: Array<{ type: "c2c" | "group"; id: string }>;
157
+ timeoutId: ReturnType<typeof setTimeout>;
158
+ };
159
+
160
+ // ─── 辅助函数 ───────────────────────────────────────────────
161
+
162
+ function toShortId(approvalId: string): string {
163
+ return approvalId.replace(/^(exec|plugin):/, "").slice(0, 8);
164
+ }
165
+
166
+ function resolveApprovalKind(approvalId: string): ApprovalKind {
167
+ return approvalId.startsWith("plugin:") ? "plugin" : "exec";
168
+ }
169
+
170
+ function buildExecApprovalText(request: ExecApprovalRequest): string {
171
+ const expiresIn = Math.max(
172
+ 0,
173
+ Math.round((request.expiresAtMs - Date.now()) / 1000)
174
+ );
175
+ const lines: string[] = ["🔐 命令执行审批", ""];
176
+ const cmd = request.request.commandPreview ?? request.request.command ?? "";
177
+ if (cmd) lines.push(`\`\`\`\n${cmd.slice(0, 300)}\n\`\`\``);
178
+ if (request.request.cwd) lines.push(`📁 目录: ${request.request.cwd}`);
179
+ if (request.request.agentId) lines.push(`🤖 Agent: ${request.request.agentId}`);
180
+ lines.push("", `⏱️ 超时: ${expiresIn} 秒`);
181
+ return lines.join("\n");
182
+ }
183
+
184
+ function buildPluginApprovalText(request: PluginApprovalRequest): string {
185
+ const timeoutSec = Math.round((request.request.timeoutMs ?? 120_000) / 1000);
186
+ const severityIcon =
187
+ request.request.severity === "critical" ? "🔴"
188
+ : request.request.severity === "info" ? "🔵"
189
+ : "🟡";
190
+
191
+ const lines: string[] = [`${severityIcon} 审批请求`, ""];
192
+ lines.push(`📋 ${request.request.title}`);
193
+ if (request.request.description) lines.push(`📝 ${request.request.description}`);
194
+ if (request.request.toolName) lines.push(`🔧 工具: ${request.request.toolName}`);
195
+ if (request.request.pluginId) lines.push(`🔌 插件: ${request.request.pluginId}`);
196
+ if (request.request.agentId) lines.push(`🤖 Agent: ${request.request.agentId}`);
197
+ lines.push("", `⏱️ 超时: ${timeoutSec} 秒`);
198
+ return lines.join("\n");
199
+ }
200
+
201
+ /**
202
+ * Inline Keyboard(内嵌回调型按钮)
203
+ * type=1(Callback):点击触发 INTERACTION_CREATE,button_data = data 字段
204
+ * group_id 相同 → 点一个后其余变灰(三选一语义)
205
+ * click_limit=1 → 每人只能点一次
206
+ * permission.type=2 → 所有人可操作
207
+ */
208
+ function buildApprovalKeyboard(approvalId: string): InlineKeyboard {
209
+ const makeBtn = (
210
+ id: string,
211
+ label: string,
212
+ visitedLabel: string,
213
+ data: string,
214
+ style: 0 | 1
215
+ ): KeyboardButton => ({
216
+ id,
217
+ render_data: { label, visited_label: visitedLabel, style },
218
+ action: {
219
+ type: 1,
220
+ data,
221
+ permission: { type: 2 },
222
+ click_limit: 1,
223
+ },
224
+ group_id: "approval",
225
+ });
226
+ return {
227
+ content: {
228
+ rows: [
229
+ {
230
+ buttons: [
231
+ makeBtn("allow", "✅ 允许一次", "已允许", `approve:${approvalId}:allow-once`, 1),
232
+ makeBtn("always", "⭐ 始终允许", "已始终允许", `approve:${approvalId}:allow-always`, 1),
233
+ makeBtn("deny", "❌ 拒绝", "已拒绝", `approve:${approvalId}:deny`, 0),
234
+ ],
235
+ },
236
+ ],
237
+ },
238
+ };
239
+ }
240
+
241
+ /** 从 sessionKey 或 turnSourceTo 提取投递目标 */
242
+ function resolveTarget(
243
+ sessionKey: string | null | undefined,
244
+ turnSourceTo: string | null | undefined
245
+ ): { type: "c2c" | "group"; id: string } | null {
246
+ // 优先从 sessionKey 解析(如 agent:main:qqbot:direct:OPENID)
247
+ const sk = sessionKey ?? turnSourceTo;
248
+ if (!sk) return null;
249
+ const m = sk.match(/qqbot:(c2c|direct|group):([A-F0-9]+)/i);
250
+ if (!m) return null;
251
+ const type = m[1]!.toLowerCase() === "group" ? "group" : "c2c";
252
+ return { type, id: m[2]! };
253
+ }
254
+
255
+ // ─── Handler 类 ──────────────────────────────────────────────
256
+
257
+ export class QQBotApprovalHandler {
258
+ private gatewayClient: GatewayClient | null = null;
259
+ private pending = new Map<string, PendingEntry>();
260
+ private requestCache = new Map<string, CachedApprovalRequest>();
261
+ private opts: QQBotApprovalHandlerOpts;
262
+ private started = false;
263
+
264
+ constructor(opts: QQBotApprovalHandlerOpts) {
265
+ this.opts = opts;
266
+ }
267
+
268
+ async start(): Promise<void> {
269
+ if (this.started) return;
270
+ this.started = true;
271
+ const { log } = this.opts;
272
+ log?.info(`[qqbot:${this.opts.accountId}] approval-handler: starting`);
273
+
274
+ // 动态加载 gateway-runtime(兼容旧版框架 / pnpm 环境)
275
+ let gatewayRuntime: { createOperatorApprovalsGatewayClient: (...args: any[]) => Promise<GatewayClient> };
276
+ try {
277
+ gatewayRuntime = loadGatewayRuntime();
278
+ } catch (err) {
279
+ log?.error(`[qqbot:${this.opts.accountId}] approval-handler: gateway-runtime module not available, approval feature disabled. Error: ${err}`);
280
+ this.started = false;
281
+ return;
282
+ }
283
+
284
+ try {
285
+ this.gatewayClient = await gatewayRuntime.createOperatorApprovalsGatewayClient({
286
+ config: this.opts.cfg,
287
+ gatewayUrl: this.opts.gatewayUrl,
288
+ clientDisplayName: "QQBot Approval Handler",
289
+ onEvent: (evt: EventFrame) => this.handleGatewayEvent(evt),
290
+ onHelloOk: () => log?.info(`[qqbot:${this.opts.accountId}] approval-handler: connected to gateway`),
291
+ onConnectError: (err: { message: string }) => log?.error(`[qqbot:${this.opts.accountId}] approval-handler: connect error: ${err.message}`),
292
+ onClose: (code: number, reason: string) => log?.debug?.(`[qqbot:${this.opts.accountId}] approval-handler: gateway closed: ${code} ${reason}`),
293
+ });
294
+ this.gatewayClient.start();
295
+ setApprovalFeatureAvailable(true);
296
+ } catch (err) {
297
+ log?.error(`[qqbot:${this.opts.accountId}] approval-handler: failed to create gateway client: ${err}`);
298
+ this.started = false;
299
+ }
300
+ }
301
+
302
+ async stop(): Promise<void> {
303
+ if (!this.started) return;
304
+ this.started = false;
305
+ for (const entry of this.pending.values()) clearTimeout(entry.timeoutId);
306
+ this.pending.clear();
307
+ this.requestCache.clear();
308
+ this.gatewayClient?.stop();
309
+ this.gatewayClient = null;
310
+ this.opts.log?.info(`[qqbot:${this.opts.accountId}] approval-handler: stopped`);
311
+ }
312
+
313
+ /** 检查是否有指定 shortId 对应的 pending 审批 */
314
+ hasShortId(shortId: string): boolean {
315
+ for (const id of this.pending.keys()) {
316
+ if (toShortId(id) === shortId) return true;
317
+ }
318
+ return false;
319
+ }
320
+
321
+ /** 解析审批请求(供 Interaction 回调或 /approve 命令调用) */
322
+ async resolveApproval(
323
+ approvalId: string,
324
+ decision: "allow-once" | "allow-always" | "deny"
325
+ ): Promise<boolean> {
326
+ if (!this.gatewayClient) return false;
327
+
328
+ // 查找完整 ID:支持完整 ID(exec:uuid / plugin:uuid)、纯 UUID、或 shortId(8位)
329
+ let fullId = approvalId;
330
+ if (this.pending.has(approvalId)) {
331
+ fullId = approvalId;
332
+ } else {
333
+ // 尝试在 pending keys 中匹配:纯 UUID 可能对应 exec:uuid 或 plugin:uuid
334
+ for (const id of this.pending.keys()) {
335
+ if (id === approvalId) { fullId = id; break; }
336
+ // 纯 UUID 匹配:pending key 的 uuid 部分等于传入值
337
+ if (id.replace(/^(exec|plugin):/, "") === approvalId) { fullId = id; break; }
338
+ // shortId 匹配
339
+ if (toShortId(id) === approvalId) { fullId = id; break; }
340
+ }
341
+ // 也在 requestCache 中查找(handleResolved 可能已清除 pending)
342
+ if (fullId === approvalId && !this.requestCache.has(approvalId)) {
343
+ for (const id of this.requestCache.keys()) {
344
+ if (id.replace(/^(exec|plugin):/, "") === approvalId) { fullId = id; break; }
345
+ }
346
+ }
347
+ }
348
+
349
+ const kind = resolveApprovalKind(fullId);
350
+ const method = kind === "plugin" ? "plugin.approval.resolve" : "exec.approval.resolve";
351
+ const isPending = this.pending.has(fullId);
352
+ const isCached = this.requestCache.has(fullId);
353
+
354
+ this.opts.log?.info(`[qqbot:${this.opts.accountId}] approval-handler: resolving ${fullId} (input=${approvalId}) kind=${kind} → ${decision}, pending=${isPending}, cached=${isCached}`);
355
+
356
+ try {
357
+ await this.gatewayClient.request(method, { id: fullId, decision });
358
+ this.opts.log?.info(`[qqbot:${this.opts.accountId}] approval-handler: RPC success ${toShortId(fullId)} → ${decision} (method=${method})`);
359
+ return true;
360
+ } catch (err) {
361
+ this.opts.log?.error(`[qqbot:${this.opts.accountId}] approval-handler: resolve failed: ${err}`);
362
+ return false;
363
+ }
364
+ }
365
+
366
+ private handleGatewayEvent(evt: EventFrame): void {
367
+ if (evt.event === "exec.approval.requested") {
368
+ void this.handleRequested(evt.payload as ExecApprovalRequest, "exec");
369
+ } else if (evt.event === "plugin.approval.requested") {
370
+ void this.handleRequested(evt.payload as PluginApprovalRequest, "plugin");
371
+ } else if (evt.event === "exec.approval.resolved") {
372
+ void this.handleResolved(evt.payload as ExecApprovalResolved);
373
+ } else if (evt.event === "plugin.approval.resolved") {
374
+ void this.handleResolved(evt.payload as PluginApprovalResolved);
375
+ }
376
+ }
377
+
378
+ private async handleRequested(
379
+ request: ExecApprovalRequest | PluginApprovalRequest,
380
+ kind: ApprovalKind
381
+ ): Promise<void> {
382
+ const { log, appId, clientSecret, accountId } = this.opts;
383
+ const shortId = toShortId(request.id);
384
+
385
+ // 只处理本账号的请求
386
+ const reqAccountId = (request.request as any).turnSourceAccountId?.trim();
387
+ if (reqAccountId && reqAccountId !== accountId) return;
388
+
389
+ // 解析投递目标
390
+ const sessionKey = (request.request as any).sessionKey;
391
+ const turnSourceTo = (request.request as any).turnSourceTo;
392
+ const target = resolveTarget(sessionKey, turnSourceTo);
393
+ if (!target) {
394
+ log?.info(`[qqbot:${accountId}] approval-handler: no QQ target for ${shortId} (session=${sessionKey})`);
395
+ return;
396
+ }
397
+
398
+ // 缓存请求
399
+ this.requestCache.set(
400
+ request.id,
401
+ kind === "plugin"
402
+ ? { kind: "plugin", request: request as PluginApprovalRequest }
403
+ : { kind: "exec", request: request as ExecApprovalRequest }
404
+ );
405
+
406
+ log?.info(`[qqbot:${accountId}] approval-handler: sending ${kind} approval ${shortId} to ${target.type}:${target.id}`);
407
+
408
+ const text = kind === "plugin"
409
+ ? buildPluginApprovalText(request as PluginApprovalRequest)
410
+ : buildExecApprovalText(request as ExecApprovalRequest);
411
+
412
+ const keyboard = buildApprovalKeyboard(request.id);
413
+
414
+ const timeoutMs = kind === "plugin"
415
+ ? ((request as PluginApprovalRequest).request.timeoutMs ?? 120_000)
416
+ : Math.max(0, (request as ExecApprovalRequest).expiresAtMs - Date.now());
417
+
418
+ // 短暂延迟,确保框架侧 waitDecision 已就绪,避免时序竞争
419
+ await new Promise((r) => setTimeout(r, 2000));
420
+
421
+ try {
422
+ const token = await getAccessToken(appId, clientSecret);
423
+ if (target.type === "c2c") {
424
+ await sendC2CMessageWithInlineKeyboard(token, target.id, text, keyboard);
425
+ } else {
426
+ await sendGroupMessageWithInlineKeyboard(token, target.id, text, keyboard);
427
+ }
428
+ log?.info(`[qqbot:${accountId}] approval-handler: sent ${kind} approval ${shortId}`);
429
+
430
+ const timeoutId = setTimeout(() => {
431
+ this.handleTimeout(request.id, target);
432
+ }, timeoutMs + 2_000);
433
+
434
+ this.pending.set(request.id, { targets: [target], timeoutId });
435
+ } catch (err) {
436
+ this.requestCache.delete(request.id);
437
+ log?.error(`[qqbot:${accountId}] approval-handler: failed to send approval ${shortId}: ${err}`);
438
+ }
439
+ }
440
+
441
+ private async handleResolved(
442
+ resolved: ExecApprovalResolved | PluginApprovalResolved
443
+ ): Promise<void> {
444
+ const entry = this.pending.get(resolved.id);
445
+ const resolvedBy = (resolved as any).resolvedBy ?? "unknown";
446
+ const kind = resolveApprovalKind(resolved.id);
447
+
448
+ this.opts.log?.info(
449
+ `[qqbot:${this.opts.accountId}] approval-handler: gateway confirmed ${toShortId(resolved.id)} → ${resolved.decision} (kind=${kind}, resolvedBy=${resolvedBy}, wasPending=${!!entry})`
450
+ );
451
+
452
+ if (!entry) return;
453
+
454
+ clearTimeout(entry.timeoutId);
455
+ this.pending.delete(resolved.id);
456
+ this.requestCache.delete(resolved.id);
457
+ // 框架 Forwarder 负责发送 resolved 通知(已通过 buildResolvedPayload=null 抑制),此处不重复发送
458
+ }
459
+
460
+ private async handleTimeout(
461
+ approvalId: string,
462
+ target: { type: "c2c" | "group"; id: string }
463
+ ): Promise<void> {
464
+ const { log, accountId } = this.opts;
465
+ if (!this.pending.has(approvalId)) return;
466
+ this.pending.delete(approvalId);
467
+ this.requestCache.delete(approvalId);
468
+ log?.info(`[qqbot:${accountId}] approval-handler: timeout ${toShortId(approvalId)}`);
469
+ // 超时由框架处理,此处仅清理状态,不重复发消息
470
+ }
471
+ }
472
+
473
+ // ─── 模块级 handler 注册 ────────────────────────────────────
474
+
475
+ const _handlers = new Map<string, QQBotApprovalHandler>();
476
+
477
+ /** 审批功能是否可用(gateway-runtime 模块加载成功则为 true) */
478
+ let _approvalFeatureAvailable = false;
479
+
480
+ export function isApprovalFeatureAvailable(): boolean {
481
+ return _approvalFeatureAvailable;
482
+ }
483
+
484
+ export function setApprovalFeatureAvailable(available: boolean): void {
485
+ _approvalFeatureAvailable = available;
486
+ }
487
+
488
+ export function registerApprovalHandler(accountId: string, handler: QQBotApprovalHandler): void {
489
+ _handlers.set(accountId, handler);
490
+ }
491
+
492
+ export function unregisterApprovalHandler(accountId: string): void {
493
+ _handlers.delete(accountId);
494
+ }
495
+
496
+ export function getApprovalHandler(accountId: string): QQBotApprovalHandler | undefined {
497
+ return _handlers.get(accountId);
498
+ }
499
+
500
+ export function findApprovalHandlerForShortId(shortId: string): QQBotApprovalHandler | undefined {
501
+ for (const handler of _handlers.values()) {
502
+ if (handler.hasShortId(shortId)) return handler;
503
+ }
504
+ return undefined;
505
+ }
package/src/channel.ts CHANGED
@@ -14,6 +14,24 @@ import { qqbotOnboardingAdapter } from "./onboarding.js";
14
14
  import { getQQBotRuntime } from "./runtime.js";
15
15
  import { saveCredentialBackup, loadCredentialBackup } from "./credential-backup.js";
16
16
  import { initApiConfig } from "./api.js";
17
+ import { getApprovalHandler } from "./approval-handler.js";
18
+
19
+ /** 检查 payload 是否为审批消息(与 getExecApprovalReplyMetadata 等效,内联避免版本兼容问题) */
20
+ function isApprovalPayload(payload: unknown): boolean {
21
+ if (!payload || typeof payload !== "object") return false;
22
+ const p = payload as Record<string, unknown>;
23
+ // channelData.execApproval 存在 → exec/plugin approval pending/resolved
24
+ const cd = p.channelData;
25
+ if (cd && typeof cd === "object" && !Array.isArray(cd)) {
26
+ const execApproval = (cd as Record<string, unknown>).execApproval;
27
+ if (execApproval && typeof execApproval === "object" && !Array.isArray(execApproval)) {
28
+ return true;
29
+ }
30
+ }
31
+ // text 匹配兜底:框架渲染的审批纯文本通知
32
+ const text = typeof p.text === "string" ? p.text : "";
33
+ return /(?:Plugin|Exec) approval (?:required|allowed|denied|expired)/i.test(text);
34
+ }
17
35
 
18
36
  /** QQ Bot 单条消息文本长度上限 */
19
37
  export const TEXT_CHUNK_LIMIT = 5000;
@@ -272,6 +290,10 @@ export const qqbotPlugin: ChannelPlugin<ResolvedQQBotAccount> = {
272
290
  chunker: (text, limit) => getQQBotRuntime().channel.text.chunkMarkdownText(text, limit),
273
291
  chunkerMode: "markdown",
274
292
  textChunkLimit: 5000,
293
+ // 3.31+ outbound 路径:dispatch-from-config → shouldSuppressLocalExecApprovalPrompt → outbound.shouldSuppressLocalPayloadPrompt
294
+ shouldSuppressLocalPayloadPrompt: ({ accountId, payload }: any) =>
295
+ getApprovalHandler(accountId ?? "") != null &&
296
+ isApprovalPayload(payload),
275
297
  sendText: async ({ to, text, accountId, replyToId, cfg }) => {
276
298
  console.log(`[qqbot:channel] sendText called — accountId=${accountId}, to=${to}, replyToId=${replyToId}, text.length=${text?.length ?? 0}`);
277
299
  console.log(`[qqbot:channel] sendText text preview: ${text?.slice(0, 100)}${(text?.length ?? 0) > 100 ? "..." : ""}`);
@@ -438,6 +460,60 @@ export const qqbotPlugin: ChannelPlugin<ResolvedQQBotAccount> = {
438
460
  lastOutboundAt: runtime?.lastOutboundAt ?? null,
439
461
  }),
440
462
  },
463
+ // QQBot approval-handler 通过独立 WS 连接自行处理 exec + plugin 审批消息投递(带 Inline Keyboard),
464
+ // 完全屏蔽框架 Forwarder 的纯文本通知。
465
+ //
466
+ // ── 3.28 扁平结构 ──
467
+ execApprovals: {
468
+ // 3.28 框架通过此方法判断 channel 是否支持审批
469
+ getInitiatingSurfaceState: ({ accountId }: { cfg: any; accountId?: string | null }) => {
470
+ return getApprovalHandler(accountId ?? "") != null
471
+ ? { kind: "enabled" as const }
472
+ : { kind: "disabled" as const };
473
+ },
474
+ shouldSuppressForwardingFallback: (...args: any[]) => {
475
+ console.log("[QQBot] shouldSuppressForwardingFallback called", JSON.stringify(args?.[0]?.target ?? null));
476
+ return true;
477
+ },
478
+ shouldSuppressLocalPrompt: ({ accountId, payload }: any) =>
479
+ getApprovalHandler(accountId ?? "") != null &&
480
+ isApprovalPayload(payload),
481
+ buildPendingPayload: () => null,
482
+ buildResolvedPayload: () => null,
483
+ },
484
+ // ── 3.31+ 嵌套结构 ──
485
+ // auth 和 approvals 是 ChannelPlugin 顶层平级字段
486
+ //
487
+ // QQBot 审批模型:
488
+ // - QQBotApprovalHandler 通过独立 WS 自行投递带 Inline Keyboard 的审批消息
489
+ // - 用户点击按钮 → INTERACTION_CREATE → resolveApproval → gateway RPC
490
+ // - /approve 文本命令作为 URGENT_COMMAND 直接入队交给框架处理
491
+ auth: {
492
+ authorizeActorAction: () => ({ authorized: true }),
493
+ getActionAvailabilityState: ({ accountId }: {
494
+ cfg: any; accountId?: string | null; action: "approve";
495
+ }) => {
496
+ return getApprovalHandler(accountId ?? "") != null
497
+ ? { kind: "enabled" as const }
498
+ : { kind: "disabled" as const };
499
+ },
500
+ },
501
+ approvals: {
502
+ delivery: {
503
+ hasConfiguredDmRoute: () => true,
504
+ shouldSuppressForwardingFallback: () => true,
505
+ },
506
+ render: {
507
+ exec: {
508
+ buildPendingPayload: () => null,
509
+ buildResolvedPayload: () => null,
510
+ },
511
+ plugin: {
512
+ buildPendingPayload: () => null,
513
+ buildResolvedPayload: () => null,
514
+ },
515
+ },
516
+ },
441
517
  };
442
518
 
443
519
  // ============ 独立的 mention 工具函数(供 gateway.ts 等直接调用) ============