@umx-bot/core 0.0.0-stage → 0.7.1
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/LICENSE +21 -0
- package/README.md +5 -2
- package/dist/access-policy.d.ts +55 -0
- package/dist/access-policy.d.ts.map +1 -0
- package/dist/access-policy.js +98 -0
- package/dist/access-policy.js.map +1 -0
- package/dist/command-parser.d.ts +11 -0
- package/dist/command-parser.d.ts.map +1 -0
- package/dist/command-parser.js +77 -0
- package/dist/command-parser.js.map +1 -0
- package/dist/command-router.d.ts +46 -0
- package/dist/command-router.d.ts.map +1 -0
- package/dist/command-router.js +412 -0
- package/dist/command-router.js.map +1 -0
- package/dist/cron.d.ts +14 -0
- package/dist/cron.d.ts.map +1 -0
- package/dist/cron.js +134 -0
- package/dist/cron.js.map +1 -0
- package/dist/event-bus.d.ts +14 -0
- package/dist/event-bus.d.ts.map +1 -0
- package/dist/event-bus.js +68 -0
- package/dist/event-bus.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/interaction-router.d.ts +18 -0
- package/dist/interaction-router.d.ts.map +1 -0
- package/dist/interaction-router.js +68 -0
- package/dist/interaction-router.js.map +1 -0
- package/dist/kernel.d.ts +126 -0
- package/dist/kernel.d.ts.map +1 -0
- package/dist/kernel.js +463 -0
- package/dist/kernel.js.map +1 -0
- package/dist/memory-store.d.ts +14 -0
- package/dist/memory-store.d.ts.map +1 -0
- package/dist/memory-store.js +77 -0
- package/dist/memory-store.js.map +1 -0
- package/dist/metrics-registry.d.ts +18 -0
- package/dist/metrics-registry.d.ts.map +1 -0
- package/dist/metrics-registry.js +191 -0
- package/dist/metrics-registry.js.map +1 -0
- package/dist/middleware-pipeline.d.ts +17 -0
- package/dist/middleware-pipeline.d.ts.map +1 -0
- package/dist/middleware-pipeline.js +74 -0
- package/dist/middleware-pipeline.js.map +1 -0
- package/dist/navigation-registry.d.ts +14 -0
- package/dist/navigation-registry.d.ts.map +1 -0
- package/dist/navigation-registry.js +148 -0
- package/dist/navigation-registry.js.map +1 -0
- package/dist/outbound-pipeline.d.ts +22 -0
- package/dist/outbound-pipeline.d.ts.map +1 -0
- package/dist/outbound-pipeline.js +66 -0
- package/dist/outbound-pipeline.js.map +1 -0
- package/dist/plugin-compatibility.d.ts +10 -0
- package/dist/plugin-compatibility.d.ts.map +1 -0
- package/dist/plugin-compatibility.js +33 -0
- package/dist/plugin-compatibility.js.map +1 -0
- package/dist/plugin-runtime.d.ts +101 -0
- package/dist/plugin-runtime.d.ts.map +1 -0
- package/dist/plugin-runtime.js +468 -0
- package/dist/plugin-runtime.js.map +1 -0
- package/dist/prefixed-store.d.ts +10 -0
- package/dist/prefixed-store.d.ts.map +1 -0
- package/dist/prefixed-store.js +34 -0
- package/dist/prefixed-store.js.map +1 -0
- package/dist/scheduler.d.ts +20 -0
- package/dist/scheduler.d.ts.map +1 -0
- package/dist/scheduler.js +169 -0
- package/dist/scheduler.js.map +1 -0
- package/dist/scoped-storage.d.ts +23 -0
- package/dist/scoped-storage.d.ts.map +1 -0
- package/dist/scoped-storage.js +322 -0
- package/dist/scoped-storage.js.map +1 -0
- package/dist/service-container.d.ts +8 -0
- package/dist/service-container.d.ts.map +1 -0
- package/dist/service-container.js +35 -0
- package/dist/service-container.js.map +1 -0
- package/dist/settings-catalog.d.ts +21 -0
- package/dist/settings-catalog.d.ts.map +1 -0
- package/dist/settings-catalog.js +27 -0
- package/dist/settings-catalog.js.map +1 -0
- package/package.json +45 -3
- package/src/access-policy.ts +186 -0
- package/src/command-parser.ts +94 -0
- package/src/command-router.ts +567 -0
- package/src/cron.ts +174 -0
- package/src/event-bus.ts +119 -0
- package/src/index.ts +27 -0
- package/src/interaction-router.ts +101 -0
- package/src/kernel.ts +641 -0
- package/src/memory-store.ts +101 -0
- package/src/metrics-registry.ts +280 -0
- package/src/middleware-pipeline.ts +109 -0
- package/src/navigation-registry.ts +207 -0
- package/src/outbound-pipeline.ts +96 -0
- package/src/plugin-compatibility.ts +56 -0
- package/src/plugin-runtime.ts +692 -0
- package/src/prefixed-store.ts +43 -0
- package/src/scheduler.ts +235 -0
- package/src/scoped-storage.ts +404 -0
- package/src/service-container.ts +57 -0
- package/src/settings-catalog.ts +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 UMX-Bot contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @umx-bot/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The lifecycle, command routing, middleware, scoped storage and plugin runtime
|
|
4
|
+
used by UMX-Bot applications and `@umx-bot/plugin-testkit`.
|
|
5
|
+
|
|
6
|
+
Plugin packages should depend on `@umx-bot/plugin-sdk`, not this package.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { Awaitable, ConversationRef, IncomingMessage, KeyValueStore, MemberRole } from "@umx-bot/plugin-sdk";
|
|
2
|
+
export interface AccessRequest {
|
|
3
|
+
message: IncomingMessage;
|
|
4
|
+
plugin: string;
|
|
5
|
+
command: string;
|
|
6
|
+
subcommand?: string;
|
|
7
|
+
requiredRole: MemberRole;
|
|
8
|
+
}
|
|
9
|
+
export type AccessDecision = {
|
|
10
|
+
allowed: true;
|
|
11
|
+
} | {
|
|
12
|
+
allowed: false;
|
|
13
|
+
/** Shown to the user. Omit to stay silent. */
|
|
14
|
+
reason?: string;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Decides whether a command may run, on top of its static role requirement.
|
|
18
|
+
* Replace it to plug in your own rules (an allowlist service, a billing
|
|
19
|
+
* check, …); `StoreAccessPolicy` is the default.
|
|
20
|
+
*/
|
|
21
|
+
export interface AccessPolicy {
|
|
22
|
+
check(request: AccessRequest): Awaitable<AccessDecision>;
|
|
23
|
+
}
|
|
24
|
+
export interface AccessInspection {
|
|
25
|
+
disabledPlugins: string[];
|
|
26
|
+
disabledCommands: string[];
|
|
27
|
+
}
|
|
28
|
+
export interface StoreAccessPolicyOptions {
|
|
29
|
+
/**
|
|
30
|
+
* Plugins that can never be disabled, so an admin cannot lock themselves
|
|
31
|
+
* out of the very commands that re-enable things.
|
|
32
|
+
*/
|
|
33
|
+
exemptPlugins?: Iterable<string>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Per-conversation plugin/command switches and a per-platform user blocklist,
|
|
37
|
+
* persisted in the bot's key-value store.
|
|
38
|
+
*
|
|
39
|
+
* Owners bypass the blocklist, so a misfired `block` can always be undone.
|
|
40
|
+
*/
|
|
41
|
+
export declare class StoreAccessPolicy implements AccessPolicy {
|
|
42
|
+
private readonly store;
|
|
43
|
+
private readonly exempt;
|
|
44
|
+
constructor(store: KeyValueStore, options?: StoreAccessPolicyOptions);
|
|
45
|
+
isExempt(plugin: string): boolean;
|
|
46
|
+
check(request: AccessRequest): Promise<AccessDecision>;
|
|
47
|
+
inspect(target: ConversationRef): Promise<AccessInspection>;
|
|
48
|
+
setPluginEnabled(target: ConversationRef, plugin: string, enabled: boolean): Promise<void>;
|
|
49
|
+
setCommandEnabled(target: ConversationRef, command: string, enabled: boolean): Promise<void>;
|
|
50
|
+
isBlocked(platform: string, userId: string): Promise<boolean>;
|
|
51
|
+
setBlocked(platform: string, userId: string, blocked: boolean): Promise<void>;
|
|
52
|
+
listBlocked(platform: string): Promise<string[]>;
|
|
53
|
+
private updateRules;
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=access-policy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"access-policy.d.ts","sourceRoot":"","sources":["../src/access-policy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,SAAS,EACT,eAAe,EACf,eAAe,EACf,aAAa,EACb,UAAU,EACX,MAAM,qBAAqB,CAAC;AAE7B,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,eAAe,CAAC;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,UAAU,CAAC;CAC1B;AAED,MAAM,MAAM,cAAc,GACtB;IAAE,OAAO,EAAE,IAAI,CAAA;CAAE,GACjB;IACE,OAAO,EAAE,KAAK,CAAC;IACf,8CAA8C;IAC9C,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEN;;;;GAIG;AACH,MAAM,WAAW,YAAY;IAC3B,KAAK,CAAC,OAAO,EAAE,aAAa,GAAG,SAAS,CAAC,cAAc,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|
+
* persisted in the bot's key-value store.
|
|
4
|
+
*
|
|
5
|
+
* Owners bypass the blocklist, so a misfired `block` can always be undone.
|
|
6
|
+
*/
|
|
7
|
+
export class StoreAccessPolicy {
|
|
8
|
+
store;
|
|
9
|
+
exempt;
|
|
10
|
+
constructor(store, options = {}) {
|
|
11
|
+
this.store = store;
|
|
12
|
+
this.exempt = new Set(options.exemptPlugins ?? []);
|
|
13
|
+
}
|
|
14
|
+
isExempt(plugin) {
|
|
15
|
+
return this.exempt.has(plugin);
|
|
16
|
+
}
|
|
17
|
+
async check(request) {
|
|
18
|
+
if (this.exempt.has(request.plugin)) {
|
|
19
|
+
return { allowed: true };
|
|
20
|
+
}
|
|
21
|
+
const { message } = request;
|
|
22
|
+
if (message.author.role !== "owner" &&
|
|
23
|
+
(await this.isBlocked(message.platform, message.author.id))) {
|
|
24
|
+
return { allowed: false };
|
|
25
|
+
}
|
|
26
|
+
const rules = await this.store.get(conversationKey(message));
|
|
27
|
+
if (!rules) {
|
|
28
|
+
return { allowed: true };
|
|
29
|
+
}
|
|
30
|
+
if (rules.plugins.includes(request.plugin)) {
|
|
31
|
+
return { allowed: false };
|
|
32
|
+
}
|
|
33
|
+
if (rules.commands.includes(request.command)) {
|
|
34
|
+
return { allowed: false };
|
|
35
|
+
}
|
|
36
|
+
return { allowed: true };
|
|
37
|
+
}
|
|
38
|
+
async inspect(target) {
|
|
39
|
+
const rules = await this.store.get(conversationKey(target));
|
|
40
|
+
return {
|
|
41
|
+
disabledPlugins: [...(rules?.plugins ?? [])].sort(),
|
|
42
|
+
disabledCommands: [...(rules?.commands ?? [])].sort()
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
async setPluginEnabled(target, plugin, enabled) {
|
|
46
|
+
if (!enabled && this.exempt.has(plugin)) {
|
|
47
|
+
throw new Error(`plugin "${plugin}" cannot be disabled`);
|
|
48
|
+
}
|
|
49
|
+
await this.updateRules(target, (rules) => ({
|
|
50
|
+
...rules,
|
|
51
|
+
plugins: toggle(rules.plugins, plugin, !enabled)
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
async setCommandEnabled(target, command, enabled) {
|
|
55
|
+
await this.updateRules(target, (rules) => ({
|
|
56
|
+
...rules,
|
|
57
|
+
commands: toggle(rules.commands, command, !enabled)
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
async isBlocked(platform, userId) {
|
|
61
|
+
const blocked = await this.store.get(blockKey(platform));
|
|
62
|
+
return blocked?.includes(userId) ?? false;
|
|
63
|
+
}
|
|
64
|
+
async setBlocked(platform, userId, blocked) {
|
|
65
|
+
await this.store.update(blockKey(platform), (current) => {
|
|
66
|
+
const next = toggle(current ?? [], userId, blocked);
|
|
67
|
+
return next.length === 0 ? undefined : next;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
async listBlocked(platform) {
|
|
71
|
+
return [...((await this.store.get(blockKey(platform))) ?? [])];
|
|
72
|
+
}
|
|
73
|
+
async updateRules(target, change) {
|
|
74
|
+
await this.store.update(conversationKey(target), (current) => {
|
|
75
|
+
const next = change(current ?? { plugins: [], commands: [] });
|
|
76
|
+
return next.plugins.length === 0 && next.commands.length === 0
|
|
77
|
+
? undefined
|
|
78
|
+
: next;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function toggle(list, value, present) {
|
|
83
|
+
const without = list.filter((item) => item !== value);
|
|
84
|
+
return present ? [...without, value].sort() : without;
|
|
85
|
+
}
|
|
86
|
+
function conversationKey(target) {
|
|
87
|
+
return [
|
|
88
|
+
"access",
|
|
89
|
+
"conversation",
|
|
90
|
+
encodeURIComponent(target.platform),
|
|
91
|
+
encodeURIComponent(target.scope),
|
|
92
|
+
encodeURIComponent(target.conversationId)
|
|
93
|
+
].join(":");
|
|
94
|
+
}
|
|
95
|
+
function blockKey(platform) {
|
|
96
|
+
return `access:blocked:${encodeURIComponent(platform)}`;
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=access-policy.js.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AO,CAAC,MAAM,KAAK,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC;gBAC5D,CAAC,CAAC,SAAS;gBACX,CAAC,CAAC,IAAI,CAAC;QACX,CAAC,CACF,CAAC;IACJ,CAAC;CACF;AAED,SAAS,MAAM,CAAC,IAAc,EAAE,KAAa,EAAE,OAAgB;IAC7D,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,KAAK,CAAC,CAAC;IACtD,OAAO,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC;AACxD,CAAC;AAED,SAAS,eAAe,CAAC,MAAuB;IAC9C,OAAO;QACL,QAAQ;QACR,cAAc;QACd,kBAAkB,CAAC,MAAM,CAAC,QAAQ,CAAC;QACnC,kBAAkB,CAAC,MAAM,CAAC,KAAK,CAAC;QAChC,kBAAkB,CAAC,MAAM,CAAC,cAAc,CAAC;KAC1C,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACd,CAAC;AAED,SAAS,QAAQ,CAAC,QAAgB;IAChC,OAAO,kBAAkB,kBAAkB,CAAC,QAAQ,CAAC,EAAE,CAAC;AAC1D,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare class CommandParseError extends Error {
|
|
2
|
+
constructor(message: string);
|
|
3
|
+
}
|
|
4
|
+
export interface ParsedCommand {
|
|
5
|
+
name: string;
|
|
6
|
+
args: string[];
|
|
7
|
+
rawArgs: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function parseCommand(input: string): ParsedCommand | undefined;
|
|
10
|
+
export declare function parseArguments(input: string): string[];
|
|
11
|
+
//# sourceMappingURL=command-parser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command-parser.d.ts","sourceRoot":"","sources":["../src/command-parser.ts"],"names":[],"mappings":"AAAA,qBAAa,iBAAkB,SAAQ,KAAK;gBACvB,OAAO,EAAE,MAAM;CAInC;AAED,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS,CAerE;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CA+DtD"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export class CommandParseError extends Error {
|
|
2
|
+
constructor(message) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = "CommandParseError";
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export function parseCommand(input) {
|
|
8
|
+
const trimmed = input.trim();
|
|
9
|
+
if (trimmed.length === 0) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
const separator = trimmed.search(/\s/u);
|
|
13
|
+
const name = separator < 0 ? trimmed : trimmed.slice(0, separator);
|
|
14
|
+
const rawArgs = separator < 0 ? "" : trimmed.slice(separator).trim();
|
|
15
|
+
return {
|
|
16
|
+
name,
|
|
17
|
+
args: parseArguments(rawArgs),
|
|
18
|
+
rawArgs
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export function parseArguments(input) {
|
|
22
|
+
const args = [];
|
|
23
|
+
let current = "";
|
|
24
|
+
let quote;
|
|
25
|
+
let escaping = false;
|
|
26
|
+
let tokenStarted = false;
|
|
27
|
+
const pushCurrent = () => {
|
|
28
|
+
if (tokenStarted) {
|
|
29
|
+
args.push(current);
|
|
30
|
+
current = "";
|
|
31
|
+
tokenStarted = false;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
for (const character of input) {
|
|
35
|
+
if (escaping) {
|
|
36
|
+
current += character;
|
|
37
|
+
tokenStarted = true;
|
|
38
|
+
escaping = false;
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (character === "\\") {
|
|
42
|
+
escaping = true;
|
|
43
|
+
tokenStarted = true;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (quote) {
|
|
47
|
+
if (character === quote) {
|
|
48
|
+
quote = undefined;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
current += character;
|
|
52
|
+
}
|
|
53
|
+
tokenStarted = true;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (character === "'" || character === '"') {
|
|
57
|
+
quote = character;
|
|
58
|
+
tokenStarted = true;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (/\s/u.test(character)) {
|
|
62
|
+
pushCurrent();
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
current += character;
|
|
66
|
+
tokenStarted = true;
|
|
67
|
+
}
|
|
68
|
+
if (escaping) {
|
|
69
|
+
throw new CommandParseError("参数末尾不能是转义符");
|
|
70
|
+
}
|
|
71
|
+
if (quote) {
|
|
72
|
+
throw new CommandParseError("参数中的引号没有闭合");
|
|
73
|
+
}
|
|
74
|
+
pushCurrent();
|
|
75
|
+
return args;
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=command-parser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command-parser.js","sourceRoot":"","sources":["../src/command-parser.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAC1C,YAAmB,OAAe;QAChC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IAClC,CAAC;CACF;AAQD,MAAM,UAAU,YAAY,CAAC,KAAa;IACxC,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;IAC7B,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,SAAS,CAAC;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SAAS,KAAK,KAAK,EAAE,CAAC;gBACxB,KAAK,GAAG,SAAS,CAAC;YACpB,CAAC;iBAAM,CAAC;gBACN,OAAO,IAAI,SAAS,CAAC;YACvB,CAAC;YACD,YAAY,GAAG,IAAI,CAAC;YACpB,SAAS;QACX,CAAC;QAED,IAAI,SAAS,KAAK,GAAG,IAAI,SAAS,KAAK,GAAG,EAAE,CAAC;YAC3C,KAAK,GAAG,SAAS,CAAC;YAClB,YAAY,GAAG,IAAI,CAAC;YACpB,SAAS;QACX,CAAC;QAED,IAAI,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC;YAC1B,WAAW,EAAE,CAAC;YACd,SAAS;QACX,CAAC;QAED,OAAO,IAAI,SAAS,CAAC;QACrB,YAAY,GAAG,IAAI,CAAC;IACtB,CAAC;IAED,IAAI,QAAQ,EAAE,CAAC;QACb,MAAM,IAAI,iBAAiB,CAAC,YAAY,CAAC,CAAC;IAC5C,CAAC;IACD,IAAI,KAAK,EAAE,CAAC;QACV,MAAM,IAAI,iBAAiB,CAAC,YAAY,CAAC,CAAC;IAC5C,CAAC;IAED,WAAW,EAAE,CAAC;IACd,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { CommandDefinition, CommandExecutedEvent, CommandSummary, Dispose, IncomingMessage, Logger, MessageSender, PluginHelpSummary } from "@umx-bot/plugin-sdk";
|
|
2
|
+
import type { AccessPolicy } from "./access-policy.js";
|
|
3
|
+
export interface CommandRouterHooks {
|
|
4
|
+
/** Extra gate evaluated after the role check. */
|
|
5
|
+
access?: AccessPolicy;
|
|
6
|
+
/** Called once per command attempt that reached a registered command. */
|
|
7
|
+
onExecuted?: (event: CommandExecutedEvent) => void | Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
export declare class CommandRouter {
|
|
10
|
+
private readonly prefix;
|
|
11
|
+
private readonly messages;
|
|
12
|
+
private readonly logger;
|
|
13
|
+
private readonly hooks;
|
|
14
|
+
private readonly commands;
|
|
15
|
+
private readonly cooldowns;
|
|
16
|
+
constructor(prefix: string, messages: MessageSender, logger: Logger, hooks?: CommandRouterHooks);
|
|
17
|
+
forPlugin(plugin: PluginHelpSummary): {
|
|
18
|
+
register(command: CommandDefinition): Dispose;
|
|
19
|
+
list(): CommandSummary[];
|
|
20
|
+
format(name: string, args?: string): string;
|
|
21
|
+
};
|
|
22
|
+
format(name: string, args?: string): string;
|
|
23
|
+
list(): CommandSummary[];
|
|
24
|
+
private summarizeSubcommand;
|
|
25
|
+
handle(message: IncomingMessage): Promise<boolean>;
|
|
26
|
+
/**
|
|
27
|
+
* Picks the subcommand named by the first argument, falling back to the
|
|
28
|
+
* command's own `execute`. Returns undefined only for a command that has
|
|
29
|
+
* subcommands but no `execute`, when nothing matched.
|
|
30
|
+
*/
|
|
31
|
+
private resolveTarget;
|
|
32
|
+
private subcommandHelp;
|
|
33
|
+
/**
|
|
34
|
+
* Records who ran what, where.
|
|
35
|
+
*
|
|
36
|
+
* Deliberately omits the message text and the parsed arguments: a command
|
|
37
|
+
* can carry an address, an API key or private content, and the audit trail
|
|
38
|
+
* must stay safe to keep and to share. The message id is enough to correlate
|
|
39
|
+
* with the platform when a real investigation is needed.
|
|
40
|
+
*/
|
|
41
|
+
private audit;
|
|
42
|
+
private register;
|
|
43
|
+
private validateDefinition;
|
|
44
|
+
private pruneCooldowns;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=command-router.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command-router.d.ts","sourceRoot":"","sources":["../src/command-router.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,iBAAiB,EACjB,oBAAoB,EACpB,cAAc,EACd,OAAO,EACP,eAAe,EACf,MAAM,EAEN,aAAa,EACb,iBAAiB,EAGlB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAevD,MAAM,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