@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,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slash-command middleware — register `/cmd` style commands.
|
|
3
|
+
*
|
|
4
|
+
* Pattern: any message whose content starts with a configured prefix
|
|
5
|
+
* (default `/`) is intercepted; the matched command's `handler` runs, and
|
|
6
|
+
* the chain is short-circuited so the `bot.on("message")` listener does
|
|
7
|
+
* not fire.
|
|
8
|
+
*
|
|
9
|
+
* Commands are **explicitly registered** on the middleware instance so the
|
|
10
|
+
* SDK does not enforce any specific business commands — users define what
|
|
11
|
+
* makes sense for their bot.
|
|
12
|
+
*/
|
|
13
|
+
import type { Middleware, MiddlewareContext } from "./types.js";
|
|
14
|
+
/** Parsed command struct exposed via `ctx.state.command`. */
|
|
15
|
+
export interface ParsedCommand {
|
|
16
|
+
/** The command name (without leading prefix). */
|
|
17
|
+
name: string;
|
|
18
|
+
/** Whitespace-separated arguments after the command name. */
|
|
19
|
+
args: string[];
|
|
20
|
+
/** Raw arguments string (everything after the command name). */
|
|
21
|
+
raw: string;
|
|
22
|
+
}
|
|
23
|
+
/** Result returned by a slash-command handler. */
|
|
24
|
+
export type SlashCommandResult = string | {
|
|
25
|
+
kind: "text";
|
|
26
|
+
content: string;
|
|
27
|
+
} | {
|
|
28
|
+
kind: "noop";
|
|
29
|
+
} | void;
|
|
30
|
+
export interface SlashCommandHandlerContext extends MiddlewareContext {
|
|
31
|
+
/** Parsed command struct. */
|
|
32
|
+
command: ParsedCommand;
|
|
33
|
+
}
|
|
34
|
+
/** Spec for one registered command. */
|
|
35
|
+
export interface SlashCommand {
|
|
36
|
+
/** Command name(s). The first is canonical; the rest are aliases. */
|
|
37
|
+
name: string | string[];
|
|
38
|
+
/** Brief description (used by `/help`). */
|
|
39
|
+
description?: string;
|
|
40
|
+
/** Optional usage hint (used by `/help`). */
|
|
41
|
+
usage?: string;
|
|
42
|
+
/** Whether this command is hidden from `/help`. Defaults to false. */
|
|
43
|
+
hidden?: boolean;
|
|
44
|
+
/** Scope restriction: "c2c" = only private chat, "group" = only group, "all" = both (default). */
|
|
45
|
+
scope?: "c2c" | "group" | "all";
|
|
46
|
+
/**
|
|
47
|
+
* Handler. Return a string to send a text reply, or `{ kind: "noop" }`
|
|
48
|
+
* to silently consume.
|
|
49
|
+
*/
|
|
50
|
+
handler: (ctx: SlashCommandHandlerContext) => Promise<SlashCommandResult> | SlashCommandResult;
|
|
51
|
+
/**
|
|
52
|
+
* Optional authorization check executed before the handler.
|
|
53
|
+
* - Return `true` to allow execution.
|
|
54
|
+
* - Return `false` or a string to block; the string is sent as an error
|
|
55
|
+
* reply (e.g. `"⚠️ 无权限"`). Default blocked message: `"⚠️ 无权限执行此命令"`.
|
|
56
|
+
*/
|
|
57
|
+
authorized?: (ctx: SlashCommandHandlerContext) => boolean | string;
|
|
58
|
+
}
|
|
59
|
+
export interface SlashCommandOptions {
|
|
60
|
+
/** Command prefix(es). Defaults to `["/"]`. */
|
|
61
|
+
prefixes?: string[];
|
|
62
|
+
/** Pre-registered commands. */
|
|
63
|
+
commands?: SlashCommand[];
|
|
64
|
+
/**
|
|
65
|
+
* If `true`, when a command throws, the error is caught and
|
|
66
|
+
* `{ kind: "text", content: errorMessage }` is sent as a reply.
|
|
67
|
+
* Defaults to `true`.
|
|
68
|
+
*/
|
|
69
|
+
catchErrors?: boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Auto-register a `/help` command listing all visible commands.
|
|
72
|
+
* Defaults to `true`.
|
|
73
|
+
*/
|
|
74
|
+
autoHelp?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Global allowlist of user OpenIDs permitted to use slash commands.
|
|
77
|
+
* If empty or not set, all users can use commands.
|
|
78
|
+
* Non-allowlisted users' command-like messages pass through to `next()`
|
|
79
|
+
* as regular messages.
|
|
80
|
+
*/
|
|
81
|
+
allowFrom?: string[];
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Slash-command middleware factory.
|
|
85
|
+
*
|
|
86
|
+
* Returns a middleware **and** a registry — the registry lets you keep
|
|
87
|
+
* adding commands after the middleware is registered with the bot.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* const slash = slashCommand({ prefixes: ["/", "!"] });
|
|
92
|
+
* slash.register({
|
|
93
|
+
* name: "ping",
|
|
94
|
+
* description: "Reply with pong",
|
|
95
|
+
* handler: () => "pong",
|
|
96
|
+
* });
|
|
97
|
+
*
|
|
98
|
+
* slash.register({
|
|
99
|
+
* name: ["whoami", "me"],
|
|
100
|
+
* handler: (ctx) => `You are ${ctx.message.senderId}`,
|
|
101
|
+
* });
|
|
102
|
+
*
|
|
103
|
+
* bot.use(slash.middleware);
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export declare function slashCommand(options?: SlashCommandOptions): {
|
|
107
|
+
middleware: Middleware;
|
|
108
|
+
register: (cmd: SlashCommand) => void;
|
|
109
|
+
unregister: (name: string) => void;
|
|
110
|
+
list: () => SlashCommand[];
|
|
111
|
+
};
|
|
112
|
+
//# sourceMappingURL=slash-command.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"slash-command.d.ts","sourceRoot":"","sources":["../../src/middleware/slash-command.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,KAAK,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAIhE,6DA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,CAAC;IACnB;;;;;OAKG;IACH,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AAID;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAE,mBAAwB,GAAG;IAC/D,UAAU,EAAE,UAAU,CAAC;IACvB,QAAQ,EAAE,CAAC,GAAG,EAAE,YAAY,KAAK,IAAI,CAAC;IACtC,UAAU,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IACnC,IAAI,EAAE,MAAM,YAAY,EAAE,CAAC;CAC5B,CAwKA"}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slash-command middleware — register `/cmd` style commands.
|
|
3
|
+
*
|
|
4
|
+
* Pattern: any message whose content starts with a configured prefix
|
|
5
|
+
* (default `/`) is intercepted; the matched command's `handler` runs, and
|
|
6
|
+
* the chain is short-circuited so the `bot.on("message")` listener does
|
|
7
|
+
* not fire.
|
|
8
|
+
*
|
|
9
|
+
* Commands are **explicitly registered** on the middleware instance so the
|
|
10
|
+
* SDK does not enforce any specific business commands — users define what
|
|
11
|
+
* makes sense for their bot.
|
|
12
|
+
*/
|
|
13
|
+
// ============ Implementation ============
|
|
14
|
+
/**
|
|
15
|
+
* Slash-command middleware factory.
|
|
16
|
+
*
|
|
17
|
+
* Returns a middleware **and** a registry — the registry lets you keep
|
|
18
|
+
* adding commands after the middleware is registered with the bot.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const slash = slashCommand({ prefixes: ["/", "!"] });
|
|
23
|
+
* slash.register({
|
|
24
|
+
* name: "ping",
|
|
25
|
+
* description: "Reply with pong",
|
|
26
|
+
* handler: () => "pong",
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* slash.register({
|
|
30
|
+
* name: ["whoami", "me"],
|
|
31
|
+
* handler: (ctx) => `You are ${ctx.message.senderId}`,
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* bot.use(slash.middleware);
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export function slashCommand(options = {}) {
|
|
38
|
+
const prefixes = options.prefixes ?? ["/"];
|
|
39
|
+
const catchErrors = options.catchErrors ?? true;
|
|
40
|
+
const autoHelp = options.autoHelp ?? true;
|
|
41
|
+
const allowFrom = options.allowFrom ?? [];
|
|
42
|
+
const registry = new Map();
|
|
43
|
+
const register = (cmd) => {
|
|
44
|
+
const names = Array.isArray(cmd.name) ? cmd.name : [cmd.name];
|
|
45
|
+
if (names.length === 0) {
|
|
46
|
+
throw new Error("slash-command: name must not be empty");
|
|
47
|
+
}
|
|
48
|
+
for (const n of names) {
|
|
49
|
+
const key = n.toLowerCase();
|
|
50
|
+
if (registry.has(key)) {
|
|
51
|
+
throw new Error(`slash-command: duplicate name "${n}"`);
|
|
52
|
+
}
|
|
53
|
+
registry.set(key, cmd);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
const unregister = (name) => {
|
|
57
|
+
registry.delete(name.toLowerCase());
|
|
58
|
+
};
|
|
59
|
+
const list = () => {
|
|
60
|
+
const seen = new Set();
|
|
61
|
+
const out = [];
|
|
62
|
+
for (const cmd of registry.values()) {
|
|
63
|
+
if (!seen.has(cmd)) {
|
|
64
|
+
seen.add(cmd);
|
|
65
|
+
out.push(cmd);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
};
|
|
70
|
+
// Pre-register commands
|
|
71
|
+
for (const cmd of options.commands ?? []) {
|
|
72
|
+
register(cmd);
|
|
73
|
+
}
|
|
74
|
+
// Auto /help
|
|
75
|
+
if (autoHelp && !registry.has("help")) {
|
|
76
|
+
register({
|
|
77
|
+
name: "help",
|
|
78
|
+
description: "List all available commands",
|
|
79
|
+
handler: () => {
|
|
80
|
+
const lines = ["Available commands:"];
|
|
81
|
+
for (const cmd of list()) {
|
|
82
|
+
if (cmd.hidden)
|
|
83
|
+
continue;
|
|
84
|
+
const names = Array.isArray(cmd.name) ? cmd.name.join(", ") : cmd.name;
|
|
85
|
+
const desc = cmd.description ?? "";
|
|
86
|
+
const usage = cmd.usage ? ` — ${cmd.usage}` : "";
|
|
87
|
+
lines.push(`/${names}${usage}${desc ? ` — ${desc}` : ""}`);
|
|
88
|
+
}
|
|
89
|
+
return lines.join("\n");
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
const middleware = async (ctx, next) => {
|
|
94
|
+
const content = (ctx.message.content ?? "").trim();
|
|
95
|
+
if (!content) {
|
|
96
|
+
await next();
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
// Strip server-side `<@bot>` mention markers from content for matching.
|
|
100
|
+
const cleaned = content.replace(/<@!?[^>]+>\s*/g, "").trim();
|
|
101
|
+
// 群聊场景:必须 @bot 才触发命令
|
|
102
|
+
if (ctx.message.kind === 'group') {
|
|
103
|
+
const msg = ctx.message;
|
|
104
|
+
const wasMentioned = msg.rawEventType === 'GROUP_AT_MESSAGE_CREATE' ||
|
|
105
|
+
msg.mentions?.some((m) => m.is_you);
|
|
106
|
+
if (!wasMentioned) {
|
|
107
|
+
await next();
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// Find matching prefix
|
|
112
|
+
const prefix = prefixes.find((p) => cleaned.startsWith(p));
|
|
113
|
+
if (!prefix) {
|
|
114
|
+
await next();
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const body = cleaned.slice(prefix.length);
|
|
118
|
+
const match = /^(\S+)(?:\s+(.*))?$/.exec(body);
|
|
119
|
+
if (!match) {
|
|
120
|
+
await next();
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const [, name, rest = ""] = match;
|
|
124
|
+
const cmd = registry.get(name.toLowerCase());
|
|
125
|
+
if (!cmd) {
|
|
126
|
+
await next();
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
// AllowFrom check: non-allowlisted users bypass commands as regular messages
|
|
130
|
+
const isAllowed = allowFrom.length === 0
|
|
131
|
+
|| allowFrom.includes('*')
|
|
132
|
+
|| allowFrom.includes(ctx.message.senderId);
|
|
133
|
+
if (!isAllowed) {
|
|
134
|
+
await next();
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const parsed = {
|
|
138
|
+
name: name.toLowerCase(),
|
|
139
|
+
args: rest ? rest.split(/\s+/) : [],
|
|
140
|
+
raw: rest,
|
|
141
|
+
};
|
|
142
|
+
ctx.state.command = parsed;
|
|
143
|
+
// Scope check: reject if command is restricted to a different scope
|
|
144
|
+
const cmdScope = cmd.scope ?? "all";
|
|
145
|
+
if (cmdScope !== "all") {
|
|
146
|
+
const msgKind = ctx.message.kind;
|
|
147
|
+
const isC2C = msgKind === "c2c" || msgKind === "dm";
|
|
148
|
+
const isGroup = msgKind === "group";
|
|
149
|
+
if ((cmdScope === "c2c" && !isC2C) || (cmdScope === "group" && !isGroup)) {
|
|
150
|
+
const hint = cmdScope === "c2c" ? "该指令仅限私聊使用" : "该指令仅限群聊使用";
|
|
151
|
+
await sendCommandResult(ctx, hint);
|
|
152
|
+
ctx.stop(`command:scope-denied:${parsed.name}`);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const handlerCtx = ctx;
|
|
157
|
+
handlerCtx.command = parsed;
|
|
158
|
+
// Per-command authorization check
|
|
159
|
+
if (cmd.authorized) {
|
|
160
|
+
const auth = cmd.authorized(handlerCtx);
|
|
161
|
+
if (auth !== true) {
|
|
162
|
+
const authMsg = typeof auth === 'string' ? auth : '⚠️ 无权限执行此命令';
|
|
163
|
+
await sendCommandResult(ctx, { kind: 'text', content: authMsg });
|
|
164
|
+
ctx.stop(`command:unauthorized:${parsed.name}`);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
try {
|
|
169
|
+
const result = await cmd.handler(handlerCtx);
|
|
170
|
+
await sendCommandResult(ctx, result);
|
|
171
|
+
}
|
|
172
|
+
catch (err) {
|
|
173
|
+
if (catchErrors) {
|
|
174
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
175
|
+
ctx.log.error?.(`[slash-command] handler "${parsed.name}" threw: ${msg}`);
|
|
176
|
+
await sendCommandResult(ctx, { kind: "text", content: `Error: ${msg}` });
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
ctx.stop(`command:error:${parsed.name}`);
|
|
180
|
+
throw err;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
// Commands always short-circuit the chain.
|
|
184
|
+
ctx.stop(`command:matched:${parsed.name}`);
|
|
185
|
+
};
|
|
186
|
+
return { middleware, register, unregister, list };
|
|
187
|
+
}
|
|
188
|
+
async function sendCommandResult(ctx, result) {
|
|
189
|
+
if (result === undefined || result === null)
|
|
190
|
+
return;
|
|
191
|
+
if (typeof result === "string") {
|
|
192
|
+
if (result)
|
|
193
|
+
await ctx.bot.sendText(ctx.replyTarget, result);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (typeof result === "object") {
|
|
197
|
+
if (result.kind === "text" && result.content) {
|
|
198
|
+
await ctx.bot.sendText(ctx.replyTarget, result.content);
|
|
199
|
+
}
|
|
200
|
+
// kind === "noop" → no-op
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
//# sourceMappingURL=slash-command.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"slash-command.js","sourceRoot":"","sources":["../../src/middleware/slash-command.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAgFH,2CAA2C;AAE3C;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,YAAY,CAAC,UAA+B,EAAE;IAM5D,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,CAAC,GAAG,CAAC,CAAC;IAC3C,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,IAAI,CAAC;I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leware: each middleware receives a context
|
|
5
|
+
* object and a `next()` function, and may run arbitrary logic before / after
|
|
6
|
+
* downstream middleware. Calling `ctx.stop()` (or simply not calling `next()`)
|
|
7
|
+
* short-circuits the chain.
|
|
8
|
+
*
|
|
9
|
+
* The SDK ships several **built-in middleware** in `./middleware/*` that cover
|
|
10
|
+
* common needs (access policy, mention gating, command dispatch). Users can
|
|
11
|
+
* compose them, replace them, or add their own.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { QQBot, accessPolicy, mentionGate } from "@tencent-connect/qqbot-nodejs";
|
|
16
|
+
*
|
|
17
|
+
* const bot = new QQBot({ appId, appSecret });
|
|
18
|
+
*
|
|
19
|
+
* bot.use(accessPolicy({ allowFrom: ["*"] }));
|
|
20
|
+
* bot.use(mentionGate({ requireMentionInGroup: true }));
|
|
21
|
+
* bot.use(async (ctx, next) => {
|
|
22
|
+
* // Custom logic before
|
|
23
|
+
* await next();
|
|
24
|
+
* // Custom logic after
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* bot.on("message", async (msg) => {
|
|
28
|
+
* await bot.sendText(msg.replyTarget, `Echo: ${msg.content}`);
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
import type { QQBot, QQBotInboundMessage, ReplyTarget } from "../QQBot.js";
|
|
33
|
+
import type { Logger } from "../protocol/types.js";
|
|
34
|
+
/**
|
|
35
|
+
* Free-form per-request state slot, augmentable via TS module augmentation.
|
|
36
|
+
* Built-in middleware fills well-known keys (see `middleware/index.ts`).
|
|
37
|
+
*/
|
|
38
|
+
export interface MiddlewareState {
|
|
39
|
+
[key: string]: unknown;
|
|
40
|
+
/**
|
|
41
|
+
* Dynamic policy injection — set by an upstream "policy injector" middleware.
|
|
42
|
+
*
|
|
43
|
+
* Built-in middlewares read well-known keys from here as a fallback:
|
|
44
|
+
*
|
|
45
|
+
* **Group policy** (`state.policy.group`):
|
|
46
|
+
* - `requireMention` — `mentionGate` fallback
|
|
47
|
+
* - `ignoreOtherMentions` — `mentionGate` fallback
|
|
48
|
+
* - `historyLimit` — `historyBuffer` fallback
|
|
49
|
+
*
|
|
50
|
+
* **Session scope** (`state.policy.scope`): `"c2c" | "group" | "dm" | "channel"`
|
|
51
|
+
*
|
|
52
|
+
* **Extensibility** — users can add any keys for custom middlewares:
|
|
53
|
+
* ```
|
|
54
|
+
* ctx.state.policy.customToolPolicy = "full";
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
policy?: {
|
|
58
|
+
group?: {
|
|
59
|
+
requireMention?: boolean;
|
|
60
|
+
ignoreOtherMentions?: boolean;
|
|
61
|
+
historyLimit?: number;
|
|
62
|
+
[key: string]: unknown;
|
|
63
|
+
};
|
|
64
|
+
scope?: "c2c" | "group" | "dm" | "channel";
|
|
65
|
+
[key: string]: unknown;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Context passed to each middleware.
|
|
70
|
+
*
|
|
71
|
+
* Mutating `ctx.message` or `ctx.replyTarget` propagates to downstream
|
|
72
|
+
* middleware and to the final `bot.on("message")` listener.
|
|
73
|
+
*/
|
|
74
|
+
export interface MiddlewareContext {
|
|
75
|
+
/** The QQBot instance — middlewares can call `bot.sendText` etc. */
|
|
76
|
+
readonly bot: QQBot;
|
|
77
|
+
/** The inbound message being processed. Mutable — middlewares can rewrite. */
|
|
78
|
+
message: QQBotInboundMessage;
|
|
79
|
+
/** The derived reply target. Same object reference as `message.replyTarget`. */
|
|
80
|
+
readonly replyTarget: ReplyTarget;
|
|
81
|
+
/**
|
|
82
|
+
* Free-form per-request state slot. Use this to share data between
|
|
83
|
+
* middlewares (e.g. a parsed command struct, an authentication result).
|
|
84
|
+
*
|
|
85
|
+
* Built-in middleware writes well-known keys here:
|
|
86
|
+
* - `command`: parsed slash-command (set by `slashCommand` middleware)
|
|
87
|
+
* - `mention`: mention-gate decision (set by `mentionGate` middleware)
|
|
88
|
+
* - `history`: buffered history entries (set by `historyBuffer` middleware)
|
|
89
|
+
*
|
|
90
|
+
* Users may also add arbitrary keys.
|
|
91
|
+
*/
|
|
92
|
+
state: MiddlewareState;
|
|
93
|
+
/** Logger. */
|
|
94
|
+
readonly log: Logger;
|
|
95
|
+
/**
|
|
96
|
+
* Stop the chain immediately. Equivalent to not calling `next()` and
|
|
97
|
+
* preventing any downstream middleware (or the `message` event listener)
|
|
98
|
+
* from running.
|
|
99
|
+
*/
|
|
100
|
+
stop(reason?: string): void;
|
|
101
|
+
/** Whether `stop()` has been called (or some upstream short-circuited). */
|
|
102
|
+
readonly stopped: boolean;
|
|
103
|
+
/** Reason passed to `stop()`, if any. */
|
|
104
|
+
readonly stopReason: string | undefined;
|
|
105
|
+
/**
|
|
106
|
+
* AbortSignal for this request. Aborted when `ctx.abort()` is called.
|
|
107
|
+
*
|
|
108
|
+
* Use this to cancel in-flight async work (e.g. streaming, LLM calls)
|
|
109
|
+
* when a middleware (like `concurrencyGuard`) decides to interrupt
|
|
110
|
+
* the current processing.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* bot.on("message", async (ctx, msg) => {
|
|
115
|
+
* for await (const chunk of llmStream) {
|
|
116
|
+
* if (ctx.signal.aborted) break;
|
|
117
|
+
* await stream.update(chunk);
|
|
118
|
+
* }
|
|
119
|
+
* });
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
readonly signal: AbortSignal;
|
|
123
|
+
/**
|
|
124
|
+
* Abort the current request processing. Triggers `signal` and calls
|
|
125
|
+
* `stop()`. Use from middleware to interrupt an in-flight handler.
|
|
126
|
+
*/
|
|
127
|
+
abort(reason?: string): void;
|
|
128
|
+
/** Whether `abort()` has been called. */
|
|
129
|
+
readonly aborted: boolean;
|
|
130
|
+
/**
|
|
131
|
+
* Timestamp (`Date.now()`) when the SDK created this context.
|
|
132
|
+
* Use to measure end-to-end latency from event arrival to handler completion.
|
|
133
|
+
*/
|
|
134
|
+
readonly receivedAt: number;
|
|
135
|
+
}
|
|
136
|
+
/** Middleware function — `next` advances the chain. */
|
|
137
|
+
export type Middleware = (ctx: MiddlewareContext, next: () => Promise<void>) => void | Promise<void>;
|
|
138
|
+
/**
|
|
139
|
+
* Resolve an option with the standard 3-tier priority chain:
|
|
140
|
+
*
|
|
141
|
+
* 1. `explicit` — the value passed directly to the middleware Options
|
|
142
|
+
* 2. `ctx.state.policy.<path>` — injected by an upstream policy injector
|
|
143
|
+
* 3. `defaultValue` — hardcoded fallback
|
|
144
|
+
*
|
|
145
|
+
* `path` uses dot notation to navigate nested policy keys, e.g.
|
|
146
|
+
* `"group.requireMention"`, `"group.historyLimit"`, `"myFeature.threshold"`.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```ts
|
|
150
|
+
* const limit = resolvePolicy(ctx, "group.historyLimit", options.limit, 10);
|
|
151
|
+
* const reqMention = resolvePolicy(ctx, "group.requireMention", options.requireMentionInGroup, true);
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
export declare function resolvePolicy<T>(ctx: MiddlewareContext, path: string, explicit: T | undefined, defaultValue: T): T;
|
|
155
|
+
/**
|
|
156
|
+
* Run a middleware chain.
|
|
157
|
+
*
|
|
158
|
+
* Returns `true` if the chain ran to completion (no `stop()`); `false` if
|
|
159
|
+
* any middleware short-circuited.
|
|
160
|
+
*
|
|
161
|
+
* Errors thrown by middleware propagate up — the caller decides whether to
|
|
162
|
+
* surface them via `bot.emit("error")` or swallow them.
|
|
163
|
+
*/
|
|
164
|
+
export declare function runMiddlewareChain(middlewares: Middleware[], ctx: MiddlewareContext): Promise<boolean>;
|
|
165
|
+
/**
|
|
166
|
+
* Build a fresh middleware context for one inbound message.
|
|
167
|
+
*
|
|
168
|
+
* Internal helper — used by {@link QQBot} when dispatching inbound events.
|
|
169
|
+
*/
|
|
170
|
+
export declare function createMiddlewareContext(params: {
|
|
171
|
+
bot: QQBot;
|
|
172
|
+
message: QQBotInboundMessage;
|
|
173
|
+
log: Logger;
|
|
174
|
+
}): MiddlewareContext;
|
|
175
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/middleware/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,mBAAmB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC3E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAInD;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;IAEvB;;;;;;;;;;;;;;;;OAgBG;IACH,MAAM,CAAC,EAAE;QACP,KAAK,CAAC,EAAE;YACN,cAAc,CAAC,EAAE,OAAO,CAAC;YACzB,mBAAmB,CAAC,EAAE,OAAO,CAAC;YAC9B,YAAY,CAAC,EAAE,MAAM,CAAC;YACtB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;SACxB,CAAC;QACF,KAAK,CAAC,EAAE,KAAK,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC;QAC3C,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;KACxB,CAAC;CACH;AAED;;;;;GAKG;AACH,MAAM,WAAW,iBAAiB;IAChC,oEAAoE;IACpE,QAAQ,CAAC,GAAG,EAAE,KAAK,CAAC;IACpB,8EAA8E;IAC9E,OAAO,EAAE,mBAAmB,CAAC;IAC7B,gFAAgF;IAChF,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC;IAClC;;;;;;;;;;OAUG;IACH,KAAK,EAAE,eAAe,CAAC;IACvB,cAAc;IACd,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB;;;;OAIG;IACH,IAAI,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,2EAA2E;IAC3E,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,yCAAyC;IACzC,QAAQ,CAAC,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;IACxC;;;;;;;;;;;;;;;;OAgBG;IACH,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B;;;OAGG;IACH,KAAK,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,yCAAyC;IACzC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B;;;OAGG;IACH,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B;AAED,uDAAuD;AACvD,MAAM,MAAM,UAAU,GAAG,CACvB,GAAG,EAAE,iBAAiB,EACtB,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,KACtB,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAI1B;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAC7B,GAAG,EAAE,iBAAiB,EACtB,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,CAAC,GAAG,SAAS,EACvB,YAAY,EAAE,CAAC,GACd,CAAC,CAcH;AAID;;;;;;;;GAQG;AACH,wBAAsB,kBAAkB,CACtC,WAAW,EAAE,UAAU,EAAE,EACzB,GAAG,EAAE,iBAAiB,GACrB,OAAO,CAAC,OAAO,CAAC,CAuBlB;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,EAAE;IAC9C,GAAG,EAAE,KAAK,CAAC;IACX,OAAO,EAAE,mBAAmB,CAAC;IAC7B,GAAG,EAAE,MAAM,CAAC;CACb,GAAG,iBAAiB,CAqCpB"}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Middleware chain — pluggable inbound message processing pipeline.
|
|
3
|
+
*
|
|
4
|
+
* Inspired by Koa / Express middleware: each middleware receives a context
|
|
5
|
+
* object and a `next()` function, and may run arbitrary logic before / after
|
|
6
|
+
* downstream middleware. Calling `ctx.stop()` (or simply not calling `next()`)
|
|
7
|
+
* short-circuits the chain.
|
|
8
|
+
*
|
|
9
|
+
* The SDK ships several **built-in middleware** in `./middleware/*` that cover
|
|
10
|
+
* common needs (access policy, mention gating, command dispatch). Users can
|
|
11
|
+
* compose them, replace them, or add their own.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { QQBot, accessPolicy, mentionGate } from "@tencent-connect/qqbot-nodejs";
|
|
16
|
+
*
|
|
17
|
+
* const bot = new QQBot({ appId, appSecret });
|
|
18
|
+
*
|
|
19
|
+
* bot.use(accessPolicy({ allowFrom: ["*"] }));
|
|
20
|
+
* bot.use(mentionGate({ requireMentionInGroup: true }));
|
|
21
|
+
* bot.use(async (ctx, next) => {
|
|
22
|
+
* // Custom logic before
|
|
23
|
+
* await next();
|
|
24
|
+
* // Custom logic after
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* bot.on("message", async (msg) => {
|
|
28
|
+
* await bot.sendText(msg.replyTarget, `Echo: ${msg.content}`);
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
// ============ Policy Resolution Helper ============
|
|
33
|
+
/**
|
|
34
|
+
* Resolve an option with the standard 3-tier priority chain:
|
|
35
|
+
*
|
|
36
|
+
* 1. `explicit` — the value passed directly to the middleware Options
|
|
37
|
+
* 2. `ctx.state.policy.<path>` — injected by an upstream policy injector
|
|
38
|
+
* 3. `defaultValue` — hardcoded fallback
|
|
39
|
+
*
|
|
40
|
+
* `path` uses dot notation to navigate nested policy keys, e.g.
|
|
41
|
+
* `"group.requireMention"`, `"group.historyLimit"`, `"myFeature.threshold"`.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* const limit = resolvePolicy(ctx, "group.historyLimit", options.limit, 10);
|
|
46
|
+
* const reqMention = resolvePolicy(ctx, "group.requireMention", options.requireMentionInGroup, true);
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export function resolvePolicy(ctx, path, explicit, defaultValue) {
|
|
50
|
+
if (explicit !== undefined && explicit !== null) {
|
|
51
|
+
return explicit;
|
|
52
|
+
}
|
|
53
|
+
const keys = path.split(".");
|
|
54
|
+
let value = ctx.state.policy;
|
|
55
|
+
for (const key of keys) {
|
|
56
|
+
if (value === null || value === undefined)
|
|
57
|
+
break;
|
|
58
|
+
value = value[key];
|
|
59
|
+
}
|
|
60
|
+
if (value !== undefined && value !== null) {
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
return defaultValue;
|
|
64
|
+
}
|
|
65
|
+
// ============ Implementation ============
|
|
66
|
+
/**
|
|
67
|
+
* Run a middleware chain.
|
|
68
|
+
*
|
|
69
|
+
* Returns `true` if the chain ran to completion (no `stop()`); `false` if
|
|
70
|
+
* any middleware short-circuited.
|
|
71
|
+
*
|
|
72
|
+
* Errors thrown by middleware propagate up — the caller decides whether to
|
|
73
|
+
* surface them via `bot.emit("error")` or swallow them.
|
|
74
|
+
*/
|
|
75
|
+
export async function runMiddlewareChain(middlewares, ctx) {
|
|
76
|
+
let index = -1;
|
|
77
|
+
const dispatch = async (i) => {
|
|
78
|
+
if (i <= index) {
|
|
79
|
+
throw new Error("next() called multiple times");
|
|
80
|
+
}
|
|
81
|
+
index = i;
|
|
82
|
+
if (ctx.stopped) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (i >= middlewares.length) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const fn = middlewares[i];
|
|
89
|
+
if (!fn) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
await fn(ctx, () => dispatch(i + 1));
|
|
93
|
+
};
|
|
94
|
+
await dispatch(0);
|
|
95
|
+
return !ctx.stopped;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Build a fresh middleware context for one inbound message.
|
|
99
|
+
*
|
|
100
|
+
* Internal helper — used by {@link QQBot} when dispatching inbound events.
|
|
101
|
+
*/
|
|
102
|
+
export function createMiddlewareContext(params) {
|
|
103
|
+
const receivedAt = Date.now();
|
|
104
|
+
let stopped = false;
|
|
105
|
+
let stopReason;
|
|
106
|
+
const ac = new AbortController();
|
|
107
|
+
const ctx = {
|
|
108
|
+
bot: params.bot,
|
|
109
|
+
message: params.message,
|
|
110
|
+
replyTarget: params.message.replyTarget,
|
|
111
|
+
state: {},
|
|
112
|
+
log: params.log,
|
|
113
|
+
stop(reason) {
|
|
114
|
+
stopped = true;
|
|
115
|
+
stopReason = reason;
|
|
116
|
+
},
|
|
117
|
+
get stopped() {
|
|
118
|
+
return stopped;
|
|
119
|
+
},
|
|
120
|
+
get stopReason() {
|
|
121
|
+
return stopReason;
|
|
122
|
+
},
|
|
123
|
+
get signal() {
|
|
124
|
+
return ac.signal;
|
|
125
|
+
},
|
|
126
|
+
abort(reason) {
|
|
127
|
+
ac.abort(reason);
|
|
128
|
+
stopped = true;
|
|
129
|
+
stopReason = reason ?? "aborted";
|
|
130
|
+
},
|
|
131
|
+
get aborted() {
|
|
132
|
+
return ac.signal.aborted;
|
|
133
|
+
},
|
|
134
|
+
receivedAt,
|
|
135
|
+
};
|
|
136
|
+
return ctx;
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/middleware/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAsHH,qDAAqD;AAErD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,aAAa,CAC3B,GAAsB,EACtB,IAAY,EACZ,QAAuB,EACvB,YAAe;IAEf,IAAI,QAAQ,KAAK,SAAS,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;QAChD,OAAO,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