@tencent-connect/qqbot-nodejs 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +205 -0
- package/USAGE.md +804 -0
- package/dist/QQBot.d.ts +536 -0
- package/dist/QQBot.d.ts.map +1 -0
- package/dist/QQBot.js +752 -0
- package/dist/QQBot.js.map +1 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +66 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware/access-policy.d.ts +54 -0
- package/dist/middleware/access-policy.d.ts.map +1 -0
- package/dist/middleware/access-policy.js +93 -0
- package/dist/middleware/access-policy.js.map +1 -0
- package/dist/middleware/concurrency-guard.d.ts +105 -0
- package/dist/middleware/concurrency-guard.d.ts.map +1 -0
- package/dist/middleware/concurrency-guard.js +325 -0
- package/dist/middleware/concurrency-guard.js.map +1 -0
- package/dist/middleware/content-sanitizer.d.ts +47 -0
- package/dist/middleware/content-sanitizer.d.ts.map +1 -0
- package/dist/middleware/content-sanitizer.js +85 -0
- package/dist/middleware/content-sanitizer.js.map +1 -0
- package/dist/middleware/envelope-formatter.d.ts +51 -0
- package/dist/middleware/envelope-formatter.d.ts.map +1 -0
- package/dist/middleware/envelope-formatter.js +97 -0
- package/dist/middleware/envelope-formatter.js.map +1 -0
- package/dist/middleware/error-handler.d.ts +36 -0
- package/dist/middleware/error-handler.d.ts.map +1 -0
- package/dist/middleware/error-handler.js +59 -0
- package/dist/middleware/error-handler.js.map +1 -0
- package/dist/middleware/history-buffer.d.ts +84 -0
- package/dist/middleware/history-buffer.d.ts.map +1 -0
- package/dist/middleware/history-buffer.js +113 -0
- package/dist/middleware/history-buffer.js.map +1 -0
- package/dist/middleware/index.d.ts +71 -0
- package/dist/middleware/index.d.ts.map +1 -0
- package/dist/middleware/index.js +48 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/middleware/mention-gate.d.ts +99 -0
- package/dist/middleware/mention-gate.d.ts.map +1 -0
- package/dist/middleware/mention-gate.js +117 -0
- package/dist/middleware/mention-gate.js.map +1 -0
- package/dist/middleware/message-filter.d.ts +38 -0
- package/dist/middleware/message-filter.d.ts.map +1 -0
- package/dist/middleware/message-filter.js +60 -0
- package/dist/middleware/message-filter.js.map +1 -0
- package/dist/middleware/quote-ref.d.ts +106 -0
- package/dist/middleware/quote-ref.d.ts.map +1 -0
- package/dist/middleware/quote-ref.js +168 -0
- package/dist/middleware/quote-ref.js.map +1 -0
- package/dist/middleware/rate-limiter.d.ts +43 -0
- package/dist/middleware/rate-limiter.d.ts.map +1 -0
- package/dist/middleware/rate-limiter.js +77 -0
- package/dist/middleware/rate-limiter.js.map +1 -0
- package/dist/middleware/slash-command.d.ts +112 -0
- package/dist/middleware/slash-command.d.ts.map +1 -0
- package/dist/middleware/slash-command.js +203 -0
- package/dist/middleware/slash-command.js.map +1 -0
- package/dist/middleware/types.d.ts +175 -0
- package/dist/middleware/types.d.ts.map +1 -0
- package/dist/middleware/types.js +138 -0
- package/dist/middleware/types.js.map +1 -0
- package/dist/middleware/typing-indicator.d.ts +54 -0
- package/dist/middleware/typing-indicator.d.ts.map +1 -0
- package/dist/middleware/typing-indicator.js +68 -0
- package/dist/middleware/typing-indicator.js.map +1 -0
- package/dist/protocol/api/api-client.d.ts +27 -0
- package/dist/protocol/api/api-client.d.ts.map +1 -0
- package/dist/protocol/api/api-client.js +119 -0
- package/dist/protocol/api/api-client.js.map +1 -0
- package/dist/protocol/api/media-chunked.d.ts +68 -0
- package/dist/protocol/api/media-chunked.d.ts.map +1 -0
- package/dist/protocol/api/media-chunked.js +298 -0
- package/dist/protocol/api/media-chunked.js.map +1 -0
- package/dist/protocol/api/media.d.ts +46 -0
- package/dist/protocol/api/media.d.ts.map +1 -0
- package/dist/protocol/api/media.js +101 -0
- package/dist/protocol/api/media.js.map +1 -0
- package/dist/protocol/api/messages.d.ts +86 -0
- package/dist/protocol/api/messages.d.ts.map +1 -0
- package/dist/protocol/api/messages.js +200 -0
- package/dist/protocol/api/messages.js.map +1 -0
- package/dist/protocol/api/retry.d.ts +23 -0
- package/dist/protocol/api/retry.d.ts.map +1 -0
- package/dist/protocol/api/retry.js +102 -0
- package/dist/protocol/api/retry.js.map +1 -0
- package/dist/protocol/api/routes.d.ts +23 -0
- package/dist/protocol/api/routes.d.ts.map +1 -0
- package/dist/protocol/api/routes.js +50 -0
- package/dist/protocol/api/routes.js.map +1 -0
- package/dist/protocol/api/token.d.ts +38 -0
- package/dist/protocol/api/token.d.ts.map +1 -0
- package/dist/protocol/api/token.js +191 -0
- package/dist/protocol/api/token.js.map +1 -0
- package/dist/protocol/gateway/codec.d.ts +9 -0
- package/dist/protocol/gateway/codec.d.ts.map +1 -0
- package/dist/protocol/gateway/codec.js +32 -0
- package/dist/protocol/gateway/codec.js.map +1 -0
- package/dist/protocol/gateway/constants.d.ts +71 -0
- package/dist/protocol/gateway/constants.d.ts.map +1 -0
- package/dist/protocol/gateway/constants.js +91 -0
- package/dist/protocol/gateway/constants.js.map +1 -0
- package/dist/protocol/gateway/event-dispatcher.d.ts +97 -0
- package/dist/protocol/gateway/event-dispatcher.d.ts.map +1 -0
- package/dist/protocol/gateway/event-dispatcher.js +155 -0
- package/dist/protocol/gateway/event-dispatcher.js.map +1 -0
- package/dist/protocol/gateway/gateway-connection.d.ts +73 -0
- package/dist/protocol/gateway/gateway-connection.d.ts.map +1 -0
- package/dist/protocol/gateway/gateway-connection.js +293 -0
- package/dist/protocol/gateway/gateway-connection.js.map +1 -0
- package/dist/protocol/gateway/reconnect.d.ts +25 -0
- package/dist/protocol/gateway/reconnect.d.ts.map +1 -0
- package/dist/protocol/gateway/reconnect.js +118 -0
- package/dist/protocol/gateway/reconnect.js.map +1 -0
- package/dist/protocol/index.d.ts +41 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/index.js +46 -0
- package/dist/protocol/index.js.map +1 -0
- package/dist/protocol/transport/index.d.ts +8 -0
- package/dist/protocol/transport/index.d.ts.map +1 -0
- package/dist/protocol/transport/index.js +4 -0
- package/dist/protocol/transport/index.js.map +1 -0
- package/dist/protocol/transport/types.d.ts +109 -0
- package/dist/protocol/transport/types.d.ts.map +1 -0
- package/dist/protocol/transport/types.js +15 -0
- package/dist/protocol/transport/types.js.map +1 -0
- package/dist/protocol/transport/webhook-server-node.d.ts +14 -0
- package/dist/protocol/transport/webhook-server-node.d.ts.map +1 -0
- package/dist/protocol/transport/webhook-server-node.js +57 -0
- package/dist/protocol/transport/webhook-server-node.js.map +1 -0
- package/dist/protocol/transport/webhook-verify.d.ts +49 -0
- package/dist/protocol/transport/webhook-verify.d.ts.map +1 -0
- package/dist/protocol/transport/webhook-verify.js +98 -0
- package/dist/protocol/transport/webhook-verify.js.map +1 -0
- package/dist/protocol/transport/webhook.d.ts +37 -0
- package/dist/protocol/transport/webhook.d.ts.map +1 -0
- package/dist/protocol/transport/webhook.js +183 -0
- package/dist/protocol/transport/webhook.js.map +1 -0
- package/dist/protocol/types.d.ts +270 -0
- package/dist/protocol/types.d.ts.map +1 -0
- package/dist/protocol/types.js +59 -0
- package/dist/protocol/types.js.map +1 -0
- package/dist/protocol/utils/audio.d.ts +79 -0
- package/dist/protocol/utils/audio.d.ts.map +1 -0
- package/dist/protocol/utils/audio.js +501 -0
- package/dist/protocol/utils/audio.js.map +1 -0
- package/dist/protocol/utils/decode-media-path.d.ts +26 -0
- package/dist/protocol/utils/decode-media-path.d.ts.map +1 -0
- package/dist/protocol/utils/decode-media-path.js +79 -0
- package/dist/protocol/utils/decode-media-path.js.map +1 -0
- package/dist/protocol/utils/ffmpeg.d.ts +38 -0
- package/dist/protocol/utils/ffmpeg.d.ts.map +1 -0
- package/dist/protocol/utils/ffmpeg.js +119 -0
- package/dist/protocol/utils/ffmpeg.js.map +1 -0
- package/dist/protocol/utils/file-utils.d.ts +42 -0
- package/dist/protocol/utils/file-utils.d.ts.map +1 -0
- package/dist/protocol/utils/file-utils.js +108 -0
- package/dist/protocol/utils/file-utils.js.map +1 -0
- package/dist/protocol/utils/format.d.ts +10 -0
- package/dist/protocol/utils/format.d.ts.map +1 -0
- package/dist/protocol/utils/format.js +68 -0
- package/dist/protocol/utils/format.js.map +1 -0
- package/dist/protocol/utils/image-size.d.ts +56 -0
- package/dist/protocol/utils/image-size.d.ts.map +1 -0
- package/dist/protocol/utils/image-size.js +185 -0
- package/dist/protocol/utils/image-size.js.map +1 -0
- package/dist/protocol/utils/media-source.d.ts +145 -0
- package/dist/protocol/utils/media-source.d.ts.map +1 -0
- package/dist/protocol/utils/media-source.js +175 -0
- package/dist/protocol/utils/media-source.js.map +1 -0
- package/dist/protocol/utils/media-tags.d.ts +15 -0
- package/dist/protocol/utils/media-tags.d.ts.map +1 -0
- package/dist/protocol/utils/media-tags.js +150 -0
- package/dist/protocol/utils/media-tags.js.map +1 -0
- package/dist/protocol/utils/media-type.d.ts +41 -0
- package/dist/protocol/utils/media-type.d.ts.map +1 -0
- package/dist/protocol/utils/media-type.js +89 -0
- package/dist/protocol/utils/media-type.js.map +1 -0
- package/dist/protocol/utils/payload.d.ts +52 -0
- package/dist/protocol/utils/payload.d.ts.map +1 -0
- package/dist/protocol/utils/payload.js +100 -0
- package/dist/protocol/utils/payload.js.map +1 -0
- package/dist/protocol/utils/ref-attachments.d.ts +80 -0
- package/dist/protocol/utils/ref-attachments.d.ts.map +1 -0
- package/dist/protocol/utils/ref-attachments.js +121 -0
- package/dist/protocol/utils/ref-attachments.js.map +1 -0
- package/dist/protocol/utils/ref-index-store.d.ts +69 -0
- package/dist/protocol/utils/ref-index-store.d.ts.map +1 -0
- package/dist/protocol/utils/ref-index-store.js +227 -0
- package/dist/protocol/utils/ref-index-store.js.map +1 -0
- package/dist/protocol/utils/reply-limiter.d.ts +75 -0
- package/dist/protocol/utils/reply-limiter.d.ts.map +1 -0
- package/dist/protocol/utils/reply-limiter.js +123 -0
- package/dist/protocol/utils/reply-limiter.js.map +1 -0
- package/dist/protocol/utils/session-store.d.ts +73 -0
- package/dist/protocol/utils/session-store.d.ts.map +1 -0
- package/dist/protocol/utils/session-store.js +274 -0
- package/dist/protocol/utils/session-store.js.map +1 -0
- package/dist/protocol/utils/target.d.ts +53 -0
- package/dist/protocol/utils/target.d.ts.map +1 -0
- package/dist/protocol/utils/target.js +104 -0
- package/dist/protocol/utils/target.js.map +1 -0
- package/dist/protocol/utils/text-chunk.d.ts +21 -0
- package/dist/protocol/utils/text-chunk.d.ts.map +1 -0
- package/dist/protocol/utils/text-chunk.js +25 -0
- package/dist/protocol/utils/text-chunk.js.map +1 -0
- package/dist/protocol/utils/text-parsing.d.ts +40 -0
- package/dist/protocol/utils/text-parsing.d.ts.map +1 -0
- package/dist/protocol/utils/text-parsing.js +126 -0
- package/dist/protocol/utils/text-parsing.js.map +1 -0
- package/dist/protocol/utils/upload-cache.d.ts +27 -0
- package/dist/protocol/utils/upload-cache.d.ts.map +1 -0
- package/dist/protocol/utils/upload-cache.js +73 -0
- package/dist/protocol/utils/upload-cache.js.map +1 -0
- package/dist/protocol/utils/voice-text.d.ts +9 -0
- package/dist/protocol/utils/voice-text.d.ts.map +1 -0
- package/dist/protocol/utils/voice-text.js +16 -0
- package/dist/protocol/utils/voice-text.js.map +1 -0
- package/dist/storage/index.d.ts +23 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +21 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/kv-store.d.ts +85 -0
- package/dist/storage/kv-store.d.ts.map +1 -0
- package/dist/storage/kv-store.js +184 -0
- package/dist/storage/kv-store.js.map +1 -0
- package/dist/storage/session-adapter.d.ts +35 -0
- package/dist/storage/session-adapter.d.ts.map +1 -0
- package/dist/storage/session-adapter.js +48 -0
- package/dist/storage/session-adapter.js.map +1 -0
- package/dist/streaming.d.ts +67 -0
- package/dist/streaming.d.ts.map +1 -0
- package/dist/streaming.js +207 -0
- package/dist/streaming.js.map +1 -0
- package/package.json +90 -0
- package/src/QQBot.ts +1123 -0
- package/src/index.ts +175 -0
- package/src/middleware/access-policy.ts +139 -0
- package/src/middleware/concurrency-guard.ts +450 -0
- package/src/middleware/content-sanitizer.ts +125 -0
- package/src/middleware/envelope-formatter.ts +140 -0
- package/src/middleware/error-handler.ts +77 -0
- package/src/middleware/history-buffer.ts +169 -0
- package/src/middleware/index.ts +129 -0
- package/src/middleware/mention-gate.ts +227 -0
- package/src/middleware/message-filter.ts +82 -0
- package/src/middleware/quote-ref.ts +237 -0
- package/src/middleware/rate-limiter.ts +112 -0
- package/src/middleware/slash-command.ts +307 -0
- package/src/middleware/types.ts +273 -0
- package/src/middleware/typing-indicator.ts +107 -0
- package/src/protocol/api/api-client.ts +170 -0
- package/src/protocol/api/media-chunked.ts +517 -0
- package/src/protocol/api/media.ts +188 -0
- package/src/protocol/api/messages.ts +345 -0
- package/src/protocol/api/retry.ts +157 -0
- package/src/protocol/api/routes.ts +62 -0
- package/src/protocol/api/token.ts +250 -0
- package/src/protocol/gateway/codec.ts +33 -0
- package/src/protocol/gateway/constants.ts +98 -0
- package/src/protocol/gateway/event-dispatcher.ts +275 -0
- package/src/protocol/gateway/gateway-connection.ts +375 -0
- package/src/protocol/gateway/reconnect.ts +155 -0
- package/src/protocol/index.ts +238 -0
- package/src/protocol/transport/index.ts +17 -0
- package/src/protocol/transport/types.ts +121 -0
- package/src/protocol/transport/webhook-server-node.ts +64 -0
- package/src/protocol/transport/webhook-verify.ts +118 -0
- package/src/protocol/transport/webhook.ts +237 -0
- package/src/protocol/types.ts +305 -0
- package/src/protocol/utils/audio.ts +663 -0
- package/src/protocol/utils/decode-media-path.ts +88 -0
- package/src/protocol/utils/ffmpeg.ts +131 -0
- package/src/protocol/utils/file-utils.ts +121 -0
- package/src/protocol/utils/format.ts +69 -0
- package/src/protocol/utils/image-size.ts +236 -0
- package/src/protocol/utils/media-source.ts +243 -0
- package/src/protocol/utils/media-tags.ts +177 -0
- package/src/protocol/utils/media-type.ts +102 -0
- package/src/protocol/utils/payload.ts +157 -0
- package/src/protocol/utils/ref-attachments.ts +190 -0
- package/src/protocol/utils/ref-index-store.ts +279 -0
- package/src/protocol/utils/reply-limiter.ts +164 -0
- package/src/protocol/utils/session-store.ts +346 -0
- package/src/protocol/utils/target.ts +121 -0
- package/src/protocol/utils/text-chunk.ts +29 -0
- package/src/protocol/utils/text-parsing.ts +153 -0
- package/src/protocol/utils/upload-cache.ts +107 -0
- package/src/protocol/utils/voice-text.ts +16 -0
- package/src/storage/index.ts +24 -0
- package/src/storage/kv-store.ts +243 -0
- package/src/storage/session-adapter.ts +66 -0
- package/src/streaming.ts +255 -0
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multi-account JSON-file session persistence for the WebSocket gateway.
|
|
3
|
+
*
|
|
4
|
+
* QQ Open Platform allows a bot to RESUME after a brief disconnect by
|
|
5
|
+
* replaying the last `(session_id, lastSeq)` it observed. This module
|
|
6
|
+
* persists that pair plus `intentLevelIndex` (so the auto-fallback logic
|
|
7
|
+
* can resume at the right intent level on restart) into per-account JSON
|
|
8
|
+
* files, with throttled writes and automatic expiry of stale sessions.
|
|
9
|
+
*
|
|
10
|
+
* Path resolution is delegated to the embedder via the constructor — the
|
|
11
|
+
* SDK does not assume any particular directory layout.
|
|
12
|
+
*/
|
|
13
|
+
import fs from "node:fs";
|
|
14
|
+
import path from "node:path";
|
|
15
|
+
import { formatErrorMessage } from "./format.js";
|
|
16
|
+
const NOOP_LOGGER = {
|
|
17
|
+
log: () => { },
|
|
18
|
+
error: () => { },
|
|
19
|
+
};
|
|
20
|
+
const DEFAULT_EXPIRE_MS = 5 * 60 * 1000;
|
|
21
|
+
const DEFAULT_SAVE_THROTTLE_MS = 1000;
|
|
22
|
+
/**
|
|
23
|
+
* Multi-account file-backed gateway session store.
|
|
24
|
+
*
|
|
25
|
+
* One instance handles all accounts owned by a single QQ Bot deployment —
|
|
26
|
+
* the per-account file path is derived deterministically from
|
|
27
|
+
* `accountId`. The on-disk filename uses base64url(accountId) so any
|
|
28
|
+
* Unicode/special-character account id is safe.
|
|
29
|
+
*/
|
|
30
|
+
export class FileSessionStore {
|
|
31
|
+
options;
|
|
32
|
+
throttleState = new Map();
|
|
33
|
+
constructor(options) {
|
|
34
|
+
this.options = {
|
|
35
|
+
getDir: options.getDir,
|
|
36
|
+
ensureDir: options.ensureDir,
|
|
37
|
+
expireMs: options.expireMs ?? DEFAULT_EXPIRE_MS,
|
|
38
|
+
saveThrottleMs: options.saveThrottleMs ?? DEFAULT_SAVE_THROTTLE_MS,
|
|
39
|
+
logger: {
|
|
40
|
+
log: options.logger?.log ?? NOOP_LOGGER.log,
|
|
41
|
+
error: options.logger?.error ?? NOOP_LOGGER.error,
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** Load a saved session, rejecting expired or mismatched-appId entries. */
|
|
46
|
+
load(accountId, expectedAppId) {
|
|
47
|
+
try {
|
|
48
|
+
let filePath = null;
|
|
49
|
+
for (const candidatePath of this.getCandidateSessionPaths(accountId)) {
|
|
50
|
+
if (fs.existsSync(candidatePath)) {
|
|
51
|
+
filePath = candidatePath;
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (!filePath) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
const data = fs.readFileSync(filePath, "utf-8");
|
|
59
|
+
const state = JSON.parse(data);
|
|
60
|
+
const now = Date.now();
|
|
61
|
+
if (now - state.savedAt > this.options.expireMs) {
|
|
62
|
+
this.options.logger.log(`[session-store] Session expired for ${accountId}, age: ${Math.round((now - state.savedAt) / 1000)}s`);
|
|
63
|
+
try {
|
|
64
|
+
fs.unlinkSync(filePath);
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
// Best-effort cleanup; ignore errors.
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
if (expectedAppId && state.appId && state.appId !== expectedAppId) {
|
|
72
|
+
this.options.logger.log(`[session-store] appId mismatch for ${accountId}: saved=${state.appId}, current=${expectedAppId}. Discarding stale session.`);
|
|
73
|
+
try {
|
|
74
|
+
fs.unlinkSync(filePath);
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
// Best-effort cleanup; ignore errors.
|
|
78
|
+
}
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
if (!state.sessionId || state.lastSeq === null || state.lastSeq === undefined) {
|
|
82
|
+
this.options.logger.log(`[session-store] Invalid session data for ${accountId}`);
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
this.options.logger.log(`[session-store] Loaded session for ${accountId}: sessionId=${state.sessionId}, lastSeq=${state.lastSeq}, appId=${state.appId ?? "unknown"}, age=${Math.round((now - state.savedAt) / 1000)}s`);
|
|
86
|
+
return state;
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
this.options.logger.error(`[session-store] Failed to load session for ${accountId}: ${formatErrorMessage(err)}`);
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/** Save session state with throttling. */
|
|
94
|
+
save(state) {
|
|
95
|
+
const { accountId } = state;
|
|
96
|
+
let throttle = this.throttleState.get(accountId);
|
|
97
|
+
if (!throttle) {
|
|
98
|
+
throttle = { pendingState: null, lastSaveTime: 0, throttleTimer: null };
|
|
99
|
+
this.throttleState.set(accountId, throttle);
|
|
100
|
+
}
|
|
101
|
+
const now = Date.now();
|
|
102
|
+
const timeSinceLastSave = now - throttle.lastSaveTime;
|
|
103
|
+
if (timeSinceLastSave >= this.options.saveThrottleMs) {
|
|
104
|
+
this.doSaveSession(state);
|
|
105
|
+
throttle.lastSaveTime = now;
|
|
106
|
+
throttle.pendingState = null;
|
|
107
|
+
if (throttle.throttleTimer) {
|
|
108
|
+
clearTimeout(throttle.throttleTimer);
|
|
109
|
+
throttle.throttleTimer = null;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
throttle.pendingState = state;
|
|
114
|
+
if (!throttle.throttleTimer) {
|
|
115
|
+
const delay = this.options.saveThrottleMs - timeSinceLastSave;
|
|
116
|
+
throttle.throttleTimer = setTimeout(() => {
|
|
117
|
+
const t = this.throttleState.get(accountId);
|
|
118
|
+
if (t?.pendingState) {
|
|
119
|
+
this.doSaveSession(t.pendingState);
|
|
120
|
+
t.lastSaveTime = Date.now();
|
|
121
|
+
t.pendingState = null;
|
|
122
|
+
}
|
|
123
|
+
if (t) {
|
|
124
|
+
t.throttleTimer = null;
|
|
125
|
+
}
|
|
126
|
+
}, delay);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/** Clear a saved session and any pending throttle state. */
|
|
131
|
+
clear(accountId) {
|
|
132
|
+
const throttle = this.throttleState.get(accountId);
|
|
133
|
+
if (throttle) {
|
|
134
|
+
if (throttle.throttleTimer) {
|
|
135
|
+
clearTimeout(throttle.throttleTimer);
|
|
136
|
+
}
|
|
137
|
+
this.throttleState.delete(accountId);
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
let cleared = false;
|
|
141
|
+
for (const filePath of this.getCandidateSessionPaths(accountId)) {
|
|
142
|
+
if (fs.existsSync(filePath)) {
|
|
143
|
+
fs.unlinkSync(filePath);
|
|
144
|
+
cleared = true;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (cleared) {
|
|
148
|
+
this.options.logger.log(`[session-store] Cleared session for ${accountId}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
catch (err) {
|
|
152
|
+
this.options.logger.error(`[session-store] Failed to clear session for ${accountId}: ${formatErrorMessage(err)}`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/** Update only lastSeq on the persisted session. */
|
|
156
|
+
updateLastSeq(accountId, lastSeq) {
|
|
157
|
+
const existing = this.load(accountId);
|
|
158
|
+
if (existing?.sessionId) {
|
|
159
|
+
this.save({ ...existing, lastSeq });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/** Load all saved sessions from disk. */
|
|
163
|
+
getAllSessions() {
|
|
164
|
+
const sessions = new Map();
|
|
165
|
+
try {
|
|
166
|
+
const sessionDir = this.options.getDir();
|
|
167
|
+
if (!fs.existsSync(sessionDir)) {
|
|
168
|
+
return [];
|
|
169
|
+
}
|
|
170
|
+
const files = fs.readdirSync(sessionDir);
|
|
171
|
+
for (const file of files) {
|
|
172
|
+
if (this.isSessionFileName(file)) {
|
|
173
|
+
try {
|
|
174
|
+
const { state } = this.readSessionStateFile(file);
|
|
175
|
+
if (typeof state.accountId !== "string" || !state.accountId) {
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
const existing = sessions.get(state.accountId);
|
|
179
|
+
if (!existing || (state.savedAt ?? 0) >= (existing.savedAt ?? 0)) {
|
|
180
|
+
sessions.set(state.accountId, state);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
// Skip malformed files; cleanup is opportunistic.
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
catch {
|
|
190
|
+
// Directory may not exist yet; return empty.
|
|
191
|
+
}
|
|
192
|
+
return [...sessions.values()];
|
|
193
|
+
}
|
|
194
|
+
/** Remove expired session files from disk. Returns number removed. */
|
|
195
|
+
cleanupExpiredSessions() {
|
|
196
|
+
let cleaned = 0;
|
|
197
|
+
try {
|
|
198
|
+
const sessionDir = this.options.getDir();
|
|
199
|
+
if (!fs.existsSync(sessionDir)) {
|
|
200
|
+
return 0;
|
|
201
|
+
}
|
|
202
|
+
const now = Date.now();
|
|
203
|
+
const files = fs.readdirSync(sessionDir);
|
|
204
|
+
for (const file of files) {
|
|
205
|
+
if (this.isSessionFileName(file)) {
|
|
206
|
+
const filePath = path.join(sessionDir, file);
|
|
207
|
+
try {
|
|
208
|
+
const { state } = this.readSessionStateFile(file);
|
|
209
|
+
if (now - state.savedAt > this.options.expireMs) {
|
|
210
|
+
fs.unlinkSync(filePath);
|
|
211
|
+
cleaned++;
|
|
212
|
+
this.options.logger.log(`[session-store] Cleaned expired session: ${file}`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
catch {
|
|
216
|
+
try {
|
|
217
|
+
fs.unlinkSync(filePath);
|
|
218
|
+
cleaned++;
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
// Best-effort cleanup; ignore errors.
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
catch {
|
|
228
|
+
// Directory may not exist yet; nothing to do.
|
|
229
|
+
}
|
|
230
|
+
return cleaned;
|
|
231
|
+
}
|
|
232
|
+
// ============ Internal ============
|
|
233
|
+
encodeAccountIdForFileName(accountId) {
|
|
234
|
+
return Buffer.from(accountId, "utf8").toString("base64url");
|
|
235
|
+
}
|
|
236
|
+
getLegacySessionPath(accountId) {
|
|
237
|
+
const safeId = accountId.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
238
|
+
return path.join(this.options.getDir(), `session-${safeId}.json`);
|
|
239
|
+
}
|
|
240
|
+
getSessionPath(accountId) {
|
|
241
|
+
const encodedId = this.encodeAccountIdForFileName(accountId);
|
|
242
|
+
return path.join(this.options.getDir(), `session-${encodedId}.json`);
|
|
243
|
+
}
|
|
244
|
+
getCandidateSessionPaths(accountId) {
|
|
245
|
+
const primaryPath = this.getSessionPath(accountId);
|
|
246
|
+
const legacyPath = this.getLegacySessionPath(accountId);
|
|
247
|
+
return primaryPath === legacyPath ? [primaryPath] : [primaryPath, legacyPath];
|
|
248
|
+
}
|
|
249
|
+
isSessionFileName(file) {
|
|
250
|
+
return file.startsWith("session-") && file.endsWith(".json");
|
|
251
|
+
}
|
|
252
|
+
readSessionStateFile(file) {
|
|
253
|
+
const filePath = path.join(this.options.getDir(), file);
|
|
254
|
+
const data = fs.readFileSync(filePath, "utf-8");
|
|
255
|
+
return { filePath, state: JSON.parse(data) };
|
|
256
|
+
}
|
|
257
|
+
doSaveSession(state) {
|
|
258
|
+
const filePath = this.getSessionPath(state.accountId);
|
|
259
|
+
const legacyPath = this.getLegacySessionPath(state.accountId);
|
|
260
|
+
try {
|
|
261
|
+
this.options.ensureDir();
|
|
262
|
+
const stateToSave = { ...state, savedAt: Date.now() };
|
|
263
|
+
fs.writeFileSync(filePath, JSON.stringify(stateToSave, null, 2), "utf-8");
|
|
264
|
+
if (legacyPath !== filePath && fs.existsSync(legacyPath)) {
|
|
265
|
+
fs.unlinkSync(legacyPath);
|
|
266
|
+
}
|
|
267
|
+
this.options.logger.log(`[session-store] Saved session for ${state.accountId}: sessionId=${state.sessionId}, lastSeq=${state.lastSeq}`);
|
|
268
|
+
}
|
|
269
|
+
catch (err) {
|
|
270
|
+
this.options.logger.error(`[session-store] Failed to save session for ${state.accountId}: ${formatErrorMessage(err)}`);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
//# sourceMappingURL=session-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-store.js","sourceRoot":"","sources":["../../../src/protocol/utils/session-store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAiCjD,MAAM,WAAW,GAAiC;IAChD,GAAG,EAAE,GAAG,EAAE,GAAE,CAAC;IACb,KAAK,EAAE,GAAG,EAAE,GAAE,CAAC;CAChB,CAAC;AAEF,MAAM,iBAAiB,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;AACxC,MAAM,wBAAwB,GAAG,IAAI,CAAC;AAEtC;;;;;;;GAOG;AACH,MAAM,OAAO,gBAAgB;IACV,OAAO,CAEtB;IAEe,aAAa,GAAG,IAAI,GAAG,EAOrC,CAAC;IAEJ,YAAY,OAAgC;QAC1C,I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AC;QACJ,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CACvB,8CAA8C,KAAK,CAAC,SAAS,KAAK,kBAAkB,CAAC,GAAG,CAAC,EAAE,CAC5F,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QQ Bot target address parser.
|
|
3
|
+
*
|
|
4
|
+
* QQ Bot conversations are addressed via strings of the form
|
|
5
|
+
* `qqbot:<scope>:<id>` where scope is `c2c | group | channel`. This
|
|
6
|
+
* module parses, validates, and canonicalizes those strings.
|
|
7
|
+
*
|
|
8
|
+
* All functions are pure (no I/O) and framework-agnostic.
|
|
9
|
+
*/
|
|
10
|
+
/** Supported target types. */
|
|
11
|
+
export type TargetType = "c2c" | "group" | "channel";
|
|
12
|
+
/** Parsed delivery target. */
|
|
13
|
+
export interface ParsedTarget {
|
|
14
|
+
type: TargetType;
|
|
15
|
+
id: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Parse a qqbot target string into a structured delivery target.
|
|
19
|
+
*
|
|
20
|
+
* Supported formats:
|
|
21
|
+
* - `qqbot:c2c:openid` → C2C direct message
|
|
22
|
+
* - `qqbot:group:groupid` → Group message
|
|
23
|
+
* - `qqbot:channel:channelid` → Channel message
|
|
24
|
+
* - `c2c:openid` → C2C (without qqbot: prefix)
|
|
25
|
+
* - `group:groupid` → Group (without qqbot: prefix)
|
|
26
|
+
* - `channel:channelid` → Channel (without qqbot: prefix)
|
|
27
|
+
* - `openid` → C2C (bare openid, default)
|
|
28
|
+
*
|
|
29
|
+
* @param to - Raw target string.
|
|
30
|
+
* @returns Parsed target with type and id.
|
|
31
|
+
* @throws {Error} When the target format is invalid.
|
|
32
|
+
*/
|
|
33
|
+
export declare function parseTarget(to: string): ParsedTarget;
|
|
34
|
+
/**
|
|
35
|
+
* Map a parsed target type to a ChatScope for API calls.
|
|
36
|
+
*
|
|
37
|
+
* Channel and DM targets are not C2C/Group scoped and should be handled
|
|
38
|
+
* separately by the caller.
|
|
39
|
+
*
|
|
40
|
+
* @returns `'c2c'` or `'group'`, or `undefined` for channel targets.
|
|
41
|
+
*/
|
|
42
|
+
export declare function targetToChatScope(target: ParsedTarget): "c2c" | "group" | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Normalize a QQ Bot target string into the canonical `qqbot:...` form.
|
|
45
|
+
*
|
|
46
|
+
* Returns `undefined` when the target does not look like a QQ Bot address.
|
|
47
|
+
*/
|
|
48
|
+
export declare function normalizeTarget(target: string): string | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Return true when the string looks like a QQ Bot target ID.
|
|
51
|
+
*/
|
|
52
|
+
export declare function looksLikeQQBotTarget(id: string): boolean;
|
|
53
|
+
//# sourceMappingURL=target.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"target.d.ts","sourceRoot":"","sources":["../../../src/protocol/utils/target.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,8BAA8B;AAC9B,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,OAAO,GAAG,SAAS,CAAC;AAErD,8BAA8B;AAC9B,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,UAAU,CAAC;IACjB,EAAE,EAAE,MAAM,CAAC;CACZ;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,YAAY,CAgCpD;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,YAAY,GAAG,KAAK,GAAG,OAAO,GAAG,SAAS,CAQnF;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAYlE;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAWxD"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QQ Bot target address parser.
|
|
3
|
+
*
|
|
4
|
+
* QQ Bot conversations are addressed via strings of the form
|
|
5
|
+
* `qqbot:<scope>:<id>` where scope is `c2c | group | channel`. This
|
|
6
|
+
* module parses, validates, and canonicalizes those strings.
|
|
7
|
+
*
|
|
8
|
+
* All functions are pure (no I/O) and framework-agnostic.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Parse a qqbot target string into a structured delivery target.
|
|
12
|
+
*
|
|
13
|
+
* Supported formats:
|
|
14
|
+
* - `qqbot:c2c:openid` → C2C direct message
|
|
15
|
+
* - `qqbot:group:groupid` → Group message
|
|
16
|
+
* - `qqbot:channel:channelid` → Channel message
|
|
17
|
+
* - `c2c:openid` → C2C (without qqbot: prefix)
|
|
18
|
+
* - `group:groupid` → Group (without qqbot: prefix)
|
|
19
|
+
* - `channel:channelid` → Channel (without qqbot: prefix)
|
|
20
|
+
* - `openid` → C2C (bare openid, default)
|
|
21
|
+
*
|
|
22
|
+
* @param to - Raw target string.
|
|
23
|
+
* @returns Parsed target with type and id.
|
|
24
|
+
* @throws {Error} When the target format is invalid.
|
|
25
|
+
*/
|
|
26
|
+
export function parseTarget(to) {
|
|
27
|
+
const id = to.replace(/^qqbot:/i, "");
|
|
28
|
+
if (id.startsWith("c2c:")) {
|
|
29
|
+
const userId = id.slice(4);
|
|
30
|
+
if (!userId) {
|
|
31
|
+
throw new Error(`Invalid c2c target format: ${to} - missing user ID`);
|
|
32
|
+
}
|
|
33
|
+
return { type: "c2c", id: userId };
|
|
34
|
+
}
|
|
35
|
+
if (id.startsWith("group:")) {
|
|
36
|
+
const groupId = id.slice(6);
|
|
37
|
+
if (!groupId) {
|
|
38
|
+
throw new Error(`Invalid group target format: ${to} - missing group ID`);
|
|
39
|
+
}
|
|
40
|
+
return { type: "group", id: groupId };
|
|
41
|
+
}
|
|
42
|
+
if (id.startsWith("channel:")) {
|
|
43
|
+
const channelId = id.slice(8);
|
|
44
|
+
if (!channelId) {
|
|
45
|
+
throw new Error(`Invalid channel target format: ${to} - missing channel ID`);
|
|
46
|
+
}
|
|
47
|
+
return { type: "channel", id: channelId };
|
|
48
|
+
}
|
|
49
|
+
if (!id) {
|
|
50
|
+
throw new Error(`Invalid target format: ${to} - empty ID after removing qqbot: prefix`);
|
|
51
|
+
}
|
|
52
|
+
return { type: "c2c", id };
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Map a parsed target type to a ChatScope for API calls.
|
|
56
|
+
*
|
|
57
|
+
* Channel and DM targets are not C2C/Group scoped and should be handled
|
|
58
|
+
* separately by the caller.
|
|
59
|
+
*
|
|
60
|
+
* @returns `'c2c'` or `'group'`, or `undefined` for channel targets.
|
|
61
|
+
*/
|
|
62
|
+
export function targetToChatScope(target) {
|
|
63
|
+
if (target.type === "c2c") {
|
|
64
|
+
return "c2c";
|
|
65
|
+
}
|
|
66
|
+
if (target.type === "group") {
|
|
67
|
+
return "group";
|
|
68
|
+
}
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Normalize a QQ Bot target string into the canonical `qqbot:...` form.
|
|
73
|
+
*
|
|
74
|
+
* Returns `undefined` when the target does not look like a QQ Bot address.
|
|
75
|
+
*/
|
|
76
|
+
export function normalizeTarget(target) {
|
|
77
|
+
const id = target.replace(/^qqbot:/i, "");
|
|
78
|
+
if (id.startsWith("c2c:") || id.startsWith("group:") || id.startsWith("channel:")) {
|
|
79
|
+
return `qqbot:${id}`;
|
|
80
|
+
}
|
|
81
|
+
if (/^[0-9a-fA-F]{32}$/.test(id)) {
|
|
82
|
+
return `qqbot:c2c:${id}`;
|
|
83
|
+
}
|
|
84
|
+
if (/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(id)) {
|
|
85
|
+
return `qqbot:c2c:${id}`;
|
|
86
|
+
}
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Return true when the string looks like a QQ Bot target ID.
|
|
91
|
+
*/
|
|
92
|
+
export function looksLikeQQBotTarget(id) {
|
|
93
|
+
if (/^qqbot:(c2c|group|channel):/i.test(id)) {
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
if (/^(c2c|group|channel):/i.test(id)) {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
if (/^[0-9a-fA-F]{32}$/.test(id)) {
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
return /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(id);
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=target.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"target.js","sourceRoot":"","sources":["../../../src/protocol/utils/target.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAWH;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,WAAW,CAAC,EAAU;IACpC,MAAM,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC;IAEtC,IAAI,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;QAC1B,MAAM,MAAM,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAC3B,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,IAAI,KAAK,CAAC,8BAA8B,EAAE,oBAAoB,CAAC,CAAC;QACxE,CAAC;QACD,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC;IACrC,CAAC;IAED,IAAI,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC5B,MAAM,OAAO,GAAG,EAAE,CAAC,KAA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EAAE,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;QAClF,OAAO,SAAS,EAAE,EAAE,CAAC;IACvB,CAAC;IACD,IAAI,mBAAmB,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;QACjC,OAAO,aAAa,EAAE,EAAE,CAAC;IAC3B,CAAC;IACD,IAAI,+EAA+E,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;QAC7F,OAAO,aAAa,EAAE,EAAE,CAAC;IAC3B,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAAC,EAAU;IAC7C,IAAI,8BAA8B,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;QAC5C,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,wBAAwB,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;QACtC,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,mBAAmB,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC;QACjC,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,+EAA+E,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAClG,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QQ Bot single-message text length limits and a naive fallback splitter.
|
|
3
|
+
*
|
|
4
|
+
* The QQ Open Platform caps a single text message at ~5000 characters.
|
|
5
|
+
* When richer markdown-aware chunking is unavailable (e.g. outside an
|
|
6
|
+
* outbound pipeline), this module provides a simple byte-window split
|
|
7
|
+
* so callers always have a safe fallback.
|
|
8
|
+
*/
|
|
9
|
+
/** Maximum text length for a single QQ Bot message. */
|
|
10
|
+
export declare const TEXT_CHUNK_LIMIT = 5000;
|
|
11
|
+
/** Text chunker function signature. */
|
|
12
|
+
export type ChunkTextFn = (text: string, limit: number) => string[];
|
|
13
|
+
/**
|
|
14
|
+
* Naive text chunking fallback.
|
|
15
|
+
*
|
|
16
|
+
* Splits on a fixed character window — does not respect markdown
|
|
17
|
+
* structure. Use this only when the embedder cannot supply a
|
|
18
|
+
* smarter chunker.
|
|
19
|
+
*/
|
|
20
|
+
export declare function chunkText(text: string, limit?: number): string[];
|
|
21
|
+
//# sourceMappingURL=text-chunk.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text-chunk.d.ts","sourceRoot":"","sources":["../../../src/protocol/utils/text-chunk.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,uDAAuD;AACvD,eAAO,MAAM,gBAAgB,OAAO,CAAC;AAErC,uCAAuC;AACvC,MAAM,MAAM,WAAW,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,EAAE,CAAC;AAEpE;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,GAAE,MAAyB,GAAG,MAAM,EAAE,CAMlF"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QQ Bot single-message text length limits and a naive fallback splitter.
|
|
3
|
+
*
|
|
4
|
+
* The QQ Open Platform caps a single text message at ~5000 characters.
|
|
5
|
+
* When richer markdown-aware chunking is unavailable (e.g. outside an
|
|
6
|
+
* outbound pipeline), this module provides a simple byte-window split
|
|
7
|
+
* so callers always have a safe fallback.
|
|
8
|
+
*/
|
|
9
|
+
/** Maximum text length for a single QQ Bot message. */
|
|
10
|
+
export const TEXT_CHUNK_LIMIT = 5000;
|
|
11
|
+
/**
|
|
12
|
+
* Naive text chunking fallback.
|
|
13
|
+
*
|
|
14
|
+
* Splits on a fixed character window — does not respect markdown
|
|
15
|
+
* structure. Use this only when the embedder cannot supply a
|
|
16
|
+
* smarter chunker.
|
|
17
|
+
*/
|
|
18
|
+
export function chunkText(text, limit = TEXT_CHUNK_LIMIT) {
|
|
19
|
+
const chunks = [];
|
|
20
|
+
for (let i = 0; i < text.length; i += limit) {
|
|
21
|
+
chunks.push(text.slice(i, i + limit));
|
|
22
|
+
}
|
|
23
|
+
return chunks.length > 0 ? chunks : [text];
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=text-chunk.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text-chunk.js","sourceRoot":"","sources":["../../../src/protocol/utils/text-chunk.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,uDAAuD;AACvD,MAAM,CAAC,MAAM,gBAAgB,GAAG,IAAI,CAAC;AAKrC;;;;;;GAMG;AACH,MAAM,UAAU,SAAS,CAAC,IAAY,EAAE,QAAgB,gBAAgB;IACtE,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,KAAK,EAAE,CAAC;QAC5C,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC;IACxC,CAAC;IACD,OAAO,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;AAC7C,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text parsing utilities — zero external dependency.
|
|
3
|
+
*
|
|
4
|
+
* Contains pure functions for QQ Bot inbound message text processing:
|
|
5
|
+
* internal-marker stripping, ref-index extraction, face-tag rendering,
|
|
6
|
+
* and attachment-summary normalization.
|
|
7
|
+
*/
|
|
8
|
+
import type { RefAttachmentSummary } from "./ref-attachments.js";
|
|
9
|
+
/** Remove internal markers like `[internal:...]`, `[debug:...]`, `[system:...]`. */
|
|
10
|
+
export declare function filterInternalMarkers(text: string | undefined | null): string;
|
|
11
|
+
/** QQ message type for quote-reply messages. */
|
|
12
|
+
export declare const MSG_TYPE_QUOTE = 103;
|
|
13
|
+
/**
|
|
14
|
+
* Parse message_scene.ext to extract refMsgIdx and msgIdx.
|
|
15
|
+
*
|
|
16
|
+
* Supports both ext prefix formats:
|
|
17
|
+
* - `ref_msg_idx=` / `msg_idx=` (platform native format)
|
|
18
|
+
* - `refMsgIdx:` / `msgIdx:` (legacy internal format)
|
|
19
|
+
*
|
|
20
|
+
* When `messageType` equals `MSG_TYPE_QUOTE` (103) and `msgElements` is
|
|
21
|
+
* provided, `msgElements[0].msg_idx` takes precedence over the ext-parsed
|
|
22
|
+
* `refMsgIdx` value — the element-level index is more authoritative for
|
|
23
|
+
* quote messages.
|
|
24
|
+
*/
|
|
25
|
+
export declare function parseRefIndices(ext?: string[], messageType?: number, msgElements?: Array<{
|
|
26
|
+
msg_idx?: string;
|
|
27
|
+
}>): {
|
|
28
|
+
refMsgIdx?: string;
|
|
29
|
+
msgIdx?: string;
|
|
30
|
+
};
|
|
31
|
+
/** Replace QQ face tags with readable text labels. */
|
|
32
|
+
export declare function parseFaceTags(text: string | undefined | null): string;
|
|
33
|
+
/** Build attachment summaries for ref-index caching. */
|
|
34
|
+
export declare function buildAttachmentSummaries(attachments?: Array<{
|
|
35
|
+
content_type: string;
|
|
36
|
+
url: string;
|
|
37
|
+
filename?: string;
|
|
38
|
+
voice_wav_url?: string;
|
|
39
|
+
}>, localPaths?: Array<string | null>): RefAttachmentSummary[] | undefined;
|
|
40
|
+
//# sourceMappingURL=text-parsing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text-parsing.d.ts","sourceRoot":"","sources":["../../../src/protocol/utils/text-parsing.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAMjE,oFAAoF;AACpF,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,GAAG,MAAM,CAK7E;AAID,gDAAgD;AAChD,eAAO,MAAM,cAAc,MAAM,CAAC;AAElC;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,CAC7B,GAAG,CAAC,EAAE,MAAM,EAAE,EACd,WAAW,CAAC,EAAE,MAAM,EACpB,WAAW,CAAC,EAAE,KAAK,CAAC;IAAE,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,GACxC;IAAE,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,CA6BzC;AAaD,sDAAsD;AACtD,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,GAAG,MAAM,CAkBrE;AASD,wDAAwD;AACxD,wBAAgB,wBAAwB,CACtC,WAAW,CAAC,EAAE,KAAK,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC,EACF,UAAU,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,GAChC,oBAAoB,EAAE,GAAG,SAAS,CA8BpC"}
|