@asabot/core 0.2.0 → 0.3.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/README.md +64 -2
- package/dist/core/bot-accounts.d.ts +2 -0
- package/dist/core/bot-accounts.js +7 -0
- package/dist/core/logger.js +4 -1
- package/dist/core/message/segment.js +3 -13
- package/dist/core/plugin/context.d.ts +15 -1
- package/dist/core/plugin/context.js +43 -3
- package/dist/core/plugin/credentials.d.ts +44 -0
- package/dist/core/plugin/credentials.js +45 -0
- package/dist/core/plugin/definition.d.ts +5 -0
- package/dist/core/plugin/icon.d.ts +2 -0
- package/dist/core/plugin/icon.js +14 -0
- package/dist/core/plugin/manager.d.ts +15 -3
- package/dist/core/plugin/manager.js +115 -34
- package/dist/core/plugin/packages.d.ts +4 -0
- package/dist/core/plugin/packages.js +39 -9
- package/dist/core/plugin/types.d.ts +24 -0
- package/dist/core/storage/credentials.d.ts +25 -0
- package/dist/core/storage/credentials.js +139 -0
- package/dist/core/storage/index.d.ts +2 -0
- package/dist/core/storage/index.js +10 -0
- package/dist/index.d.ts +2 -1
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +88 -21
- package/dist/server/schemas.d.ts +13 -0
- package/dist/server/schemas.js +28 -0
- package/dist/server/views.d.ts +7 -0
- package/dist/server/views.js +3 -1
- package/docs/DEPLOY.md +22 -1
- package/package.json +1 -1
- package/web/dist/assets/index-C3RsfNwC.js +51 -0
- package/web/dist/assets/index-Dz7T5hDM.css +2 -0
- package/web/dist/assets/{markdown-MEN1AImu.js → markdown-HrgPSKyp.js} +4 -4
- package/web/dist/assets/plugin-credentials-CuMBPnPO.js +1 -0
- package/web/dist/index.html +2 -2
- package/web/dist/assets/index-BAbj8e7E.css +0 -2
- package/web/dist/assets/index-CMuBy6y5.js +0 -51
|
@@ -10,8 +10,10 @@ import { PluginConfigError, PluginError } from '../errors.js';
|
|
|
10
10
|
import { groupIdentity } from '../identity.js';
|
|
11
11
|
import { mergeConfig } from '../utils/merge.js';
|
|
12
12
|
import { Context } from './context.js';
|
|
13
|
+
import { credentialGroupsSchema } from './credentials.js';
|
|
13
14
|
import { serviceDependencies } from './definition.js';
|
|
14
15
|
import { pluginExecution } from './execution.js';
|
|
16
|
+
import { parsePluginIconUrl, pluginIconFromKeywords } from './icon.js';
|
|
15
17
|
import { importPlugin, registerPluginRoots } from './loader.js';
|
|
16
18
|
import { PackageService, redact } from './packages.js';
|
|
17
19
|
import { ManagementError, uploadContents } from './upload.js';
|
|
@@ -21,6 +23,18 @@ function errorMessage(error) {
|
|
|
21
23
|
function ignoredEntry(name) {
|
|
22
24
|
return name.startsWith('_') || name.startsWith('.') || /\.(?:test|spec)\.|\.d\.ts$/.test(name);
|
|
23
25
|
}
|
|
26
|
+
function catalogEntry({ state, manifest }) {
|
|
27
|
+
return {
|
|
28
|
+
name: state.name,
|
|
29
|
+
version: state.version ?? manifest?.version,
|
|
30
|
+
description: state.description ?? manifest?.description,
|
|
31
|
+
source: state.source,
|
|
32
|
+
state: state.state,
|
|
33
|
+
enabled: state.enabled,
|
|
34
|
+
provides: [...state.provides],
|
|
35
|
+
inject: state.inject.map((service) => ({ ...service })),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
24
38
|
function validateDefinition(value) {
|
|
25
39
|
if (typeof value !== 'object' ||
|
|
26
40
|
value === null ||
|
|
@@ -56,6 +70,10 @@ function validateDefinition(value) {
|
|
|
56
70
|
value.capabilities !== undefined &&
|
|
57
71
|
!requirementsSchema.safeParse(value.capabilities).success)
|
|
58
72
|
throw new PluginError(`Invalid capability requirements for plugin "${value.name}"`);
|
|
73
|
+
if ('credentials' in value &&
|
|
74
|
+
value.credentials !== undefined &&
|
|
75
|
+
!credentialGroupsSchema.safeParse(value.credentials).success)
|
|
76
|
+
throw new PluginError(`Invalid credential groups for plugin "${value.name}"`);
|
|
59
77
|
return value;
|
|
60
78
|
}
|
|
61
79
|
/** 管理插件注册、依赖协调、启停、重载和服务。 */
|
|
@@ -72,6 +90,7 @@ export class PluginManager {
|
|
|
72
90
|
uninstalling = new Set();
|
|
73
91
|
watchers = [];
|
|
74
92
|
packageUpdateSubscribers = new Set();
|
|
93
|
+
listSubscribers = new Set();
|
|
75
94
|
timers = new Map();
|
|
76
95
|
groupConfigCache = new Map();
|
|
77
96
|
releaseRoots;
|
|
@@ -121,11 +140,30 @@ export class PluginManager {
|
|
|
121
140
|
const state = this.entries.get(name)?.state;
|
|
122
141
|
return state && { ...state };
|
|
123
142
|
}
|
|
143
|
+
listCatalog() {
|
|
144
|
+
return [...this.entries.values()].map(catalogEntry);
|
|
145
|
+
}
|
|
146
|
+
getCatalogEntry(name) {
|
|
147
|
+
const entry = this.entries.get(name);
|
|
148
|
+
return entry && catalogEntry(entry);
|
|
149
|
+
}
|
|
124
150
|
provide(plugin, name, value) {
|
|
151
|
+
return this.registerService(name, { plugin, kind: 'shared', value });
|
|
152
|
+
}
|
|
153
|
+
provideScoped(plugin, name, factory) {
|
|
154
|
+
return this.registerService(name, { plugin, kind: 'scoped', factory, instances: new WeakMap() });
|
|
155
|
+
}
|
|
156
|
+
serviceConsumers(name) {
|
|
157
|
+
return [...this.entries.values()]
|
|
158
|
+
.filter(({ state }) => state.inject.some((item) => item.name === name))
|
|
159
|
+
.map(({ state }) => state.name)
|
|
160
|
+
.sort();
|
|
161
|
+
}
|
|
162
|
+
registerService(name, service) {
|
|
163
|
+
const { plugin } = service;
|
|
125
164
|
const existing = this.services.get(name);
|
|
126
165
|
if (existing)
|
|
127
166
|
throw new PluginError(`服务 ${name} 冲突:插件 "${existing.plugin}" 已提供,插件 "${plugin}" 无法重复提供`);
|
|
128
|
-
const service = { plugin, value };
|
|
129
167
|
this.services.set(name, service);
|
|
130
168
|
this.serviceProviders.set(name, plugin);
|
|
131
169
|
const entry = this.require(plugin);
|
|
@@ -143,14 +181,24 @@ export class PluginManager {
|
|
|
143
181
|
this.requestReconcile();
|
|
144
182
|
};
|
|
145
183
|
}
|
|
146
|
-
inject(name) {
|
|
184
|
+
inject(name, consumer) {
|
|
147
185
|
const service = this.services.get(name);
|
|
148
186
|
if (!service)
|
|
149
187
|
throw new PluginError(`服务 ${name} 不可用`);
|
|
150
|
-
return service
|
|
188
|
+
return this.serviceValue(name, service, consumer);
|
|
151
189
|
}
|
|
152
|
-
injectOptional(name) {
|
|
153
|
-
|
|
190
|
+
injectOptional(name, consumer) {
|
|
191
|
+
const service = this.services.get(name);
|
|
192
|
+
return service && this.serviceValue(name, service, consumer);
|
|
193
|
+
}
|
|
194
|
+
serviceValue(name, service, consumer) {
|
|
195
|
+
if (service.kind === 'shared')
|
|
196
|
+
return service.value;
|
|
197
|
+
if (!consumer)
|
|
198
|
+
throw new PluginError(`服务 ${name} 必须在插件上下文中注入`);
|
|
199
|
+
if (!service.instances.has(consumer))
|
|
200
|
+
service.instances.set(consumer, service.factory(consumer));
|
|
201
|
+
return service.instances.get(consumer);
|
|
154
202
|
}
|
|
155
203
|
getConfig(name) {
|
|
156
204
|
const entry = this.entries.get(name);
|
|
@@ -264,9 +312,11 @@ export class PluginManager {
|
|
|
264
312
|
this.started = true;
|
|
265
313
|
await this.packages.init();
|
|
266
314
|
this.releaseRoots = registerPluginRoots([...this.dirs, this.packages.dir]);
|
|
267
|
-
for (const
|
|
268
|
-
if (definition)
|
|
269
|
-
|
|
315
|
+
for (const entry of this.entries.values()) {
|
|
316
|
+
if (!entry.definition)
|
|
317
|
+
continue;
|
|
318
|
+
await this.initializeDefaultOnNewBots(entry.definition);
|
|
319
|
+
this.syncCredentialGroups(entry);
|
|
270
320
|
}
|
|
271
321
|
for (const dir of this.dirs)
|
|
272
322
|
await this.discover(dir);
|
|
@@ -290,6 +340,7 @@ export class PluginManager {
|
|
|
290
340
|
pluginName = `${base}-${suffix++}`;
|
|
291
341
|
const state = {
|
|
292
342
|
name: pluginName,
|
|
343
|
+
iconUrl: pluginIconFromKeywords(manifest.keywords),
|
|
293
344
|
source: 'package',
|
|
294
345
|
state: 'failed',
|
|
295
346
|
enabled: this.app.config.plugins[pluginName]?.enabled ?? true,
|
|
@@ -451,9 +502,11 @@ export class PluginManager {
|
|
|
451
502
|
catch (error) {
|
|
452
503
|
await this.unload(old);
|
|
453
504
|
old.definition = undefined;
|
|
505
|
+
old.state.credentialGroups = undefined;
|
|
454
506
|
old.failure = 'import';
|
|
455
507
|
this.setState(old, 'failed', errorMessage(error));
|
|
456
508
|
await this.reconcile();
|
|
509
|
+
this.publishListChange();
|
|
457
510
|
}
|
|
458
511
|
}
|
|
459
512
|
else
|
|
@@ -497,6 +550,7 @@ export class PluginManager {
|
|
|
497
550
|
this.entries.set(name, entry);
|
|
498
551
|
this.files.set(path, entry);
|
|
499
552
|
await this.reconcile();
|
|
553
|
+
this.publishListChange();
|
|
500
554
|
throw error;
|
|
501
555
|
}
|
|
502
556
|
if (purge)
|
|
@@ -508,10 +562,31 @@ export class PluginManager {
|
|
|
508
562
|
const manifest = this.entries.get(name)?.manifest;
|
|
509
563
|
return manifest ? this.packages.info(manifest) : undefined;
|
|
510
564
|
}
|
|
565
|
+
packageReadme(name) {
|
|
566
|
+
const manifest = this.entries.get(name)?.manifest;
|
|
567
|
+
if (!manifest)
|
|
568
|
+
throw new ManagementError(404, 'Plugin package is not installed');
|
|
569
|
+
return this.packages.installedReadme(manifest);
|
|
570
|
+
}
|
|
511
571
|
subscribePackageUpdates(subscriber) {
|
|
512
572
|
this.packageUpdateSubscribers.add(subscriber);
|
|
513
573
|
return () => this.packageUpdateSubscribers.delete(subscriber);
|
|
514
574
|
}
|
|
575
|
+
/** 发现、移除或重读插件定义;运行状态另由 plugin.state 通知。 */
|
|
576
|
+
subscribeListChanges(subscriber) {
|
|
577
|
+
this.listSubscribers.add(subscriber);
|
|
578
|
+
return () => this.listSubscribers.delete(subscriber);
|
|
579
|
+
}
|
|
580
|
+
publishListChange() {
|
|
581
|
+
for (const subscriber of this.listSubscribers) {
|
|
582
|
+
try {
|
|
583
|
+
subscriber();
|
|
584
|
+
}
|
|
585
|
+
catch (error) {
|
|
586
|
+
this.app.logger.error({ err: error }, 'Plugin list subscriber failed');
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
515
590
|
updateConfig(name, value) {
|
|
516
591
|
return this.serial(async () => {
|
|
517
592
|
const entry = this.requireAvailable(name);
|
|
@@ -598,7 +673,7 @@ export class PluginManager {
|
|
|
598
673
|
else
|
|
599
674
|
this.app.logger.debug({ plugin: name, field }, 'Plugin configuration changed in memory only');
|
|
600
675
|
}
|
|
601
|
-
add(definition, source, path, files = new Set(), release) {
|
|
676
|
+
add(definition, source, path, files = new Set(), release, manifest) {
|
|
602
677
|
const name = definition.name;
|
|
603
678
|
const state = {
|
|
604
679
|
name,
|
|
@@ -610,7 +685,7 @@ export class PluginManager {
|
|
|
610
685
|
inject: [],
|
|
611
686
|
capabilities: definition.capabilities,
|
|
612
687
|
};
|
|
613
|
-
const entry = { state, definition, files, release };
|
|
688
|
+
const entry = { state, definition, files, release, manifest };
|
|
614
689
|
this.metadata(entry);
|
|
615
690
|
if (this.entries.has(name)) {
|
|
616
691
|
entry.failure = 'duplicate';
|
|
@@ -618,8 +693,11 @@ export class PluginManager {
|
|
|
618
693
|
entry.state.error = `Duplicate plugin name "${name}"`;
|
|
619
694
|
this.duplicates.push(entry);
|
|
620
695
|
}
|
|
621
|
-
else
|
|
696
|
+
else {
|
|
697
|
+
if (this.started)
|
|
698
|
+
this.syncCredentialGroups(entry);
|
|
622
699
|
this.entries.set(name, entry);
|
|
700
|
+
}
|
|
623
701
|
if (path)
|
|
624
702
|
this.files.set(path, entry);
|
|
625
703
|
return entry;
|
|
@@ -631,7 +709,13 @@ export class PluginManager {
|
|
|
631
709
|
entry.state.capabilities = definition.capabilities;
|
|
632
710
|
entry.state.version = definition.version;
|
|
633
711
|
entry.state.description = definition.description;
|
|
712
|
+
entry.state.iconUrl = entry.manifest
|
|
713
|
+
? pluginIconFromKeywords(entry.manifest.keywords)
|
|
714
|
+
: parsePluginIconUrl(definition.iconUrl);
|
|
634
715
|
entry.state.depends = definition.depends;
|
|
716
|
+
entry.state.credentialGroups =
|
|
717
|
+
definition.credentials &&
|
|
718
|
+
Object.entries(definition.credentials).map(([id, { title, description }]) => Object.freeze({ id, title: title.trim(), description: description?.trim() }));
|
|
635
719
|
const dependencies = serviceDependencies(definition.inject);
|
|
636
720
|
entry.state.inject = [
|
|
637
721
|
...dependencies.required.map((name) => ({ name, optional: false })),
|
|
@@ -641,6 +725,9 @@ export class PluginManager {
|
|
|
641
725
|
? z.toJSONSchema(definition.config, { unrepresentable: 'any' })
|
|
642
726
|
: undefined;
|
|
643
727
|
}
|
|
728
|
+
syncCredentialGroups(entry) {
|
|
729
|
+
this.app.storage.credentials.retireMissingGroups(entry.state.name, entry.state.credentialGroups?.map(({ id }) => id) ?? []);
|
|
730
|
+
}
|
|
644
731
|
async discover(dir) {
|
|
645
732
|
let names;
|
|
646
733
|
try {
|
|
@@ -754,6 +841,7 @@ export class PluginManager {
|
|
|
754
841
|
this.files.delete(entry.state.path);
|
|
755
842
|
entry.release?.();
|
|
756
843
|
}
|
|
844
|
+
this.publishListChange();
|
|
757
845
|
}
|
|
758
846
|
async rescanFiles() {
|
|
759
847
|
const previous = new Set(this.files.keys());
|
|
@@ -776,10 +864,13 @@ export class PluginManager {
|
|
|
776
864
|
for (const dir of this.dirs)
|
|
777
865
|
await this.discover(dir);
|
|
778
866
|
await this.reconcile();
|
|
867
|
+
const added = [...this.files]
|
|
868
|
+
.filter(([path, entry]) => !previous.has(path) && entry.state.source === 'file')
|
|
869
|
+
.map(([, entry]) => entry.state.name);
|
|
870
|
+
if (added.length)
|
|
871
|
+
this.publishListChange();
|
|
779
872
|
return {
|
|
780
|
-
added
|
|
781
|
-
.filter(([path, entry]) => !previous.has(path) && entry.state.source === 'file')
|
|
782
|
-
.map(([, entry]) => entry.state.name),
|
|
873
|
+
added,
|
|
783
874
|
removed: missing.map((entry) => entry.state.name),
|
|
784
875
|
};
|
|
785
876
|
}
|
|
@@ -824,7 +915,7 @@ export class PluginManager {
|
|
|
824
915
|
const target = manifest.exports === undefined ? (manifest.main ?? 'index.js') : select(exports);
|
|
825
916
|
if (!target || !/\.(?:js|mjs)$/.test(target))
|
|
826
917
|
throw new ManagementError(400, 'Plugin package entry must be JavaScript');
|
|
827
|
-
const root =
|
|
918
|
+
const root = this.packages.packageRoot(manifest.name);
|
|
828
919
|
const path = resolve(root, target);
|
|
829
920
|
this.ensureInside(root, path);
|
|
830
921
|
await this.ensureRealInside(root, path);
|
|
@@ -842,9 +933,7 @@ export class PluginManager {
|
|
|
842
933
|
throw new ManagementError(409, 'Updated package changed its plugin name');
|
|
843
934
|
if (!existing) {
|
|
844
935
|
await this.initializeDefaultOnNewBots(definition);
|
|
845
|
-
|
|
846
|
-
entry.manifest = manifest;
|
|
847
|
-
return entry;
|
|
936
|
+
return this.add(definition, 'package', path, imported.files, imported.release, manifest);
|
|
848
937
|
}
|
|
849
938
|
const oldPath = existing.state.path;
|
|
850
939
|
const oldManifest = existing.manifest;
|
|
@@ -888,6 +977,8 @@ export class PluginManager {
|
|
|
888
977
|
throw new ManagementError(409, 'Plugin changed during package installation');
|
|
889
978
|
const entry = await this.addPackage(installed, previous);
|
|
890
979
|
await this.reconcile();
|
|
980
|
+
if (!previous)
|
|
981
|
+
this.publishListChange();
|
|
891
982
|
return { ...entry.state };
|
|
892
983
|
});
|
|
893
984
|
}
|
|
@@ -1215,7 +1306,9 @@ export class PluginManager {
|
|
|
1215
1306
|
entry.release?.();
|
|
1216
1307
|
entry.release = release;
|
|
1217
1308
|
this.metadata(entry);
|
|
1309
|
+
this.syncCredentialGroups(entry);
|
|
1218
1310
|
await this.reconcile();
|
|
1311
|
+
this.publishListChange();
|
|
1219
1312
|
}
|
|
1220
1313
|
async handleChange(dir, path) {
|
|
1221
1314
|
const removed = [];
|
|
@@ -1233,22 +1326,8 @@ export class PluginManager {
|
|
|
1233
1326
|
removed.push(entry);
|
|
1234
1327
|
}
|
|
1235
1328
|
}
|
|
1236
|
-
if (removed.length
|
|
1237
|
-
|
|
1238
|
-
for (const entry of removed)
|
|
1239
|
-
entry.state.enabled = false;
|
|
1240
|
-
await this.reconcile();
|
|
1241
|
-
for (const entry of removed) {
|
|
1242
|
-
if (this.entries.get(entry.state.name) === entry)
|
|
1243
|
-
this.entries.delete(entry.state.name);
|
|
1244
|
-
const duplicate = this.duplicates.indexOf(entry);
|
|
1245
|
-
if (duplicate !== -1)
|
|
1246
|
-
this.duplicates.splice(duplicate, 1);
|
|
1247
|
-
if (entry.state.path)
|
|
1248
|
-
this.files.delete(entry.state.path);
|
|
1249
|
-
entry.release?.();
|
|
1250
|
-
}
|
|
1251
|
-
}
|
|
1329
|
+
if (removed.length)
|
|
1330
|
+
await this.removeEntries(removed);
|
|
1252
1331
|
const previous = new Set(this.files.keys());
|
|
1253
1332
|
await this.discover(dir);
|
|
1254
1333
|
const affected = [...this.entries.values()].filter((entry) => entry.state.path &&
|
|
@@ -1263,6 +1342,8 @@ export class PluginManager {
|
|
|
1263
1342
|
}
|
|
1264
1343
|
}
|
|
1265
1344
|
await this.reconcile();
|
|
1345
|
+
if (this.files.size !== previous.size)
|
|
1346
|
+
this.publishListChange();
|
|
1266
1347
|
}
|
|
1267
1348
|
setState(entry, state, error) {
|
|
1268
1349
|
if (entry.state.state === state && entry.state.error === error)
|
|
@@ -35,6 +35,7 @@ export interface PackageSearchItem {
|
|
|
35
35
|
name: string;
|
|
36
36
|
version: string;
|
|
37
37
|
description?: string;
|
|
38
|
+
iconUrl?: string;
|
|
38
39
|
author?: string;
|
|
39
40
|
date?: string;
|
|
40
41
|
links: {
|
|
@@ -147,7 +148,9 @@ export declare class PackageService {
|
|
|
147
148
|
}, host: PackageHost, options?: PackageOptions);
|
|
148
149
|
init(): Promise<void>;
|
|
149
150
|
dependencies(): Promise<Record<string, string>>;
|
|
151
|
+
packageRoot(name: string): string;
|
|
150
152
|
manifest(name: string): Promise<PackageManifest>;
|
|
153
|
+
installedReadme(manifest: PackageManifest): Promise<string | undefined>;
|
|
151
154
|
info(manifest: PackageManifest): PackageInfo;
|
|
152
155
|
private refreshLatest;
|
|
153
156
|
ensureLatest(): Promise<void>;
|
|
@@ -174,6 +177,7 @@ export declare class PackageService {
|
|
|
174
177
|
installed?: string | undefined;
|
|
175
178
|
versions: {
|
|
176
179
|
version: string;
|
|
180
|
+
iconUrl: string | undefined;
|
|
177
181
|
date?: string | undefined;
|
|
178
182
|
peerRange?: string | undefined;
|
|
179
183
|
compatible: boolean;
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
2
|
import { randomUUID } from 'node:crypto';
|
|
3
|
-
import {
|
|
3
|
+
import { createReadStream } from 'node:fs';
|
|
4
|
+
import { access, mkdir, readdir, readFile, writeFile } from 'node:fs/promises';
|
|
4
5
|
import { delimiter, isAbsolute, join, resolve } from 'node:path';
|
|
5
6
|
import { fileURLToPath } from 'node:url';
|
|
6
7
|
import { gunzipSync } from 'fflate';
|
|
7
8
|
import { maxSatisfying, rcompare, satisfies, valid, validRange } from 'semver';
|
|
8
9
|
import { z } from 'zod';
|
|
10
|
+
import { pluginIconFromKeywords } from './icon.js';
|
|
9
11
|
import { ManagementError } from './upload.js';
|
|
10
12
|
const manifestSchema = z.object({
|
|
11
13
|
name: z.string(),
|
|
@@ -83,7 +85,13 @@ export function redact(value) {
|
|
|
83
85
|
return result;
|
|
84
86
|
}
|
|
85
87
|
const maxTarballBytes = 5 * 1024 * 1024;
|
|
86
|
-
const
|
|
88
|
+
const maxReadmeBytes = 200 * 1024;
|
|
89
|
+
const readmeFilename = /^readme(?:\.(?:md|markdown|txt))?$/i;
|
|
90
|
+
function decodeReadme(bytes) {
|
|
91
|
+
return new TextDecoder().decode(bytes.subarray(0, maxReadmeBytes), {
|
|
92
|
+
stream: bytes.length > maxReadmeBytes,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
87
95
|
/** The top-level README of an npm tarball (gzipped ustar), or undefined when there is none. */
|
|
88
96
|
export function readmeFromTarball(tgz) {
|
|
89
97
|
const tar = gunzipSync(tgz);
|
|
@@ -104,7 +112,8 @@ export function readmeFromTarball(tgz) {
|
|
|
104
112
|
const prefix = field(offset + 345, 155);
|
|
105
113
|
const path = prefix ? `${prefix}/${name}` : name;
|
|
106
114
|
const start = offset + 512;
|
|
107
|
-
|
|
115
|
+
const slash = path.indexOf('/');
|
|
116
|
+
if ((type === '' || type === '0') && slash > 0 && readmeFilename.test(path.slice(slash + 1)))
|
|
108
117
|
return decoder.decode(tar.subarray(start, start + size));
|
|
109
118
|
offset = start + Math.ceil(size / 512) * 512;
|
|
110
119
|
}
|
|
@@ -165,6 +174,7 @@ export class FetchRegistry {
|
|
|
165
174
|
name: item.name,
|
|
166
175
|
version: item.version,
|
|
167
176
|
...(item.description === undefined ? {} : { description: item.description }),
|
|
177
|
+
iconUrl: pluginIconFromKeywords(item.keywords),
|
|
168
178
|
...(item.author === undefined
|
|
169
179
|
? {}
|
|
170
180
|
: { author: typeof item.author === 'string' ? item.author : item.author.name }),
|
|
@@ -291,15 +301,35 @@ export class PackageService {
|
|
|
291
301
|
const data = JSON.parse(await readFile(join(this.dir, 'package.json'), 'utf8'));
|
|
292
302
|
return data.dependencies ?? {};
|
|
293
303
|
}
|
|
304
|
+
packageRoot(name) {
|
|
305
|
+
return join(this.dir, 'node_modules', ...name.split('/'));
|
|
306
|
+
}
|
|
294
307
|
async manifest(name) {
|
|
295
308
|
if (!validPackageName(name))
|
|
296
309
|
throw new ManagementError(400, 'Invalid package name');
|
|
297
|
-
const data = JSON.parse(await readFile(join(this.
|
|
310
|
+
const data = JSON.parse(await readFile(join(this.packageRoot(name), 'package.json'), 'utf8'));
|
|
298
311
|
const parsed = manifestSchema.safeParse(data);
|
|
299
312
|
if (!parsed.success || parsed.data.name !== name || !valid(parsed.data.version))
|
|
300
313
|
throw new ManagementError(400, 'Invalid installed package metadata');
|
|
301
314
|
return parsed.data;
|
|
302
315
|
}
|
|
316
|
+
async installedReadme(manifest) {
|
|
317
|
+
const root = this.packageRoot(manifest.name);
|
|
318
|
+
try {
|
|
319
|
+
const files = await readdir(root, { withFileTypes: true });
|
|
320
|
+
const readme = files.find((file) => file.isFile() && readmeFilename.test(file.name));
|
|
321
|
+
if (!readme)
|
|
322
|
+
return undefined;
|
|
323
|
+
const stream = createReadStream(join(root, readme.name), { end: maxReadmeBytes });
|
|
324
|
+
const chunks = await stream.toArray();
|
|
325
|
+
return decodeReadme(Buffer.concat(chunks));
|
|
326
|
+
}
|
|
327
|
+
catch (error) {
|
|
328
|
+
if (error.code === 'ENOENT')
|
|
329
|
+
return undefined;
|
|
330
|
+
throw error;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
303
333
|
info(manifest) {
|
|
304
334
|
const peerRange = manifest.peerDependencies?.['@asabot/core'];
|
|
305
335
|
const cached = this.latestCache.get(manifest.name);
|
|
@@ -411,10 +441,7 @@ export class PackageService {
|
|
|
411
441
|
const data = cached.value;
|
|
412
442
|
const installed = this.host.installedPackages().get(name)?.manifest.version;
|
|
413
443
|
const repository = typeof data.repository === 'string' ? data.repository : data.repository?.url;
|
|
414
|
-
const
|
|
415
|
-
const readme = bytes && bytes.length > 200 * 1024
|
|
416
|
-
? new TextDecoder().decode(bytes.subarray(0, 200 * 1024), { stream: true })
|
|
417
|
-
: data.readme;
|
|
444
|
+
const readme = data.readme === undefined ? undefined : decodeReadme(Buffer.from(data.readme, 'utf8'));
|
|
418
445
|
const versions = Object.keys(data.versions)
|
|
419
446
|
.filter((version) => valid(version))
|
|
420
447
|
.sort(rcompare)
|
|
@@ -424,6 +451,7 @@ export class PackageService {
|
|
|
424
451
|
.object({
|
|
425
452
|
peerDependencies: z.record(z.string(), z.string()).optional(),
|
|
426
453
|
deprecated: z.string().optional(),
|
|
454
|
+
keywords: z.unknown().optional(),
|
|
427
455
|
})
|
|
428
456
|
.safeParse(data.versions[version]);
|
|
429
457
|
const peerRange = value.success ? value.data.peerDependencies?.['@asabot/core'] : undefined;
|
|
@@ -431,6 +459,7 @@ export class PackageService {
|
|
|
431
459
|
const date = data.time?.[version];
|
|
432
460
|
return {
|
|
433
461
|
version,
|
|
462
|
+
iconUrl: value.success ? pluginIconFromKeywords(value.data.keywords) : undefined,
|
|
434
463
|
...(date === undefined ? {} : { date }),
|
|
435
464
|
...(peerRange === undefined ? {} : { peerRange }),
|
|
436
465
|
compatible: !!peerRange && !!validRange(peerRange) && satisfies(frameworkVersion, peerRange),
|
|
@@ -459,7 +488,8 @@ export class PackageService {
|
|
|
459
488
|
(version !== undefined &&
|
|
460
489
|
(version.length > 256 || (!validRange(version) && !/^[a-zA-Z0-9][a-zA-Z0-9._+-]*$/.test(version)))))
|
|
461
490
|
throw new ManagementError(400, 'Invalid package name or version');
|
|
462
|
-
|
|
491
|
+
// The abbreviated npm packument omits keywords from version manifests.
|
|
492
|
+
const metadata = await this.registry.fullMetadata(name);
|
|
463
493
|
this.latestCache.set(name, { value: metadata['dist-tags']?.latest, expires: Date.now() + 3_600_000 });
|
|
464
494
|
const selected = version === undefined
|
|
465
495
|
? metadata['dist-tags']?.latest
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Bot } from '../bot.ts';
|
|
2
2
|
import type { CapabilityRequirements } from '../capabilities.ts';
|
|
3
3
|
import type { BaseEvent } from '../event/index.ts';
|
|
4
|
+
import type { PluginCredentialGroup } from './credentials.ts';
|
|
4
5
|
/** 插件面板页面的标题、图标、静态目录和可选入口文件。 */
|
|
5
6
|
export interface PanelPageOptions {
|
|
6
7
|
title: string;
|
|
@@ -14,11 +15,22 @@ export interface PanelPage {
|
|
|
14
15
|
root: string;
|
|
15
16
|
entry: string;
|
|
16
17
|
}
|
|
18
|
+
/** 由框架绑定的服务使用方;事件信息只在该插件的分发处理期间可用。 */
|
|
19
|
+
export interface ServiceConsumer {
|
|
20
|
+
readonly plugin: string;
|
|
21
|
+
readonly signal: AbortSignal;
|
|
22
|
+
event(): {
|
|
23
|
+
eventId: string;
|
|
24
|
+
platform: string;
|
|
25
|
+
selfId: string;
|
|
26
|
+
} | undefined;
|
|
27
|
+
}
|
|
17
28
|
/** 插件的来源、运行状态、依赖服务及可选错误信息。 */
|
|
18
29
|
export interface PluginState {
|
|
19
30
|
name: string;
|
|
20
31
|
version?: string;
|
|
21
32
|
description?: string;
|
|
33
|
+
iconUrl?: string;
|
|
22
34
|
depends?: string[];
|
|
23
35
|
source: 'builtin' | 'file' | 'package';
|
|
24
36
|
path?: string;
|
|
@@ -32,6 +44,18 @@ export interface PluginState {
|
|
|
32
44
|
}[];
|
|
33
45
|
configSchema?: Record<string, unknown>;
|
|
34
46
|
capabilities?: CapabilityRequirements;
|
|
47
|
+
credentialGroups?: readonly PluginCredentialGroup[];
|
|
48
|
+
}
|
|
49
|
+
/** 已发现插件的公开目录条目,不包含路径、配置和错误详情。 */
|
|
50
|
+
export interface PluginCatalogEntry {
|
|
51
|
+
readonly name: string;
|
|
52
|
+
readonly version?: string;
|
|
53
|
+
readonly description?: string;
|
|
54
|
+
readonly source: PluginState['source'];
|
|
55
|
+
readonly state: PluginState['state'];
|
|
56
|
+
readonly enabled: boolean;
|
|
57
|
+
readonly provides: readonly string[];
|
|
58
|
+
readonly inject: readonly Readonly<PluginState['inject'][number]>[];
|
|
35
59
|
}
|
|
36
60
|
/** 插件状态变化事件;机器人可能尚未上线,bot 因而可缺省。 */
|
|
37
61
|
export interface PluginStateEvent extends Omit<BaseEvent, 'bot'> {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { DatabaseSync } from 'node:sqlite';
|
|
2
|
+
import { PluginError } from '../errors.ts';
|
|
3
|
+
import type { PluginCredentialInfo, PluginCredentialValue } from '../plugin/credentials.ts';
|
|
4
|
+
export declare class CredentialUnavailableError extends PluginError {
|
|
5
|
+
constructor(cause?: unknown);
|
|
6
|
+
}
|
|
7
|
+
export declare class DuplicateCredentialError extends PluginError {
|
|
8
|
+
constructor();
|
|
9
|
+
}
|
|
10
|
+
export declare class CredentialStore {
|
|
11
|
+
private readonly database;
|
|
12
|
+
private readonly keyFile;
|
|
13
|
+
constructor(database: () => DatabaseSync, dataDir: string);
|
|
14
|
+
has(plugin: string): boolean;
|
|
15
|
+
list(plugin: string, { group, activeOnly }?: {
|
|
16
|
+
group?: string;
|
|
17
|
+
activeOnly?: boolean;
|
|
18
|
+
}): PluginCredentialInfo[];
|
|
19
|
+
read(plugin: string, id: string): string | undefined;
|
|
20
|
+
readAll(plugin: string, group: string): PluginCredentialValue[];
|
|
21
|
+
create(plugin: string, group: string, label: string, value?: string): PluginCredentialInfo;
|
|
22
|
+
revoke(plugin: string, id: string): boolean;
|
|
23
|
+
retireMissingGroups(plugin: string, groups: string[]): void;
|
|
24
|
+
private readKey;
|
|
25
|
+
}
|