@asabot/core 0.1.0 → 0.2.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/CHANGELOG.md +17 -0
- package/README.md +78 -13
- package/asabot.config.example.yaml +47 -29
- package/dist/adapters/feishu/adapter.d.ts +19 -0
- package/dist/adapters/feishu/adapter.js +621 -0
- package/dist/adapters/feishu/api.d.ts +142 -0
- package/dist/adapters/feishu/api.js +306 -0
- package/dist/adapters/feishu/bot.d.ts +95 -0
- package/dist/adapters/feishu/bot.js +539 -0
- package/dist/adapters/feishu/cards.d.ts +93 -0
- package/dist/adapters/feishu/cards.js +106 -0
- package/dist/adapters/feishu/config.d.ts +13 -0
- package/dist/adapters/feishu/config.js +36 -0
- package/dist/adapters/feishu/events.d.ts +33 -0
- package/dist/adapters/feishu/events.js +14 -0
- package/dist/adapters/feishu/index.d.ts +8 -0
- package/dist/adapters/feishu/index.js +5 -0
- package/dist/adapters/feishu/message.d.ts +64 -0
- package/dist/adapters/feishu/message.js +197 -0
- package/dist/adapters/feishu/transport.d.ts +34 -0
- package/dist/adapters/feishu/transport.js +110 -0
- package/dist/adapters/index.d.ts +1 -7
- package/dist/adapters/index.js +75 -6
- package/dist/adapters/loader.d.ts +6 -0
- package/dist/adapters/loader.js +73 -0
- package/dist/adapters/media-source.d.ts +7 -0
- package/dist/adapters/media-source.js +137 -0
- package/dist/adapters/onebot11/adapter.d.ts +5 -0
- package/dist/adapters/onebot11/adapter.js +23 -11
- package/dist/adapters/onebot11/api.d.ts +4 -1
- package/dist/adapters/onebot11/api.js +21 -8
- package/dist/adapters/onebot11/errors.d.ts +3 -0
- package/dist/adapters/onebot11/errors.js +6 -0
- package/dist/adapters/virtual/index.js +7 -1
- package/dist/adapters/wecom/adapter.d.ts +14 -0
- package/dist/adapters/wecom/adapter.js +220 -0
- package/dist/adapters/wecom/bot.d.ts +71 -0
- package/dist/adapters/wecom/bot.js +312 -0
- package/dist/adapters/wecom/cards.d.ts +37 -0
- package/dist/adapters/wecom/cards.js +30 -0
- package/dist/adapters/wecom/config.d.ts +8 -0
- package/dist/adapters/wecom/config.js +10 -0
- package/dist/adapters/wecom/events.d.ts +93 -0
- package/dist/adapters/wecom/events.js +46 -0
- package/dist/adapters/wecom/index.d.ts +7 -0
- package/dist/adapters/wecom/index.js +21 -0
- package/dist/adapters/wecom/media.d.ts +50 -0
- package/dist/adapters/wecom/media.js +149 -0
- package/dist/adapters/wecom/message.d.ts +151 -0
- package/dist/adapters/wecom/message.js +116 -0
- package/dist/adapters/wecom/transport.d.ts +39 -0
- package/dist/adapters/wecom/transport.js +116 -0
- package/dist/adapters/weixin-clawbot/adapter.d.ts +16 -0
- package/dist/adapters/weixin-clawbot/adapter.js +173 -0
- package/dist/adapters/weixin-clawbot/bot.d.ts +41 -0
- package/dist/adapters/weixin-clawbot/bot.js +254 -0
- package/dist/adapters/weixin-clawbot/index.d.ts +2 -0
- package/dist/adapters/weixin-clawbot/index.js +19 -0
- package/dist/adapters/weixin-clawbot/media.d.ts +57 -0
- package/dist/adapters/weixin-clawbot/media.js +196 -0
- package/dist/adapters/weixin-clawbot/message.d.ts +12 -0
- package/dist/adapters/weixin-clawbot/message.js +79 -0
- package/dist/adapters/weixin-clawbot/protocol.d.ts +145 -0
- package/dist/adapters/weixin-clawbot/protocol.js +178 -0
- package/dist/adapters/weixin-clawbot/setup.d.ts +2 -0
- package/dist/adapters/weixin-clawbot/setup.js +121 -0
- package/dist/bootstrap.d.ts +5 -0
- package/dist/bootstrap.js +5 -20
- package/dist/cli.js +9 -3
- package/dist/core/adapter.d.ts +69 -3
- package/dist/core/adapter.js +11 -1
- package/dist/core/app.d.ts +12 -9
- package/dist/core/app.js +50 -51
- package/dist/core/bot-accounts.d.ts +28 -0
- package/dist/core/bot-accounts.js +78 -0
- package/dist/core/bot-policies.d.ts +30 -0
- package/dist/core/bot-policies.js +112 -0
- package/dist/core/bot.d.ts +19 -1
- package/dist/core/bot.js +13 -0
- package/dist/core/capabilities.d.ts +54 -0
- package/dist/core/capabilities.js +25 -0
- package/dist/core/config/schema.d.ts +61 -8
- package/dist/core/config/schema.js +15 -3
- package/dist/core/config/write.js +5 -1
- package/dist/core/connections.d.ts +84 -0
- package/dist/core/connections.js +326 -0
- package/dist/core/delivery.d.ts +53 -0
- package/dist/core/delivery.js +115 -0
- package/dist/core/dispatch/api.d.ts +3 -1
- package/dist/core/dispatch/api.js +12 -5
- package/dist/core/dispatch/dispatcher.d.ts +1 -0
- package/dist/core/dispatch/dispatcher.js +35 -15
- package/dist/core/dispatch/outgoing.d.ts +1 -0
- package/dist/core/dispatch/outgoing.js +5 -3
- package/dist/core/dispatch/trace.d.ts +2 -0
- package/dist/core/dispatch/types.d.ts +6 -0
- package/dist/core/dispatch/types.js +1 -2
- package/dist/core/errors.d.ts +4 -1
- package/dist/core/errors.js +7 -1
- package/dist/core/event/session.d.ts +3 -1
- package/dist/core/event/session.js +7 -4
- package/dist/core/event/types.d.ts +5 -1
- package/dist/core/identity.d.ts +18 -0
- package/dist/core/identity.js +31 -0
- package/dist/core/logger.js +5 -1
- package/dist/core/message/types.d.ts +2 -0
- package/dist/core/permission.d.ts +2 -1
- package/dist/core/permission.js +7 -2
- package/dist/core/plugin/context.d.ts +10 -10
- package/dist/core/plugin/context.js +20 -18
- package/dist/core/plugin/definition.d.ts +5 -0
- package/dist/core/plugin/execution.d.ts +3 -0
- package/dist/core/plugin/execution.js +3 -0
- package/dist/core/plugin/manager.d.ts +18 -4
- package/dist/core/plugin/manager.js +137 -45
- package/dist/core/plugin/tasks.d.ts +131 -24
- package/dist/core/plugin/tasks.js +517 -117
- package/dist/core/plugin/types.d.ts +3 -0
- package/dist/core/ratelimit.js +6 -1
- package/dist/core/storage/index.d.ts +3 -1
- package/dist/core/storage/index.js +52 -2
- package/dist/core/target.d.ts +24 -6
- package/dist/core/target.js +26 -6
- package/dist/core/utils/merge.d.ts +3 -0
- package/dist/core/utils/merge.js +18 -0
- package/dist/core/utils/redact.d.ts +6 -0
- package/dist/core/utils/redact.js +76 -0
- package/dist/index.d.ts +13 -5
- package/dist/index.js +7 -2
- package/dist/plugins/admin.js +30 -3
- package/dist/plugins/help.js +1 -0
- package/dist/plugins/history.d.ts +4 -0
- package/dist/plugins/history.js +13 -3
- package/dist/plugins/http.js +1 -0
- package/dist/plugins/long-message.js +20 -12
- package/dist/plugins/status.js +1 -0
- package/dist/server/auth.d.ts +5 -1
- package/dist/server/auth.js +11 -7
- package/dist/server/connection-setups.d.ts +126 -0
- package/dist/server/connection-setups.js +188 -0
- package/dist/server/groups.d.ts +1 -0
- package/dist/server/groups.js +8 -8
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +229 -29
- package/dist/server/schemas.d.ts +6 -2
- package/dist/server/settings.d.ts +66 -20
- package/dist/server/settings.js +158 -61
- package/dist/server/views.d.ts +95 -17
- package/dist/server/views.js +48 -13
- package/dist/testing/create-test-app.js +2 -2
- package/dist/testing/dispatch-fixture.js +2 -1
- package/dist/testing/fake-bot.d.ts +2 -1
- package/dist/testing/fake-bot.js +4 -1
- package/docs/DEPLOY.md +190 -0
- package/docs/FEISHU.md +170 -0
- package/docs/WECOM.md +107 -0
- package/docs/WEIXIN-CLAWBOT.md +56 -0
- package/package.json +29 -3
- package/web/dist/assets/index-BAbj8e7E.css +2 -0
- package/web/dist/assets/index-CMuBy6y5.js +51 -0
- package/web/dist/assets/{markdown-CDlgpI7K.js → markdown-MEN1AImu.js} +3 -3
- package/web/dist/index.html +2 -2
- package/web/dist/plugin-pages/_sdk.js +26 -1
- package/web/dist/assets/index-BI5GCpiI.css +0 -2
- package/web/dist/assets/index-C-rshHwD.js +0 -51
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { App } from '../app.ts';
|
|
2
|
-
import type
|
|
2
|
+
import { type BotIdentity } from '../identity.ts';
|
|
3
3
|
import { type PluginDefinition } from './definition.ts';
|
|
4
4
|
import { type PackageOptions, PackageService } from './packages.ts';
|
|
5
5
|
import type { PluginState } from './types.ts';
|
|
@@ -19,7 +19,6 @@ export declare class PluginManager {
|
|
|
19
19
|
private readonly packageUpdateSubscribers;
|
|
20
20
|
private readonly timers;
|
|
21
21
|
private readonly groupConfigCache;
|
|
22
|
-
private readonly groupConfigWarnings;
|
|
23
22
|
private releaseRoots?;
|
|
24
23
|
private queue;
|
|
25
24
|
private started;
|
|
@@ -34,7 +33,16 @@ export declare class PluginManager {
|
|
|
34
33
|
getConfig(name: string): unknown;
|
|
35
34
|
getGroupConfig(name: string, groupKey: string): Record<string, unknown> | undefined;
|
|
36
35
|
setGroupConfig(name: string, groupKey: string, override: Record<string, unknown> | null): void;
|
|
37
|
-
|
|
36
|
+
setBotConfig(name: string, bot: BotIdentity, override: Record<string, unknown> | null): void;
|
|
37
|
+
setBotEnabled(name: string, bot: BotIdentity, enabled: boolean | null): void;
|
|
38
|
+
private requireConfig;
|
|
39
|
+
private rawConfig;
|
|
40
|
+
configFor(name: string, target: string | (BotIdentity & {
|
|
41
|
+
groupId?: string;
|
|
42
|
+
})): unknown;
|
|
43
|
+
configSources(name: string, target: BotIdentity & {
|
|
44
|
+
groupId?: string;
|
|
45
|
+
}): Record<string, string>;
|
|
38
46
|
private invalidateGroupConfigs;
|
|
39
47
|
start(): Promise<void>;
|
|
40
48
|
watch(): Promise<void>;
|
|
@@ -52,13 +60,19 @@ export declare class PluginManager {
|
|
|
52
60
|
packageInfo(name: string): import("./packages.ts").PackageInfo | undefined;
|
|
53
61
|
subscribePackageUpdates(subscriber: (state: PluginState) => void): () => void;
|
|
54
62
|
updateConfig(name: string, value: unknown): Promise<void>;
|
|
63
|
+
setDefaultOnNewBots(name: string, enabled: boolean): Promise<void>;
|
|
64
|
+
defaultsForNewBot(): string[];
|
|
55
65
|
setGroupEnabled(name: string, groupKey: string, enabled: boolean): void;
|
|
56
|
-
|
|
66
|
+
isEnabledOnBot(name: string, bot: BotIdentity): boolean;
|
|
67
|
+
isEnabledFor(name: string, event: BotIdentity & {
|
|
68
|
+
groupId?: string;
|
|
69
|
+
}): boolean;
|
|
57
70
|
serial<T>(work: () => Promise<T>): Promise<T>;
|
|
58
71
|
private requestReconcile;
|
|
59
72
|
private require;
|
|
60
73
|
private requireAvailable;
|
|
61
74
|
private entryConfig;
|
|
75
|
+
private initializeDefaultOnNewBots;
|
|
62
76
|
private persist;
|
|
63
77
|
private add;
|
|
64
78
|
private metadata;
|
|
@@ -3,10 +3,15 @@ import { watch } from 'node:fs';
|
|
|
3
3
|
import { lstat, mkdir, mkdtemp, readdir, realpath, rename, rm, stat, writeFile } from 'node:fs/promises';
|
|
4
4
|
import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
5
5
|
import { z } from 'zod';
|
|
6
|
+
import { requirementsSchema } from '../capabilities.js';
|
|
6
7
|
import { deleteConfigValue, writeConfigValue } from '../config/write.js';
|
|
8
|
+
import { groupKeyOf } from '../dispatch/types.js';
|
|
7
9
|
import { PluginConfigError, PluginError } from '../errors.js';
|
|
10
|
+
import { groupIdentity } from '../identity.js';
|
|
11
|
+
import { mergeConfig } from '../utils/merge.js';
|
|
8
12
|
import { Context } from './context.js';
|
|
9
13
|
import { serviceDependencies } from './definition.js';
|
|
14
|
+
import { pluginExecution } from './execution.js';
|
|
10
15
|
import { importPlugin, registerPluginRoots } from './loader.js';
|
|
11
16
|
import { PackageService, redact } from './packages.js';
|
|
12
17
|
import { ManagementError, uploadContents } from './upload.js';
|
|
@@ -27,6 +32,10 @@ function validateDefinition(value) {
|
|
|
27
32
|
typeof value.setup !== 'function') {
|
|
28
33
|
throw new PluginError('Plugin must default-export a definition with a valid name and setup function');
|
|
29
34
|
}
|
|
35
|
+
if ('defaultOnNewBots' in value &&
|
|
36
|
+
value.defaultOnNewBots !== undefined &&
|
|
37
|
+
typeof value.defaultOnNewBots !== 'boolean')
|
|
38
|
+
throw new PluginError(`Invalid new bot default for plugin "${value.name}"`);
|
|
30
39
|
if ('depends' in value &&
|
|
31
40
|
value.depends !== undefined &&
|
|
32
41
|
(!Array.isArray(value.depends) || !value.depends.every((item) => typeof item === 'string'))) {
|
|
@@ -43,6 +52,10 @@ function validateDefinition(value) {
|
|
|
43
52
|
throw new PluginError(`Invalid service dependencies for plugin "${value.name}"`);
|
|
44
53
|
}
|
|
45
54
|
}
|
|
55
|
+
if ('capabilities' in value &&
|
|
56
|
+
value.capabilities !== undefined &&
|
|
57
|
+
!requirementsSchema.safeParse(value.capabilities).success)
|
|
58
|
+
throw new PluginError(`Invalid capability requirements for plugin "${value.name}"`);
|
|
46
59
|
return value;
|
|
47
60
|
}
|
|
48
61
|
/** 管理插件注册、依赖协调、启停、重载和服务。 */
|
|
@@ -61,7 +74,6 @@ export class PluginManager {
|
|
|
61
74
|
packageUpdateSubscribers = new Set();
|
|
62
75
|
timers = new Map();
|
|
63
76
|
groupConfigCache = new Map();
|
|
64
|
-
groupConfigWarnings = new Set();
|
|
65
77
|
releaseRoots;
|
|
66
78
|
queue = Promise.resolve();
|
|
67
79
|
started = false;
|
|
@@ -157,47 +169,91 @@ export class PluginManager {
|
|
|
157
169
|
}
|
|
158
170
|
setGroupConfig(name, groupKey, override) {
|
|
159
171
|
const entry = this.requireAvailable(name);
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
{ path: `plugins.${name}.config`, message: 'Plugin has no configuration schema' },
|
|
164
|
-
]);
|
|
165
|
-
this.validateConfig(entry, {
|
|
166
|
-
...this.app.config.plugins[name]?.config,
|
|
167
|
-
...override,
|
|
168
|
-
});
|
|
169
|
-
}
|
|
172
|
+
const target = groupIdentity(groupKey);
|
|
173
|
+
this.requireConfig(entry);
|
|
174
|
+
this.validateConfig(entry, this.rawConfig(name, target, { group: override }));
|
|
170
175
|
this.app.storage.setGroupConfig(name, groupKey, override);
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
176
|
+
this.invalidateGroupConfigs(name);
|
|
177
|
+
}
|
|
178
|
+
setBotConfig(name, bot, override) {
|
|
179
|
+
const entry = this.requireAvailable(name);
|
|
180
|
+
this.requireConfig(entry);
|
|
181
|
+
this.validateConfig(entry, this.rawConfig(name, bot, { bot: override }));
|
|
182
|
+
for (const group of this.app.storage.listGroups(name)) {
|
|
183
|
+
const target = groupIdentity(group.groupKey);
|
|
184
|
+
if (target.platform === bot.platform && target.selfId === bot.selfId)
|
|
185
|
+
this.validateConfig(entry, this.rawConfig(name, target, { bot: override }));
|
|
186
|
+
}
|
|
187
|
+
this.app.policies.setPlugin(bot, name, {
|
|
188
|
+
...this.app.policies.plugin(bot, name),
|
|
189
|
+
config: override ?? undefined,
|
|
190
|
+
});
|
|
191
|
+
this.invalidateGroupConfigs(name);
|
|
192
|
+
}
|
|
193
|
+
setBotEnabled(name, bot, enabled) {
|
|
194
|
+
this.requireAvailable(name);
|
|
195
|
+
this.app.policies.setPlugin(bot, name, {
|
|
196
|
+
...this.app.policies.plugin(bot, name),
|
|
197
|
+
enabled: enabled ?? undefined,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
requireConfig(entry) {
|
|
201
|
+
if (!entry.definition?.config)
|
|
202
|
+
throw new PluginConfigError([
|
|
203
|
+
{ path: `plugins.${entry.state.name}.config`, message: 'Plugin has no configuration schema' },
|
|
204
|
+
]);
|
|
174
205
|
}
|
|
175
|
-
|
|
176
|
-
const
|
|
206
|
+
rawConfig(name, target, changes = {}) {
|
|
207
|
+
const global = 'global' in changes ? changes.global : this.app.config.plugins[name]?.config;
|
|
208
|
+
const bot = 'bot' in changes ? changes.bot : this.app.policies.plugin(target, name).config;
|
|
209
|
+
const key = groupKeyOf(target);
|
|
210
|
+
const group = 'group' in changes
|
|
211
|
+
? changes.group
|
|
212
|
+
: key === undefined
|
|
213
|
+
? undefined
|
|
214
|
+
: this.app.storage.getGroupConfig(name, key);
|
|
215
|
+
return mergeConfig(mergeConfig(global ?? {}, bot ?? {}), group ?? {});
|
|
216
|
+
}
|
|
217
|
+
configFor(name, target) {
|
|
218
|
+
const destination = typeof target === 'string' ? groupIdentity(target) : target;
|
|
219
|
+
const key = JSON.stringify([name, destination.platform, destination.selfId, destination.groupId]);
|
|
177
220
|
if (this.groupConfigCache.has(key))
|
|
178
221
|
return this.groupConfigCache.get(key);
|
|
179
222
|
const entry = this.requireAvailable(name);
|
|
180
|
-
const
|
|
181
|
-
let result = this.getConfig(name);
|
|
182
|
-
if (override !== undefined) {
|
|
183
|
-
try {
|
|
184
|
-
result = this.validateConfig(entry, {
|
|
185
|
-
...this.app.config.plugins[name]?.config,
|
|
186
|
-
...override,
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
catch (error) {
|
|
190
|
-
if (!(error instanceof PluginConfigError))
|
|
191
|
-
throw error;
|
|
192
|
-
if (!this.groupConfigWarnings.has(key)) {
|
|
193
|
-
this.groupConfigWarnings.add(key);
|
|
194
|
-
this.app.logger.warn({ plugin: name, groupKey, issues: error.issues }, 'Group configuration invalid; using global configuration');
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
}
|
|
223
|
+
const result = this.validateConfig(entry, this.rawConfig(name, destination));
|
|
198
224
|
this.groupConfigCache.set(key, result);
|
|
199
225
|
return result;
|
|
200
226
|
}
|
|
227
|
+
configSources(name, target) {
|
|
228
|
+
const key = groupKeyOf(target);
|
|
229
|
+
const layers = [
|
|
230
|
+
['global', this.app.config.plugins[name]?.config],
|
|
231
|
+
['bot', this.app.policies.plugin(target, name).config],
|
|
232
|
+
['group', key === undefined ? undefined : this.app.storage.getGroupConfig(name, key)],
|
|
233
|
+
];
|
|
234
|
+
const sources = {};
|
|
235
|
+
const walk = (value, path) => {
|
|
236
|
+
if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
|
|
237
|
+
for (const [key, item] of Object.entries(value))
|
|
238
|
+
walk(item, [...path, key]);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
let source = 'default';
|
|
242
|
+
for (const [name, config] of layers) {
|
|
243
|
+
let current = config;
|
|
244
|
+
for (const part of path)
|
|
245
|
+
current =
|
|
246
|
+
current !== null && typeof current === 'object' && Object.hasOwn(current, part)
|
|
247
|
+
? current[part]
|
|
248
|
+
: undefined;
|
|
249
|
+
if (current !== undefined)
|
|
250
|
+
source = name;
|
|
251
|
+
}
|
|
252
|
+
sources[path.join('.')] = source;
|
|
253
|
+
};
|
|
254
|
+
walk(this.configFor(name, target), []);
|
|
255
|
+
return sources;
|
|
256
|
+
}
|
|
201
257
|
invalidateGroupConfigs(name) {
|
|
202
258
|
for (const key of this.groupConfigCache.keys()) {
|
|
203
259
|
if (JSON.parse(key)[0] === name)
|
|
@@ -208,6 +264,10 @@ export class PluginManager {
|
|
|
208
264
|
this.started = true;
|
|
209
265
|
await this.packages.init();
|
|
210
266
|
this.releaseRoots = registerPluginRoots([...this.dirs, this.packages.dir]);
|
|
267
|
+
for (const { definition } of this.entries.values()) {
|
|
268
|
+
if (definition)
|
|
269
|
+
await this.initializeDefaultOnNewBots(definition);
|
|
270
|
+
}
|
|
211
271
|
for (const dir of this.dirs)
|
|
212
272
|
await this.discover(dir);
|
|
213
273
|
for (const name of Object.keys(await this.packages.dependencies())) {
|
|
@@ -456,21 +516,42 @@ export class PluginManager {
|
|
|
456
516
|
return this.serial(async () => {
|
|
457
517
|
const entry = this.requireAvailable(name);
|
|
458
518
|
this.validateConfig(entry, value);
|
|
519
|
+
for (const bot of this.app.policies.configured(name))
|
|
520
|
+
this.validateConfig(entry, this.rawConfig(name, bot, { global: value }));
|
|
521
|
+
for (const group of this.app.storage.listGroups(name))
|
|
522
|
+
this.validateConfig(entry, this.rawConfig(name, groupIdentity(group.groupKey), { global: value }));
|
|
459
523
|
await this.persist(name, 'config', value);
|
|
460
524
|
this.entryConfig(name).config = value;
|
|
461
525
|
this.invalidateGroupConfigs(name);
|
|
462
526
|
await this.reloadEntry(entry);
|
|
463
527
|
});
|
|
464
528
|
}
|
|
529
|
+
setDefaultOnNewBots(name, enabled) {
|
|
530
|
+
return this.serial(async () => {
|
|
531
|
+
const entry = this.requireAvailable(name);
|
|
532
|
+
await this.persist(name, 'defaultOnNewBots', enabled);
|
|
533
|
+
this.entryConfig(name).defaultOnNewBots = enabled;
|
|
534
|
+
this.publishState(entry);
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
defaultsForNewBot() {
|
|
538
|
+
return [...this.entries.keys()].filter((name) => this.app.config.plugins[name]?.defaultOnNewBots);
|
|
539
|
+
}
|
|
465
540
|
setGroupEnabled(name, groupKey, enabled) {
|
|
466
541
|
this.requireAvailable(name);
|
|
467
542
|
this.app.storage.setGroupEnabled(name, groupKey, enabled);
|
|
468
543
|
}
|
|
544
|
+
isEnabledOnBot(name, bot) {
|
|
545
|
+
return (name === 'core' ||
|
|
546
|
+
(this.entries.get(name)?.state.enabled !== false &&
|
|
547
|
+
(this.app.policies.plugin(bot, name).enabled ?? this.app.policies.defaultPluginEnabled(bot, name))));
|
|
548
|
+
}
|
|
469
549
|
isEnabledFor(name, event) {
|
|
470
550
|
if (name === 'core')
|
|
471
551
|
return true;
|
|
472
|
-
const
|
|
473
|
-
return
|
|
552
|
+
const groupKey = groupKeyOf(event);
|
|
553
|
+
return (this.isEnabledOnBot(name, event) &&
|
|
554
|
+
(groupKey === undefined || this.app.storage.isGroupEnabled(name, groupKey)));
|
|
474
555
|
}
|
|
475
556
|
serial(work) {
|
|
476
557
|
const result = this.queue.then(work);
|
|
@@ -504,6 +585,12 @@ export class PluginManager {
|
|
|
504
585
|
this.app.config.plugins[name] ??= { enabled: true };
|
|
505
586
|
return this.app.config.plugins[name];
|
|
506
587
|
}
|
|
588
|
+
async initializeDefaultOnNewBots({ name, defaultOnNewBots = false }) {
|
|
589
|
+
if (this.app.config.plugins[name]?.defaultOnNewBots !== undefined)
|
|
590
|
+
return;
|
|
591
|
+
await this.persist(name, 'defaultOnNewBots', defaultOnNewBots);
|
|
592
|
+
this.entryConfig(name).defaultOnNewBots = defaultOnNewBots;
|
|
593
|
+
}
|
|
507
594
|
async persist(name, field, value) {
|
|
508
595
|
if (this.configPath) {
|
|
509
596
|
await writeConfigValue(this.configPath, ['plugins', name, field], value, field === 'config' ? { previousValue: this.app.config.plugins[name]?.config } : undefined);
|
|
@@ -521,6 +608,7 @@ export class PluginManager {
|
|
|
521
608
|
enabled: this.app.config.plugins[name]?.enabled ?? true,
|
|
522
609
|
provides: [],
|
|
523
610
|
inject: [],
|
|
611
|
+
capabilities: definition.capabilities,
|
|
524
612
|
};
|
|
525
613
|
const entry = { state, definition, files, release };
|
|
526
614
|
this.metadata(entry);
|
|
@@ -540,8 +628,10 @@ export class PluginManager {
|
|
|
540
628
|
const definition = entry.definition;
|
|
541
629
|
if (!definition)
|
|
542
630
|
return;
|
|
631
|
+
entry.state.capabilities = definition.capabilities;
|
|
543
632
|
entry.state.version = definition.version;
|
|
544
633
|
entry.state.description = definition.description;
|
|
634
|
+
entry.state.depends = definition.depends;
|
|
545
635
|
const dependencies = serviceDependencies(definition.inject);
|
|
546
636
|
entry.state.inject = [
|
|
547
637
|
...dependencies.required.map((name) => ({ name, optional: false })),
|
|
@@ -586,7 +676,10 @@ export class PluginManager {
|
|
|
586
676
|
try {
|
|
587
677
|
const imported = await importPlugin(path);
|
|
588
678
|
try {
|
|
589
|
-
|
|
679
|
+
const definition = validateDefinition(imported.value);
|
|
680
|
+
if (!this.entries.has(definition.name))
|
|
681
|
+
await this.initializeDefaultOnNewBots(definition);
|
|
682
|
+
this.add(definition, 'file', path, imported.files, imported.release);
|
|
590
683
|
}
|
|
591
684
|
catch (error) {
|
|
592
685
|
imported.release();
|
|
@@ -635,10 +728,8 @@ export class PluginManager {
|
|
|
635
728
|
await deleteConfigValue(this.configPath, ['plugins', name]);
|
|
636
729
|
delete this.app.config.plugins[name];
|
|
637
730
|
this.app.storage.purgePlugin(name);
|
|
731
|
+
this.app.policies.load();
|
|
638
732
|
this.invalidateGroupConfigs(name);
|
|
639
|
-
for (const key of this.groupConfigWarnings)
|
|
640
|
-
if (JSON.parse(key)[0] === name)
|
|
641
|
-
this.groupConfigWarnings.delete(key);
|
|
642
733
|
const path = join(this.app.config.dataDir, 'plugins', name);
|
|
643
734
|
try {
|
|
644
735
|
await this.ensureRealInside(this.app.config.dataDir, path);
|
|
@@ -750,6 +841,7 @@ export class PluginManager {
|
|
|
750
841
|
if (existing?.definition && definition.name !== existing.state.name)
|
|
751
842
|
throw new ManagementError(409, 'Updated package changed its plugin name');
|
|
752
843
|
if (!existing) {
|
|
844
|
+
await this.initializeDefaultOnNewBots(definition);
|
|
753
845
|
const entry = this.add(definition, 'package', path, imported.files, imported.release);
|
|
754
846
|
entry.manifest = manifest;
|
|
755
847
|
return entry;
|
|
@@ -1030,7 +1122,7 @@ export class PluginManager {
|
|
|
1030
1122
|
context = new Context(this.app, entry.state.name, parsed, entry.definition?.inject);
|
|
1031
1123
|
let timedOut = false;
|
|
1032
1124
|
let timer;
|
|
1033
|
-
const setup = Promise.resolve().then(() => entry.definition?.setup(context));
|
|
1125
|
+
const setup = Promise.resolve().then(() => pluginExecution.run(entry.state.name, () => entry.definition?.setup(context)));
|
|
1034
1126
|
void setup.catch((error) => {
|
|
1035
1127
|
if (timedOut)
|
|
1036
1128
|
this.app.logger.error({ err: error, plugin: entry.state.name }, 'Plugin setup failed after timeout');
|
|
@@ -1052,8 +1144,8 @@ export class PluginManager {
|
|
|
1052
1144
|
}
|
|
1053
1145
|
entry.context = context;
|
|
1054
1146
|
entry.failure = undefined;
|
|
1055
|
-
context.warnUnregisteredTasks();
|
|
1056
1147
|
this.setState(entry, 'active');
|
|
1148
|
+
context.activateTasks();
|
|
1057
1149
|
}
|
|
1058
1150
|
catch (error) {
|
|
1059
1151
|
await context?.dispose();
|
|
@@ -1077,10 +1169,10 @@ export class PluginManager {
|
|
|
1077
1169
|
const imported = await importPlugin(entry.state.path);
|
|
1078
1170
|
release = imported.release;
|
|
1079
1171
|
definition = validateDefinition(imported.value);
|
|
1172
|
+
if (definition.name !== entry.state.name && (entry.definition || this.entries.has(definition.name)))
|
|
1173
|
+
throw new PluginError(`Plugin name changed from "${entry.state.name}" to "${definition.name}"`);
|
|
1174
|
+
await this.initializeDefaultOnNewBots(definition);
|
|
1080
1175
|
if (definition.name !== entry.state.name) {
|
|
1081
|
-
if (entry.definition || this.entries.has(definition.name)) {
|
|
1082
|
-
throw new PluginError(`Plugin name changed from "${entry.state.name}" to "${definition.name}"`);
|
|
1083
|
-
}
|
|
1084
1176
|
this.entries.delete(entry.state.name);
|
|
1085
1177
|
entry.state.name = definition.name;
|
|
1086
1178
|
entry.state.enabled = this.app.config.plugins[definition.name]?.enabled ?? true;
|
|
@@ -1,43 +1,150 @@
|
|
|
1
1
|
import type { Logger } from 'pino';
|
|
2
|
+
import { type Config } from '../config/schema.ts';
|
|
3
|
+
import { type DeliveryFailure } from '../delivery.ts';
|
|
2
4
|
import type { Storage } from '../storage/index.ts';
|
|
3
|
-
|
|
5
|
+
import { type Duration } from '../utils/duration.ts';
|
|
6
|
+
export declare const taskStates: readonly ['pending', 'running', 'retry_wait', 'cancelling', 'succeeded', 'failed', 'cancelled'];
|
|
7
|
+
export type TaskState = (typeof taskStates)[number];
|
|
8
|
+
export type TaskOutcome = 'running' | 'succeeded' | 'failed' | 'cancelled' | 'interrupted' | 'timeout';
|
|
9
|
+
/** at 和 delay 必须且只能提供一个;相同逻辑键替换旧任务。 */
|
|
4
10
|
export interface TaskOptions {
|
|
5
11
|
at?: Date | number;
|
|
6
|
-
delay?:
|
|
12
|
+
delay?: Duration | string;
|
|
7
13
|
key?: string;
|
|
8
14
|
}
|
|
9
|
-
/**
|
|
15
|
+
/** 默认不自动重试、不限制执行时长;中止始终是协作式的。 */
|
|
16
|
+
export interface TaskRegistrationOptions {
|
|
17
|
+
retry?: {
|
|
18
|
+
maxRetries: number;
|
|
19
|
+
delay?: Duration | string;
|
|
20
|
+
maxDelay?: Duration | string;
|
|
21
|
+
};
|
|
22
|
+
timeout?: Duration | string;
|
|
23
|
+
}
|
|
24
|
+
export interface TaskPolicy {
|
|
25
|
+
maxRetries: number;
|
|
26
|
+
delay: number;
|
|
27
|
+
maxDelay: number;
|
|
28
|
+
timeout?: number;
|
|
29
|
+
}
|
|
30
|
+
export interface TaskRecord<T = unknown> {
|
|
31
|
+
id: string;
|
|
32
|
+
plugin: string;
|
|
33
|
+
name: string;
|
|
34
|
+
key: string;
|
|
35
|
+
payload: T;
|
|
36
|
+
state: TaskState;
|
|
37
|
+
runAt: number;
|
|
38
|
+
nextRunAt: number;
|
|
39
|
+
createdAt: number;
|
|
40
|
+
updatedAt: number;
|
|
41
|
+
startedAt?: number;
|
|
42
|
+
finishedAt?: number;
|
|
43
|
+
attempt: number;
|
|
44
|
+
failures: number;
|
|
45
|
+
policy: TaskPolicy;
|
|
46
|
+
error?: string;
|
|
47
|
+
deliveryFailure?: DeliveryFailure;
|
|
48
|
+
blockedReason?: string;
|
|
49
|
+
canRetry: boolean;
|
|
50
|
+
}
|
|
51
|
+
export interface TaskAttempt {
|
|
52
|
+
id: string;
|
|
53
|
+
taskId: string;
|
|
54
|
+
attempt: number;
|
|
55
|
+
startedAt: number;
|
|
56
|
+
finishedAt?: number;
|
|
57
|
+
outcome: TaskOutcome;
|
|
58
|
+
error?: string;
|
|
59
|
+
deliveryFailure?: DeliveryFailure;
|
|
60
|
+
}
|
|
61
|
+
export interface TaskQuery {
|
|
62
|
+
plugin?: string;
|
|
63
|
+
name?: string;
|
|
64
|
+
state?: TaskState;
|
|
65
|
+
q?: string;
|
|
66
|
+
offset?: number;
|
|
67
|
+
limit?: number;
|
|
68
|
+
}
|
|
69
|
+
/** 插件按逻辑键管理任务,每次 schedule 创建独立的任务及历史。 */
|
|
10
70
|
export interface TaskHandle<T> {
|
|
11
|
-
/** 按 at 或 delay 调度任务并返回任务键;两者同时指定或都未指定时抛错,相同键会覆盖旧任务。 */
|
|
12
71
|
schedule(payload: T, options: TaskOptions): string;
|
|
13
|
-
/** 按任务键取消待执行任务;找到并取消时返回 true。 */
|
|
14
72
|
cancel(key: string): boolean;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
/** 持久化任务的处理函数,接收载荷、任务键、计划时间及取消信号。 */
|
|
73
|
+
list(): TaskRecord<T>[];
|
|
74
|
+
get(key: string): TaskRecord<T> | undefined;
|
|
75
|
+
attempts(key: string): TaskAttempt[];
|
|
76
|
+
retry(key: string, options?: {
|
|
77
|
+
confirmDelivery?: boolean;
|
|
78
|
+
}): boolean;
|
|
79
|
+
}
|
|
23
80
|
export type TaskHandler<T> = (payload: T, context: {
|
|
24
81
|
signal: AbortSignal;
|
|
82
|
+
taskId: string;
|
|
83
|
+
attempt: number;
|
|
25
84
|
key: string;
|
|
26
85
|
runAt: number;
|
|
27
86
|
}) => unknown | Promise<unknown>;
|
|
28
|
-
export declare class
|
|
29
|
-
readonly running: Map<string, Promise<void>>;
|
|
87
|
+
export declare class TaskConflictError extends Error {
|
|
30
88
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
89
|
+
interface Registration {
|
|
90
|
+
plugin: string;
|
|
91
|
+
name: string;
|
|
92
|
+
handler: TaskHandler<unknown>;
|
|
93
|
+
policy: TaskPolicy;
|
|
94
|
+
active: boolean;
|
|
95
|
+
}
|
|
96
|
+
/** 应用级单实例调度器,拥有执行代际、状态及数据库生命周期。 */
|
|
97
|
+
export declare class TaskService {
|
|
34
98
|
private readonly storage;
|
|
35
|
-
private readonly
|
|
36
|
-
private readonly signal;
|
|
99
|
+
private readonly config;
|
|
37
100
|
private readonly logger;
|
|
38
|
-
private readonly
|
|
101
|
+
private readonly registrations;
|
|
102
|
+
private readonly running;
|
|
103
|
+
private readonly locks;
|
|
104
|
+
private readonly subscribers;
|
|
105
|
+
private cancelTimer?;
|
|
106
|
+
private sweep?;
|
|
107
|
+
private queued;
|
|
108
|
+
private alive;
|
|
109
|
+
constructor(storage: Storage, config: Config, logger: Logger);
|
|
110
|
+
start(): void;
|
|
111
|
+
stop(): void;
|
|
112
|
+
register(plugin: string, name: string, handler: TaskHandler<unknown>, options: TaskRegistrationOptions): Registration;
|
|
113
|
+
activate(registrations: Registration[]): void;
|
|
114
|
+
unregister(registration: Registration): void;
|
|
115
|
+
schedule(registration: Registration, payload: unknown, options: TaskOptions): string;
|
|
116
|
+
get(id: string): TaskRecord | undefined;
|
|
117
|
+
latest(plugin: string, name: string, key: string): TaskRecord | undefined;
|
|
118
|
+
query(options?: TaskQuery): {
|
|
119
|
+
items: TaskRecord[];
|
|
120
|
+
total: number;
|
|
121
|
+
};
|
|
122
|
+
list(plugin: string, name: string): TaskRecord[];
|
|
123
|
+
attempts(taskId: string): TaskAttempt[];
|
|
124
|
+
retry(id: string, options?: {
|
|
125
|
+
confirmDelivery?: boolean;
|
|
126
|
+
}): TaskRecord;
|
|
127
|
+
cancel(id: string, reason?: 'user' | 'superseded'): boolean;
|
|
128
|
+
subscribe(listener: (id?: string) => void): () => void;
|
|
129
|
+
clean(now?: number): void;
|
|
130
|
+
private record;
|
|
131
|
+
private changed;
|
|
132
|
+
private wake;
|
|
133
|
+
private pump;
|
|
134
|
+
private execute;
|
|
135
|
+
private persistResult;
|
|
136
|
+
private finish;
|
|
137
|
+
private errorText;
|
|
138
|
+
}
|
|
139
|
+
/** 插件级入口,注册在 setup 成功后激活,卸载时解除注册。 */
|
|
140
|
+
export declare class PersistentTasks {
|
|
141
|
+
private activated;
|
|
142
|
+
private readonly registrations;
|
|
143
|
+
private readonly service;
|
|
144
|
+
private readonly owner;
|
|
39
145
|
private readonly assertActive;
|
|
40
|
-
constructor(
|
|
41
|
-
register<T>(name: string, handler: TaskHandler<T
|
|
42
|
-
|
|
146
|
+
constructor(service: TaskService, owner: string, signal: AbortSignal, assertActive: () => void);
|
|
147
|
+
register<T>(name: string, handler: TaskHandler<T>, options?: TaskRegistrationOptions): TaskHandle<T>;
|
|
148
|
+
activate(): void;
|
|
43
149
|
}
|
|
150
|
+
export {};
|