@tencent-connect/qqbot-nodejs 1.0.4 → 1.0.6
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/CHANGELOG.md +114 -0
- package/LICENSE +21 -0
- package/README.md +181 -86
- package/README.zh-CN.md +293 -0
- package/USAGE.md +80 -27
- package/assets/docs/feedback.jpg +0 -0
- package/assets/docs/qqchannel.jpg +0 -0
- package/dist/QQBot.d.ts +66 -3
- package/dist/QQBot.d.ts.map +1 -1
- package/dist/QQBot.js +144 -33
- package/dist/QQBot.js.map +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/protocol/api/token.d.ts.map +1 -1
- package/dist/protocol/api/token.js +6 -2
- package/dist/protocol/api/token.js.map +1 -1
- package/dist/protocol/gateway/constants.d.ts +6 -1
- package/dist/protocol/gateway/constants.d.ts.map +1 -1
- package/dist/protocol/gateway/constants.js +6 -1
- package/dist/protocol/gateway/constants.js.map +1 -1
- package/dist/protocol/gateway/errors.d.ts +22 -0
- package/dist/protocol/gateway/errors.d.ts.map +1 -0
- package/dist/protocol/gateway/errors.js +24 -0
- package/dist/protocol/gateway/errors.js.map +1 -0
- package/dist/protocol/gateway/event-dispatcher.d.ts +22 -0
- package/dist/protocol/gateway/event-dispatcher.d.ts.map +1 -1
- package/dist/protocol/gateway/event-dispatcher.js +17 -0
- package/dist/protocol/gateway/event-dispatcher.js.map +1 -1
- package/dist/protocol/gateway/gateway-connection.d.ts +69 -4
- package/dist/protocol/gateway/gateway-connection.d.ts.map +1 -1
- package/dist/protocol/gateway/gateway-connection.js +349 -146
- package/dist/protocol/gateway/gateway-connection.js.map +1 -1
- package/dist/protocol/gateway/reconnect.d.ts +24 -1
- package/dist/protocol/gateway/reconnect.d.ts.map +1 -1
- package/dist/protocol/gateway/reconnect.js +56 -9
- package/dist/protocol/gateway/reconnect.js.map +1 -1
- package/dist/protocol/index.d.ts +5 -4
- package/dist/protocol/index.d.ts.map +1 -1
- package/dist/protocol/index.js +3 -2
- package/dist/protocol/index.js.map +1 -1
- package/dist/protocol/types.d.ts +49 -10
- package/dist/protocol/types.d.ts.map +1 -1
- package/dist/protocol/types.js.map +1 -1
- package/dist/protocol/utils/file-utils.d.ts +2 -2
- package/dist/protocol/utils/file-utils.js +2 -2
- package/dist/protocol/utils/image-size.d.ts +3 -3
- package/dist/protocol/utils/image-size.js +3 -3
- package/dist/protocol/utils/ref-index-store.d.ts +1 -1
- package/dist/protocol/utils/ref-index-store.js +1 -1
- package/package.json +24 -7
- package/src/QQBot.ts +173 -36
- package/src/index.ts +6 -0
- package/src/protocol/api/token.ts +6 -2
- package/src/protocol/gateway/constants.ts +6 -1
- package/src/protocol/gateway/errors.ts +32 -0
- package/src/protocol/gateway/event-dispatcher.ts +41 -0
- package/src/protocol/gateway/gateway-connection.ts +389 -160
- package/src/protocol/gateway/reconnect.ts +76 -10
- package/src/protocol/index.ts +11 -1
- package/src/protocol/types.ts +46 -10
- package/src/protocol/utils/file-utils.ts +2 -2
- package/src/protocol/utils/image-size.ts +3 -3
- package/src/protocol/utils/ref-index-store.ts +1 -1
|
@@ -21,28 +21,80 @@ export interface CloseAction {
|
|
|
21
21
|
reason: string;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
/** Connection-level reconnect budget. */
|
|
25
|
+
export interface ReconnectPolicy {
|
|
26
|
+
/**
|
|
27
|
+
* Consecutive reconnect attempts before giving up. Defaults to `Infinity`
|
|
28
|
+
* ({@link MAX_RECONNECT_ATTEMPTS}): only fatal closes end the loop.
|
|
29
|
+
*/
|
|
30
|
+
maxAttempts: number;
|
|
31
|
+
/** Backoff delays (ms) indexed by attempt; the last entry repeats. Defaults to {@link RECONNECT_DELAYS}. */
|
|
32
|
+
delays: readonly number[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const DEFAULT_RECONNECT_POLICY: ReconnectPolicy = {
|
|
36
|
+
maxAttempts: MAX_RECONNECT_ATTEMPTS,
|
|
37
|
+
delays: RECONNECT_DELAYS,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** Merge a partial policy with defaults, rejecting values that would break the retry loop. */
|
|
41
|
+
export function resolveReconnectPolicy(policy?: Partial<ReconnectPolicy>): ReconnectPolicy {
|
|
42
|
+
const maxAttempts = policy?.maxAttempts ?? DEFAULT_RECONNECT_POLICY.maxAttempts;
|
|
43
|
+
const delays = policy?.delays ?? DEFAULT_RECONNECT_POLICY.delays;
|
|
44
|
+
const isUnlimited = maxAttempts === Number.POSITIVE_INFINITY;
|
|
45
|
+
if (!isUnlimited && (!Number.isInteger(maxAttempts) || maxAttempts < 0)) {
|
|
46
|
+
throw new RangeError(
|
|
47
|
+
`reconnect.maxAttempts must be a non-negative integer or Infinity, got ${maxAttempts}`,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
if (delays.length === 0 || delays.some((d) => !Number.isFinite(d) || d < 0)) {
|
|
51
|
+
throw new RangeError("reconnect.delays must be a non-empty list of non-negative numbers");
|
|
52
|
+
}
|
|
53
|
+
return { maxAttempts, delays: [...delays] };
|
|
54
|
+
}
|
|
55
|
+
|
|
24
56
|
export class ReconnectState {
|
|
25
57
|
private attempts = 0;
|
|
26
58
|
private lastConnectTime = 0;
|
|
27
59
|
private quickDisconnectCount = 0;
|
|
60
|
+
private readonly policy: ReconnectPolicy;
|
|
28
61
|
|
|
29
62
|
constructor(
|
|
30
63
|
private readonly accountId: string,
|
|
31
64
|
private readonly log?: Logger,
|
|
32
|
-
|
|
65
|
+
policy?: Partial<ReconnectPolicy>,
|
|
66
|
+
) {
|
|
67
|
+
this.policy = resolveReconnectPolicy(policy);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Socket opened — starts the quick-disconnect window. Does not reset the budget. */
|
|
71
|
+
onSocketOpen(): void {
|
|
72
|
+
this.lastConnectTime = Date.now();
|
|
73
|
+
}
|
|
33
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Session established (READY / RESUMED) — resets the reconnect budget.
|
|
77
|
+
*
|
|
78
|
+
* Resetting on socket open instead would let "open then immediately kicked"
|
|
79
|
+
* loops (e.g. repeated 4004) retry forever.
|
|
80
|
+
*/
|
|
34
81
|
onConnected(): void {
|
|
35
82
|
this.attempts = 0;
|
|
36
83
|
this.lastConnectTime = Date.now();
|
|
37
84
|
}
|
|
38
85
|
|
|
39
86
|
isExhausted(): boolean {
|
|
40
|
-
return this.attempts >=
|
|
87
|
+
return this.attempts >= this.policy.maxAttempts;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Maximum consecutive attempts allowed by the active policy. */
|
|
91
|
+
get maxAttempts(): number {
|
|
92
|
+
return this.policy.maxAttempts;
|
|
41
93
|
}
|
|
42
94
|
|
|
43
95
|
getNextDelay(customDelay?: number): number {
|
|
44
|
-
const
|
|
45
|
-
|
|
96
|
+
const { delays } = this.policy;
|
|
97
|
+
const delay = customDelay ?? delays[Math.min(this.attempts, delays.length - 1)];
|
|
46
98
|
this.attempts++;
|
|
47
99
|
this.log?.debug?.(`[${this.accountId}] Reconnecting in ${delay}ms (attempt ${this.attempts})`);
|
|
48
100
|
return delay;
|
|
@@ -88,15 +140,27 @@ export class ReconnectState {
|
|
|
88
140
|
};
|
|
89
141
|
}
|
|
90
142
|
|
|
143
|
+
// 4009 是服务端"请重连"的通用关闭码(最大连接时长到期、负载转移、Ping 超时等),
|
|
144
|
+
// 服务端会保留 session 等待客户端 RESUME 并按 seq 重放断线期间的事件。
|
|
145
|
+
// 这里必须保留 session:清掉会让重连退化为 IDENTIFY 新会话,丢失积压消息。
|
|
146
|
+
if (code === GatewayCloseCode.SESSION_TIMEOUT) {
|
|
147
|
+
this.log?.info(`[${this.accountId}] Server requested reconnect (4009), will resume`);
|
|
148
|
+
return {
|
|
149
|
+
shouldReconnect: !isAborted,
|
|
150
|
+
clearSession: false,
|
|
151
|
+
refreshToken: false,
|
|
152
|
+
fatal: false,
|
|
153
|
+
reason: "server requested reconnect",
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
91
157
|
if (
|
|
92
158
|
code === GatewayCloseCode.INVALID_SESSION ||
|
|
93
|
-
code === GatewayCloseCode.SEQ_OUT_OF_RANGE
|
|
94
|
-
code === GatewayCloseCode.SESSION_TIMEOUT
|
|
159
|
+
code === GatewayCloseCode.SEQ_OUT_OF_RANGE
|
|
95
160
|
) {
|
|
96
161
|
const codeDesc: Record<number, string> = {
|
|
97
162
|
[GatewayCloseCode.INVALID_SESSION]: "session no longer valid",
|
|
98
163
|
[GatewayCloseCode.SEQ_OUT_OF_RANGE]: "invalid seq on resume",
|
|
99
|
-
[GatewayCloseCode.SESSION_TIMEOUT]: "session timed out",
|
|
100
164
|
};
|
|
101
165
|
this.log?.info(`[${this.accountId}] Error ${code} (${codeDesc[code]}), will re-identify`);
|
|
102
166
|
return {
|
|
@@ -111,7 +175,7 @@ export class ReconnectState {
|
|
|
111
175
|
if (code >= GatewayCloseCode.SERVER_ERROR_START && code <= GatewayCloseCode.SERVER_ERROR_END) {
|
|
112
176
|
this.log?.info(`[${this.accountId}] Internal error (${code}), will re-identify`);
|
|
113
177
|
return {
|
|
114
|
-
shouldReconnect: !isAborted
|
|
178
|
+
shouldReconnect: !isAborted,
|
|
115
179
|
clearSession: true,
|
|
116
180
|
refreshToken: true,
|
|
117
181
|
fatal: false,
|
|
@@ -132,7 +196,7 @@ export class ReconnectState {
|
|
|
132
196
|
);
|
|
133
197
|
this.quickDisconnectCount = 0;
|
|
134
198
|
return {
|
|
135
|
-
shouldReconnect: !isAborted
|
|
199
|
+
shouldReconnect: !isAborted,
|
|
136
200
|
reconnectDelay: RATE_LIMIT_DELAY,
|
|
137
201
|
clearSession: false,
|
|
138
202
|
refreshToken: false,
|
|
@@ -144,8 +208,10 @@ export class ReconnectState {
|
|
|
144
208
|
this.quickDisconnectCount = 0;
|
|
145
209
|
}
|
|
146
210
|
|
|
211
|
+
// A remote 1000 is a server-side graceful close (deploy / drain), not a
|
|
212
|
+
// client shutdown: local closes retire the socket before its close event.
|
|
147
213
|
return {
|
|
148
|
-
shouldReconnect: !isAborted
|
|
214
|
+
shouldReconnect: !isAborted,
|
|
149
215
|
clearSession: false,
|
|
150
216
|
refreshToken: false,
|
|
151
217
|
fatal: false,
|
package/src/protocol/index.ts
CHANGED
|
@@ -61,6 +61,8 @@ export {
|
|
|
61
61
|
GatewayCloseCode,
|
|
62
62
|
GatewayEvent,
|
|
63
63
|
GatewayOp,
|
|
64
|
+
HANDSHAKE_TIMEOUT_MS,
|
|
65
|
+
INVALID_SESSION_DELAY,
|
|
64
66
|
InteractionType,
|
|
65
67
|
MAX_QUICK_DISCONNECT_COUNT,
|
|
66
68
|
MAX_RECONNECT_ATTEMPTS,
|
|
@@ -69,10 +71,17 @@ export {
|
|
|
69
71
|
RECONNECT_DELAYS,
|
|
70
72
|
} from "./gateway/constants.js";
|
|
71
73
|
export { decodeGatewayMessageData, readOptionalMessageSceneExt } from "./gateway/codec.js";
|
|
72
|
-
export {
|
|
74
|
+
export {
|
|
75
|
+
ReconnectState,
|
|
76
|
+
resolveReconnectPolicy,
|
|
77
|
+
type CloseAction,
|
|
78
|
+
type ReconnectPolicy,
|
|
79
|
+
} from "./gateway/reconnect.js";
|
|
80
|
+
export { GatewayError, GatewayErrorCode } from "./gateway/errors.js";
|
|
73
81
|
export {
|
|
74
82
|
dispatchEvent,
|
|
75
83
|
type DispatchResult,
|
|
84
|
+
type InboundAuthor,
|
|
76
85
|
type InboundMessage,
|
|
77
86
|
type InboundAttachment,
|
|
78
87
|
type InboundMsgElement,
|
|
@@ -80,6 +89,7 @@ export {
|
|
|
80
89
|
export {
|
|
81
90
|
GatewayConnection,
|
|
82
91
|
type GatewayConnectionOptions,
|
|
92
|
+
type GatewayDisconnect,
|
|
83
93
|
type PersistedSession,
|
|
84
94
|
type SessionPersistencePort,
|
|
85
95
|
} from "./gateway/gateway-connection.js";
|
package/src/protocol/types.ts
CHANGED
|
@@ -73,7 +73,9 @@ export interface MessageResponse {
|
|
|
73
73
|
/** Reference index for future quoting. */
|
|
74
74
|
ext_info?: {
|
|
75
75
|
ref_idx?: string;
|
|
76
|
-
};
|
|
76
|
+
} & OpenFields;
|
|
77
|
+
/** Platform-added fields not declared here. */
|
|
78
|
+
[key: string]: unknown;
|
|
77
79
|
}
|
|
78
80
|
|
|
79
81
|
// ============ Media Types ============
|
|
@@ -92,6 +94,8 @@ export interface UploadMediaResponse {
|
|
|
92
94
|
file_info: string;
|
|
93
95
|
ttl: number;
|
|
94
96
|
id?: string;
|
|
97
|
+
/** Platform-added fields not declared here. */
|
|
98
|
+
[key: string]: unknown;
|
|
95
99
|
}
|
|
96
100
|
|
|
97
101
|
/** Structured metadata recorded for outbound messages. */
|
|
@@ -119,6 +123,8 @@ export interface ApiClientConfig {
|
|
|
119
123
|
export interface UploadPart {
|
|
120
124
|
index: number;
|
|
121
125
|
presigned_url: string;
|
|
126
|
+
/** Platform-added fields not declared here. */
|
|
127
|
+
[key: string]: unknown;
|
|
122
128
|
}
|
|
123
129
|
|
|
124
130
|
export interface UploadPrepareResponse {
|
|
@@ -127,6 +133,8 @@ export interface UploadPrepareResponse {
|
|
|
127
133
|
parts: UploadPart[];
|
|
128
134
|
concurrency?: number;
|
|
129
135
|
retry_timeout?: number;
|
|
136
|
+
/** Platform-added fields not declared here. */
|
|
137
|
+
[key: string]: unknown;
|
|
130
138
|
}
|
|
131
139
|
|
|
132
140
|
export interface UploadPrepareHashes {
|
|
@@ -192,6 +200,11 @@ export interface InlineKeyboard {
|
|
|
192
200
|
// ============ Interaction Event ============
|
|
193
201
|
|
|
194
202
|
export interface InteractionEvent {
|
|
203
|
+
/**
|
|
204
|
+
* Boundary type: forwarded to consumers as the gateway sends it (open to
|
|
205
|
+
* additive fields). A closed `InboundInteraction` domain model can be
|
|
206
|
+
* introduced later if field-level tooling ever needs it.
|
|
207
|
+
*/
|
|
195
208
|
id: string;
|
|
196
209
|
type: number;
|
|
197
210
|
scene?: string;
|
|
@@ -211,8 +224,10 @@ export interface InteractionEvent {
|
|
|
211
224
|
user_id?: string;
|
|
212
225
|
feature_id?: string;
|
|
213
226
|
message_id?: string;
|
|
214
|
-
};
|
|
215
|
-
};
|
|
227
|
+
} & OpenFields;
|
|
228
|
+
} & OpenFields;
|
|
229
|
+
/** Platform-added fields not declared here. */
|
|
230
|
+
[key: string]: unknown;
|
|
216
231
|
}
|
|
217
232
|
|
|
218
233
|
// ============ Gateway WebSocket Types ============
|
|
@@ -230,11 +245,15 @@ export interface RawMessageAttachment {
|
|
|
230
245
|
filename?: string;
|
|
231
246
|
voice_wav_url?: string;
|
|
232
247
|
asr_refer_text?: string;
|
|
248
|
+
/** Platform-added fields not declared here. */
|
|
249
|
+
[key: string]: unknown;
|
|
233
250
|
}
|
|
234
251
|
|
|
235
252
|
export interface RawMsgElement {
|
|
236
253
|
msg_idx?: string;
|
|
237
254
|
content?: string;
|
|
255
|
+
/** Platform-added fields not declared here. */
|
|
256
|
+
[key: string]: unknown;
|
|
238
257
|
attachments?: Array<
|
|
239
258
|
RawMessageAttachment & {
|
|
240
259
|
height?: number;
|
|
@@ -244,26 +263,39 @@ export interface RawMsgElement {
|
|
|
244
263
|
>;
|
|
245
264
|
}
|
|
246
265
|
|
|
266
|
+
/**
|
|
267
|
+
* Fields the platform sends but this SDK has not declared yet.
|
|
268
|
+
*
|
|
269
|
+
* Platform releases additive event fields before SDK type updates; consumers
|
|
270
|
+
* can still read them (`author.someNewField`) instead of escaping the type
|
|
271
|
+
* system with `as any`.
|
|
272
|
+
*/
|
|
273
|
+
export type OpenFields = { [key: string]: unknown };
|
|
274
|
+
|
|
247
275
|
export interface C2CMessageEvent {
|
|
248
276
|
id: string;
|
|
249
277
|
content: string;
|
|
250
278
|
timestamp: string;
|
|
251
|
-
author: { user_openid: string };
|
|
279
|
+
author: { user_openid: string; avatar?: string } & OpenFields;
|
|
252
280
|
attachments?: RawMessageAttachment[];
|
|
253
|
-
message_scene?: { ext?: string[] };
|
|
281
|
+
message_scene?: { ext?: string[] } & OpenFields;
|
|
254
282
|
message_type?: number;
|
|
255
283
|
msg_elements?: RawMsgElement[];
|
|
284
|
+
/** Platform-added fields not declared here. */
|
|
285
|
+
[key: string]: unknown;
|
|
256
286
|
}
|
|
257
287
|
|
|
258
288
|
export interface GuildMessageEvent {
|
|
259
289
|
id: string;
|
|
260
290
|
content: string;
|
|
261
291
|
timestamp: string;
|
|
262
|
-
author: { id: string; username?: string };
|
|
292
|
+
author: { id: string; username?: string; avatar?: string } & OpenFields;
|
|
263
293
|
channel_id: string;
|
|
264
294
|
guild_id: string;
|
|
265
295
|
attachments?: RawMessageAttachment[];
|
|
266
|
-
message_scene?: { ext?: string[] };
|
|
296
|
+
message_scene?: { ext?: string[] } & OpenFields;
|
|
297
|
+
/** Platform-added fields not declared here. */
|
|
298
|
+
[key: string]: unknown;
|
|
267
299
|
}
|
|
268
300
|
|
|
269
301
|
export interface GroupMessageEvent {
|
|
@@ -274,7 +306,9 @@ export interface GroupMessageEvent {
|
|
|
274
306
|
member_openid: string;
|
|
275
307
|
username?: string;
|
|
276
308
|
bot?: boolean;
|
|
277
|
-
|
|
309
|
+
/** Sender avatar URL; the platform started sending it as an additive field. */
|
|
310
|
+
avatar?: string;
|
|
311
|
+
} & OpenFields;
|
|
278
312
|
group_openid: string;
|
|
279
313
|
attachments?: RawMessageAttachment[];
|
|
280
314
|
mentions?: Array<{
|
|
@@ -286,10 +320,12 @@ export interface GroupMessageEvent {
|
|
|
286
320
|
username?: string;
|
|
287
321
|
bot?: boolean;
|
|
288
322
|
is_you?: boolean;
|
|
289
|
-
}>;
|
|
290
|
-
message_scene?: { source?: string; ext?: string[] };
|
|
323
|
+
} & OpenFields>;
|
|
324
|
+
message_scene?: { source?: string; ext?: string[] } & OpenFields;
|
|
291
325
|
message_type?: number;
|
|
292
326
|
msg_elements?: RawMsgElement[];
|
|
327
|
+
/** Platform-added fields not declared here. */
|
|
328
|
+
[key: string]: unknown;
|
|
293
329
|
}
|
|
294
330
|
|
|
295
331
|
// ============ Account ============
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Pure helpers: filename sanitization, size constants, file size formatting.
|
|
5
5
|
* The SDK does NOT bundle SSRF / remote-fetch logic — those are concerns
|
|
6
|
-
* of the host application
|
|
7
|
-
*
|
|
6
|
+
* of the host application and should be implemented at the integration
|
|
7
|
+
* layer.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { MediaFileType } from "../types.js";
|
|
@@ -5,9 +5,9 @@
|
|
|
5
5
|
*
|
|
6
6
|
* The header parsers are all pure functions. URL probing requires a
|
|
7
7
|
* caller-provided {@link ImageBytesFetcher} so this module stays free of
|
|
8
|
-
* networking / SSRF concerns — the
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* networking / SSRF concerns — the embedding application is responsible
|
|
9
|
+
* for enforcing public-network-only / hostname-allowlist policies before
|
|
10
|
+
* any byte hits this code.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
import { Buffer } from "node:buffer";
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
*
|
|
14
14
|
* Path resolution is **delegated to the embedder** via the constructor:
|
|
15
15
|
* the SDK itself does not know where the host application wants its data
|
|
16
|
-
* (`~/.
|
|
16
|
+
* (e.g. `~/.myapp/qqbot/data/ref-index.jsonl`, `/var/lib/qqbot/...`).
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import fs from "node:fs";
|