@mushinjs/plugin-bots 0.0.0-stage → 0.2.0-next.20261113103537

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.
@@ -0,0 +1,458 @@
1
+ import { AdapterType, BotConnectionInfo, BotPluginEntry, BotRecord, BotRole, BotStatus, Contact, ForwardedMessage, PermissionProvider, ResolvedMedia } from "./types.js";
2
+ import { ArgValue, CommandArg, CommandDef, CommandEntry, CommandSettings, CommandSource } from "./commands.js";
3
+ import { Bot, Session } from "./bot.js";
4
+ import { ChannelInfo, Context, LogTag, Message, MessageRecord, MissingDependency, Plugin, Provenance, Segment, Service, UsageEvent } from "@mushinjs/core";
5
+ declare module '@mushinjs/core' {
6
+ interface Context {
7
+ bots: BotManager;
8
+ bot: Bot;
9
+ }
10
+ interface Events {
11
+ 'bots/changed'(): void;
12
+ 'bots/status'(status: BotStatusInfo): void;
13
+ 'bots/message'(record: MessageRecord): void;
14
+ 'bots/log'(line: BotLogLine): void;
15
+ 'bots/command'(call: CommandCall): void;
16
+ }
17
+ }
18
+ /** One log line written inside a bot's context: its adapter, its plugins, or the bot service itself. */
19
+ export interface BotLogLine {
20
+ botId: string;
21
+ sn: number;
22
+ ts: number;
23
+ type: string;
24
+ name: string;
25
+ tag: LogTag;
26
+ text: string;
27
+ }
28
+ /** A message ran a registered command. */
29
+ export interface CommandCall {
30
+ botId: string;
31
+ name: string;
32
+ source: CommandEntry['source'];
33
+ userId: string;
34
+ channel: ChannelInfo;
35
+ at: number;
36
+ }
37
+ export interface RunCommandInput {
38
+ bot: string;
39
+ command: string;
40
+ target?: 'master' | 'group';
41
+ group?: string;
42
+ user?: string;
43
+ }
44
+ /** Lists commands a plugin provides on a bot, e.g. the command nodes of the flows attached to it. */
45
+ export type CommandProvider = (botId: string) => CommandEntry[];
46
+ /** A command an app plugin answers on every bot, or on the bots `bots` picks. */
47
+ interface AppCommand {
48
+ def: CommandDef;
49
+ plugin: string;
50
+ handler: (session: Session) => Promise<unknown> | unknown;
51
+ bots?: (botId: string) => boolean;
52
+ }
53
+ export interface BotStatusInfo {
54
+ id: string;
55
+ status: BotStatus;
56
+ error?: string;
57
+ selfId?: string;
58
+ selfName?: string;
59
+ selfAvatar?: string;
60
+ }
61
+ /** What handling one message cost; see `bots.usage`. */
62
+ export interface MessageUsage {
63
+ events: UsageEvent[];
64
+ /** Size on the wire, roughly. */
65
+ bytes?: number;
66
+ /** For a sent message: how long the platform took to accept it. */
67
+ sendMs?: number;
68
+ /** For a sent message: the incoming message being handled when it was sent. */
69
+ cause?: string;
70
+ /** For an incoming message: what was sent while handling it. */
71
+ replies: string[];
72
+ }
73
+ /**
74
+ * Owns bot records and runs each enabled bot in its own isolated context:
75
+ * the `bot` service, the adapter connection and the bot's plugins live there.
76
+ */
77
+ export declare class BotManager extends Service {
78
+ static inject: string[];
79
+ private home;
80
+ private records;
81
+ private adapterStore;
82
+ private runtimes;
83
+ private startErrors;
84
+ private buffers;
85
+ /** Messages in the database, when the database plugin runs. */
86
+ private archive?;
87
+ /** Runtime fiber of each running bot, to attribute log lines to the bot that wrote them. */
88
+ private runtimeFibers;
89
+ private logs;
90
+ /** Sequence numbers of recent bot log lines; still recognized after the bot that wrote them stops. */
91
+ private botLogSns;
92
+ private commandProviders;
93
+ private appCommands;
94
+ private permissionProvider?;
95
+ constructor(ctx: Context);
96
+ /**
97
+ * Bot lifecycle lines for the global log, tagged `bot` in the dashboard. Written from the manager's
98
+ * own context, so they stay out of the bot's log, which has its own lines from inside the bot.
99
+ */
100
+ private get logger();
101
+ [Service.init](): AsyncGenerator<import("cordis").Disposable<Promise<void>>, void, unknown>;
102
+ /** Registers an adapter type. Bots using it start when it loads and stop when it unloads. */
103
+ defineAdapter<C>(adapter: AdapterType<C>): import("cordis").Disposable<Promise<void>>;
104
+ adapters(): {
105
+ id: string;
106
+ displayName: import("@mushinjs/core").LocalizedText;
107
+ configSchema: import("@mushinjs/core").JsonSchema;
108
+ secrets: string[];
109
+ segments: string[];
110
+ groupRoles: boolean;
111
+ avatars: {
112
+ user?: string;
113
+ group?: string;
114
+ };
115
+ }[];
116
+ /** Adapters that can send every segment in a message. */
117
+ platformsFor(segments: Segment[]): string[];
118
+ get(id: string): BotRecord;
119
+ bot(id: string): Bot | undefined;
120
+ status(id: string): BotStatusInfo;
121
+ private emitStatus;
122
+ private redact;
123
+ describe(record: BotRecord): {
124
+ connection: BotConnectionInfo | undefined;
125
+ id: string;
126
+ status: BotStatus;
127
+ error?: string;
128
+ selfId?: string;
129
+ selfName?: string;
130
+ selfAvatar?: string;
131
+ adapter: {
132
+ config: {
133
+ [x: string]: any;
134
+ };
135
+ type: string;
136
+ };
137
+ name: string;
138
+ enabled: boolean;
139
+ plugins: BotPluginEntry[];
140
+ provider?: string;
141
+ commands?: CommandSettings;
142
+ createdAt: number;
143
+ };
144
+ private connectionOf;
145
+ list(): {
146
+ connection: BotConnectionInfo | undefined;
147
+ id: string;
148
+ status: BotStatus;
149
+ error?: string;
150
+ selfId?: string;
151
+ selfName?: string;
152
+ selfAvatar?: string;
153
+ adapter: {
154
+ config: {
155
+ [x: string]: any;
156
+ };
157
+ type: string;
158
+ };
159
+ name: string;
160
+ enabled: boolean;
161
+ plugins: BotPluginEntry[];
162
+ provider?: string;
163
+ commands?: CommandSettings;
164
+ createdAt: number;
165
+ }[];
166
+ private save;
167
+ private validateAdapter;
168
+ create(input: {
169
+ name: string;
170
+ adapter: {
171
+ type: string;
172
+ config: Record<string, any>;
173
+ };
174
+ plugins?: {
175
+ package: string;
176
+ config?: Record<string, any>;
177
+ }[];
178
+ enabled?: boolean;
179
+ provider?: string;
180
+ commands?: CommandSettings;
181
+ }): Promise<{
182
+ connection: BotConnectionInfo | undefined;
183
+ id: string;
184
+ status: BotStatus;
185
+ error?: string;
186
+ selfId?: string;
187
+ selfName?: string;
188
+ selfAvatar?: string;
189
+ adapter: {
190
+ config: {
191
+ [x: string]: any;
192
+ };
193
+ type: string;
194
+ };
195
+ name: string;
196
+ enabled: boolean;
197
+ plugins: BotPluginEntry[];
198
+ provider?: string;
199
+ commands?: CommandSettings;
200
+ createdAt: number;
201
+ }>;
202
+ update(id: string, patch: {
203
+ name?: string;
204
+ adapter?: {
205
+ type: string;
206
+ config: Record<string, any>;
207
+ };
208
+ provider?: string | null;
209
+ commands?: CommandSettings;
210
+ }): Promise<{
211
+ connection: BotConnectionInfo | undefined;
212
+ id: string;
213
+ status: BotStatus;
214
+ error?: string;
215
+ selfId?: string;
216
+ selfName?: string;
217
+ selfAvatar?: string;
218
+ adapter: {
219
+ config: {
220
+ [x: string]: any;
221
+ };
222
+ type: string;
223
+ };
224
+ name: string;
225
+ enabled: boolean;
226
+ plugins: BotPluginEntry[];
227
+ provider?: string;
228
+ commands?: CommandSettings;
229
+ createdAt: number;
230
+ }>;
231
+ remove(id: string): Promise<{
232
+ deleted: string;
233
+ }>;
234
+ private startSafely;
235
+ start(id: string): Promise<BotStatusInfo>;
236
+ private stopRuntime;
237
+ setEnabled(id: string, enabled: boolean): Promise<BotStatusInfo>;
238
+ /** Log lines name a bot by its id; the dashboard links it to the bot. */
239
+ private label;
240
+ loadBotPlugin(name: string): Promise<Plugin>;
241
+ plugins(id: string): Promise<{
242
+ missing?: MissingDependency[] | undefined;
243
+ package: string;
244
+ version: string;
245
+ manifest: import("@mushinjs/core").PluginManifest;
246
+ enabled: boolean;
247
+ config: Record<string, any>;
248
+ configSchema: import("@mushinjs/core").JsonSchema | undefined;
249
+ status: string;
250
+ error: string | undefined;
251
+ dependencies: {
252
+ requires: import("@mushinjs/core").DependencyState[];
253
+ optional: import("@mushinjs/core").DependencyState[];
254
+ };
255
+ }[]>;
256
+ /** Required app plugins that are not installed, do not fit or are disabled. */
257
+ private missingApps;
258
+ enablePlugin(id: string, name: string, config?: Record<string, any>): Promise<{
259
+ missing?: MissingDependency[] | undefined;
260
+ package: string;
261
+ version: string;
262
+ manifest: import("@mushinjs/core").PluginManifest;
263
+ enabled: boolean;
264
+ config: Record<string, any>;
265
+ configSchema: import("@mushinjs/core").JsonSchema | undefined;
266
+ status: string;
267
+ error: string | undefined;
268
+ dependencies: {
269
+ requires: import("@mushinjs/core").DependencyState[];
270
+ optional: import("@mushinjs/core").DependencyState[];
271
+ };
272
+ } | undefined>;
273
+ disablePlugin(id: string, name: string): Promise<{
274
+ missing?: MissingDependency[] | undefined;
275
+ package: string;
276
+ version: string;
277
+ manifest: import("@mushinjs/core").PluginManifest;
278
+ enabled: boolean;
279
+ config: Record<string, any>;
280
+ configSchema: import("@mushinjs/core").JsonSchema | undefined;
281
+ status: string;
282
+ error: string | undefined;
283
+ dependencies: {
284
+ requires: import("@mushinjs/core").DependencyState[];
285
+ optional: import("@mushinjs/core").DependencyState[];
286
+ };
287
+ } | undefined>;
288
+ configurePlugin(id: string, name: string, config: Record<string, any>): Promise<{
289
+ missing?: MissingDependency[] | undefined;
290
+ package: string;
291
+ version: string;
292
+ manifest: import("@mushinjs/core").PluginManifest;
293
+ enabled: boolean;
294
+ config: Record<string, any>;
295
+ configSchema: import("@mushinjs/core").JsonSchema | undefined;
296
+ status: string;
297
+ error: string | undefined;
298
+ dependencies: {
299
+ requires: import("@mushinjs/core").DependencyState[];
300
+ optional: import("@mushinjs/core").DependencyState[];
301
+ };
302
+ } | undefined>;
303
+ private reloadPlugin;
304
+ /** Adds commands to every bot's list. Removed when the calling plugin unloads. */
305
+ defineCommands(provider: CommandProvider): import("cordis").Disposable<Promise<void>>;
306
+ /**
307
+ * Answers a command on every bot (or those `bots` picks) from an app plugin, such as `/persona`.
308
+ * It answers after flows, like `/help`, and shows in each bot's command list.
309
+ */
310
+ defineCommand(def: CommandDef, handler: AppCommand['handler'], options?: {
311
+ bots?: (botId: string) => boolean;
312
+ }): import("cordis").Disposable<Promise<void>>;
313
+ /** The app plugin command behind a command entry, if an app plugin answers it. */
314
+ appCommandFor(botId: string, entry: CommandEntry): AppCommand | undefined;
315
+ /** Lets the permissions plugin say who is master, admin or user on each bot. */
316
+ usePermissions(provider: PermissionProvider): import("cordis").Disposable<Promise<void>>;
317
+ /** A user's role on a bot; nothing when the permissions plugin is not running. */
318
+ roleOf(botId: string, userId: string): BotRole | undefined;
319
+ /** Commands registered on one bot: from command providers such as flows, its plugins, and the built-in help. */
320
+ commands(botId: string): CommandEntry[];
321
+ /** Commands of one bot or of every bot, with names that more than one source registers. */
322
+ commandList(botId?: string): {
323
+ commands: CommandEntry[];
324
+ conflicts: {
325
+ botId: string;
326
+ name: string;
327
+ sources: CommandSource[];
328
+ }[];
329
+ };
330
+ /**
331
+ * Runs a command as if a master sent it. With a target it really runs on the bot: in that group,
332
+ * or in the master's private chat, and the replies are sent. Without one it only previews where
333
+ * the message would go (through flows), so nothing is sent.
334
+ */
335
+ runCommand(input: RunCommandInput, source?: Provenance): Promise<{
336
+ simulated: boolean;
337
+ text: string;
338
+ route: unknown;
339
+ channel?: undefined;
340
+ user?: undefined;
341
+ replies?: undefined;
342
+ } | {
343
+ simulated: boolean;
344
+ text: string;
345
+ channel: ChannelInfo;
346
+ user: string;
347
+ replies: string[];
348
+ route?: undefined;
349
+ }>;
350
+ /** Finds the bot whose context wrote a log message, by walking up from the writing fiber. */
351
+ private botOf;
352
+ /** True when a log line was written inside a bot; it belongs in that bot's log, not the global one. */
353
+ isBotLog(message: Message): boolean;
354
+ private captureLog;
355
+ logLines(id: string, limit?: number): Promise<BotLogLine[]>;
356
+ /** Messages recorded since Mushin started, for the live conversation. */
357
+ private recent;
358
+ /** What handling each recent message cost, in memory only; see `bots.usage`. */
359
+ private usages;
360
+ private usageEntry;
361
+ /** A message's costs, and for a reply the costs of the message it answered. */
362
+ usage(botId: string, messageId: string): {
363
+ causeEvents?: UsageEvent[] | undefined;
364
+ events: UsageEvent[];
365
+ /** Size on the wire, roughly. */
366
+ bytes?: number;
367
+ /** For a sent message: how long the platform took to accept it. */
368
+ sendMs?: number;
369
+ /** For a sent message: the incoming message being handled when it was sent. */
370
+ cause?: string;
371
+ /** For an incoming message: what was sent while handling it. */
372
+ replies: string[];
373
+ } | null;
374
+ private record;
375
+ /**
376
+ * Recent messages of a bot, oldest first. With the database plugin, a channel's history and
377
+ * older pages (`before`) come from the database; otherwise from today's log and memory.
378
+ */
379
+ messages(id: string, options?: {
380
+ limit?: number;
381
+ channel?: string;
382
+ before?: number;
383
+ sinceStart?: boolean;
384
+ }): Promise<MessageRecord[]>;
385
+ /** Channels seen recently, newest first, for the live view's channel list. */
386
+ channels(id: string, options?: {
387
+ sinceStart?: boolean;
388
+ }): Promise<(ChannelInfo & {
389
+ lastAt: number;
390
+ lastText: string;
391
+ })[]>;
392
+ /** How a command reads a message; see the `bots.tryCommand` action. */
393
+ tryCommand(input: {
394
+ name: string;
395
+ aliases?: string[];
396
+ prefixes?: string[];
397
+ args: CommandArg[];
398
+ text: string;
399
+ bot?: string;
400
+ }): {
401
+ matched: false;
402
+ reason: "prefix";
403
+ prefixes: string[];
404
+ usage: string;
405
+ } | {
406
+ matched: false;
407
+ reason: "name";
408
+ prefixes: string[];
409
+ usage: string;
410
+ } | {
411
+ ok: true;
412
+ values: Record<string, ArgValue>;
413
+ matched: true;
414
+ prefix: string;
415
+ rest: string;
416
+ usage: string;
417
+ reason?: undefined;
418
+ prefixes?: undefined;
419
+ } | {
420
+ ok: false;
421
+ error: string;
422
+ arg?: string;
423
+ matched: true;
424
+ prefix: string;
425
+ rest: string;
426
+ usage: string;
427
+ reason?: undefined;
428
+ prefixes?: undefined;
429
+ };
430
+ /**
431
+ * People or groups the bots have seen recently, newest first, as `{ value, label, group }`
432
+ * choices: the label is `name (id)`, the group the bot's name. For lists such as a permission
433
+ * node's allow and block lists.
434
+ */
435
+ seen(kind: 'user' | 'group'): Promise<{
436
+ value: string;
437
+ label: string;
438
+ group: string;
439
+ }[]>;
440
+ /** The account's friends and groups, from the platform; empty when the bot is offline or the adapter cannot tell. */
441
+ contacts(id: string): Promise<Contact[]>;
442
+ /** The sandbox's test user writes to the bot; see `Bot.sandbox`. */
443
+ sandbox(id: string, text: string): {
444
+ ok: boolean;
445
+ };
446
+ sendManual(id: string, channel: ChannelInfo, text: string): Promise<MessageRecord | undefined>;
447
+ /** A fresh address of a message's image, video or file, from the platform. */
448
+ media(id: string, segment: Segment, options: {
449
+ channel?: ChannelInfo;
450
+ inline?: boolean;
451
+ }): Promise<ResolvedMedia>;
452
+ /** The messages of a forwarded chat history, from the platform. */
453
+ forwarded(id: string, forward: string): Promise<{
454
+ messages: ForwardedMessage[];
455
+ }>;
456
+ private defineActions;
457
+ }
458
+ export {};