@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,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error-handler middleware — catch downstream errors and send a friendly reply.
|
|
3
|
+
*
|
|
4
|
+
* Add this **first** in the chain (before any other middleware) so it sees
|
|
5
|
+
* errors from all downstream stages.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* bot.use(errorHandler({
|
|
10
|
+
* format: (err) => `抱歉,处理消息时出错:${err.message}`,
|
|
11
|
+
* }));
|
|
12
|
+
* bot.use(slashCommand(...));
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
import type { Middleware, MiddlewareContext } from "./types.js";
|
|
16
|
+
export interface ErrorHandlerOptions {
|
|
17
|
+
/** Build a user-facing reply from the caught error. */
|
|
18
|
+
format?: (err: Error, ctx: MiddlewareContext) => string;
|
|
19
|
+
/**
|
|
20
|
+
* If true, also re-throw the error so `bot.on("error")` listeners fire.
|
|
21
|
+
* Defaults to `false` (consume the error after replying).
|
|
22
|
+
*/
|
|
23
|
+
rethrow?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Filter — only handle errors that match. Defaults to all `Error`
|
|
26
|
+
* instances. Useful to skip e.g. ApiError 4xx (let the user see them).
|
|
27
|
+
*/
|
|
28
|
+
filter?: (err: Error) => boolean;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Build an error-handler middleware. The middleware **wraps** the rest of
|
|
32
|
+
* the chain — if any downstream middleware (or the `message` listener)
|
|
33
|
+
* throws, it catches the error, sends a reply, and optionally re-emits.
|
|
34
|
+
*/
|
|
35
|
+
export declare function errorHandler(options?: ErrorHandlerOptions): Middleware;
|
|
36
|
+
//# sourceMappingURL=error-handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-handler.d.ts","sourceRoot":"","sources":["../../src/middleware/error-handler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAGH,OAAO,KAAK,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAEhE,MAAM,WAAW,mBAAmB;IAClC,uDAAuD;IACvD,MAAM,CAAC,EAAE,CAAC,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,iBAAiB,KAAK,MAAM,CAAC;IACxD;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;;OAGG;IACH,MAAM,CAAC,EAAE,CAAC,GAAG,EAAE,KAAK,KAAK,OAAO,CAAC;CAClC;AAUD;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAE,mBAAwB,GAAG,UAAU,CA8B1E"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error-handler middleware — catch downstream errors and send a friendly reply.
|
|
3
|
+
*
|
|
4
|
+
* Add this **first** in the chain (before any other middleware) so it sees
|
|
5
|
+
* errors from all downstream stages.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* bot.use(errorHandler({
|
|
10
|
+
* format: (err) => `抱歉,处理消息时出错:${err.message}`,
|
|
11
|
+
* }));
|
|
12
|
+
* bot.use(slashCommand(...));
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
import { ApiError } from "../protocol/types.js";
|
|
16
|
+
const DEFAULT_FORMAT = (err) => {
|
|
17
|
+
if (err instanceof ApiError) {
|
|
18
|
+
if (err.bizMessage)
|
|
19
|
+
return `[QQ ${err.bizCode ?? err.httpStatus}] ${err.bizMessage}`;
|
|
20
|
+
return `[QQ ${err.httpStatus}] ${err.message}`;
|
|
21
|
+
}
|
|
22
|
+
return err.message || "Unknown error";
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Build an error-handler middleware. The middleware **wraps** the rest of
|
|
26
|
+
* the chain — if any downstream middleware (or the `message` listener)
|
|
27
|
+
* throws, it catches the error, sends a reply, and optionally re-emits.
|
|
28
|
+
*/
|
|
29
|
+
export function errorHandler(options = {}) {
|
|
30
|
+
const format = options.format ?? DEFAULT_FORMAT;
|
|
31
|
+
const rethrow = options.rethrow ?? false;
|
|
32
|
+
const filter = options.filter ?? (() => true);
|
|
33
|
+
return async (ctx, next) => {
|
|
34
|
+
try {
|
|
35
|
+
await next();
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
39
|
+
if (!filter(e)) {
|
|
40
|
+
if (rethrow)
|
|
41
|
+
throw e;
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
ctx.log.error?.(`[error-handler] caught: ${e.message}`);
|
|
45
|
+
try {
|
|
46
|
+
const reply = format(e, ctx);
|
|
47
|
+
if (reply) {
|
|
48
|
+
await ctx.bot.sendText(ctx.replyTarget, reply);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
catch (sendErr) {
|
|
52
|
+
ctx.log.error?.(`[error-handler] failed to send error reply: ${sendErr instanceof Error ? sendErr.message : String(sendErr)}`);
|
|
53
|
+
}
|
|
54
|
+
if (rethrow)
|
|
55
|
+
throw e;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=error-handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-handler.js","sourceRoot":"","sources":["../../src/middleware/error-handler.ts"],"names":[],"mappings":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;gBACH,MAAM,KAAK,GAAG,MAAM,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;gBAC7B,IAAI,KAAK,EAAE,CAAC;oBACV,MAAM,GAAG,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;gBACjD,CAAC;YACH,CAAC;YAAC,OAAO,OAAO,EAAE,CAAC;gBACjB,GAAG,CAAC,GAAG,CAAC,KAAK,EAAE,CACb,+CAA+C,OAAO,YAAY,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAC9G,CAAC;YACJ,CAAC;YAED,IAAI,OAAO;gBAAE,MAAM,CAAC,CAAC;QACvB,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* History-buffer middleware — keep recent group messages around.
|
|
3
|
+
*
|
|
4
|
+
* Many bots want to feed the LLM a few rounds of recent context, even for
|
|
5
|
+
* messages that did not directly @-mention the bot. This middleware
|
|
6
|
+
* silently records every group message into a per-group ring buffer, then
|
|
7
|
+
* exposes the buffered list via `ctx.state.history`.
|
|
8
|
+
*
|
|
9
|
+
* **Pluggable storage** — the actual store is whatever object the user
|
|
10
|
+
* passes in. The SDK ships an in-memory implementation; users can drop in
|
|
11
|
+
* Redis / SQL / file-backed stores via the `HistoryStore` interface.
|
|
12
|
+
*/
|
|
13
|
+
import type { Middleware, MiddlewareContext } from "./types.js";
|
|
14
|
+
/** One buffered message. */
|
|
15
|
+
export interface HistoryEntry {
|
|
16
|
+
senderId: string;
|
|
17
|
+
senderName?: string;
|
|
18
|
+
content: string;
|
|
19
|
+
timestamp: number;
|
|
20
|
+
/** Original message id (for de-duplication). */
|
|
21
|
+
messageId: string;
|
|
22
|
+
}
|
|
23
|
+
/** Pluggable storage backend. */
|
|
24
|
+
export interface HistoryStore {
|
|
25
|
+
/** Append one entry into a group's buffer. Caller-provided ordering. */
|
|
26
|
+
append(groupKey: string, entry: HistoryEntry, limit: number): void | Promise<void>;
|
|
27
|
+
/** Return the most-recent entries for a group (oldest → newest). */
|
|
28
|
+
list(groupKey: string, limit: number): HistoryEntry[] | Promise<HistoryEntry[]>;
|
|
29
|
+
/** Optional: clear a group's buffer. */
|
|
30
|
+
clear?(groupKey: string): void | Promise<void>;
|
|
31
|
+
}
|
|
32
|
+
export interface HistoryBufferOptions {
|
|
33
|
+
/** Max entries kept per group. Defaults to 50. */
|
|
34
|
+
limit?: number;
|
|
35
|
+
/** Storage backend. Defaults to {@link MemoryHistoryStore}. */
|
|
36
|
+
store?: HistoryStore;
|
|
37
|
+
/**
|
|
38
|
+
* Whether to record the message even when an upstream middleware called
|
|
39
|
+
* `ctx.stop()` (e.g. mention-gate skipped a no-mention group message).
|
|
40
|
+
* Defaults to `true` — history exists precisely to capture the chatter
|
|
41
|
+
* we did not respond to.
|
|
42
|
+
*/
|
|
43
|
+
recordOnSkip?: boolean;
|
|
44
|
+
/** Group-key derivation. Defaults to `groupOpenid`. */
|
|
45
|
+
groupKey?: (ctx: MiddlewareContext) => string | undefined;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Default in-memory history store. Keeps a ring buffer per group key.
|
|
49
|
+
*
|
|
50
|
+
* Suitable for single-process bots; for multi-instance deployments use
|
|
51
|
+
* a Redis / SQL backed implementation.
|
|
52
|
+
*/
|
|
53
|
+
export declare class MemoryHistoryStore implements HistoryStore {
|
|
54
|
+
private readonly buffers;
|
|
55
|
+
append(groupKey: string, entry: HistoryEntry, limit: number): void;
|
|
56
|
+
list(groupKey: string, limit: number): HistoryEntry[];
|
|
57
|
+
clear(groupKey: string): void;
|
|
58
|
+
/** Diagnostic: number of groups with buffered history. */
|
|
59
|
+
size(): number;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Build a history-buffer middleware.
|
|
63
|
+
*
|
|
64
|
+
* The middleware:
|
|
65
|
+
* 1. Records the current group message into the store (before downstream).
|
|
66
|
+
* 2. Exposes `ctx.state.history: HistoryEntry[]` to downstream middleware
|
|
67
|
+
* and to the final `bot.on("message")` listener (oldest → newest,
|
|
68
|
+
* excluding the current message).
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* const history = historyBuffer({ limit: 5 });
|
|
73
|
+
* bot.use(mentionGate({ passthrough: true })); // record even on no-mention
|
|
74
|
+
* bot.use(history);
|
|
75
|
+
*
|
|
76
|
+
* bot.on("message", async (msg) => {
|
|
77
|
+
* const buf = msg.history ?? [];
|
|
78
|
+
* const prompt = buf.map(h => `${h.senderName}: ${h.content}`).join("\n");
|
|
79
|
+
* // ...
|
|
80
|
+
* });
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export declare function historyBuffer(options?: HistoryBufferOptions): Middleware;
|
|
84
|
+
//# sourceMappingURL=history-buffer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"history-buffer.d.ts","sourceRoot":"","sources":["../../src/middleware/history-buffer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAKhE,4BAA4B;AAC5B,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,gDAAgD;IAChD,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,iCAAiC;AACjC,MAAM,WAAW,YAAY;IAC3B,wEAAwE;IACxE,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACnF,oEAAoE;IACpE,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,YAAY,EAAE,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,CAAC;IAChF,wCAAwC;IACxC,KAAK,CAAC,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAChD;AAED,MAAM,WAAW,oBAAoB;IACnC,kDAAkD;IAClD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,+DAA+D;IAC/D,KAAK,CAAC,EAAE,YAAY,CAAC;IACrB;;;;;OAKG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,uDAAuD;IACvD,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,iBAAiB,KAAK,MAAM,GAAG,SAAS,CAAC;CAC3D;AAID;;;;;GAKG;AACH,qBAAa,kBAAmB,YAAW,YAAY;IACrD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAqC;IAE7D,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAgBlE,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,YAAY,EAAE;IAMrD,KAAK,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAI7B,0DAA0D;IAC1D,IAAI,IAAI,MAAM;CAGf;AAID;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,aAAa,CAAC,OAAO,GAAE,oBAAyB,GAAG,UAAU,CA+C5E"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* History-buffer middleware — keep recent group messages around.
|
|
3
|
+
*
|
|
4
|
+
* Many bots want to feed the LLM a few rounds of recent context, even for
|
|
5
|
+
* messages that did not directly @-mention the bot. This middleware
|
|
6
|
+
* silently records every group message into a per-group ring buffer, then
|
|
7
|
+
* exposes the buffered list via `ctx.state.history`.
|
|
8
|
+
*
|
|
9
|
+
* **Pluggable storage** — the actual store is whatever object the user
|
|
10
|
+
* passes in. The SDK ships an in-memory implementation; users can drop in
|
|
11
|
+
* Redis / SQL / file-backed stores via the `HistoryStore` interface.
|
|
12
|
+
*/
|
|
13
|
+
import { resolvePolicy } from "./types.js";
|
|
14
|
+
// ============ In-memory store ============
|
|
15
|
+
/**
|
|
16
|
+
* Default in-memory history store. Keeps a ring buffer per group key.
|
|
17
|
+
*
|
|
18
|
+
* Suitable for single-process bots; for multi-instance deployments use
|
|
19
|
+
* a Redis / SQL backed implementation.
|
|
20
|
+
*/
|
|
21
|
+
export class MemoryHistoryStore {
|
|
22
|
+
buffers = new Map();
|
|
23
|
+
append(groupKey, entry, limit) {
|
|
24
|
+
let buf = this.buffers.get(groupKey);
|
|
25
|
+
if (!buf) {
|
|
26
|
+
buf = [];
|
|
27
|
+
this.buffers.set(groupKey, buf);
|
|
28
|
+
}
|
|
29
|
+
// De-dup by messageId.
|
|
30
|
+
if (buf.some((e) => e.messageId === entry.messageId)) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
buf.push(entry);
|
|
34
|
+
if (buf.length > limit) {
|
|
35
|
+
buf.splice(0, buf.length - limit);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
list(groupKey, limit) {
|
|
39
|
+
const buf = this.buffers.get(groupKey);
|
|
40
|
+
if (!buf)
|
|
41
|
+
return [];
|
|
42
|
+
return buf.slice(-limit);
|
|
43
|
+
}
|
|
44
|
+
clear(groupKey) {
|
|
45
|
+
this.buffers.delete(groupKey);
|
|
46
|
+
}
|
|
47
|
+
/** Diagnostic: number of groups with buffered history. */
|
|
48
|
+
size() {
|
|
49
|
+
return this.buffers.size;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
// ============ Middleware ============
|
|
53
|
+
/**
|
|
54
|
+
* Build a history-buffer middleware.
|
|
55
|
+
*
|
|
56
|
+
* The middleware:
|
|
57
|
+
* 1. Records the current group message into the store (before downstream).
|
|
58
|
+
* 2. Exposes `ctx.state.history: HistoryEntry[]` to downstream middleware
|
|
59
|
+
* and to the final `bot.on("message")` listener (oldest → newest,
|
|
60
|
+
* excluding the current message).
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* const history = historyBuffer({ limit: 5 });
|
|
65
|
+
* bot.use(mentionGate({ passthrough: true })); // record even on no-mention
|
|
66
|
+
* bot.use(history);
|
|
67
|
+
*
|
|
68
|
+
* bot.on("message", async (msg) => {
|
|
69
|
+
* const buf = msg.history ?? [];
|
|
70
|
+
* const prompt = buf.map(h => `${h.senderName}: ${h.content}`).join("\n");
|
|
71
|
+
* // ...
|
|
72
|
+
* });
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export function historyBuffer(options = {}) {
|
|
76
|
+
const limit = options.limit ?? 50;
|
|
77
|
+
const store = options.store ?? new MemoryHistoryStore();
|
|
78
|
+
const recordOnSkip = options.recordOnSkip ?? true;
|
|
79
|
+
const getKey = options.groupKey ??
|
|
80
|
+
((ctx) => (ctx.message.kind === "group" ? ctx.message.groupOpenid : undefined));
|
|
81
|
+
const m = async (ctx, next) => {
|
|
82
|
+
const key = getKey(ctx);
|
|
83
|
+
if (!key) {
|
|
84
|
+
await next();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
// Effective limit — priority: static option > ctx.state.policy > default 50
|
|
88
|
+
const effectiveLimit = resolvePolicy(ctx, "group.historyLimit", limit, 50);
|
|
89
|
+
// 1. List buffered (excluding current message)
|
|
90
|
+
const buffered = await store.list(key, effectiveLimit);
|
|
91
|
+
ctx.state.history = buffered;
|
|
92
|
+
// 2. Record the current message (after exposing previous buffer)
|
|
93
|
+
const entry = {
|
|
94
|
+
senderId: ctx.message.senderId,
|
|
95
|
+
senderName: ctx.message.senderName,
|
|
96
|
+
content: ctx.message.content,
|
|
97
|
+
timestamp: Date.parse(ctx.message.timestamp) || Date.now(),
|
|
98
|
+
messageId: ctx.message.messageId,
|
|
99
|
+
};
|
|
100
|
+
try {
|
|
101
|
+
await store.append(key, entry, effectiveLimit);
|
|
102
|
+
}
|
|
103
|
+
catch (err) {
|
|
104
|
+
ctx.log.error?.(`[history-buffer] append failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
105
|
+
}
|
|
106
|
+
if (ctx.stopped && !recordOnSkip) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
await next();
|
|
110
|
+
};
|
|
111
|
+
return m;
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=history-buffer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"history-buffer.js","sourceRoot":"","sources":["../../src/middleware/history-buffer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAwC3C,4CAA4C;AAE5C;;;;;GAKG;AACH,MAAM,OAAO,kBAAkB;IACZ,OAAO,GAAG,IAAI,GAAG,EAA0B,CAAC;IAE7D,MAAM,CAAC,QAAgB,EAAE,KAAmB,EAAE,KAAa;QACzD,IAAI,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACrC,IAAI,CAAC,GAAG,EAAE,CAAC;YACT,GAAG,GAAG,EAAE,CAAC;YACT,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;QAClC,CAAC;QACD,uBAAuB;QACvB,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS,KAAK,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC;YACrD,OAAO;QACT,CAAC;QACD,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAChB,IAAI,GAAG,CAAC,MAAM,GAAG,KAAK,EAAE,CAAC;YACvB,GAAG,CAAC,MAAM,CAAC,CAAC,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leware that cover the most common QQ Bot needs:
|
|
6
|
+
*
|
|
7
|
+
* **Core pipeline (dataflow stages):**
|
|
8
|
+
* - {@link selfEchoFilter} — drop bot's own echo messages
|
|
9
|
+
* - {@link deduplication} — drop duplicate messages (QQ retry overlap)
|
|
10
|
+
* - {@link contentSanitizer} — strip @markers, face tags, whitespace
|
|
11
|
+
* - {@link accessPolicy} — gate by allow / deny lists
|
|
12
|
+
* - {@link mentionGate} — only respond to @mentions in groups
|
|
13
|
+
* - {@link refIndexRecorder} — record messages for quote resolution
|
|
14
|
+
* - {@link quoteResolver} — resolve quoted/referenced messages
|
|
15
|
+
* - {@link slashCommand} — `/cmd` style command dispatch
|
|
16
|
+
* - {@link historyBuffer} — buffer recent group messages
|
|
17
|
+
* - {@link envelopeFormatter} — assemble LLM prompt context
|
|
18
|
+
* - {@link typingIndicator} — auto-show typing for C2C
|
|
19
|
+
* - {@link errorHandler} — catch errors and reply
|
|
20
|
+
*
|
|
21
|
+
* **Policy / protection:**
|
|
22
|
+
* - {@link rateLimiter} — multi-tier sliding-window rate limiting
|
|
23
|
+
*
|
|
24
|
+
* Compose them or replace them — `bot.use(...)` accepts any
|
|
25
|
+
* {@link Middleware} function.
|
|
26
|
+
*/
|
|
27
|
+
export type { Middleware, MiddlewareContext } from "./types.js";
|
|
28
|
+
export { runMiddlewareChain, createMiddlewareContext, resolvePolicy } from "./types.js";
|
|
29
|
+
export { messageFilter } from "./message-filter.js";
|
|
30
|
+
export type { MessageFilterOptions } from "./message-filter.js";
|
|
31
|
+
export { contentSanitizer } from "./content-sanitizer.js";
|
|
32
|
+
export type { ContentSanitizerOptions } from "./content-sanitizer.js";
|
|
33
|
+
export { rateLimiter } from "./rate-limiter.js";
|
|
34
|
+
export type { RateLimiterOptions, RateLimitTier } from "./rate-limiter.js";
|
|
35
|
+
export { concurrencyGuard } from "./concurrency-guard.js";
|
|
36
|
+
export type { ConcurrencyGuardOptions, ConcurrencyStrategy } from "./concurrency-guard.js";
|
|
37
|
+
export { accessPolicy } from "./access-policy.js";
|
|
38
|
+
export type { AccessMatcher, AccessPolicy, ScopePolicy, } from "./access-policy.js";
|
|
39
|
+
export { mentionGate } from "./mention-gate.js";
|
|
40
|
+
export type { MentionDecision, MentionGateOptions } from "./mention-gate.js";
|
|
41
|
+
export { quoteRef, MemoryRefIndexStore } from "./quote-ref.js";
|
|
42
|
+
export type { QuoteRefOptions, RefEntry, RefIndexStore, ResolvedQuote, QuotedAttachment, } from "./quote-ref.js";
|
|
43
|
+
export { envelopeFormatter } from "./envelope-formatter.js";
|
|
44
|
+
export type { EnvelopeFormatterOptions } from "./envelope-formatter.js";
|
|
45
|
+
export { slashCommand } from "./slash-command.js";
|
|
46
|
+
export type { ParsedCommand, SlashCommand, SlashCommandHandlerContext, SlashCommandOptions, SlashCommandResult, } from "./slash-command.js";
|
|
47
|
+
export { historyBuffer, MemoryHistoryStore, } from "./history-buffer.js";
|
|
48
|
+
export type { HistoryBufferOptions, HistoryEntry, HistoryStore, } from "./history-buffer.js";
|
|
49
|
+
export { typingIndicator } from "./typing-indicator.js";
|
|
50
|
+
export type { TypingIndicatorOptions } from "./typing-indicator.js";
|
|
51
|
+
export { errorHandler } from "./error-handler.js";
|
|
52
|
+
export type { ErrorHandlerOptions } from "./error-handler.js";
|
|
53
|
+
import type { MentionDecision } from "./mention-gate.js";
|
|
54
|
+
import type { ParsedCommand } from "./slash-command.js";
|
|
55
|
+
import type { HistoryEntry } from "./history-buffer.js";
|
|
56
|
+
import type { ResolvedQuote } from "./quote-ref.js";
|
|
57
|
+
declare module "./types.js" {
|
|
58
|
+
interface MiddlewareState {
|
|
59
|
+
/** Set by {@link mentionGate} middleware. */
|
|
60
|
+
mention?: MentionDecision;
|
|
61
|
+
/** Set by {@link slashCommand} middleware when a command matches. */
|
|
62
|
+
command?: ParsedCommand;
|
|
63
|
+
/** Set by {@link historyBuffer} middleware. */
|
|
64
|
+
history?: HistoryEntry[];
|
|
65
|
+
/** Set by {@link quoteResolver} middleware. */
|
|
66
|
+
quote?: ResolvedQuote;
|
|
67
|
+
/** Set by {@link envelopeFormatter} middleware. Formatted LLM context string. */
|
|
68
|
+
envelope?: string;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/middleware/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAGH,YAAY,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAChE,OAAO,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAIxF,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,YAAY,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAEhE,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,YAAY,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AAEtE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,YAAY,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAE3E,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,YAAY,EAAE,uBAAuB,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAI3F,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,YAAY,EACV,aAAa,EACb,YAAY,EACZ,WAAW,GACZ,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,YAAY,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAM7E,OAAO,EAAE,QAAQ,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAC/D,YAAY,EACV,e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C;KACnB;CACF"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Middleware module barrel.
|
|
3
|
+
*
|
|
4
|
+
* Provides a Koa-style middleware chain plus a curated set of built-in
|
|
5
|
+
* middleware that cover the most common QQ Bot needs:
|
|
6
|
+
*
|
|
7
|
+
* **Core pipeline (dataflow stages):**
|
|
8
|
+
* - {@link selfEchoFilter} — drop bot's own echo messages
|
|
9
|
+
* - {@link deduplication} — drop duplicate messages (QQ retry overlap)
|
|
10
|
+
* - {@link contentSanitizer} — strip @markers, face tags, whitespace
|
|
11
|
+
* - {@link accessPolicy} — gate by allow / deny lists
|
|
12
|
+
* - {@link mentionGate} — only respond to @mentions in groups
|
|
13
|
+
* - {@link refIndexRecorder} — record messages for quote resolution
|
|
14
|
+
* - {@link quoteResolver} — resolve quoted/referenced messages
|
|
15
|
+
* - {@link slashCommand} — `/cmd` style command dispatch
|
|
16
|
+
* - {@link historyBuffer} — buffer recent group messages
|
|
17
|
+
* - {@link envelopeFormatter} — assemble LLM prompt context
|
|
18
|
+
* - {@link typingIndicator} — auto-show typing for C2C
|
|
19
|
+
* - {@link errorHandler} — catch errors and reply
|
|
20
|
+
*
|
|
21
|
+
* **Policy / protection:**
|
|
22
|
+
* - {@link rateLimiter} — multi-tier sliding-window rate limiting
|
|
23
|
+
*
|
|
24
|
+
* Compose them or replace them — `bot.use(...)` accepts any
|
|
25
|
+
* {@link Middleware} function.
|
|
26
|
+
*/
|
|
27
|
+
export { runMiddlewareChain, createMiddlewareContext, resolvePolicy } from "./types.js";
|
|
28
|
+
// ── P0: Filters & protection ──
|
|
29
|
+
export { messageFilter } from "./message-filter.js";
|
|
30
|
+
export { contentSanitizer } from "./content-sanitizer.js";
|
|
31
|
+
export { rateLimiter } from "./rate-limiter.js";
|
|
32
|
+
export { concurrencyGuard } from "./concurrency-guard.js";
|
|
33
|
+
// ── Access & gating ──
|
|
34
|
+
export { accessPolicy } from "./access-policy.js";
|
|
35
|
+
export { mentionGate } from "./mention-gate.js";
|
|
36
|
+
// ── Protocol (dataflow stages) ──
|
|
37
|
+
// ── Quote & ref-index (combined) ──
|
|
38
|
+
export { quoteRef, MemoryRefIndexStore } from "./quote-ref.js";
|
|
39
|
+
export { envelopeFormatter } from "./envelope-formatter.js";
|
|
40
|
+
// ── Commands ──
|
|
41
|
+
export { slashCommand } from "./slash-command.js";
|
|
42
|
+
// ── History ──
|
|
43
|
+
export { historyBuffer, MemoryHistoryStore, } from "./history-buffer.js";
|
|
44
|
+
// ── UX ──
|
|
45
|
+
export { typingIndicator } from "./typing-indicator.js";
|
|
46
|
+
// ── Error handling ──
|
|
47
|
+
export { errorHandler } from "./error-handler.js";
|
|
48
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/middleware/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAIH,OAAO,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAExF,iCAAiC;AAEjC,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAGpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAG1D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGhD,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAG1D,wBAAwB;AAExB,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAOlD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGhD,mCAAmC;AAEnC,qCAAqC;AAErC,OAAO,EAAE,QAAQ,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAS/D,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAG5D,iBAAiB;AAEjB,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AASlD,gBAAgB;AAEhB,OAAO,EACL,aAAa,EACb,kBAAkB,GACnB,MAAM,qBAAqB,CAAC;AAO7B,WAAW;AAEX,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAGxD,uBAAuB;AAEvB,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mention-gate middleware — decide whether a group message should be answered.
|
|
3
|
+
*
|
|
4
|
+
* QQ Bot group messages may or may not contain an `@bot` mention. Many
|
|
5
|
+
* production bots want to **only respond to mentions** in group chats while
|
|
6
|
+
* still listening to all C2C messages. This middleware encapsulates that
|
|
7
|
+
* common decision.
|
|
8
|
+
*
|
|
9
|
+
* The decision is exposed in `ctx.state.mention` for downstream middleware
|
|
10
|
+
* to inspect (e.g. a history-buffer middleware may still record non-mention
|
|
11
|
+
* messages without triggering a reply).
|
|
12
|
+
*/
|
|
13
|
+
import type { Middleware, MiddlewareContext } from "./types.js";
|
|
14
|
+
export interface MentionDecision {
|
|
15
|
+
/** True if the bot was explicitly @-mentioned. */
|
|
16
|
+
wasMentioned: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* True if the message satisfies an *implicit* mention rule (e.g. it is a
|
|
19
|
+
* reply to a previous bot message).
|
|
20
|
+
*/
|
|
21
|
+
implicit: boolean;
|
|
22
|
+
/** Effective answer: whether the bot should respond. */
|
|
23
|
+
shouldAnswer: boolean;
|
|
24
|
+
/** When `shouldAnswer === false`, the gate's reason for skipping. */
|
|
25
|
+
reason: "no_mention" | "other_mention" | "passthrough";
|
|
26
|
+
}
|
|
27
|
+
export interface MentionGateOptions {
|
|
28
|
+
/**
|
|
29
|
+
* Require an `@bot` mention in group messages. Defaults to `true`.
|
|
30
|
+
* When `false`, every group message is treated as addressed to the bot.
|
|
31
|
+
*/
|
|
32
|
+
requireMentionInGroup?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Treat C2C messages as always addressed to the bot. Defaults to `true`.
|
|
35
|
+
*/
|
|
36
|
+
alwaysAnswerC2C?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Optional implicit-mention probe — return true to bypass the
|
|
39
|
+
* `requireMentionInGroup` check (e.g. when the message is a reply to a
|
|
40
|
+
* prior bot message). This is a per-message signal, orthogonal to
|
|
41
|
+
* `resolveConfig` which is a per-group policy.
|
|
42
|
+
*/
|
|
43
|
+
isImplicitMention?: (ctx: MiddlewareContext) => boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Hook invoked when the gate skips a message. Use for telemetry or to
|
|
46
|
+
* record the message into a history buffer despite skipping.
|
|
47
|
+
*/
|
|
48
|
+
onSkip?: (ctx: MiddlewareContext, decision: MentionDecision) => void;
|
|
49
|
+
/**
|
|
50
|
+
* If `true`, skip group messages that mention other users/bots but do
|
|
51
|
+
* **not** mention this bot. This prevents the bot from responding to
|
|
52
|
+
* conversations directed at other participants.
|
|
53
|
+
*
|
|
54
|
+
* Defaults to `false`.
|
|
55
|
+
*/
|
|
56
|
+
ignoreOtherMentions?: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* If `true`, do **not** stop the middleware chain when the message should
|
|
59
|
+
* not be answered — only annotate `ctx.state.mention`. Useful when other
|
|
60
|
+
* middleware (e.g. history buffering) still needs to run.
|
|
61
|
+
*
|
|
62
|
+
* Defaults to `false` (stop the chain on skip).
|
|
63
|
+
*/
|
|
64
|
+
passthrough?: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Dynamic config resolver — called for each message before the gate logic
|
|
67
|
+
* runs. The returned values override the static defaults
|
|
68
|
+
* (`requireMentionInGroup`, `ignoreOtherMentions`).
|
|
69
|
+
*
|
|
70
|
+
* Use this when different groups / contexts need different mention policies
|
|
71
|
+
* without registering separate `mentionGate` instances.
|
|
72
|
+
*/
|
|
73
|
+
resolveConfig?: (ctx: MiddlewareContext) => {
|
|
74
|
+
requireMentionInGroup?: boolean;
|
|
75
|
+
ignoreOtherMentions?: boolean;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Build a mention-gate middleware.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* // Static config
|
|
84
|
+
* bot.use(mentionGate({ requireMentionInGroup: true }));
|
|
85
|
+
*
|
|
86
|
+
* // Dynamic per-group config
|
|
87
|
+
* bot.use(mentionGate({
|
|
88
|
+
* resolveConfig: (ctx) => {
|
|
89
|
+
* const cfg = resolveGroupConfig(ctx.message.groupId);
|
|
90
|
+
* return {
|
|
91
|
+
* requireMentionInGroup: cfg.requireMention,
|
|
92
|
+
* ignoreOtherMentions: cfg.ignoreOtherMentions,
|
|
93
|
+
* };
|
|
94
|
+
* },
|
|
95
|
+
* }));
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export declare function mentionGate(options?: MentionGateOptions): Middleware;
|
|
99
|
+
//# sourceMappingURL=mention-gate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mention-gate.d.ts","sourceRoot":"","sources":["../../src/middleware/mention-gate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAKhE,MAAM,WAAW,eAAe;IAC9B,kDAAkD;IAClD,YAAY,EAAE,OAAO,CAAC;IACtB;;;OAGG;IACH,QAAQ,EAAE,OAAO,CAAC;IAClB,wDAAwD;IACxD,YAAY,EAAE,OAAO,CAAC;IACtB,qEAAqE;IACrE,MAAM,EAAE,YAAY,GAAG,eAAe,GAAG,aAAa,CAAC;CACxD;AAED,MAAM,WAAW,kBAAkB;IACjC;;;OAGG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAEhC;;OAEG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAE1B;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,CAAC,GAAG,EAAE,iBAAiB,KAAK,OAAO,CAAC;IAExD;;;OAGG;IACH,MAAM,CAAC,EAAE,CAAC,GAAG,EAAE,iBAAiB,EAAE,QAAQ,EAAE,eAAe,KAAK,IAAI,CAAC;IAErE;;;;;;OAMG;IACH,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAE9B;;;;;;OAMG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IAEtB;;;;;;;OAOG;IACH,aAAa,CAAC,EAAE,CAAC,GAAG,EAAE,iBAAiB,KAAK;QAC1C,qBAAqB,CAAC,EAAE,OAAO,CAAC;QAChC,mBAAmB,CAAC,EAAE,OAAO,CAAC;KAC/B,CAAC;CACH;AAaD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,WAAW,CAAC,OAAO,GAAE,kBAAuB,GAAG,UAAU,CAyGxE"}
|