@gakr-gakr/qqbot 0.1.0

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 (149) hide show
  1. package/api.ts +56 -0
  2. package/autobot.plugin.json +167 -0
  3. package/channel-plugin-api.ts +1 -0
  4. package/index.ts +33 -0
  5. package/package.json +64 -0
  6. package/runtime-api.ts +9 -0
  7. package/secret-contract-api.ts +5 -0
  8. package/setup-entry.ts +13 -0
  9. package/setup-plugin-api.ts +3 -0
  10. package/skills/qqbot-channel/SKILL.md +262 -0
  11. package/skills/qqbot-channel/references/api_references.md +521 -0
  12. package/skills/qqbot-media/SKILL.md +37 -0
  13. package/skills/qqbot-remind/SKILL.md +153 -0
  14. package/src/bridge/approval/capability.ts +225 -0
  15. package/src/bridge/approval/handler-runtime.ts +204 -0
  16. package/src/bridge/bootstrap.ts +135 -0
  17. package/src/bridge/channel-entry.ts +18 -0
  18. package/src/bridge/commands/framework-context-adapter.ts +60 -0
  19. package/src/bridge/commands/framework-registration.ts +66 -0
  20. package/src/bridge/commands/from-parser.ts +60 -0
  21. package/src/bridge/commands/result-dispatcher.ts +76 -0
  22. package/src/bridge/config-shared.ts +132 -0
  23. package/src/bridge/config.ts +176 -0
  24. package/src/bridge/gateway.ts +178 -0
  25. package/src/bridge/logger.ts +31 -0
  26. package/src/bridge/narrowing.ts +31 -0
  27. package/src/bridge/plugin-version.ts +102 -0
  28. package/src/bridge/runtime.ts +25 -0
  29. package/src/bridge/sdk-adapter.ts +164 -0
  30. package/src/bridge/setup/finalize.ts +144 -0
  31. package/src/bridge/setup/surface.ts +34 -0
  32. package/src/bridge/tools/channel.ts +58 -0
  33. package/src/bridge/tools/index.ts +15 -0
  34. package/src/bridge/tools/remind.ts +91 -0
  35. package/src/channel.setup.ts +33 -0
  36. package/src/channel.ts +399 -0
  37. package/src/config-schema.ts +84 -0
  38. package/src/engine/access/index.ts +2 -0
  39. package/src/engine/access/resolve-policy.ts +30 -0
  40. package/src/engine/access/sender-match.ts +55 -0
  41. package/src/engine/access/types.ts +2 -0
  42. package/src/engine/adapter/audio.port.ts +27 -0
  43. package/src/engine/adapter/commands.port.ts +22 -0
  44. package/src/engine/adapter/history.port.ts +52 -0
  45. package/src/engine/adapter/index.ts +76 -0
  46. package/src/engine/adapter/mention-gate.port.ts +50 -0
  47. package/src/engine/adapter/types.ts +38 -0
  48. package/src/engine/api/api-client.ts +212 -0
  49. package/src/engine/api/media-chunked.ts +644 -0
  50. package/src/engine/api/media.ts +218 -0
  51. package/src/engine/api/messages.ts +293 -0
  52. package/src/engine/api/retry.ts +217 -0
  53. package/src/engine/api/routes.ts +95 -0
  54. package/src/engine/api/token.ts +277 -0
  55. package/src/engine/approval/index.ts +224 -0
  56. package/src/engine/commands/builtin/log-helpers.ts +341 -0
  57. package/src/engine/commands/builtin/register-all.ts +17 -0
  58. package/src/engine/commands/builtin/register-approve.ts +201 -0
  59. package/src/engine/commands/builtin/register-basic.ts +95 -0
  60. package/src/engine/commands/builtin/register-clear-storage.ts +187 -0
  61. package/src/engine/commands/builtin/register-logs.ts +20 -0
  62. package/src/engine/commands/builtin/register-streaming.ts +138 -0
  63. package/src/engine/commands/builtin/state.ts +31 -0
  64. package/src/engine/commands/slash-command-auth.ts +88 -0
  65. package/src/engine/commands/slash-command-handler.ts +168 -0
  66. package/src/engine/commands/slash-command-test-support.ts +39 -0
  67. package/src/engine/commands/slash-commands-impl.ts +61 -0
  68. package/src/engine/commands/slash-commands.ts +202 -0
  69. package/src/engine/config/credential-backup.ts +108 -0
  70. package/src/engine/config/credentials.ts +76 -0
  71. package/src/engine/config/group.ts +227 -0
  72. package/src/engine/config/resolve.ts +283 -0
  73. package/src/engine/config/setup-logic.ts +84 -0
  74. package/src/engine/gateway/active-cfg.ts +52 -0
  75. package/src/engine/gateway/codec.ts +47 -0
  76. package/src/engine/gateway/constants.ts +117 -0
  77. package/src/engine/gateway/event-dispatcher.ts +177 -0
  78. package/src/engine/gateway/gateway-connection.ts +356 -0
  79. package/src/engine/gateway/gateway.ts +267 -0
  80. package/src/engine/gateway/inbound-attachments.ts +360 -0
  81. package/src/engine/gateway/inbound-context.ts +82 -0
  82. package/src/engine/gateway/inbound-pipeline.ts +171 -0
  83. package/src/engine/gateway/interaction-handler.ts +345 -0
  84. package/src/engine/gateway/message-queue.ts +404 -0
  85. package/src/engine/gateway/outbound-dispatch.ts +590 -0
  86. package/src/engine/gateway/reconnect.ts +199 -0
  87. package/src/engine/gateway/stages/access-stage.ts +99 -0
  88. package/src/engine/gateway/stages/assembly-stage.ts +156 -0
  89. package/src/engine/gateway/stages/content-stage.ts +77 -0
  90. package/src/engine/gateway/stages/envelope-stage.ts +144 -0
  91. package/src/engine/gateway/stages/group-gate-stage.ts +223 -0
  92. package/src/engine/gateway/stages/index.ts +18 -0
  93. package/src/engine/gateway/stages/quote-stage.ts +113 -0
  94. package/src/engine/gateway/stages/refidx-stage.ts +62 -0
  95. package/src/engine/gateway/stages/stub-contexts.ts +77 -0
  96. package/src/engine/gateway/types.ts +230 -0
  97. package/src/engine/gateway/typing-keepalive.ts +102 -0
  98. package/src/engine/gateway/ws-client.ts +16 -0
  99. package/src/engine/group/activation.ts +88 -0
  100. package/src/engine/group/history.ts +321 -0
  101. package/src/engine/group/mention.ts +114 -0
  102. package/src/engine/group/message-gating.ts +108 -0
  103. package/src/engine/messaging/decode-media-path.ts +82 -0
  104. package/src/engine/messaging/media-source.ts +210 -0
  105. package/src/engine/messaging/media-type-detect.ts +27 -0
  106. package/src/engine/messaging/outbound-audio-port.ts +38 -0
  107. package/src/engine/messaging/outbound-deliver.ts +810 -0
  108. package/src/engine/messaging/outbound-media-send.ts +658 -0
  109. package/src/engine/messaging/outbound-reply.ts +27 -0
  110. package/src/engine/messaging/outbound-result-helpers.ts +54 -0
  111. package/src/engine/messaging/outbound-types.ts +47 -0
  112. package/src/engine/messaging/outbound.ts +485 -0
  113. package/src/engine/messaging/reply-dispatcher.ts +597 -0
  114. package/src/engine/messaging/reply-limiter.ts +164 -0
  115. package/src/engine/messaging/sender.ts +741 -0
  116. package/src/engine/messaging/streaming-c2c.ts +1192 -0
  117. package/src/engine/messaging/streaming-media-send.ts +544 -0
  118. package/src/engine/messaging/target-parser.ts +104 -0
  119. package/src/engine/ref/format-message-ref.ts +142 -0
  120. package/src/engine/ref/format-ref-entry.ts +27 -0
  121. package/src/engine/ref/store.ts +211 -0
  122. package/src/engine/ref/types.ts +27 -0
  123. package/src/engine/session/known-users.ts +138 -0
  124. package/src/engine/session/session-store.ts +207 -0
  125. package/src/engine/tools/channel-api.ts +244 -0
  126. package/src/engine/tools/remind-logic.ts +377 -0
  127. package/src/engine/types.ts +313 -0
  128. package/src/engine/utils/attachment-tags.ts +174 -0
  129. package/src/engine/utils/audio.ts +525 -0
  130. package/src/engine/utils/data-paths.ts +38 -0
  131. package/src/engine/utils/diagnostics.ts +93 -0
  132. package/src/engine/utils/file-utils.ts +215 -0
  133. package/src/engine/utils/format.ts +70 -0
  134. package/src/engine/utils/image-size.ts +249 -0
  135. package/src/engine/utils/log.ts +77 -0
  136. package/src/engine/utils/media-tags.ts +177 -0
  137. package/src/engine/utils/payload.ts +157 -0
  138. package/src/engine/utils/platform.ts +265 -0
  139. package/src/engine/utils/request-context.ts +60 -0
  140. package/src/engine/utils/string-normalize.ts +91 -0
  141. package/src/engine/utils/stt.ts +103 -0
  142. package/src/engine/utils/text-parsing.ts +155 -0
  143. package/src/engine/utils/upload-cache.ts +96 -0
  144. package/src/engine/utils/voice-text.ts +15 -0
  145. package/src/exec-approvals.ts +237 -0
  146. package/src/qqbot-test-support.ts +29 -0
  147. package/src/secret-contract.ts +82 -0
  148. package/src/types.ts +210 -0
  149. package/tsconfig.json +16 -0
@@ -0,0 +1,207 @@
1
+ /**
2
+ * Gateway session persistence — JSONL file-based store.
3
+ *
4
+ * Migrated from src/session-store.ts. Dependencies are only Node.js
5
+ * built-ins + log + platform (both zero plugin-sdk).
6
+ */
7
+
8
+ import fs from "node:fs";
9
+ import path from "node:path";
10
+ import { privateFileStoreSync } from "autobot/plugin-sdk/security-runtime";
11
+ import { formatErrorMessage } from "../utils/format.js";
12
+ import { debugLog, debugError } from "../utils/log.js";
13
+ import { getQQBotDataDir, getQQBotDataPath } from "../utils/platform.js";
14
+
15
+ /** Persisted gateway session state. */
16
+ export interface SessionState {
17
+ sessionId: string | null;
18
+ lastSeq: number | null;
19
+ lastConnectedAt: number;
20
+ intentLevelIndex: number;
21
+ accountId: string;
22
+ savedAt: number;
23
+ appId?: string;
24
+ }
25
+
26
+ const SESSION_EXPIRE_TIME = 5 * 60 * 1000;
27
+ const SAVE_THROTTLE_MS = 1000;
28
+
29
+ const throttleState = new Map<
30
+ string,
31
+ {
32
+ pendingState: SessionState | null;
33
+ lastSaveTime: number;
34
+ throttleTimer: ReturnType<typeof setTimeout> | null;
35
+ }
36
+ >();
37
+
38
+ function ensureDir(): void {
39
+ getQQBotDataDir("sessions");
40
+ }
41
+
42
+ function getSessionDir(): string {
43
+ return getQQBotDataPath("sessions");
44
+ }
45
+
46
+ function encodeAccountIdForFileName(accountId: string): string {
47
+ return Buffer.from(accountId, "utf8").toString("base64url");
48
+ }
49
+
50
+ function getLegacySessionPath(accountId: string): string {
51
+ const safeId = accountId.replace(/[^a-zA-Z0-9_-]/g, "_");
52
+ return path.join(getSessionDir(), `session-${safeId}.json`);
53
+ }
54
+
55
+ function getSessionPath(accountId: string): string {
56
+ const encodedId = encodeAccountIdForFileName(accountId);
57
+ return path.join(getSessionDir(), `session-${encodedId}.json`);
58
+ }
59
+
60
+ function getCandidateSessionPaths(accountId: string): string[] {
61
+ const primaryPath = getSessionPath(accountId);
62
+ const legacyPath = getLegacySessionPath(accountId);
63
+ return primaryPath === legacyPath ? [primaryPath] : [primaryPath, legacyPath];
64
+ }
65
+
66
+ /** Load a saved session, rejecting expired or mismatched appId entries. */
67
+ export function loadSession(accountId: string, expectedAppId?: string): SessionState | null {
68
+ try {
69
+ let filePath: string | null = null;
70
+ let state: SessionState | null = null;
71
+ for (const candidatePath of getCandidateSessionPaths(accountId)) {
72
+ state = privateFileStoreSync(path.dirname(candidatePath)).readJsonIfExists<SessionState>(
73
+ path.basename(candidatePath),
74
+ );
75
+ if (state) {
76
+ filePath = candidatePath;
77
+ break;
78
+ }
79
+ }
80
+ if (!filePath || !state) {
81
+ return null;
82
+ }
83
+
84
+ const now = Date.now();
85
+
86
+ if (now - state.savedAt > SESSION_EXPIRE_TIME) {
87
+ debugLog(
88
+ `[session-store] Session expired for ${accountId}, age: ${Math.round((now - state.savedAt) / 1000)}s`,
89
+ );
90
+ try {
91
+ fs.unlinkSync(filePath);
92
+ } catch {}
93
+ return null;
94
+ }
95
+
96
+ if (expectedAppId && state.appId && state.appId !== expectedAppId) {
97
+ debugLog(
98
+ `[session-store] appId mismatch for ${accountId}: saved=${state.appId}, current=${expectedAppId}. Discarding stale session.`,
99
+ );
100
+ try {
101
+ fs.unlinkSync(filePath);
102
+ } catch {}
103
+ return null;
104
+ }
105
+
106
+ if (!state.sessionId || state.lastSeq === null || state.lastSeq === undefined) {
107
+ debugLog(`[session-store] Invalid session data for ${accountId}`);
108
+ return null;
109
+ }
110
+
111
+ debugLog(
112
+ `[session-store] Loaded session for ${accountId}: sessionId=${state.sessionId}, lastSeq=${state.lastSeq}, appId=${state.appId ?? "unknown"}, age=${Math.round((now - state.savedAt) / 1000)}s`,
113
+ );
114
+ return state;
115
+ } catch (err) {
116
+ debugError(
117
+ `[session-store] Failed to load session for ${accountId}: ${formatErrorMessage(err)}`,
118
+ );
119
+ return null;
120
+ }
121
+ }
122
+
123
+ /** Save session state with throttling. */
124
+ export function saveSession(state: SessionState): void {
125
+ const { accountId } = state;
126
+ let throttle = throttleState.get(accountId);
127
+ if (!throttle) {
128
+ throttle = { pendingState: null, lastSaveTime: 0, throttleTimer: null };
129
+ throttleState.set(accountId, throttle);
130
+ }
131
+
132
+ const now = Date.now();
133
+ const timeSinceLastSave = now - throttle.lastSaveTime;
134
+
135
+ if (timeSinceLastSave >= SAVE_THROTTLE_MS) {
136
+ doSaveSession(state);
137
+ throttle.lastSaveTime = now;
138
+ throttle.pendingState = null;
139
+ if (throttle.throttleTimer) {
140
+ clearTimeout(throttle.throttleTimer);
141
+ throttle.throttleTimer = null;
142
+ }
143
+ } else {
144
+ throttle.pendingState = state;
145
+ if (!throttle.throttleTimer) {
146
+ const delay = SAVE_THROTTLE_MS - timeSinceLastSave;
147
+ throttle.throttleTimer = setTimeout(() => {
148
+ const t = throttleState.get(accountId);
149
+ if (t?.pendingState) {
150
+ doSaveSession(t.pendingState);
151
+ t.lastSaveTime = Date.now();
152
+ t.pendingState = null;
153
+ }
154
+ if (t) {
155
+ t.throttleTimer = null;
156
+ }
157
+ }, delay);
158
+ }
159
+ }
160
+ }
161
+
162
+ function doSaveSession(state: SessionState): void {
163
+ const filePath = getSessionPath(state.accountId);
164
+ const legacyPath = getLegacySessionPath(state.accountId);
165
+ try {
166
+ ensureDir();
167
+ const stateToSave: SessionState = { ...state, savedAt: Date.now() };
168
+ privateFileStoreSync(path.dirname(filePath)).writeJson(path.basename(filePath), stateToSave);
169
+ if (legacyPath !== filePath && fs.existsSync(legacyPath)) {
170
+ fs.unlinkSync(legacyPath);
171
+ }
172
+ debugLog(
173
+ `[session-store] Saved session for ${state.accountId}: sessionId=${state.sessionId}, lastSeq=${state.lastSeq}`,
174
+ );
175
+ } catch (err) {
176
+ debugError(
177
+ `[session-store] Failed to save session for ${state.accountId}: ${formatErrorMessage(err)}`,
178
+ );
179
+ }
180
+ }
181
+
182
+ /** Clear a saved session and any pending throttle state. */
183
+ export function clearSession(accountId: string): void {
184
+ const throttle = throttleState.get(accountId);
185
+ if (throttle) {
186
+ if (throttle.throttleTimer) {
187
+ clearTimeout(throttle.throttleTimer);
188
+ }
189
+ throttleState.delete(accountId);
190
+ }
191
+ try {
192
+ let cleared = false;
193
+ for (const filePath of getCandidateSessionPaths(accountId)) {
194
+ if (fs.existsSync(filePath)) {
195
+ fs.unlinkSync(filePath);
196
+ cleared = true;
197
+ }
198
+ }
199
+ if (cleared) {
200
+ debugLog(`[session-store] Cleared session for ${accountId}`);
201
+ }
202
+ } catch (err) {
203
+ debugError(
204
+ `[session-store] Failed to clear session for ${accountId}: ${formatErrorMessage(err)}`,
205
+ );
206
+ }
207
+ }
@@ -0,0 +1,244 @@
1
+ /**
2
+ * QQ Channel API proxy tool core logic.
3
+ * QQ 频道 API 代理工具核心逻辑。
4
+ *
5
+ * Provides an authenticated HTTP proxy for the QQ Open Platform channel
6
+ * APIs. The caller (old tools/channel.ts shell) resolves the access
7
+ * token and passes it in; this module handles URL building, path
8
+ * validation, fetch, and structured response formatting.
9
+ */
10
+
11
+ import { formatErrorMessage } from "../utils/format.js";
12
+ import { debugLog, debugError } from "../utils/log.js";
13
+
14
+ const API_BASE = "https://api.sgroup.qq.com";
15
+ const DEFAULT_TIMEOUT_MS = 30000;
16
+
17
+ /**
18
+ * Channel API call parameters.
19
+ * 频道 API 调用参数。
20
+ */
21
+ export interface ChannelApiParams {
22
+ method: string;
23
+ path: string;
24
+ body?: Record<string, unknown>;
25
+ query?: Record<string, string>;
26
+ }
27
+
28
+ /**
29
+ * JSON Schema for AI tool parameters (used by framework registration).
30
+ * AI Tool 参数的 JSON Schema 定义(供框架注册使用)。
31
+ */
32
+ export const ChannelApiSchema = {
33
+ type: "object",
34
+ properties: {
35
+ method: {
36
+ type: "string",
37
+ description: "HTTP method. Allowed values: GET, POST, PUT, PATCH, DELETE.",
38
+ enum: ["GET", "POST", "PUT", "PATCH", "DELETE"],
39
+ },
40
+ path: {
41
+ type: "string",
42
+ description:
43
+ "API path without the host. Replace placeholders with concrete values. " +
44
+ "Examples: /users/@me/guilds, /guilds/{guild_id}/channels, /channels/{channel_id}.",
45
+ },
46
+ body: {
47
+ type: "object",
48
+ description:
49
+ "JSON request body for POST/PUT/PATCH requests. GET/DELETE usually do not need it.",
50
+ },
51
+ query: {
52
+ type: "object",
53
+ description:
54
+ "URL query parameters as key/value pairs appended to the path. " +
55
+ 'For example, { "limit": "100", "after": "0" } becomes ?limit=100&after=0.',
56
+ additionalProperties: { type: "string" },
57
+ },
58
+ },
59
+ required: ["method", "path"],
60
+ } as const;
61
+
62
+ /**
63
+ * Build the full API URL from base + path + query params.
64
+ * 拼接 API 基地址 + 路径 + 查询参数。
65
+ */
66
+ function buildUrl(path: string, query?: Record<string, string>): string {
67
+ let url = `${API_BASE}${path}`;
68
+ if (query && Object.keys(query).length > 0) {
69
+ const params = new URLSearchParams();
70
+ for (const [key, value] of Object.entries(query)) {
71
+ if (value !== undefined && value !== null && value !== "") {
72
+ params.set(key, value);
73
+ }
74
+ }
75
+ const qs = params.toString();
76
+ if (qs) {
77
+ url += `?${qs}`;
78
+ }
79
+ }
80
+ return url;
81
+ }
82
+
83
+ /**
84
+ * Validate API path format; returns an error string or null if valid.
85
+ * 校验 API 路径格式,返回错误描述或 null(合法)。
86
+ */
87
+ function validatePath(path: string): string | null {
88
+ if (!path.startsWith("/")) {
89
+ return "path must start with /";
90
+ }
91
+ if (path.includes("..") || path.includes("//")) {
92
+ return "path must not contain .. or //";
93
+ }
94
+ if (!/^\/[a-zA-Z0-9\-._~:@!$&'()*+,;=/%]+$/.test(path) && path !== "/") {
95
+ return "path contains unsupported characters";
96
+ }
97
+ return null;
98
+ }
99
+
100
+ function json(data: unknown) {
101
+ return {
102
+ content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }],
103
+ details: data,
104
+ };
105
+ }
106
+
107
+ /**
108
+ * Options provided by the caller when executing a channel API request.
109
+ * 执行频道 API 请求时由调用方提供的选项。
110
+ */
111
+ interface ChannelApiExecuteOptions {
112
+ accessToken: string;
113
+ }
114
+
115
+ /**
116
+ * Execute a channel API proxy request.
117
+ * 执行频道 API 代理请求。
118
+ *
119
+ * The caller provides the access token; this function handles
120
+ * URL building, path validation, HTTP fetch, and structured
121
+ * response formatting suitable for AI tool output.
122
+ */
123
+ export async function executeChannelApi(
124
+ params: ChannelApiParams,
125
+ options: ChannelApiExecuteOptions,
126
+ ) {
127
+ if (!params.method) {
128
+ return json({ error: "method is required" });
129
+ }
130
+ if (!params.path) {
131
+ return json({ error: "path is required" });
132
+ }
133
+
134
+ const method = params.method.toUpperCase();
135
+ if (!["GET", "POST", "PUT", "PATCH", "DELETE"].includes(method)) {
136
+ return json({
137
+ error: `Unsupported HTTP method: ${method}. Allowed values: GET, POST, PUT, PATCH, DELETE`,
138
+ });
139
+ }
140
+
141
+ const pathError = validatePath(params.path);
142
+ if (pathError) {
143
+ return json({ error: pathError });
144
+ }
145
+
146
+ if (
147
+ (method === "GET" || method === "DELETE") &&
148
+ params.body &&
149
+ Object.keys(params.body).length > 0
150
+ ) {
151
+ debugLog(`[qqbot-channel-api] ${method} request with body, body will be ignored`);
152
+ }
153
+
154
+ try {
155
+ const url = buildUrl(params.path, params.query);
156
+ const headers: Record<string, string> = {
157
+ Authorization: `QQBot ${options.accessToken}`,
158
+ "Content-Type": "application/json",
159
+ };
160
+
161
+ const controller = new AbortController();
162
+ const timeoutId = setTimeout(() => controller.abort(), DEFAULT_TIMEOUT_MS);
163
+
164
+ const fetchOptions: RequestInit = {
165
+ method,
166
+ headers,
167
+ signal: controller.signal,
168
+ };
169
+
170
+ if (params.body && ["POST", "PUT", "PATCH"].includes(method)) {
171
+ fetchOptions.body = JSON.stringify(params.body);
172
+ }
173
+
174
+ debugLog(`[qqbot-channel-api] >>> ${method} ${url} (timeout: ${DEFAULT_TIMEOUT_MS}ms)`);
175
+
176
+ let res: Response;
177
+ try {
178
+ res = await fetch(url, fetchOptions);
179
+ } catch (err) {
180
+ clearTimeout(timeoutId);
181
+ if (err instanceof Error && err.name === "AbortError") {
182
+ debugError(`[qqbot-channel-api] <<< Request timeout after ${DEFAULT_TIMEOUT_MS}ms`);
183
+ return json({
184
+ error: `Request timed out after ${DEFAULT_TIMEOUT_MS}ms`,
185
+ path: params.path,
186
+ });
187
+ }
188
+ debugError("[qqbot-channel-api] <<< Network error:", err);
189
+ return json({
190
+ error: `Network error: ${formatErrorMessage(err)}`,
191
+ path: params.path,
192
+ });
193
+ } finally {
194
+ clearTimeout(timeoutId);
195
+ }
196
+
197
+ debugLog(`[qqbot-channel-api] <<< Status: ${res.status} ${res.statusText}`);
198
+
199
+ const rawBody = await res.text();
200
+ if (!rawBody || rawBody.trim() === "") {
201
+ if (res.ok) {
202
+ return json({ success: true, status: res.status, path: params.path });
203
+ }
204
+ return json({
205
+ error: `API returned ${res.status} ${res.statusText}`,
206
+ status: res.status,
207
+ path: params.path,
208
+ });
209
+ }
210
+
211
+ let parsed: unknown;
212
+ try {
213
+ parsed = JSON.parse(rawBody);
214
+ } catch {
215
+ parsed = rawBody;
216
+ }
217
+
218
+ if (!res.ok) {
219
+ const errMsg =
220
+ typeof parsed === "object" && parsed && "message" in parsed
221
+ ? String((parsed as { message?: unknown }).message)
222
+ : `${res.status} ${res.statusText}`;
223
+ debugError(`[qqbot-channel-api] Error [${method} ${params.path}]: ${errMsg}`);
224
+ return json({
225
+ error: errMsg,
226
+ status: res.status,
227
+ path: params.path,
228
+ details: parsed,
229
+ });
230
+ }
231
+
232
+ return json({
233
+ success: true,
234
+ status: res.status,
235
+ path: params.path,
236
+ data: parsed,
237
+ });
238
+ } catch (err) {
239
+ return json({
240
+ error: formatErrorMessage(err),
241
+ path: params.path,
242
+ });
243
+ }
244
+ }