@leoqlin/openclaw-qqbot 1.6.7-alpha1

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.
Files changed (218) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +484 -0
  3. package/README.zh.md +479 -0
  4. package/bin/qqbot-cli.js +243 -0
  5. package/dist/index.d.ts +17 -0
  6. package/dist/index.js +26 -0
  7. package/dist/src/admin-resolver.d.ts +33 -0
  8. package/dist/src/admin-resolver.js +157 -0
  9. package/dist/src/api.d.ts +301 -0
  10. package/dist/src/api.js +890 -0
  11. package/dist/src/channel.d.ts +29 -0
  12. package/dist/src/channel.js +452 -0
  13. package/dist/src/config.d.ts +56 -0
  14. package/dist/src/config.js +278 -0
  15. package/dist/src/credential-backup.d.ts +31 -0
  16. package/dist/src/credential-backup.js +66 -0
  17. package/dist/src/deliver-debounce.d.ts +74 -0
  18. package/dist/src/deliver-debounce.js +174 -0
  19. package/dist/src/gateway.d.ts +18 -0
  20. package/dist/src/gateway.js +2005 -0
  21. package/dist/src/group-history.d.ts +136 -0
  22. package/dist/src/group-history.js +226 -0
  23. package/dist/src/image-server.d.ts +87 -0
  24. package/dist/src/image-server.js +570 -0
  25. package/dist/src/inbound-attachments.d.ts +60 -0
  26. package/dist/src/inbound-attachments.js +248 -0
  27. package/dist/src/known-users.d.ts +100 -0
  28. package/dist/src/known-users.js +263 -0
  29. package/dist/src/message-gating.d.ts +53 -0
  30. package/dist/src/message-gating.js +107 -0
  31. package/dist/src/message-queue.d.ts +89 -0
  32. package/dist/src/message-queue.js +257 -0
  33. package/dist/src/onboarding.d.ts +10 -0
  34. package/dist/src/onboarding.js +203 -0
  35. package/dist/src/outbound-deliver.d.ts +48 -0
  36. package/dist/src/outbound-deliver.js +392 -0
  37. package/dist/src/outbound.d.ts +205 -0
  38. package/dist/src/outbound.js +938 -0
  39. package/dist/src/proactive.d.ts +170 -0
  40. package/dist/src/proactive.js +399 -0
  41. package/dist/src/ref-index-store.d.ts +101 -0
  42. package/dist/src/ref-index-store.js +298 -0
  43. package/dist/src/reply-dispatcher.d.ts +35 -0
  44. package/dist/src/reply-dispatcher.js +311 -0
  45. package/dist/src/request-context.d.ts +25 -0
  46. package/dist/src/request-context.js +37 -0
  47. package/dist/src/runtime.d.ts +3 -0
  48. package/dist/src/runtime.js +10 -0
  49. package/dist/src/session-store.d.ts +52 -0
  50. package/dist/src/session-store.js +254 -0
  51. package/dist/src/slash-commands.d.ts +77 -0
  52. package/dist/src/slash-commands.js +1866 -0
  53. package/dist/src/startup-greeting.d.ts +30 -0
  54. package/dist/src/startup-greeting.js +97 -0
  55. package/dist/src/streaming.d.ts +247 -0
  56. package/dist/src/streaming.js +899 -0
  57. package/dist/src/stt.d.ts +21 -0
  58. package/dist/src/stt.js +70 -0
  59. package/dist/src/tools/channel.d.ts +16 -0
  60. package/dist/src/tools/channel.js +234 -0
  61. package/dist/src/tools/remind.d.ts +2 -0
  62. package/dist/src/tools/remind.js +256 -0
  63. package/dist/src/types.d.ts +367 -0
  64. package/dist/src/types.js +17 -0
  65. package/dist/src/typing-keepalive.d.ts +27 -0
  66. package/dist/src/typing-keepalive.js +64 -0
  67. package/dist/src/update-checker.d.ts +36 -0
  68. package/dist/src/update-checker.js +171 -0
  69. package/dist/src/utils/audio-convert.d.ts +98 -0
  70. package/dist/src/utils/audio-convert.js +755 -0
  71. package/dist/src/utils/chunked-upload.d.ts +68 -0
  72. package/dist/src/utils/chunked-upload.js +341 -0
  73. package/dist/src/utils/file-utils.d.ts +61 -0
  74. package/dist/src/utils/file-utils.js +172 -0
  75. package/dist/src/utils/image-size.d.ts +51 -0
  76. package/dist/src/utils/image-size.js +234 -0
  77. package/dist/src/utils/media-send.d.ts +158 -0
  78. package/dist/src/utils/media-send.js +499 -0
  79. package/dist/src/utils/media-tags.d.ts +14 -0
  80. package/dist/src/utils/media-tags.js +165 -0
  81. package/dist/src/utils/payload.d.ts +112 -0
  82. package/dist/src/utils/payload.js +186 -0
  83. package/dist/src/utils/pkg-version.d.ts +5 -0
  84. package/dist/src/utils/pkg-version.js +61 -0
  85. package/dist/src/utils/platform.d.ts +137 -0
  86. package/dist/src/utils/platform.js +390 -0
  87. package/dist/src/utils/ssrf-guard.d.ts +25 -0
  88. package/dist/src/utils/ssrf-guard.js +91 -0
  89. package/dist/src/utils/text-parsing.d.ts +36 -0
  90. package/dist/src/utils/text-parsing.js +75 -0
  91. package/dist/src/utils/upload-cache.d.ts +34 -0
  92. package/dist/src/utils/upload-cache.js +93 -0
  93. package/index.ts +31 -0
  94. package/node_modules/@eshaz/web-worker/LICENSE +201 -0
  95. package/node_modules/@eshaz/web-worker/README.md +134 -0
  96. package/node_modules/@eshaz/web-worker/browser.js +17 -0
  97. package/node_modules/@eshaz/web-worker/cjs/browser.js +16 -0
  98. package/node_modules/@eshaz/web-worker/cjs/node.js +219 -0
  99. package/node_modules/@eshaz/web-worker/index.d.ts +4 -0
  100. package/node_modules/@eshaz/web-worker/node.js +223 -0
  101. package/node_modules/@eshaz/web-worker/package.json +54 -0
  102. package/node_modules/@wasm-audio-decoders/common/index.js +5 -0
  103. package/node_modules/@wasm-audio-decoders/common/package.json +36 -0
  104. package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderCommon.js +231 -0
  105. package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderWorker.js +129 -0
  106. package/node_modules/@wasm-audio-decoders/common/src/puff/README +67 -0
  107. package/node_modules/@wasm-audio-decoders/common/src/puff/build_puff.js +31 -0
  108. package/node_modules/@wasm-audio-decoders/common/src/puff/puff.c +863 -0
  109. package/node_modules/@wasm-audio-decoders/common/src/puff/puff.h +35 -0
  110. package/node_modules/@wasm-audio-decoders/common/src/utilities.js +3 -0
  111. package/node_modules/@wasm-audio-decoders/common/types.d.ts +7 -0
  112. package/node_modules/mpg123-decoder/README.md +265 -0
  113. package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js +185 -0
  114. package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js.map +1 -0
  115. package/node_modules/mpg123-decoder/index.js +8 -0
  116. package/node_modules/mpg123-decoder/package.json +58 -0
  117. package/node_modules/mpg123-decoder/src/EmscriptenWasm.js +464 -0
  118. package/node_modules/mpg123-decoder/src/MPEGDecoder.js +200 -0
  119. package/node_modules/mpg123-decoder/src/MPEGDecoderWebWorker.js +21 -0
  120. package/node_modules/mpg123-decoder/types.d.ts +30 -0
  121. package/node_modules/silk-wasm/LICENSE +21 -0
  122. package/node_modules/silk-wasm/README.md +85 -0
  123. package/node_modules/silk-wasm/lib/index.cjs +16 -0
  124. package/node_modules/silk-wasm/lib/index.d.ts +70 -0
  125. package/node_modules/silk-wasm/lib/index.mjs +16 -0
  126. package/node_modules/silk-wasm/lib/silk.wasm +0 -0
  127. package/node_modules/silk-wasm/lib/utils.d.ts +4 -0
  128. package/node_modules/silk-wasm/package.json +39 -0
  129. package/node_modules/simple-yenc/.github/FUNDING.yml +1 -0
  130. package/node_modules/simple-yenc/.prettierignore +1 -0
  131. package/node_modules/simple-yenc/LICENSE +7 -0
  132. package/node_modules/simple-yenc/README.md +163 -0
  133. package/node_modules/simple-yenc/dist/esm.js +1 -0
  134. package/node_modules/simple-yenc/dist/index.js +1 -0
  135. package/node_modules/simple-yenc/package.json +50 -0
  136. package/node_modules/simple-yenc/rollup.config.js +27 -0
  137. package/node_modules/simple-yenc/src/simple-yenc.js +302 -0
  138. package/node_modules/ws/LICENSE +20 -0
  139. package/node_modules/ws/README.md +548 -0
  140. package/node_modules/ws/browser.js +8 -0
  141. package/node_modules/ws/index.js +22 -0
  142. package/node_modules/ws/lib/buffer-util.js +131 -0
  143. package/node_modules/ws/lib/constants.js +19 -0
  144. package/node_modules/ws/lib/event-target.js +292 -0
  145. package/node_modules/ws/lib/extension.js +203 -0
  146. package/node_modules/ws/lib/limiter.js +55 -0
  147. package/node_modules/ws/lib/permessage-deflate.js +528 -0
  148. package/node_modules/ws/lib/receiver.js +706 -0
  149. package/node_modules/ws/lib/sender.js +602 -0
  150. package/node_modules/ws/lib/stream.js +161 -0
  151. package/node_modules/ws/lib/subprotocol.js +62 -0
  152. package/node_modules/ws/lib/validation.js +152 -0
  153. package/node_modules/ws/lib/websocket-server.js +554 -0
  154. package/node_modules/ws/lib/websocket.js +1393 -0
  155. package/node_modules/ws/package.json +70 -0
  156. package/node_modules/ws/wrapper.mjs +21 -0
  157. package/openclaw.plugin.json +17 -0
  158. package/package.json +70 -0
  159. package/preload.cjs +33 -0
  160. package/scripts/cleanup-legacy-plugins.sh +124 -0
  161. package/scripts/link-sdk-core.cjs +185 -0
  162. package/scripts/postinstall-link-sdk.js +126 -0
  163. package/scripts/proactive-api-server.ts +369 -0
  164. package/scripts/send-proactive.ts +293 -0
  165. package/scripts/set-markdown.sh +156 -0
  166. package/scripts/test-sendmedia.ts +116 -0
  167. package/scripts/upgrade-via-npm.ps1 +460 -0
  168. package/scripts/upgrade-via-npm.sh +652 -0
  169. package/scripts/upgrade-via-source.sh +1026 -0
  170. package/skills/qqbot-channel/SKILL.md +263 -0
  171. package/skills/qqbot-channel/references/api_references.md +521 -0
  172. package/skills/qqbot-media/SKILL.md +60 -0
  173. package/skills/qqbot-remind/SKILL.md +159 -0
  174. package/src/admin-resolver.ts +181 -0
  175. package/src/api.ts +1284 -0
  176. package/src/channel.ts +477 -0
  177. package/src/config.ts +347 -0
  178. package/src/credential-backup.ts +72 -0
  179. package/src/deliver-debounce.ts +229 -0
  180. package/src/gateway.ts +2245 -0
  181. package/src/group-history.ts +328 -0
  182. package/src/image-server.ts +675 -0
  183. package/src/inbound-attachments.ts +321 -0
  184. package/src/known-users.ts +353 -0
  185. package/src/message-gating.ts +190 -0
  186. package/src/message-queue.ts +352 -0
  187. package/src/onboarding.ts +274 -0
  188. package/src/openclaw-plugin-sdk.d.ts +587 -0
  189. package/src/outbound-deliver.ts +473 -0
  190. package/src/outbound.ts +1131 -0
  191. package/src/proactive.ts +530 -0
  192. package/src/ref-index-store.ts +412 -0
  193. package/src/reply-dispatcher.ts +334 -0
  194. package/src/request-context.ts +49 -0
  195. package/src/runtime.ts +14 -0
  196. package/src/session-store.ts +303 -0
  197. package/src/slash-commands.ts +2030 -0
  198. package/src/startup-greeting.ts +120 -0
  199. package/src/streaming.ts +1077 -0
  200. package/src/stt.ts +86 -0
  201. package/src/tools/channel.ts +281 -0
  202. package/src/tools/remind.ts +308 -0
  203. package/src/types.ts +391 -0
  204. package/src/typing-keepalive.ts +59 -0
  205. package/src/update-checker.ts +186 -0
  206. package/src/utils/audio-convert.ts +859 -0
  207. package/src/utils/chunked-upload.ts +483 -0
  208. package/src/utils/file-utils.ts +193 -0
  209. package/src/utils/image-size.ts +266 -0
  210. package/src/utils/media-send.ts +631 -0
  211. package/src/utils/media-tags.ts +183 -0
  212. package/src/utils/payload.ts +265 -0
  213. package/src/utils/pkg-version.ts +64 -0
  214. package/src/utils/platform.ts +435 -0
  215. package/src/utils/ssrf-guard.ts +102 -0
  216. package/src/utils/text-parsing.ts +85 -0
  217. package/src/utils/upload-cache.ts +128 -0
  218. package/tsconfig.json +16 -0
@@ -0,0 +1,21 @@
1
+ /**
2
+ * 通用 OpenAI 兼容 STT(语音转文字)
3
+ *
4
+ * 为什么在插件侧做 STT 而不走框架管道?
5
+ * 框架的 applyMediaUnderstanding 同时执行 runCapability("audio") 和 extractFileBlocks。
6
+ * 后者会把 WAV 文件的 PCM 二进制当文本注入 Body(looksLikeUtf8Text 误判),导致 context 爆炸。
7
+ * 在插件侧完成 STT 后不把 WAV 放入 MediaPaths,即可规避此框架 bug。
8
+ *
9
+ * 配置解析策略(与 TTS 统一的两级回退):
10
+ * 1. 优先 channels.qqbot.stt(插件专属配置)
11
+ * 2. 回退 tools.media.audio.models[0](框架级配置)
12
+ * 3. 再从 models.providers.[provider] 继承 apiKey/baseUrl
13
+ * 4. 支持任何 OpenAI 兼容的 STT 服务
14
+ */
15
+ export interface STTConfig {
16
+ baseUrl: string;
17
+ apiKey: string;
18
+ model: string;
19
+ }
20
+ export declare function resolveSTTConfig(cfg: Record<string, unknown>): STTConfig | null;
21
+ export declare function transcribeAudio(audioPath: string, cfg: Record<string, unknown>): Promise<string | null>;
@@ -0,0 +1,70 @@
1
+ /**
2
+ * 通用 OpenAI 兼容 STT(语音转文字)
3
+ *
4
+ * 为什么在插件侧做 STT 而不走框架管道?
5
+ * 框架的 applyMediaUnderstanding 同时执行 runCapability("audio") 和 extractFileBlocks。
6
+ * 后者会把 WAV 文件的 PCM 二进制当文本注入 Body(looksLikeUtf8Text 误判),导致 context 爆炸。
7
+ * 在插件侧完成 STT 后不把 WAV 放入 MediaPaths,即可规避此框架 bug。
8
+ *
9
+ * 配置解析策略(与 TTS 统一的两级回退):
10
+ * 1. 优先 channels.qqbot.stt(插件专属配置)
11
+ * 2. 回退 tools.media.audio.models[0](框架级配置)
12
+ * 3. 再从 models.providers.[provider] 继承 apiKey/baseUrl
13
+ * 4. 支持任何 OpenAI 兼容的 STT 服务
14
+ */
15
+ import * as fs from "node:fs";
16
+ import path from "node:path";
17
+ import { sanitizeFileName } from "./utils/platform.js";
18
+ export function resolveSTTConfig(cfg) {
19
+ const c = cfg;
20
+ // 优先使用 channels.qqbot.stt(插件专属配置)
21
+ const channelStt = c?.channels?.qqbot?.stt;
22
+ if (channelStt && channelStt.enabled !== false) {
23
+ const providerId = channelStt?.provider || "openai";
24
+ const providerCfg = c?.models?.providers?.[providerId];
25
+ const baseUrl = channelStt?.baseUrl || providerCfg?.baseUrl;
26
+ const apiKey = channelStt?.apiKey || providerCfg?.apiKey;
27
+ const model = channelStt?.model || "whisper-1";
28
+ if (baseUrl && apiKey) {
29
+ return { baseUrl: baseUrl.replace(/\/+$/, ""), apiKey, model };
30
+ }
31
+ }
32
+ // 回退到 tools.media.audio.models[0](框架级配置)
33
+ const audioModelEntry = c?.tools?.media?.audio?.models?.[0];
34
+ if (audioModelEntry) {
35
+ const providerId = audioModelEntry?.provider || "openai";
36
+ const providerCfg = c?.models?.providers?.[providerId];
37
+ const baseUrl = audioModelEntry?.baseUrl || providerCfg?.baseUrl;
38
+ const apiKey = audioModelEntry?.apiKey || providerCfg?.apiKey;
39
+ const model = audioModelEntry?.model || "whisper-1";
40
+ if (baseUrl && apiKey) {
41
+ return { baseUrl: baseUrl.replace(/\/+$/, ""), apiKey, model };
42
+ }
43
+ }
44
+ return null;
45
+ }
46
+ export async function transcribeAudio(audioPath, cfg) {
47
+ const sttCfg = resolveSTTConfig(cfg);
48
+ if (!sttCfg)
49
+ return null;
50
+ const fileBuffer = fs.readFileSync(audioPath);
51
+ const fileName = sanitizeFileName(path.basename(audioPath));
52
+ const mime = fileName.endsWith(".wav") ? "audio/wav"
53
+ : fileName.endsWith(".mp3") ? "audio/mpeg"
54
+ : fileName.endsWith(".ogg") ? "audio/ogg"
55
+ : "application/octet-stream";
56
+ const form = new FormData();
57
+ form.append("file", new Blob([fileBuffer], { type: mime }), fileName);
58
+ form.append("model", sttCfg.model);
59
+ const resp = await fetch(`${sttCfg.baseUrl}/audio/transcriptions`, {
60
+ method: "POST",
61
+ headers: { "Authorization": `Bearer ${sttCfg.apiKey}` },
62
+ body: form,
63
+ });
64
+ if (!resp.ok) {
65
+ const detail = await resp.text().catch(() => "");
66
+ throw new Error(`STT failed (HTTP ${resp.status}): ${detail.slice(0, 300)}`);
67
+ }
68
+ const result = await resp.json();
69
+ return result.text?.trim() || null;
70
+ }
@@ -0,0 +1,16 @@
1
+ import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
2
+ /**
3
+ * 注册 QQ 频道 API 代理工具。
4
+ *
5
+ * 该工具作为 QQ 开放平台频道 API 的 HTTP 代理,自动处理 Token 鉴权。
6
+ * AI 通过 skill 文档了解各接口的路径、方法和参数,构造请求后由此工具代理发送。
7
+ *
8
+ * 支持的能力:
9
+ * - 频道管理(Guild)
10
+ * - 子频道管理(Channel)
11
+ * - 成员管理(Member)
12
+ * - 公告管理(Announces)
13
+ * - 论坛管理(Forum Thread)
14
+ * - 日程管理(Schedule)
15
+ */
16
+ export declare function registerChannelTool(api: OpenClawPluginApi): void;
@@ -0,0 +1,234 @@
1
+ import { resolveQQBotAccount } from "../config.js";
2
+ import { listQQBotAccountIds } from "../config.js";
3
+ import { getAccessToken } from "../api.js";
4
+ // ========== 常量 ==========
5
+ const API_BASE = "https://api.sgroup.qq.com";
6
+ const DEFAULT_TIMEOUT_MS = 30000;
7
+ // ========== JSON Schema ==========
8
+ const ChannelApiSchema = {
9
+ type: "object",
10
+ properties: {
11
+ method: {
12
+ type: "string",
13
+ description: "HTTP 请求方法。可选值:GET, POST, PUT, PATCH, DELETE",
14
+ enum: ["GET", "POST", "PUT", "PATCH", "DELETE"],
15
+ },
16
+ path: {
17
+ type: "string",
18
+ description: "API 路径(不含域名),占位符需替换为实际值。" +
19
+ "示例:/users/@me/guilds, /guilds/{guild_id}/channels, /channels/{channel_id}",
20
+ },
21
+ body: {
22
+ type: "object",
23
+ description: "请求体(JSON),用于 POST/PUT/PATCH 请求。" +
24
+ "GET/DELETE 请求不需要此参数。",
25
+ },
26
+ query: {
27
+ type: "object",
28
+ description: "URL 查询参数(键值对),会拼接到路径后面。" +
29
+ "如 { \"limit\": \"100\", \"after\": \"0\" } 会拼接为 ?limit=100&after=0",
30
+ additionalProperties: { type: "string" },
31
+ },
32
+ },
33
+ required: ["method", "path"],
34
+ };
35
+ // ========== 工具函数 ==========
36
+ function json(data) {
37
+ return {
38
+ content: [{ type: "text", text: JSON.stringify(data, null, 2) }],
39
+ details: data,
40
+ };
41
+ }
42
+ /**
43
+ * 构建完整的请求 URL
44
+ */
45
+ function buildUrl(path, query) {
46
+ let url = `${API_BASE}${path}`;
47
+ if (query && Object.keys(query).length > 0) {
48
+ const params = new URLSearchParams();
49
+ for (const [key, value] of Object.entries(query)) {
50
+ if (value !== undefined && value !== null && value !== "") {
51
+ params.set(key, value);
52
+ }
53
+ }
54
+ const qs = params.toString();
55
+ if (qs) {
56
+ url += `?${qs}`;
57
+ }
58
+ }
59
+ return url;
60
+ }
61
+ /**
62
+ * 校验 path 防止 SSRF
63
+ */
64
+ function validatePath(path) {
65
+ if (!path.startsWith("/")) {
66
+ return "path 必须以 / 开头";
67
+ }
68
+ if (path.includes("..") || path.includes("//")) {
69
+ return "path 不允许包含 .. 或 //";
70
+ }
71
+ // 只允许合法的 URL path 字符
72
+ if (!/^\/[a-zA-Z0-9\-._~:@!$&'()*+,;=/%]+$/.test(path) && path !== "/") {
73
+ return "path 包含非法字符";
74
+ }
75
+ return null;
76
+ }
77
+ // ========== 注册入口 ==========
78
+ /**
79
+ * 注册 QQ 频道 API 代理工具。
80
+ *
81
+ * 该工具作为 QQ 开放平台频道 API 的 HTTP 代理,自动处理 Token 鉴权。
82
+ * AI 通过 skill 文档了解各接口的路径、方法和参数,构造请求后由此工具代理发送。
83
+ *
84
+ * 支持的能力:
85
+ * - 频道管理(Guild)
86
+ * - 子频道管理(Channel)
87
+ * - 成员管理(Member)
88
+ * - 公告管理(Announces)
89
+ * - 论坛管理(Forum Thread)
90
+ * - 日程管理(Schedule)
91
+ */
92
+ export function registerChannelTool(api) {
93
+ const cfg = api.config;
94
+ if (!cfg) {
95
+ console.log("[qqbot-channel-api] No config available, skipping");
96
+ return;
97
+ }
98
+ const accountIds = listQQBotAccountIds(cfg);
99
+ if (accountIds.length === 0) {
100
+ console.log("[qqbot-channel-api] No QQBot accounts configured, skipping");
101
+ return;
102
+ }
103
+ const firstAccountId = accountIds[0];
104
+ const account = resolveQQBotAccount(cfg, firstAccountId);
105
+ if (!account.appId || !account.clientSecret) {
106
+ console.log("[qqbot-channel-api] Account not fully configured, skipping");
107
+ return;
108
+ }
109
+ api.registerTool({
110
+ name: "qqbot_channel_api",
111
+ label: "QQBot Channel API",
112
+ description: "QQ 开放平台频道 API HTTP 代理,自动填充鉴权 Token。" +
113
+ "常用接口速查:" +
114
+ "频道列表 GET /users/@me/guilds | " +
115
+ "子频道列表 GET /guilds/{guild_id}/channels | " +
116
+ "子频道详情 GET /channels/{channel_id} | " +
117
+ "创建子频道 POST /guilds/{guild_id}/channels | " +
118
+ "成员列表 GET /guilds/{guild_id}/members?after=0&limit=100 | " +
119
+ "成员详情 GET /guilds/{guild_id}/members/{user_id} | " +
120
+ "帖子列表 GET /channels/{channel_id}/threads | " +
121
+ "发帖 PUT /channels/{channel_id}/threads | " +
122
+ "创建公告 POST /guilds/{guild_id}/announces | " +
123
+ "创建日程 POST /channels/{channel_id}/schedules。" +
124
+ "更多接口和参数详情请阅读 qqbot-channel skill。",
125
+ parameters: ChannelApiSchema,
126
+ async execute(_toolCallId, params) {
127
+ const p = params;
128
+ // 参数校验
129
+ if (!p.method) {
130
+ return json({ error: "method 为必填参数" });
131
+ }
132
+ if (!p.path) {
133
+ return json({ error: "path 为必填参数" });
134
+ }
135
+ const method = p.method.toUpperCase();
136
+ if (!["GET", "POST", "PUT", "PATCH", "DELETE"].includes(method)) {
137
+ return json({ error: `不支持的 HTTP 方法: ${method},可选值:GET, POST, PUT, PATCH, DELETE` });
138
+ }
139
+ // 路径安全校验
140
+ const pathError = validatePath(p.path);
141
+ if (pathError) {
142
+ return json({ error: pathError });
143
+ }
144
+ // GET/DELETE 不应有 body
145
+ if ((method === "GET" || method === "DELETE") && p.body && Object.keys(p.body).length > 0) {
146
+ console.log(`[qqbot-channel-api] ${method} request with body, body will be ignored`);
147
+ }
148
+ try {
149
+ // 获取鉴权 Token
150
+ const accessToken = await getAccessToken(account.appId, account.clientSecret);
151
+ // 构建请求
152
+ const url = buildUrl(p.path, p.query);
153
+ const headers = {
154
+ Authorization: `QQBot ${accessToken}`,
155
+ "Content-Type": "application/json",
156
+ };
157
+ const controller = new AbortController();
158
+ const timeoutId = setTimeout(() => controller.abort(), DEFAULT_TIMEOUT_MS);
159
+ const fetchOptions = {
160
+ method,
161
+ headers,
162
+ signal: controller.signal,
163
+ };
164
+ // POST/PUT/PATCH 才发送 body
165
+ if (p.body && ["POST", "PUT", "PATCH"].includes(method)) {
166
+ fetchOptions.body = JSON.stringify(p.body);
167
+ }
168
+ console.log(`[qqbot-channel-api] >>> ${method} ${url} (timeout: ${DEFAULT_TIMEOUT_MS}ms)`);
169
+ let res;
170
+ try {
171
+ res = await fetch(url, fetchOptions);
172
+ }
173
+ catch (err) {
174
+ clearTimeout(timeoutId);
175
+ if (err instanceof Error && err.name === "AbortError") {
176
+ console.error(`[qqbot-channel-api] <<< Request timeout after ${DEFAULT_TIMEOUT_MS}ms`);
177
+ return json({ error: `请求超时(${DEFAULT_TIMEOUT_MS}ms)`, path: p.path });
178
+ }
179
+ console.error(`[qqbot-channel-api] <<< Network error:`, err);
180
+ return json({
181
+ error: `网络错误: ${err instanceof Error ? err.message : String(err)}`,
182
+ path: p.path,
183
+ });
184
+ }
185
+ finally {
186
+ clearTimeout(timeoutId);
187
+ }
188
+ console.log(`[qqbot-channel-api] <<< Status: ${res.status} ${res.statusText}`);
189
+ // 解析响应
190
+ const rawBody = await res.text();
191
+ // 空响应(如 DELETE 204)
192
+ if (!rawBody || rawBody.trim() === "") {
193
+ if (res.ok) {
194
+ return json({ success: true, status: res.status, path: p.path });
195
+ }
196
+ return json({
197
+ error: `API 返回 ${res.status} ${res.statusText}`,
198
+ status: res.status,
199
+ path: p.path,
200
+ });
201
+ }
202
+ let data;
203
+ try {
204
+ data = JSON.parse(rawBody);
205
+ }
206
+ catch {
207
+ return json({
208
+ error: "响应解析失败",
209
+ status: res.status,
210
+ raw: rawBody.slice(0, 500),
211
+ path: p.path,
212
+ });
213
+ }
214
+ if (!res.ok) {
215
+ const errData = data;
216
+ return json({
217
+ error: errData.message ?? `API 错误 (HTTP ${res.status})`,
218
+ code: errData.code,
219
+ status: res.status,
220
+ path: p.path,
221
+ detail: data,
222
+ });
223
+ }
224
+ return json(data);
225
+ }
226
+ catch (err) {
227
+ const errMsg = err instanceof Error ? err.message : String(err);
228
+ console.error(`[qqbot-channel-api] Error [${method} ${p.path}]: ${errMsg}`);
229
+ return json({ error: errMsg, path: p.path });
230
+ }
231
+ },
232
+ }, { name: "qqbot_channel_api" });
233
+ console.log("[qqbot-channel-api] Registered QQ channel API proxy tool");
234
+ }
@@ -0,0 +1,2 @@
1
+ import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
2
+ export declare function registerRemindTool(api: OpenClawPluginApi): void;
@@ -0,0 +1,256 @@
1
+ import { getRequestTarget, getRequestAccountId } from "../request-context.js";
2
+ // ========== JSON Schema ==========
3
+ const RemindSchema = {
4
+ type: "object",
5
+ properties: {
6
+ action: {
7
+ type: "string",
8
+ description: "操作类型。add=创建提醒, list=查看已有提醒, remove=删除提醒",
9
+ enum: ["add", "list", "remove"],
10
+ },
11
+ content: {
12
+ type: "string",
13
+ description: '提醒内容,如"喝水"、"开会"。action=add 时必填。',
14
+ },
15
+ to: {
16
+ type: "string",
17
+ description: "投递目标地址(可选)。系统会自动从当前会话获取,通常无需手动填写。" +
18
+ "私聊格式:qqbot:c2c:user_openid,群聊格式:qqbot:group:group_openid。",
19
+ },
20
+ time: {
21
+ type: "string",
22
+ description: "时间描述。支持两种格式:\n" +
23
+ "1. 相对时间:如 \"5m\"(5分钟后)、\"1h\"(1小时后)、\"1h30m\"(1.5小时后)、\"2d\"(2天后)\n" +
24
+ "2. cron 表达式:如 \"0 8 * * *\"(每天8点)、\"0 9 * * 1-5\"(工作日9点)\n" +
25
+ "系统会自动判断:包含空格的视为 cron 表达式(周期提醒),否则视为相对时间(一次性提醒)。\n" +
26
+ "action=add 时必填。",
27
+ },
28
+ timezone: {
29
+ type: "string",
30
+ description: "时区,仅周期提醒(cron)时需要。默认 \"Asia/Shanghai\"。",
31
+ },
32
+ name: {
33
+ type: "string",
34
+ description: "提醒任务名称(可选)。默认自动从 content 截取前 20 字。",
35
+ },
36
+ jobId: {
37
+ type: "string",
38
+ description: "要删除的任务 ID。action=remove 时必填,先用 list 获取。",
39
+ },
40
+ },
41
+ required: ["action"],
42
+ };
43
+ // ========== 工具函数 ==========
44
+ function json(data) {
45
+ return {
46
+ content: [{ type: "text", text: JSON.stringify(data, null, 2) }],
47
+ details: data,
48
+ };
49
+ }
50
+ /**
51
+ * 解析相对时间字符串为毫秒数
52
+ * 支持格式:5m, 1h, 1h30m, 2d, 30s, 1d2h30m 等
53
+ */
54
+ function parseRelativeTime(timeStr) {
55
+ const s = timeStr.trim().toLowerCase();
56
+ // 纯数字 → 视为分钟
57
+ if (/^\d+$/.test(s)) {
58
+ return parseInt(s, 10) * 60_000;
59
+ }
60
+ let totalMs = 0;
61
+ let matched = false;
62
+ const regex = /(\d+(?:\.\d+)?)\s*(d|h|m|s)/g;
63
+ let match;
64
+ while ((match = regex.exec(s)) !== null) {
65
+ matched = true;
66
+ const value = parseFloat(match[1]);
67
+ const unit = match[2];
68
+ switch (unit) {
69
+ case "d":
70
+ totalMs += value * 86_400_000;
71
+ break;
72
+ case "h":
73
+ totalMs += value * 3_600_000;
74
+ break;
75
+ case "m":
76
+ totalMs += value * 60_000;
77
+ break;
78
+ case "s":
79
+ totalMs += value * 1_000;
80
+ break;
81
+ }
82
+ }
83
+ return matched ? Math.round(totalMs) : null;
84
+ }
85
+ /**
86
+ * 判断是否为 cron 表达式(包含空格且有 3~6 段)
87
+ */
88
+ function isCronExpression(timeStr) {
89
+ const parts = timeStr.trim().split(/\s+/);
90
+ return parts.length >= 3 && parts.length <= 6;
91
+ }
92
+ /**
93
+ * 自动生成任务名称
94
+ */
95
+ function generateJobName(content) {
96
+ const trimmed = content.trim();
97
+ const short = trimmed.length > 20 ? trimmed.slice(0, 20) + "…" : trimmed;
98
+ return `提醒: ${short}`;
99
+ }
100
+ /**
101
+ * 构建一次性提醒的 cron 工具参数
102
+ */
103
+ function buildOnceJob(params, delayMs, to, accountId) {
104
+ const atMs = Date.now() + delayMs;
105
+ const content = params.content;
106
+ const name = params.name || generateJobName(content);
107
+ return {
108
+ action: "add",
109
+ job: {
110
+ name,
111
+ schedule: { kind: "at", atMs },
112
+ sessionTarget: "isolated",
113
+ wakeMode: "now",
114
+ deleteAfterRun: true,
115
+ payload: {
116
+ kind: "agentTurn",
117
+ message: buildReminderPrompt(content),
118
+ },
119
+ delivery: {
120
+ mode: "announce",
121
+ channel: "qqbot",
122
+ to,
123
+ accountId,
124
+ },
125
+ },
126
+ };
127
+ }
128
+ /**
129
+ * 构建周期提醒的 cron 工具参数
130
+ */
131
+ function buildCronJob(params, to, accountId) {
132
+ const content = params.content;
133
+ const name = params.name || generateJobName(content);
134
+ const tz = params.timezone || "Asia/Shanghai";
135
+ return {
136
+ action: "add",
137
+ job: {
138
+ name,
139
+ schedule: { kind: "cron", expr: params.time.trim(), tz },
140
+ sessionTarget: "isolated",
141
+ wakeMode: "now",
142
+ payload: {
143
+ kind: "agentTurn",
144
+ message: buildReminderPrompt(content),
145
+ },
146
+ delivery: {
147
+ mode: "announce",
148
+ channel: "qqbot",
149
+ to,
150
+ accountId,
151
+ },
152
+ },
153
+ };
154
+ }
155
+ /**
156
+ * 构建提醒 payload 中的 AI prompt
157
+ */
158
+ function buildReminderPrompt(content) {
159
+ return (`你是一个暖心的提醒助手。请用温暖、有趣的方式提醒用户:${content}。` +
160
+ `要求:(1) 不要回复HEARTBEAT_OK (2) 不要解释你是谁 ` +
161
+ `(3) 直接输出一条暖心的提醒消息 (4) 可以加一句简短的鸡汤或关怀的话 ` +
162
+ `(5) 控制在2-3句话以内 (6) 用emoji点缀`);
163
+ }
164
+ /**
165
+ * 格式化延迟时间为人类可读文本
166
+ */
167
+ function formatDelay(ms) {
168
+ const totalSeconds = Math.round(ms / 1000);
169
+ if (totalSeconds < 60)
170
+ return `${totalSeconds}秒`;
171
+ const totalMinutes = Math.round(ms / 60_000);
172
+ if (totalMinutes < 60)
173
+ return `${totalMinutes}分钟`;
174
+ const hours = Math.floor(totalMinutes / 60);
175
+ const minutes = totalMinutes % 60;
176
+ if (minutes === 0)
177
+ return `${hours}小时`;
178
+ return `${hours}小时${minutes}分钟`;
179
+ }
180
+ // ========== 注册入口 ==========
181
+ export function registerRemindTool(api) {
182
+ api.registerTool({
183
+ name: "qqbot_remind",
184
+ label: "QQBot 定时提醒",
185
+ description: "创建、查询、删除 QQ 定时提醒。" +
186
+ "使用简单参数即可,无需手动构造 cron JSON。\n" +
187
+ "创建提醒:action=add, content=提醒内容, to=目标地址, time=时间\n" +
188
+ "查看提醒:action=list\n" +
189
+ "删除提醒:action=remove, jobId=任务ID(先 list 获取)\n" +
190
+ "时间格式示例:\"5m\"(5分钟后) \"1h\"(1小时后) \"0 8 * * *\"(每天8点)",
191
+ parameters: RemindSchema,
192
+ execute(_toolCallId, params) {
193
+ const p = params;
194
+ // ===== list =====
195
+ if (p.action === "list") {
196
+ return json({
197
+ _instruction: "请立即使用 cron 工具执行以下参数",
198
+ cronParams: { action: "list" },
199
+ });
200
+ }
201
+ // ===== remove =====
202
+ if (p.action === "remove") {
203
+ if (!p.jobId) {
204
+ return json({ error: "action=remove 时 jobId 为必填参数。请先用 action=list 获取 jobId。" });
205
+ }
206
+ return json({
207
+ _instruction: "请立即使用 cron 工具执行以下参数",
208
+ cronParams: { action: "remove", jobId: p.jobId },
209
+ });
210
+ }
211
+ // ===== add =====
212
+ if (!p.content) {
213
+ return json({ error: "action=add 时 content(提醒内容)为必填参数" });
214
+ }
215
+ // 优先使用 AI 传入的 to,否则自动从请求级上下文获取(AsyncLocalStorage)
216
+ const resolvedTo = p.to || getRequestTarget();
217
+ if (!resolvedTo) {
218
+ return json({ error: "action=add 时无法确定投递目标地址。请稍后重试。" });
219
+ }
220
+ // 从请求上下文获取当前账户 ID(多账户场景),fallback 到 "default"
221
+ const resolvedAccountId = getRequestAccountId() || "default";
222
+ if (!p.time) {
223
+ return json({ error: "action=add 时 time(时间)为必填参数。示例:\"5m\"、\"1h30m\"、\"0 8 * * *\"" });
224
+ }
225
+ // 判断是 cron 表达式还是相对时间
226
+ if (isCronExpression(p.time)) {
227
+ // 周期提醒
228
+ const cronJob = buildCronJob(p, resolvedTo, resolvedAccountId);
229
+ return json({
230
+ _instruction: "请立即使用 cron 工具,将 cronParams 的值原样作为参数传入(不要修改或省略任何字段,特别是 delivery.accountId),然后回复用户提醒已设置",
231
+ cronParams: cronJob,
232
+ summary: `⏰ 周期提醒: "${p.content}" (${p.time}, tz=${p.timezone || "Asia/Shanghai"})`,
233
+ });
234
+ }
235
+ // 一次性提醒
236
+ const delayMs = parseRelativeTime(p.time);
237
+ if (!delayMs || delayMs <= 0) {
238
+ return json({
239
+ error: `无法解析时间 "${p.time}"。支持格式:` +
240
+ `相对时间如 "5m"、"1h"、"1h30m"、"2d";` +
241
+ `cron 表达式如 "0 8 * * *"(每天8点)`,
242
+ });
243
+ }
244
+ if (delayMs < 30_000) {
245
+ return json({ error: "提醒时间不能少于 30 秒" });
246
+ }
247
+ const onceJob = buildOnceJob(p, delayMs, resolvedTo, resolvedAccountId);
248
+ return json({
249
+ _instruction: "请立即使用 cron 工具,将 cronParams 的值原样作为参数传入(不要修改或省略任何字段,特别是 delivery.accountId),然后回复用户提醒已设置",
250
+ cronParams: onceJob,
251
+ summary: `⏰ ${formatDelay(delayMs)}后提醒: "${p.content}"`,
252
+ });
253
+ },
254
+ }, { name: "qqbot_remind" });
255
+ console.log("[qqbot-remind] Registered QQBot remind tool");
256
+ }