@mushinjs/plugin-bots 0.3.0 → 0.4.0-next.20261124175848
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/dist/bot.d.ts +30 -4
- package/dist/bot.js +90 -11
- package/dist/bot.js.map +1 -1
- package/dist/commands.d.ts +1 -1
- package/dist/commands.js.map +1 -1
- package/dist/index.d.ts +5 -4
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/manager.d.ts +171 -11
- package/dist/manager.js +259 -21
- package/dist/manager.js.map +1 -1
- package/dist/media.d.ts +28 -0
- package/dist/media.js +91 -0
- package/dist/media.js.map +1 -0
- package/dist/types.d.ts +53 -0
- package/dist/types.js.map +1 -1
- package/package.json +4 -4
package/dist/manager.js
CHANGED
|
@@ -25,6 +25,8 @@ const LEVELS = [
|
|
|
25
25
|
const DAY = 864e5;
|
|
26
26
|
const STORAGE_KEY = "bots";
|
|
27
27
|
const BUFFER_SIZE = 500;
|
|
28
|
+
/** Plugin runs kept in memory for each bot; older ones stay in the daily files. */
|
|
29
|
+
const RUN_BUFFER_SIZE = 300;
|
|
28
30
|
const LOG_BUFFER_SIZE = 500;
|
|
29
31
|
const LOG_LEVEL_DEBUG = 3;
|
|
30
32
|
const plainExporter = {
|
|
@@ -52,7 +54,7 @@ const HELP_COMMAND = {
|
|
|
52
54
|
name: "help",
|
|
53
55
|
description: "列出所有指令"
|
|
54
56
|
};
|
|
55
|
-
const pluginInput = idInput.extend({ package: z.string().describe("
|
|
57
|
+
const pluginInput = idInput.extend({ package: z.string().describe("Plugin package name from bots.plugins.list: a bot plugin, or an app plugin with chat commands") });
|
|
56
58
|
/**
|
|
57
59
|
* Owns bot records and runs each enabled bot in its own isolated context:
|
|
58
60
|
* the `bot` service, the adapter connection and the bot's plugins live there.
|
|
@@ -78,6 +80,11 @@ var BotManager = class extends Service {
|
|
|
78
80
|
botLogSns = /* @__PURE__ */ new Set();
|
|
79
81
|
commandProviders = /* @__PURE__ */ new Set();
|
|
80
82
|
appCommands = /* @__PURE__ */ new Set();
|
|
83
|
+
/** Bot plugins turned off for the whole project; they run on no bot, whatever each bot says. */
|
|
84
|
+
projectOff = /* @__PURE__ */ new Set();
|
|
85
|
+
runs = /* @__PURE__ */ new Map();
|
|
86
|
+
/** The context that registered each adapter, to tell which installed plugin adds it. */
|
|
87
|
+
adapterFibers = /* @__PURE__ */ new Map();
|
|
81
88
|
permissionProvider;
|
|
82
89
|
constructor(ctx) {
|
|
83
90
|
super(ctx, "bots");
|
|
@@ -93,6 +100,7 @@ var BotManager = class extends Service {
|
|
|
93
100
|
async *[Service.init]() {
|
|
94
101
|
const stored = await this.home.storage.get(STORAGE_KEY);
|
|
95
102
|
this.records = stored?.bots ?? [];
|
|
103
|
+
this.projectOff = new Set(stored?.off ?? []);
|
|
96
104
|
for (const record of this.records) for (const plugin of record.plugins ?? []) plugin.package = currentPackageName(plugin.package);
|
|
97
105
|
this.logger.info("loaded %d bot(s)", this.records.length);
|
|
98
106
|
yield this.home.logger.exporter({
|
|
@@ -101,6 +109,7 @@ var BotManager = class extends Service {
|
|
|
101
109
|
export: (message) => this.captureLog(message)
|
|
102
110
|
});
|
|
103
111
|
yield* this.defineActions();
|
|
112
|
+
yield this.home.manager.defineProvides(() => this.provided());
|
|
104
113
|
this.home.inject(["model", "database"], async (ctx) => {
|
|
105
114
|
const archive = new MessageArchive(ctx, (error) => this.logger.warn("saving a message to the database failed: %s", error));
|
|
106
115
|
this.archive = archive;
|
|
@@ -124,6 +133,7 @@ var BotManager = class extends Service {
|
|
|
124
133
|
return ctx.effect(() => {
|
|
125
134
|
if (this.adapterStore.has(adapter.id)) throw new Error(`adapter "${adapter.id}" is already defined`);
|
|
126
135
|
this.adapterStore.set(adapter.id, adapter);
|
|
136
|
+
this.adapterFibers.set(adapter.id, ctx.fiber);
|
|
127
137
|
this.logger.info("adapter %s is available", adapter.id);
|
|
128
138
|
this.secureRecords(adapter).catch((error) => this.logger.warn("could not move the %s secrets of bots to .env: %s", adapter.id, error));
|
|
129
139
|
for (const record of this.records) {
|
|
@@ -133,6 +143,7 @@ var BotManager = class extends Service {
|
|
|
133
143
|
ctx.emit("bots/changed");
|
|
134
144
|
return async () => {
|
|
135
145
|
this.adapterStore.delete(adapter.id);
|
|
146
|
+
this.adapterFibers.delete(adapter.id);
|
|
136
147
|
this.logger.info("adapter %s was removed", adapter.id);
|
|
137
148
|
for (const record of this.records) {
|
|
138
149
|
if (record.adapter.type !== adapter.id || !this.runtimes.has(record.id)) continue;
|
|
@@ -152,7 +163,10 @@ var BotManager = class extends Service {
|
|
|
152
163
|
secrets: adapter.secrets ?? [],
|
|
153
164
|
segments: adapter.segments,
|
|
154
165
|
groupRoles: !!adapter.groupRoles,
|
|
155
|
-
avatars: adapter.avatars ?? {}
|
|
166
|
+
avatars: adapter.avatars ?? {},
|
|
167
|
+
hiddenFields: adapter.hiddenFields ?? {},
|
|
168
|
+
links: adapter.links ?? [],
|
|
169
|
+
...adapter.qrSetup ? { qrSetup: adapter.qrSetup } : {}
|
|
156
170
|
}));
|
|
157
171
|
}
|
|
158
172
|
/** Adapters that can send every segment in a message. */
|
|
@@ -175,7 +189,8 @@ var BotManager = class extends Service {
|
|
|
175
189
|
error: bot.error,
|
|
176
190
|
selfId: bot.selfId,
|
|
177
191
|
selfName: bot.selfName,
|
|
178
|
-
selfAvatar: bot.selfAvatar
|
|
192
|
+
selfAvatar: bot.selfAvatar,
|
|
193
|
+
invite: bot.invite
|
|
179
194
|
};
|
|
180
195
|
const error = this.startErrors.get(id);
|
|
181
196
|
return {
|
|
@@ -241,7 +256,10 @@ var BotManager = class extends Service {
|
|
|
241
256
|
return this.records.map((record) => this.describe(record));
|
|
242
257
|
}
|
|
243
258
|
async save() {
|
|
244
|
-
await this.home.storage.set(STORAGE_KEY, {
|
|
259
|
+
await this.home.storage.set(STORAGE_KEY, {
|
|
260
|
+
bots: this.records,
|
|
261
|
+
...this.projectOff.size ? { off: [...this.projectOff] } : {}
|
|
262
|
+
});
|
|
245
263
|
this.home.emit("bots/changed");
|
|
246
264
|
}
|
|
247
265
|
validateAdapter(type, config) {
|
|
@@ -316,6 +334,7 @@ var BotManager = class extends Service {
|
|
|
316
334
|
this.records = this.records.filter((item) => item !== record);
|
|
317
335
|
this.buffers.delete(id);
|
|
318
336
|
this.logs.delete(id);
|
|
337
|
+
this.runs.delete(id);
|
|
319
338
|
this.startErrors.delete(id);
|
|
320
339
|
await this.save();
|
|
321
340
|
this.logger.info("deleted bot %s", this.label(record));
|
|
@@ -342,7 +361,7 @@ var BotManager = class extends Service {
|
|
|
342
361
|
};
|
|
343
362
|
const loaded = [];
|
|
344
363
|
for (const entry of record.plugins) {
|
|
345
|
-
if (!entry.enabled) continue;
|
|
364
|
+
if (!entry.enabled || this.projectOff.has(entry.package)) continue;
|
|
346
365
|
try {
|
|
347
366
|
const plugin = await this.loadBotPlugin(entry.package);
|
|
348
367
|
const secured = await this.securePlugin(record, entry.package, plugin, entry.config ?? {}, entry.config);
|
|
@@ -393,7 +412,8 @@ var BotManager = class extends Service {
|
|
|
393
412
|
return helpText(entries, bot.record.commands);
|
|
394
413
|
},
|
|
395
414
|
roleOf: (bot, userId) => this.permissionProvider?.roleOf(bot.id, userId),
|
|
396
|
-
appCommand: (bot, entry) => this.appCommandFor(bot.id, entry)
|
|
415
|
+
appCommand: (bot, entry) => this.appCommandFor(bot.id, entry),
|
|
416
|
+
pluginRun: (bot, run, fiber) => this.recordRun(bot.id, run, fiber),
|
|
397
417
|
usageScope: (bot, message) => {
|
|
398
418
|
const entry = this.usageEntry(bot.id, message.id, true);
|
|
399
419
|
entry.bytes = byteSize(message.segments);
|
|
@@ -464,10 +484,32 @@ var BotManager = class extends Service {
|
|
|
464
484
|
if (entry.manifest.kind !== "bot") throw new MushinError("invalid_input", `"${name}" is an app plugin, not a bot plugin`);
|
|
465
485
|
return this.home.manager.loadPlugin(name);
|
|
466
486
|
}
|
|
487
|
+
/**
|
|
488
|
+
* The plugins a bot can turn on: every installed bot plugin, then each app plugin with chat commands (`kind: 'app'`),
|
|
489
|
+
* with whether it is on for this bot and whether it is turned off for the whole project.
|
|
490
|
+
*/
|
|
467
491
|
async plugins(id) {
|
|
468
492
|
const record = this.get(id);
|
|
469
|
-
const runtime = this.runtimes.get(id);
|
|
470
493
|
const graph = await this.home.manager.graph();
|
|
494
|
+
const apps = this.chatApps();
|
|
495
|
+
const appEntries = graph.catalog.filter((entry) => entry.manifest.kind === "app" && (apps.has(entry.package) || record.appPlugins?.includes(entry.package))).map((entry) => {
|
|
496
|
+
const commands = apps.get(entry.package);
|
|
497
|
+
const enabled = !!record.appPlugins?.includes(entry.package);
|
|
498
|
+
return {
|
|
499
|
+
package: entry.package,
|
|
500
|
+
kind: "app",
|
|
501
|
+
version: entry.version,
|
|
502
|
+
manifest: entry.manifest,
|
|
503
|
+
enabled,
|
|
504
|
+
...commands ? { commands } : { projectOff: true },
|
|
505
|
+
status: enabled && commands ? "active" : "disabled"
|
|
506
|
+
};
|
|
507
|
+
});
|
|
508
|
+
return [...await this.botPlugins(id, graph), ...appEntries];
|
|
509
|
+
}
|
|
510
|
+
async botPlugins(id, graph) {
|
|
511
|
+
const record = this.get(id);
|
|
512
|
+
const runtime = this.runtimes.get(id);
|
|
471
513
|
const catalog = graph.catalog.filter((entry) => entry.manifest.kind === "bot");
|
|
472
514
|
return Promise.all(catalog.map(async (entry) => {
|
|
473
515
|
const configured = record.plugins.find((item) => item.package === entry.package);
|
|
@@ -481,10 +523,13 @@ var BotManager = class extends Service {
|
|
|
481
523
|
const fiber = runtime?.plugins.get(entry.package);
|
|
482
524
|
const report = dependencyReport(graph, entry.package);
|
|
483
525
|
const missing = this.missingApps(report.requires);
|
|
484
|
-
|
|
526
|
+
const projectOff = this.projectOff.has(entry.package);
|
|
527
|
+
let status = configured?.enabled && !projectOff ? runtime ? fiberStatus(fiber) : "disabled" : "disabled";
|
|
485
528
|
if (status === "pending" && missing.length) status = "missing";
|
|
486
529
|
return {
|
|
487
530
|
package: entry.package,
|
|
531
|
+
kind: "bot",
|
|
532
|
+
...projectOff ? { projectOff } : {},
|
|
488
533
|
version: entry.version,
|
|
489
534
|
manifest: entry.manifest,
|
|
490
535
|
enabled: !!configured?.enabled,
|
|
@@ -509,8 +554,27 @@ var BotManager = class extends Service {
|
|
|
509
554
|
...dep.version ? { version: dep.version } : {}
|
|
510
555
|
}));
|
|
511
556
|
}
|
|
557
|
+
/** The bot's entry for a plugin in `plugins()`. */
|
|
558
|
+
async pluginInfo(id, name) {
|
|
559
|
+
return (await this.plugins(id)).find((item) => item.package === name);
|
|
560
|
+
}
|
|
561
|
+
/** Turns an app plugin's chat commands on or off for one bot. */
|
|
562
|
+
async setAppPlugin(id, name, enabled) {
|
|
563
|
+
const record = this.get(id);
|
|
564
|
+
const list = new Set(record.appPlugins ?? []);
|
|
565
|
+
if (enabled) list.add(name);
|
|
566
|
+
else list.delete(name);
|
|
567
|
+
record.appPlugins = [...list];
|
|
568
|
+
await this.save();
|
|
569
|
+
this.logger.info("%s the commands of %s on bot %s", enabled ? "turned on" : "turned off", name, this.label(record));
|
|
570
|
+
return this.pluginInfo(id, name);
|
|
571
|
+
}
|
|
512
572
|
async enablePlugin(id, name, config) {
|
|
513
573
|
const record = this.get(id);
|
|
574
|
+
if ((await this.home.manager.findPackage(name))?.manifest.kind === "app") {
|
|
575
|
+
if (config) throw new MushinError("invalid_input", `${name} is an app plugin: its settings are on the Plugins page (plugins.configure)`);
|
|
576
|
+
return this.setAppPlugin(id, name, true);
|
|
577
|
+
}
|
|
514
578
|
const plugin = await this.loadBotPlugin(name);
|
|
515
579
|
const missing = this.missingApps(dependencyReport(await this.home.manager.graph(), name).requires);
|
|
516
580
|
if (missing.length) throw new MushinError("unavailable", `${name} cannot run on this bot yet. ${describeMissing(missing)}; enable them on the plugins page`, { missing });
|
|
@@ -531,10 +595,11 @@ var BotManager = class extends Service {
|
|
|
531
595
|
await this.save();
|
|
532
596
|
this.logger.info("enabled plugin %s on bot %s", name, this.label(record));
|
|
533
597
|
this.reloadPlugin(id, entry, plugin);
|
|
534
|
-
return this.
|
|
598
|
+
return this.pluginInfo(id, name);
|
|
535
599
|
}
|
|
536
600
|
async disablePlugin(id, name) {
|
|
537
601
|
const record = this.get(id);
|
|
602
|
+
if ((await this.home.manager.findPackage(name))?.manifest.kind === "app" || record.appPlugins?.includes(name)) return this.setAppPlugin(id, name, false);
|
|
538
603
|
const entry = record.plugins.find((item) => item.package === name);
|
|
539
604
|
if (!entry) throw new MushinError("not_found", `${name} is not configured on this bot`);
|
|
540
605
|
entry.enabled = false;
|
|
@@ -544,10 +609,11 @@ var BotManager = class extends Service {
|
|
|
544
609
|
const fiber = runtime?.plugins.get(name);
|
|
545
610
|
runtime?.plugins.delete(name);
|
|
546
611
|
await fiber?.dispose();
|
|
547
|
-
return this.
|
|
612
|
+
return this.pluginInfo(id, name);
|
|
548
613
|
}
|
|
549
614
|
async configurePlugin(id, name, config) {
|
|
550
615
|
const record = this.get(id);
|
|
616
|
+
if ((await this.home.manager.findPackage(name))?.manifest.kind === "app") throw new MushinError("invalid_input", `${name} is an app plugin: its settings are on the Plugins page (plugins.configure), the same for every bot`);
|
|
551
617
|
const entry = record.plugins.find((item) => item.package === name);
|
|
552
618
|
if (!entry) return this.enablePlugin(id, name, config);
|
|
553
619
|
const plugin = await this.loadBotPlugin(name);
|
|
@@ -557,7 +623,7 @@ var BotManager = class extends Service {
|
|
|
557
623
|
await this.save();
|
|
558
624
|
this.logger.info("config of plugin %s on bot %s changed", name, this.label(record));
|
|
559
625
|
if (entry.enabled) this.reloadPlugin(id, entry, plugin);
|
|
560
|
-
return this.
|
|
626
|
+
return this.pluginInfo(id, name);
|
|
561
627
|
}
|
|
562
628
|
/** Puts the plain values of a bot plugin's secret fields into .env and returns the config with `${NAME}` references. */
|
|
563
629
|
async securePlugin(record, name, plugin, config, previous) {
|
|
@@ -576,11 +642,63 @@ var BotManager = class extends Service {
|
|
|
576
642
|
}
|
|
577
643
|
reloadPlugin(id, entry, plugin) {
|
|
578
644
|
const runtime = this.runtimes.get(id);
|
|
579
|
-
if (!runtime?.ctx) return;
|
|
645
|
+
if (!runtime?.ctx || this.projectOff.has(entry.package)) return;
|
|
580
646
|
runtime.plugins.get(entry.package)?.dispose();
|
|
581
647
|
runtime.pluginErrors.delete(entry.package);
|
|
582
648
|
runtime.plugins.set(entry.package, runtime.ctx.plugin(plugin, resolveEnvReferences(entry.config)));
|
|
583
649
|
}
|
|
650
|
+
/**
|
|
651
|
+
* Turns a bot plugin on or off for the whole project. Off, it runs on no bot; each bot keeps its own switch and
|
|
652
|
+
* settings, so turning it back on starts it again where it was on.
|
|
653
|
+
*/
|
|
654
|
+
async setProjectPlugin(name, enabled) {
|
|
655
|
+
const entry = await this.home.manager.findPackage(name);
|
|
656
|
+
if (!entry) throw new MushinError("not_found", `Plugin "${name}" is not installed`);
|
|
657
|
+
if (entry.manifest.kind !== "bot") throw new MushinError("invalid_input", `${name} is an app plugin: turn it on or off for the project with plugins.enable or plugins.disable`);
|
|
658
|
+
if (!enabled && entry.manifest.essential) throw new MushinError("forbidden", `${name} cannot be turned off for the whole project; turn it off on each bot instead`);
|
|
659
|
+
if (enabled === !this.projectOff.has(name)) return this.projectPlugin(name);
|
|
660
|
+
if (enabled) this.projectOff.delete(name);
|
|
661
|
+
else this.projectOff.add(name);
|
|
662
|
+
await this.save();
|
|
663
|
+
this.logger.info("%s %s for every bot", enabled ? "turned on" : "turned off", name);
|
|
664
|
+
for (const record of this.records) {
|
|
665
|
+
const item = record.plugins.find((plugin) => plugin.package === name && plugin.enabled);
|
|
666
|
+
const runtime = this.runtimes.get(record.id);
|
|
667
|
+
if (!item || !runtime) continue;
|
|
668
|
+
if (enabled) try {
|
|
669
|
+
this.reloadPlugin(record.id, item, await this.loadBotPlugin(name));
|
|
670
|
+
} catch (error) {
|
|
671
|
+
runtime.pluginErrors.set(name, error instanceof Error ? error.message : String(error));
|
|
672
|
+
}
|
|
673
|
+
else {
|
|
674
|
+
const fiber = runtime.plugins.get(name);
|
|
675
|
+
runtime.plugins.delete(name);
|
|
676
|
+
await fiber?.dispose();
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
return this.projectPlugin(name);
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* The chat plugins across the project: each bot plugin with its project switch, and each app plugin with chat
|
|
683
|
+
* commands, with the bots that have it on.
|
|
684
|
+
*/
|
|
685
|
+
async projectPlugins() {
|
|
686
|
+
const catalog = (await this.home.manager.graph()).catalog;
|
|
687
|
+
const apps = this.chatApps();
|
|
688
|
+
return catalog.filter((entry) => entry.manifest.kind === "bot" || apps.has(entry.package)).map((entry) => {
|
|
689
|
+
const bot = entry.manifest.kind === "bot";
|
|
690
|
+
return {
|
|
691
|
+
package: entry.package,
|
|
692
|
+
kind: bot ? "bot" : "app",
|
|
693
|
+
enabled: bot ? !this.projectOff.has(entry.package) : true,
|
|
694
|
+
...bot && entry.manifest.essential ? { essential: true } : {},
|
|
695
|
+
bots: this.records.filter((record) => bot ? record.plugins.some((item) => item.package === entry.package && item.enabled) : record.appPlugins?.includes(entry.package)).map((record) => record.id)
|
|
696
|
+
};
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
async projectPlugin(name) {
|
|
700
|
+
return (await this.projectPlugins()).find((item) => item.package === name);
|
|
701
|
+
}
|
|
584
702
|
/** Adds commands to every bot's list. Removed when the calling plugin unloads. */
|
|
585
703
|
defineCommands(provider) {
|
|
586
704
|
return this.ctx.effect(() => {
|
|
@@ -589,14 +707,17 @@ var BotManager = class extends Service {
|
|
|
589
707
|
}, "bots.defineCommands()");
|
|
590
708
|
}
|
|
591
709
|
/**
|
|
592
|
-
* Answers a command
|
|
593
|
-
*
|
|
710
|
+
* Answers a command from an app plugin, such as `/persona`, on the bots the user turned the plugin on for (on the
|
|
711
|
+
* bot's Plugins page, or with `bots.plugins.enable`). A plugin that decides itself which bots answer, from its own
|
|
712
|
+
* per-bot settings, passes `bots`, and then has no switch on the bot's page. It answers after flows, like `/help`,
|
|
713
|
+
* and shows in the command list of each bot it answers on.
|
|
594
714
|
*/
|
|
595
715
|
defineCommand(def, handler, options = {}) {
|
|
596
716
|
const ctx = this.ctx;
|
|
597
717
|
const command = {
|
|
598
718
|
def,
|
|
599
719
|
plugin: ctx.fiber.name,
|
|
720
|
+
fiber: ctx.fiber,
|
|
600
721
|
handler,
|
|
601
722
|
bots: options.bots
|
|
602
723
|
};
|
|
@@ -608,7 +729,26 @@ var BotManager = class extends Service {
|
|
|
608
729
|
/** The app plugin command behind a command entry, if an app plugin answers it. */
|
|
609
730
|
appCommandFor(botId, entry) {
|
|
610
731
|
if (entry.source.kind !== "plugin") return;
|
|
611
|
-
return [...this.appCommands].find((item) => item.plugin === entry.source.id && commandMatches(item.def, entry.name) && (item
|
|
732
|
+
return [...this.appCommands].find((item) => item.plugin === entry.source.id && commandMatches(item.def, entry.name) && this.answersOn(item, botId));
|
|
733
|
+
}
|
|
734
|
+
/** Whether an app plugin's command answers on a bot: the plugin's own choice, or the bot's switch for the plugin. */
|
|
735
|
+
answersOn(command, botId) {
|
|
736
|
+
if (command.bots) return command.bots(botId);
|
|
737
|
+
const name = this.home.manager.packageOf(command.fiber);
|
|
738
|
+
return !!name && !!this.records.find((record) => record.id === botId)?.appPlugins?.includes(name);
|
|
739
|
+
}
|
|
740
|
+
/** App plugins whose commands have a switch on each bot, with the names of those commands, by package. */
|
|
741
|
+
chatApps() {
|
|
742
|
+
const apps = /* @__PURE__ */ new Map();
|
|
743
|
+
for (const command of this.appCommands) {
|
|
744
|
+
if (command.bots) continue;
|
|
745
|
+
const name = this.home.manager.packageOf(command.fiber);
|
|
746
|
+
if (!name) continue;
|
|
747
|
+
const list = apps.get(name) ?? [];
|
|
748
|
+
if (!list.includes(command.def.name)) list.push(command.def.name);
|
|
749
|
+
apps.set(name, list);
|
|
750
|
+
}
|
|
751
|
+
return apps;
|
|
612
752
|
}
|
|
613
753
|
/** Lets the permissions plugin say who is master, admin or user on each bot. */
|
|
614
754
|
usePermissions(provider) {
|
|
@@ -635,7 +775,7 @@ var BotManager = class extends Service {
|
|
|
635
775
|
}
|
|
636
776
|
entries.push(...this.runtimes.get(botId)?.bot?.localCommands() ?? []);
|
|
637
777
|
for (const command of this.appCommands) {
|
|
638
|
-
if (
|
|
778
|
+
if (!this.answersOn(command, botId)) continue;
|
|
639
779
|
entries.push({
|
|
640
780
|
...command.def,
|
|
641
781
|
botId,
|
|
@@ -729,6 +869,75 @@ var BotManager = class extends Service {
|
|
|
729
869
|
replies: replies.map((reply) => segmentsToText(reply.segments))
|
|
730
870
|
};
|
|
731
871
|
}
|
|
872
|
+
/** The installed plugin a context of a bot belongs to: one of the bot's plugins, else an app plugin. */
|
|
873
|
+
packageIn(botId, fiber) {
|
|
874
|
+
if (!fiber) return;
|
|
875
|
+
const runtime = this.runtimes.get(botId);
|
|
876
|
+
if (runtime) {
|
|
877
|
+
const owners = /* @__PURE__ */ new Map();
|
|
878
|
+
for (const [name, plugin] of runtime.plugins) owners.set(plugin.ctx.fiber, name);
|
|
879
|
+
for (let current = fiber; current; current = current.parent?.fiber === current ? void 0 : current.parent?.fiber) {
|
|
880
|
+
const name = owners.get(current);
|
|
881
|
+
if (name) return name;
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
return this.home.manager.packageOf(fiber);
|
|
885
|
+
}
|
|
886
|
+
recordRun(botId, report, fiber) {
|
|
887
|
+
const run = {
|
|
888
|
+
id: randomId(8),
|
|
889
|
+
botId,
|
|
890
|
+
...report,
|
|
891
|
+
plugin: this.packageIn(botId, fiber) ?? report.plugin
|
|
892
|
+
};
|
|
893
|
+
let buffer = this.runs.get(botId);
|
|
894
|
+
if (!buffer) this.runs.set(botId, buffer = []);
|
|
895
|
+
buffer.push(run);
|
|
896
|
+
if (buffer.length > RUN_BUFFER_SIZE) buffer.splice(0, buffer.length - RUN_BUFFER_SIZE);
|
|
897
|
+
const day = new Date(run.startedAt).toISOString().slice(0, 10);
|
|
898
|
+
this.home.storage.append(`runs/${botId}/${day}`, run).catch(() => {});
|
|
899
|
+
this.home.emit("bots/run", run);
|
|
900
|
+
}
|
|
901
|
+
/**
|
|
902
|
+
* What plugins did with a bot's messages, newest first: each message a plugin replied to, kept from the plugins
|
|
903
|
+
* after it, or failed on. Flow runs are in `flows.history`.
|
|
904
|
+
*/
|
|
905
|
+
async pluginRuns(id, query = {}) {
|
|
906
|
+
this.get(id);
|
|
907
|
+
let buffer = this.runs.get(id);
|
|
908
|
+
if (!buffer) {
|
|
909
|
+
const day = (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
|
|
910
|
+
const stored = await this.home.storage.tail(`runs/${id}/${day}`, RUN_BUFFER_SIZE);
|
|
911
|
+
const current = this.runs.get(id) ?? [];
|
|
912
|
+
buffer = [...stored.filter((run) => !current.some((item) => item.id === run.id)), ...current].slice(-300);
|
|
913
|
+
this.runs.set(id, buffer);
|
|
914
|
+
}
|
|
915
|
+
const limit = query.limit ?? 50;
|
|
916
|
+
const matching = buffer.filter((run) => (query.before === void 0 || run.startedAt < query.before) && (!query.plugin || run.plugin === query.plugin) && (!query.status || run.status === query.status));
|
|
917
|
+
return {
|
|
918
|
+
runs: matching.slice(-limit).reverse(),
|
|
919
|
+
more: matching.length > limit
|
|
920
|
+
};
|
|
921
|
+
}
|
|
922
|
+
/** What this plugin knows of what others add: chat commands (app plugins' and bot plugins'), and adapters. */
|
|
923
|
+
*provided() {
|
|
924
|
+
for (const command of this.appCommands) yield {
|
|
925
|
+
kind: "command",
|
|
926
|
+
name: command.def.name,
|
|
927
|
+
fiber: command.fiber
|
|
928
|
+
};
|
|
929
|
+
for (const [id, runtime] of this.runtimes) for (const command of runtime.bot?.ownCommandList() ?? []) yield {
|
|
930
|
+
kind: "command",
|
|
931
|
+
name: command.name,
|
|
932
|
+
package: this.packageIn(id, command.fiber),
|
|
933
|
+
fiber: command.fiber
|
|
934
|
+
};
|
|
935
|
+
for (const [id, fiber] of this.adapterFibers) yield {
|
|
936
|
+
kind: "adapter",
|
|
937
|
+
name: id,
|
|
938
|
+
fiber
|
|
939
|
+
};
|
|
940
|
+
}
|
|
732
941
|
/** Finds the bot whose context wrote a log message, by walking up from the writing fiber. */
|
|
733
942
|
botOf(message) {
|
|
734
943
|
let fiber = message.fiber?.deref();
|
|
@@ -1043,8 +1252,8 @@ var BotManager = class extends Service {
|
|
|
1043
1252
|
group: z.string().optional().describe("Group id, with target \"group\""),
|
|
1044
1253
|
user: z.string().optional().describe("Run as this user id instead of the first master")
|
|
1045
1254
|
}),
|
|
1046
|
-
handler: (input, caller) => this.runCommand(input, caller.kind === "
|
|
1047
|
-
kind: "
|
|
1255
|
+
handler: (input, caller) => this.runCommand(input, caller.kind === "fairy" && caller.session ? {
|
|
1256
|
+
kind: "fairy",
|
|
1048
1257
|
session: caller.session
|
|
1049
1258
|
} : void 0)
|
|
1050
1259
|
});
|
|
@@ -1071,21 +1280,21 @@ var BotManager = class extends Service {
|
|
|
1071
1280
|
});
|
|
1072
1281
|
yield actions.define({
|
|
1073
1282
|
name: "bots.plugins.list",
|
|
1074
|
-
description: "List
|
|
1283
|
+
description: "List the plugins a bot can turn on, with whether each is on for this bot: every installed bot plugin (kind bot, with its config and status), then each app plugin that adds chat commands (kind app, with its commands). projectOff means it is turned off for the whole project, so it does not run even where it is on.",
|
|
1075
1284
|
risk: "read",
|
|
1076
1285
|
input: idInput,
|
|
1077
1286
|
handler: ({ id }) => this.plugins(id)
|
|
1078
1287
|
});
|
|
1079
1288
|
yield actions.define({
|
|
1080
1289
|
name: "bots.plugins.enable",
|
|
1081
|
-
description: "
|
|
1290
|
+
description: "Turn a plugin on for one bot: a bot plugin, optionally with config, or an app plugin's chat commands (an app plugin's commands answer only on the bots it is turned on for).",
|
|
1082
1291
|
risk: "write",
|
|
1083
1292
|
input: pluginInput.extend({ config: z.record(z.string(), z.any()).optional() }),
|
|
1084
1293
|
handler: ({ id, package: name, config }) => this.enablePlugin(id, name, config)
|
|
1085
1294
|
});
|
|
1086
1295
|
yield actions.define({
|
|
1087
1296
|
name: "bots.plugins.disable",
|
|
1088
|
-
description: "
|
|
1297
|
+
description: "Turn a bot plugin, or an app plugin's chat commands, off for one bot. A bot plugin's config is kept.",
|
|
1089
1298
|
risk: "write",
|
|
1090
1299
|
input: pluginInput,
|
|
1091
1300
|
handler: ({ id, package: name }) => this.disablePlugin(id, name)
|
|
@@ -1097,6 +1306,35 @@ var BotManager = class extends Service {
|
|
|
1097
1306
|
input: pluginInput.extend({ config: z.record(z.string(), z.any()) }),
|
|
1098
1307
|
handler: ({ id, package: name, config }) => this.configurePlugin(id, name, config)
|
|
1099
1308
|
});
|
|
1309
|
+
yield actions.define({
|
|
1310
|
+
name: "bots.plugins.project",
|
|
1311
|
+
description: "The chat plugins across the project: each bot plugin with its project switch (off, it runs on no bot), and each app plugin that adds chat commands. Lists the bots that have each one on.",
|
|
1312
|
+
risk: "read",
|
|
1313
|
+
input: z.object({}),
|
|
1314
|
+
handler: () => this.projectPlugins()
|
|
1315
|
+
});
|
|
1316
|
+
yield actions.define({
|
|
1317
|
+
name: "bots.plugins.setProject",
|
|
1318
|
+
description: "Turn a bot plugin on or off for the whole project. Off, it runs on no bot; each bot keeps its own switch and settings. App plugins are turned on and off with plugins.enable and plugins.disable.",
|
|
1319
|
+
risk: "write",
|
|
1320
|
+
input: z.object({
|
|
1321
|
+
package: z.string().describe("Bot plugin package name"),
|
|
1322
|
+
enabled: z.boolean()
|
|
1323
|
+
}),
|
|
1324
|
+
handler: ({ package: name, enabled }) => this.setProjectPlugin(name, enabled)
|
|
1325
|
+
});
|
|
1326
|
+
yield actions.define({
|
|
1327
|
+
name: "bots.runs",
|
|
1328
|
+
description: "What plugins did with a bot's messages, newest first: each message a bot plugin or an app plugin's command replied to (replies), kept from the plugins after it (kept), or failed on (status error, with the error). Flow runs are in flows.history. The message field is the record id, for bots.logs and bots.usage.",
|
|
1329
|
+
risk: "read",
|
|
1330
|
+
input: idInput.extend({
|
|
1331
|
+
limit: z.number().int().min(1).max(RUN_BUFFER_SIZE).optional(),
|
|
1332
|
+
before: z.number().optional().describe("Only runs started before this time (ms since 1970), to page back"),
|
|
1333
|
+
plugin: z.string().optional().describe("Only this plugin's runs: its package name"),
|
|
1334
|
+
status: z.enum(["ok", "error"]).optional()
|
|
1335
|
+
}),
|
|
1336
|
+
handler: ({ id, ...query }) => this.pluginRuns(id, query)
|
|
1337
|
+
});
|
|
1100
1338
|
yield actions.define({
|
|
1101
1339
|
name: "bots.messages",
|
|
1102
1340
|
description: "Recent messages of a bot, oldest first, with type, supported platforms and source. Without a channel or `before`, the last 500 the bot saw.",
|