@umx-bot/core 0.7.0 → 0.8.0
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 +1 -1
- package/README.md +1 -1
- package/dist/access-policy.d.ts +2 -1
- package/dist/access-policy.d.ts.map +1 -1
- package/dist/access-policy.js +6 -2
- package/dist/access-policy.js.map +1 -1
- package/dist/command-router.d.ts +13 -1
- package/dist/command-router.d.ts.map +1 -1
- package/dist/command-router.js +99 -20
- package/dist/command-router.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/kernel.d.ts +8 -2
- package/dist/kernel.d.ts.map +1 -1
- package/dist/kernel.js +22 -9
- package/dist/kernel.js.map +1 -1
- package/dist/navigation-registry.d.ts +11 -2
- package/dist/navigation-registry.d.ts.map +1 -1
- package/dist/navigation-registry.js +26 -2
- package/dist/navigation-registry.js.map +1 -1
- package/dist/plugin-runtime.d.ts +19 -0
- package/dist/plugin-runtime.d.ts.map +1 -1
- package/dist/plugin-runtime.js +49 -5
- package/dist/plugin-runtime.js.map +1 -1
- package/dist/service-container.d.ts +3 -1
- package/dist/service-container.d.ts.map +1 -1
- package/dist/service-container.js +4 -0
- package/dist/service-container.js.map +1 -1
- package/package.json +4 -4
- package/src/access-policy.ts +5 -2
- package/src/command-router.ts +144 -24
- package/src/index.ts +1 -0
- package/src/kernel.ts +25 -12
- package/src/navigation-registry.ts +45 -2
- package/src/plugin-runtime.ts +66 -6
- package/src/service-container.ts +5 -0
package/src/command-router.ts
CHANGED
|
@@ -7,6 +7,7 @@ import type {
|
|
|
7
7
|
IncomingMessage,
|
|
8
8
|
Logger,
|
|
9
9
|
MemberRole,
|
|
10
|
+
MessageContent,
|
|
10
11
|
MessageSender,
|
|
11
12
|
PluginHelpSummary,
|
|
12
13
|
SubcommandDefinition,
|
|
@@ -33,12 +34,21 @@ export interface CommandRouterHooks {
|
|
|
33
34
|
access?: AccessPolicy;
|
|
34
35
|
/** Called once per command attempt that reached a registered command. */
|
|
35
36
|
onExecuted?: (event: CommandExecutedEvent) => void | Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Answers a command that has only subcommands when none matched. Return
|
|
39
|
+
* undefined to fall back to the built-in subcommand list.
|
|
40
|
+
*/
|
|
41
|
+
subcommandHelp?: (
|
|
42
|
+
message: IncomingMessage,
|
|
43
|
+
command: string
|
|
44
|
+
) => Promise<MessageContent | undefined>;
|
|
36
45
|
}
|
|
37
46
|
|
|
38
47
|
/** What actually runs: the command itself or one of its subcommands. */
|
|
39
48
|
interface Target {
|
|
40
49
|
subcommand: SubcommandDefinition | undefined;
|
|
41
50
|
permission: MemberRole;
|
|
51
|
+
botOwnerOnly: boolean;
|
|
42
52
|
cooldownMs: number;
|
|
43
53
|
scopes: readonly string[] | undefined;
|
|
44
54
|
args: CommandDefinition["args"];
|
|
@@ -64,11 +74,13 @@ export class CommandRouter {
|
|
|
64
74
|
public forPlugin(plugin: PluginHelpSummary): {
|
|
65
75
|
register(command: CommandDefinition): Dispose;
|
|
66
76
|
list(): CommandSummary[];
|
|
77
|
+
available(message: IncomingMessage): Promise<CommandSummary[]>;
|
|
67
78
|
format(name: string, args?: string): string;
|
|
68
79
|
} {
|
|
69
80
|
return {
|
|
70
81
|
register: (command) => this.register(plugin, command),
|
|
71
82
|
list: () => this.list(),
|
|
83
|
+
available: (message) => this.available(message),
|
|
72
84
|
format: (name, args) => this.format(name, args)
|
|
73
85
|
};
|
|
74
86
|
}
|
|
@@ -94,6 +106,7 @@ export class CommandRouter {
|
|
|
94
106
|
this.format(alias)
|
|
95
107
|
),
|
|
96
108
|
permission: definition.permission ?? "member",
|
|
109
|
+
...(definition.botOwnerOnly ? { botOwnerOnly: true as const } : {}),
|
|
97
110
|
plugin: { ...plugin },
|
|
98
111
|
usage: this.format(
|
|
99
112
|
definition.name,
|
|
@@ -120,6 +133,74 @@ export class CommandRouter {
|
|
|
120
133
|
);
|
|
121
134
|
}
|
|
122
135
|
|
|
136
|
+
/**
|
|
137
|
+
* What the message's author can run in its chat: leaves out hidden
|
|
138
|
+
* commands and subcommands, those scoped to other chats, those above the
|
|
139
|
+
* author's role and commands the access policy blocks here.
|
|
140
|
+
*/
|
|
141
|
+
public async available(message: IncomingMessage): Promise<CommandSummary[]> {
|
|
142
|
+
const definitions = new Map<string, RegisteredCommand>();
|
|
143
|
+
for (const registered of this.commands.values()) {
|
|
144
|
+
definitions.set(registered.definition.name, registered);
|
|
145
|
+
}
|
|
146
|
+
const result: CommandSummary[] = [];
|
|
147
|
+
for (const summary of this.list()) {
|
|
148
|
+
const registered = definitions.get(summary.name);
|
|
149
|
+
if (!registered || summary.hidden) {
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
const { definition } = registered;
|
|
153
|
+
const reachable = (
|
|
154
|
+
permission: MemberRole,
|
|
155
|
+
scopes: readonly string[] | undefined,
|
|
156
|
+
botOwnerOnly: boolean | undefined
|
|
157
|
+
) =>
|
|
158
|
+
(botOwnerOnly !== true || message.author.botOwner === true) &&
|
|
159
|
+
roleWeight[message.author.role] >= roleWeight[permission] &&
|
|
160
|
+
(scopes === undefined || scopes.includes(message.scope));
|
|
161
|
+
const subcommands = (definition.subcommands ?? []).filter(
|
|
162
|
+
(sub) =>
|
|
163
|
+
!(sub.hidden ?? false) &&
|
|
164
|
+
reachable(
|
|
165
|
+
sub.permission ?? definition.permission ?? "member",
|
|
166
|
+
sub.scopes ?? definition.scopes,
|
|
167
|
+
sub.botOwnerOnly ?? definition.botOwnerOnly
|
|
168
|
+
)
|
|
169
|
+
);
|
|
170
|
+
const ownReachable =
|
|
171
|
+
definition.execute !== undefined &&
|
|
172
|
+
reachable(
|
|
173
|
+
summary.permission,
|
|
174
|
+
definition.scopes,
|
|
175
|
+
definition.botOwnerOnly
|
|
176
|
+
);
|
|
177
|
+
if (!ownReachable && subcommands.length === 0) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (this.hooks.access) {
|
|
181
|
+
const decision = await this.hooks.access.check({
|
|
182
|
+
message,
|
|
183
|
+
plugin: registered.plugin.name,
|
|
184
|
+
command: summary.name,
|
|
185
|
+
requiredRole: summary.permission
|
|
186
|
+
});
|
|
187
|
+
if (!decision.allowed) {
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const visibleNames = new Set(subcommands.map((sub) => sub.name));
|
|
192
|
+
const { subcommands: allSubcommands, ...rest } = summary;
|
|
193
|
+
const kept = (allSubcommands ?? []).filter((sub) =>
|
|
194
|
+
visibleNames.has(sub.name)
|
|
195
|
+
);
|
|
196
|
+
result.push({
|
|
197
|
+
...rest,
|
|
198
|
+
...(kept.length > 0 ? { subcommands: kept } : {})
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
return result;
|
|
202
|
+
}
|
|
203
|
+
|
|
123
204
|
private summarizeSubcommand(
|
|
124
205
|
parent: CommandDefinition,
|
|
125
206
|
sub: SubcommandDefinition
|
|
@@ -131,6 +212,9 @@ export class CommandRouter {
|
|
|
131
212
|
description: sub.description,
|
|
132
213
|
aliases: sub.aliases ?? [],
|
|
133
214
|
permission: sub.permission ?? parent.permission ?? "member",
|
|
215
|
+
...((sub.botOwnerOnly ?? parent.botOwnerOnly)
|
|
216
|
+
? { botOwnerOnly: true as const }
|
|
217
|
+
: {}),
|
|
134
218
|
usage: this.format(path, sub.usage ?? sub.args?.usage),
|
|
135
219
|
examples: (sub.examples ?? []).map((example) => ({
|
|
136
220
|
command: this.format(path, example.args),
|
|
@@ -172,9 +256,18 @@ export class CommandRouter {
|
|
|
172
256
|
const target = this.resolveTarget(registered.definition, parsed);
|
|
173
257
|
if (!target) {
|
|
174
258
|
// Only subcommands, none matched: tell the user what exists.
|
|
259
|
+
let help: MessageContent | undefined;
|
|
260
|
+
try {
|
|
261
|
+
help = await this.hooks.subcommandHelp?.(
|
|
262
|
+
message,
|
|
263
|
+
registered.definition.name
|
|
264
|
+
);
|
|
265
|
+
} catch (error) {
|
|
266
|
+
this.logger.error({ error }, "subcommand help hook failed");
|
|
267
|
+
}
|
|
175
268
|
await this.messages.reply(
|
|
176
269
|
message,
|
|
177
|
-
this.subcommandHelp(registered.definition)
|
|
270
|
+
help ?? this.subcommandHelp(registered.definition, message)
|
|
178
271
|
);
|
|
179
272
|
return true;
|
|
180
273
|
}
|
|
@@ -207,9 +300,15 @@ export class CommandRouter {
|
|
|
207
300
|
const auditExtra = subcommandName ? { subcommand: subcommandName } : {};
|
|
208
301
|
|
|
209
302
|
const requiredRole = target.permission;
|
|
210
|
-
|
|
303
|
+
const lacksBotOwner =
|
|
304
|
+
target.botOwnerOnly && message.author.botOwner !== true;
|
|
305
|
+
if (
|
|
306
|
+
roleWeight[message.author.role] < roleWeight[requiredRole] ||
|
|
307
|
+
lacksBotOwner
|
|
308
|
+
) {
|
|
211
309
|
this.audit("denied", message, registered, {
|
|
212
310
|
requiredRole,
|
|
311
|
+
...(target.botOwnerOnly ? { requiredBotOwner: true } : {}),
|
|
213
312
|
...auditExtra
|
|
214
313
|
});
|
|
215
314
|
await this.messages.reply(message, "权限不足,无法执行该命令。");
|
|
@@ -238,6 +337,28 @@ export class CommandRouter {
|
|
|
238
337
|
}
|
|
239
338
|
}
|
|
240
339
|
|
|
340
|
+
let typed: unknown;
|
|
341
|
+
if (target.args) {
|
|
342
|
+
try {
|
|
343
|
+
typed = target.args.parse({
|
|
344
|
+
args: parsed.args,
|
|
345
|
+
rawArgs: parsed.rawArgs
|
|
346
|
+
});
|
|
347
|
+
} catch (error) {
|
|
348
|
+
const detail =
|
|
349
|
+
error instanceof CommandArgumentError ? error.message : "参数不合法";
|
|
350
|
+
const usage = this.format(
|
|
351
|
+
subcommandName ? `${name} ${subcommandName}` : name,
|
|
352
|
+
target.usage ?? target.args.usage
|
|
353
|
+
);
|
|
354
|
+
await this.messages.reply(message, `${detail}\n用法:${usage}`);
|
|
355
|
+
await finish("rejected", "arguments");
|
|
356
|
+
return true;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// Only a well-formed invocation starts the cooldown: a typo in the
|
|
361
|
+
// arguments should not lock the user out for the whole period.
|
|
241
362
|
if (target.cooldownMs > 0) {
|
|
242
363
|
const cooldownKey = [
|
|
243
364
|
name,
|
|
@@ -260,26 +381,6 @@ export class CommandRouter {
|
|
|
260
381
|
this.pruneCooldowns(now);
|
|
261
382
|
}
|
|
262
383
|
|
|
263
|
-
let typed: unknown;
|
|
264
|
-
if (target.args) {
|
|
265
|
-
try {
|
|
266
|
-
typed = target.args.parse({
|
|
267
|
-
args: parsed.args,
|
|
268
|
-
rawArgs: parsed.rawArgs
|
|
269
|
-
});
|
|
270
|
-
} catch (error) {
|
|
271
|
-
const detail =
|
|
272
|
-
error instanceof CommandArgumentError ? error.message : "参数不合法";
|
|
273
|
-
const usage = this.format(
|
|
274
|
-
subcommandName ? `${name} ${subcommandName}` : name,
|
|
275
|
-
target.usage ?? target.args.usage
|
|
276
|
-
);
|
|
277
|
-
await this.messages.reply(message, `${detail}\n用法:${usage}`);
|
|
278
|
-
await finish("rejected", "arguments");
|
|
279
|
-
return true;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
384
|
const context: CommandContext = {
|
|
284
385
|
message,
|
|
285
386
|
command: name,
|
|
@@ -348,6 +449,7 @@ export class CommandRouter {
|
|
|
348
449
|
return {
|
|
349
450
|
subcommand: sub,
|
|
350
451
|
permission: sub.permission ?? definition.permission ?? "member",
|
|
452
|
+
botOwnerOnly: sub.botOwnerOnly ?? definition.botOwnerOnly ?? false,
|
|
351
453
|
cooldownMs: sub.cooldownMs ?? definition.cooldownMs ?? 0,
|
|
352
454
|
scopes: sub.scopes ?? definition.scopes,
|
|
353
455
|
args: sub.args,
|
|
@@ -362,6 +464,7 @@ export class CommandRouter {
|
|
|
362
464
|
return {
|
|
363
465
|
subcommand: undefined,
|
|
364
466
|
permission: definition.permission ?? "member",
|
|
467
|
+
botOwnerOnly: definition.botOwnerOnly ?? false,
|
|
365
468
|
cooldownMs: definition.cooldownMs ?? 0,
|
|
366
469
|
scopes: definition.scopes,
|
|
367
470
|
args: definition.args,
|
|
@@ -370,13 +473,29 @@ export class CommandRouter {
|
|
|
370
473
|
};
|
|
371
474
|
}
|
|
372
475
|
|
|
373
|
-
private subcommandHelp(
|
|
476
|
+
private subcommandHelp(
|
|
477
|
+
definition: CommandDefinition,
|
|
478
|
+
message: IncomingMessage
|
|
479
|
+
): string {
|
|
374
480
|
const lines = (definition.subcommands ?? [])
|
|
375
|
-
.filter((sub) =>
|
|
481
|
+
.filter((sub) => {
|
|
482
|
+
const permission = sub.permission ?? definition.permission ?? "member";
|
|
483
|
+
const scopes = sub.scopes ?? definition.scopes;
|
|
484
|
+
const botOwnerOnly = sub.botOwnerOnly ?? definition.botOwnerOnly;
|
|
485
|
+
return (
|
|
486
|
+
!(sub.hidden ?? definition.hidden ?? false) &&
|
|
487
|
+
(botOwnerOnly !== true || message.author.botOwner === true) &&
|
|
488
|
+
roleWeight[message.author.role] >= roleWeight[permission] &&
|
|
489
|
+
(scopes === undefined || scopes.includes(message.scope))
|
|
490
|
+
);
|
|
491
|
+
})
|
|
376
492
|
.map((sub) => {
|
|
377
493
|
const summary = this.summarizeSubcommand(definition, sub);
|
|
378
494
|
return `• ${summary.usage} — ${summary.description}`;
|
|
379
495
|
});
|
|
496
|
+
if (lines.length === 0) {
|
|
497
|
+
return `${this.format(definition.name)} 没有你在这里可用的子命令。`;
|
|
498
|
+
}
|
|
380
499
|
return [`${this.format(definition.name)} 可用的子命令:`, ...lines].join(
|
|
381
500
|
"\n"
|
|
382
501
|
);
|
|
@@ -407,6 +526,7 @@ export class CommandRouter {
|
|
|
407
526
|
conversationId: message.conversationId,
|
|
408
527
|
authorId: message.author.id,
|
|
409
528
|
role: message.author.role,
|
|
529
|
+
...(message.author.botOwner ? { botOwner: true } : {}),
|
|
410
530
|
messageId: message.id,
|
|
411
531
|
...extra
|
|
412
532
|
},
|
package/src/index.ts
CHANGED
|
@@ -22,5 +22,6 @@ export type {
|
|
|
22
22
|
PluginLoadOptions,
|
|
23
23
|
PluginSnapshot
|
|
24
24
|
} from "./plugin-runtime.js";
|
|
25
|
+
export { RESERVED_PLUGIN_NAMES } from "./plugin-runtime.js";
|
|
25
26
|
export type { SettingsCatalogEntry } from "./settings-catalog.js";
|
|
26
27
|
export { SettingsCatalog } from "./settings-catalog.js";
|
package/src/kernel.ts
CHANGED
|
@@ -10,6 +10,7 @@ import type {
|
|
|
10
10
|
NavigationPageSummary,
|
|
11
11
|
OutgoingMessage
|
|
12
12
|
} from "@umx-bot/plugin-sdk";
|
|
13
|
+
import { helpService } from "@umx-bot/plugin-sdk";
|
|
13
14
|
import type { AccessPolicy } from "./access-policy.js";
|
|
14
15
|
import { StoreAccessPolicy } from "./access-policy.js";
|
|
15
16
|
import { CommandRouter } from "./command-router.js";
|
|
@@ -72,7 +73,8 @@ export interface BotKernelOptions {
|
|
|
72
73
|
store?: KeyValueStore;
|
|
73
74
|
shutdownTimeoutMs?: number;
|
|
74
75
|
/**
|
|
75
|
-
* Platform user ids treated as `owner` in every conversation
|
|
76
|
+
* Platform user ids treated as `owner` in every conversation and marked
|
|
77
|
+
* `author.botOwner`, which `botOwnerOnly` commands require.
|
|
76
78
|
*
|
|
77
79
|
* Platform roles only describe a single group, so without this the operator
|
|
78
80
|
* of a self-hosted bot has no way to reach owner-level commands in a group
|
|
@@ -205,8 +207,11 @@ export class BotKernel {
|
|
|
205
207
|
this.events = new EventBus(options.logger);
|
|
206
208
|
this.middleware = new MiddlewarePipeline(messages);
|
|
207
209
|
const commandPrefix = options.commandPrefix ?? "/";
|
|
210
|
+
const services = new ServiceContainer();
|
|
208
211
|
this.commands = new CommandRouter(commandPrefix, messages, options.logger, {
|
|
209
212
|
access: options.accessPolicy ?? new StoreAccessPolicy(store),
|
|
213
|
+
subcommandHelp: async (message, command) =>
|
|
214
|
+
services.lookup(helpService)?.render(message, command),
|
|
210
215
|
onExecuted: async (event) => {
|
|
211
216
|
commandCount.inc({
|
|
212
217
|
plugin: event.plugin,
|
|
@@ -251,7 +256,7 @@ export class BotKernel {
|
|
|
251
256
|
}
|
|
252
257
|
}),
|
|
253
258
|
store,
|
|
254
|
-
|
|
259
|
+
services,
|
|
255
260
|
options.logger,
|
|
256
261
|
{
|
|
257
262
|
...(options.contextExtensions
|
|
@@ -498,19 +503,27 @@ export class BotKernel {
|
|
|
498
503
|
}
|
|
499
504
|
}
|
|
500
505
|
|
|
501
|
-
/**
|
|
506
|
+
/**
|
|
507
|
+
* Marks and elevates a configured bot owner before anything inspects the
|
|
508
|
+
* author. `botOwner` comes only from here: a value an adapter set is
|
|
509
|
+
* dropped, so a platform role such as a QQ group owner never turns into
|
|
510
|
+
* bot-wide authority.
|
|
511
|
+
*/
|
|
502
512
|
private applyOwnerIds(message: IncomingMessage): IncomingMessage {
|
|
503
|
-
if (
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
513
|
+
if (this.ownerIds.has(message.author.id)) {
|
|
514
|
+
if (message.author.role === "owner" && message.author.botOwner === true) {
|
|
515
|
+
return message;
|
|
516
|
+
}
|
|
517
|
+
return {
|
|
518
|
+
...message,
|
|
519
|
+
author: { ...message.author, role: "owner", botOwner: true }
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
if (message.author.botOwner === undefined) {
|
|
508
523
|
return message;
|
|
509
524
|
}
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
author: { ...message.author, role: "owner" }
|
|
513
|
-
};
|
|
525
|
+
const { botOwner: _forged, ...author } = message.author;
|
|
526
|
+
return { ...message, author };
|
|
514
527
|
}
|
|
515
528
|
|
|
516
529
|
private async processMessage(message: IncomingMessage): Promise<void> {
|
|
@@ -2,6 +2,8 @@ import type {
|
|
|
2
2
|
ChatScope,
|
|
3
3
|
CommandSummary,
|
|
4
4
|
Dispose,
|
|
5
|
+
IncomingMessage,
|
|
6
|
+
MemberRole,
|
|
5
7
|
NavigationItemDefinition,
|
|
6
8
|
NavigationPageDefinition,
|
|
7
9
|
NavigationPageSummary,
|
|
@@ -20,6 +22,35 @@ const allScopes: ChatScope[] = ["group", "direct", "guild"];
|
|
|
20
22
|
const defaultSurfaces: NavigationSurface[] = ["message"];
|
|
21
23
|
const allSurfaces: NavigationSurface[] = ["message", "menu", "panel"];
|
|
22
24
|
const idPattern = /^[a-z0-9][a-z0-9._-]*$/u;
|
|
25
|
+
const roleWeight: Record<MemberRole, number> = {
|
|
26
|
+
member: 0,
|
|
27
|
+
admin: 1,
|
|
28
|
+
owner: 2
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Keeps the items `message`'s author can use in its chat and drops pages left
|
|
33
|
+
* empty. With `available`, items must also point at one of those commands.
|
|
34
|
+
*/
|
|
35
|
+
export function filterNavigation(
|
|
36
|
+
pages: NavigationPageSummary[],
|
|
37
|
+
available: readonly CommandSummary[] | undefined,
|
|
38
|
+
message: IncomingMessage
|
|
39
|
+
): NavigationPageSummary[] {
|
|
40
|
+
const commands = available
|
|
41
|
+
? new Set(available.map((command) => command.name))
|
|
42
|
+
: undefined;
|
|
43
|
+
return pages.flatMap((page) => {
|
|
44
|
+
const items = page.items.filter(
|
|
45
|
+
(item) =>
|
|
46
|
+
(commands === undefined || commands.has(item.commandName)) &&
|
|
47
|
+
item.scopes.includes(message.scope) &&
|
|
48
|
+
roleWeight[message.author.role] >=
|
|
49
|
+
roleWeight[item.permission ?? "member"]
|
|
50
|
+
);
|
|
51
|
+
return items.length > 0 ? [{ ...page, items }] : [];
|
|
52
|
+
});
|
|
53
|
+
}
|
|
23
54
|
|
|
24
55
|
export class BotNavigationRegistry {
|
|
25
56
|
private readonly pages = new Map<string, RegisteredPage>();
|
|
@@ -31,10 +62,22 @@ export class BotNavigationRegistry {
|
|
|
31
62
|
}
|
|
32
63
|
}
|
|
33
64
|
|
|
34
|
-
public forPlugin(
|
|
65
|
+
public forPlugin(
|
|
66
|
+
plugin: PluginHelpSummary,
|
|
67
|
+
/** Without it, `available` filters by scope and role only. */
|
|
68
|
+
commands?: {
|
|
69
|
+
available(message: IncomingMessage): Promise<CommandSummary[]>;
|
|
70
|
+
}
|
|
71
|
+
): NavigationRegistry {
|
|
35
72
|
return {
|
|
36
73
|
register: (page) => this.register(plugin, page),
|
|
37
|
-
list: () => this.list()
|
|
74
|
+
list: () => this.list(),
|
|
75
|
+
available: async (message) =>
|
|
76
|
+
filterNavigation(
|
|
77
|
+
this.list(),
|
|
78
|
+
await commands?.available(message),
|
|
79
|
+
message
|
|
80
|
+
)
|
|
38
81
|
};
|
|
39
82
|
}
|
|
40
83
|
|
package/src/plugin-runtime.ts
CHANGED
|
@@ -88,6 +88,24 @@ const RESERVED_CONTEXT_KEYS = new Set([
|
|
|
88
88
|
|
|
89
89
|
const MESSAGING_REQUIREMENTS = new Set(["recall", "typing", "stream"]);
|
|
90
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Plugin names the host keeps for itself.
|
|
93
|
+
*
|
|
94
|
+
* A plugin's store is the shared store under `<name>:`, and the host keeps its
|
|
95
|
+
* own records in that same store (`access:…` rules and blocklists,
|
|
96
|
+
* `adapter:…` gateway sessions and the Outbox, `scheduler:…` cron state). A
|
|
97
|
+
* plugin with one of these names would read and write host data, whatever
|
|
98
|
+
* permissions it was granted. The rest are held back for the same reason.
|
|
99
|
+
*/
|
|
100
|
+
export const RESERVED_PLUGIN_NAMES: ReadonlySet<string> = new Set([
|
|
101
|
+
"access",
|
|
102
|
+
"adapter",
|
|
103
|
+
"core",
|
|
104
|
+
"host",
|
|
105
|
+
"kernel",
|
|
106
|
+
"scheduler"
|
|
107
|
+
]);
|
|
108
|
+
|
|
91
109
|
export interface PluginSnapshot {
|
|
92
110
|
name: string;
|
|
93
111
|
version: string;
|
|
@@ -191,8 +209,10 @@ export class PluginRuntime {
|
|
|
191
209
|
};
|
|
192
210
|
const pluginHelp = this.helpSummary(plugin);
|
|
193
211
|
const commandRegistry = this.commands.forPlugin(pluginHelp);
|
|
194
|
-
const navigationRegistry: NavigationRegistry =
|
|
195
|
-
|
|
212
|
+
const navigationRegistry: NavigationRegistry = this.navigation.forPlugin(
|
|
213
|
+
pluginHelp,
|
|
214
|
+
commandRegistry
|
|
215
|
+
);
|
|
196
216
|
const middlewareRegistry = this.middleware.forPlugin(plugin.name);
|
|
197
217
|
const outboundRegistry = this.options.outbound.forPlugin(plugin.name);
|
|
198
218
|
const serviceRegistry = this.services.forPlugin(plugin.name);
|
|
@@ -231,11 +251,13 @@ export class PluginRuntime {
|
|
|
231
251
|
commands: {
|
|
232
252
|
register: (command) => track(commandRegistry.register(command)),
|
|
233
253
|
list: () => commandRegistry.list(),
|
|
254
|
+
available: (message) => commandRegistry.available(message),
|
|
234
255
|
format: (name, args) => commandRegistry.format(name, args)
|
|
235
256
|
},
|
|
236
257
|
navigation: {
|
|
237
258
|
register: (page) => track(navigationRegistry.register(page)),
|
|
238
|
-
list: () => navigationRegistry.list()
|
|
259
|
+
list: () => navigationRegistry.list(),
|
|
260
|
+
available: (message) => navigationRegistry.available(message)
|
|
239
261
|
},
|
|
240
262
|
middleware: {
|
|
241
263
|
use: (handler, options) => {
|
|
@@ -353,10 +375,18 @@ export class PluginRuntime {
|
|
|
353
375
|
}
|
|
354
376
|
|
|
355
377
|
public async unload(name: string): Promise<boolean> {
|
|
378
|
+
return this.unloadOne(name, false);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
private async unloadOne(name: string, force: boolean): Promise<boolean> {
|
|
356
382
|
const loaded = this.loaded.get(name);
|
|
357
383
|
if (!loaded) {
|
|
358
384
|
return false;
|
|
359
385
|
}
|
|
386
|
+
if (force) {
|
|
387
|
+
await this.release(name, loaded);
|
|
388
|
+
return true;
|
|
389
|
+
}
|
|
360
390
|
const dependents = [...this.loaded.values()]
|
|
361
391
|
.filter((candidate) =>
|
|
362
392
|
this.dependenciesOf(candidate.plugin).some(
|
|
@@ -370,6 +400,11 @@ export class PluginRuntime {
|
|
|
370
400
|
);
|
|
371
401
|
}
|
|
372
402
|
|
|
403
|
+
await this.release(name, loaded);
|
|
404
|
+
return true;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
private async release(name: string, loaded: LoadedPlugin): Promise<void> {
|
|
373
408
|
this.loaded.delete(name);
|
|
374
409
|
loaded.controller.abort();
|
|
375
410
|
await this.disposeAll(
|
|
@@ -377,12 +412,32 @@ export class PluginRuntime {
|
|
|
377
412
|
this.logger.child({ plugin: name })
|
|
378
413
|
);
|
|
379
414
|
this.logger.info({ plugin: name }, "plugin unloaded");
|
|
380
|
-
return true;
|
|
381
415
|
}
|
|
382
416
|
|
|
417
|
+
/**
|
|
418
|
+
* Unloads every plugin, dependents before what they depend on.
|
|
419
|
+
*
|
|
420
|
+
* Optional dependencies count for the order too, but never block: a plugin
|
|
421
|
+
* hot-loaded after its optional dependent would otherwise refuse to go, and
|
|
422
|
+
* shutdown would stop half-way with the rest still running.
|
|
423
|
+
*/
|
|
383
424
|
public async unloadAll(): Promise<void> {
|
|
384
|
-
|
|
385
|
-
|
|
425
|
+
while (this.loaded.size > 0) {
|
|
426
|
+
const names = [...this.loaded.keys()];
|
|
427
|
+
const next =
|
|
428
|
+
[...names]
|
|
429
|
+
.reverse()
|
|
430
|
+
.find(
|
|
431
|
+
(name) =>
|
|
432
|
+
!names.some(
|
|
433
|
+
(other) =>
|
|
434
|
+
other !== name &&
|
|
435
|
+
this.dependenciesOf(this.loaded.get(other)!.plugin).some(
|
|
436
|
+
(dependency) => dependency.name === name
|
|
437
|
+
)
|
|
438
|
+
)
|
|
439
|
+
) ?? names.at(-1)!;
|
|
440
|
+
await this.unloadOne(next, true);
|
|
386
441
|
}
|
|
387
442
|
}
|
|
388
443
|
|
|
@@ -494,6 +549,11 @@ export class PluginRuntime {
|
|
|
494
549
|
`invalid plugin name "${plugin.name}"; use lowercase letters, numbers, dot, underscore or dash`
|
|
495
550
|
);
|
|
496
551
|
}
|
|
552
|
+
if (RESERVED_PLUGIN_NAMES.has(plugin.name)) {
|
|
553
|
+
throw new Error(
|
|
554
|
+
`plugin name "${plugin.name}" is reserved for the host; choose another name`
|
|
555
|
+
);
|
|
556
|
+
}
|
|
497
557
|
assertPluginVersion(plugin.version, `plugin "${plugin.name}" version`);
|
|
498
558
|
const apiVersion = plugin.apiVersion ?? LEGACY_PLUGIN_API_VERSION;
|
|
499
559
|
if (
|
package/src/service-container.ts
CHANGED
|
@@ -21,6 +21,11 @@ export class ServiceContainer {
|
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
/** The service behind `token`, or undefined while nobody provides it. */
|
|
25
|
+
public lookup<T>(token: ServiceToken<T>): T | undefined {
|
|
26
|
+
return this.services.get(token.id)?.value as T | undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
24
29
|
private provide<T>(
|
|
25
30
|
plugin: string,
|
|
26
31
|
token: ServiceToken<T>,
|