@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
package/dist/server/settings.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
1
2
|
import { z } from 'zod';
|
|
2
|
-
import { adapters } from '../adapters/index.js';
|
|
3
3
|
import { formatIssuePath, InterpolationError, interpolateConfigValues, readRawConfig, } from '../core/config/load.js';
|
|
4
4
|
import { configSchema } from '../core/config/schema.js';
|
|
5
5
|
import { writeConfigValue } from '../core/config/write.js';
|
|
6
|
+
import { connectionSchema } from '../core/connections.js';
|
|
7
|
+
import { ConfigError } from '../core/errors.js';
|
|
8
|
+
import { actorIdentitySchema } from '../core/identity.js';
|
|
6
9
|
import { constantTimeEqual } from '../core/utils/security.js';
|
|
7
10
|
const placeholder = '********';
|
|
8
|
-
const sensitive = new Set(['accessToken', 'token', 'password', 'apiToken']);
|
|
11
|
+
const sensitive = new Set(['accessToken', 'token', 'password', 'apiToken', 'appSecret']);
|
|
9
12
|
const reference = /^\$\{[A-Za-z_][A-Za-z\d_]*(?::-[^}]*)?\}$/;
|
|
10
13
|
const commandInput = z
|
|
11
14
|
.object({
|
|
@@ -19,10 +22,9 @@ const commandInput = z
|
|
|
19
22
|
.strict();
|
|
20
23
|
export const settingsInput = z
|
|
21
24
|
.object({
|
|
22
|
-
superusers: z.array(
|
|
25
|
+
superusers: z.array(actorIdentitySchema).optional(),
|
|
23
26
|
timezone: z.string().optional(),
|
|
24
27
|
command: commandInput.optional(),
|
|
25
|
-
adapters: z.record(z.string(), z.unknown()).optional(),
|
|
26
28
|
})
|
|
27
29
|
.strict();
|
|
28
30
|
export const setupInput = z
|
|
@@ -33,12 +35,23 @@ export const setupInput = z
|
|
|
33
35
|
.string()
|
|
34
36
|
.min(8)
|
|
35
37
|
.refine((value) => !value.includes('${'), 'Password must not contain "${"'),
|
|
36
|
-
superusers: z.array(
|
|
37
|
-
adapters: z.record(z.string(), z.unknown()).optional(),
|
|
38
|
+
superusers: z.array(actorIdentitySchema),
|
|
38
39
|
})
|
|
39
40
|
.strict();
|
|
40
41
|
export class SetupCodeError extends Error {
|
|
41
42
|
}
|
|
43
|
+
export class ConnectionSetupError extends Error {
|
|
44
|
+
status;
|
|
45
|
+
constructor(status, message) {
|
|
46
|
+
super(message);
|
|
47
|
+
this.status = status;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export const connectionSetupSaveInput = z.strictObject({
|
|
51
|
+
name: connectionSchema.shape.name.optional(),
|
|
52
|
+
enabled: z.boolean().optional(),
|
|
53
|
+
replace: z.boolean().optional(),
|
|
54
|
+
});
|
|
42
55
|
export class SettingsValidationError extends Error {
|
|
43
56
|
issues;
|
|
44
57
|
constructor(issues) {
|
|
@@ -49,9 +62,10 @@ export class SettingsValidationError extends Error {
|
|
|
49
62
|
function isRecord(value) {
|
|
50
63
|
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
51
64
|
}
|
|
52
|
-
function merge(previous, next, path = []) {
|
|
65
|
+
function merge(previous, next, path = [], schema) {
|
|
53
66
|
const key = path.at(-1);
|
|
54
|
-
if (typeof key === 'string' && sensitive.has(key) &&
|
|
67
|
+
if (((typeof key === 'string' && sensitive.has(key)) || (isRecord(schema) && schema.writeOnly === true)) &&
|
|
68
|
+
next === placeholder) {
|
|
55
69
|
if (typeof previous !== 'string') {
|
|
56
70
|
throw new SettingsValidationError([
|
|
57
71
|
{ path: formatIssuePath(path), message: 'No existing value to preserve' },
|
|
@@ -60,12 +74,12 @@ function merge(previous, next, path = []) {
|
|
|
60
74
|
return previous;
|
|
61
75
|
}
|
|
62
76
|
if (Array.isArray(next))
|
|
63
|
-
return next.map((item, index) => merge(Array.isArray(previous) ? previous[index] : undefined, item, [...path, index]));
|
|
77
|
+
return next.map((item, index) => merge(Array.isArray(previous) ? previous[index] : undefined, item, [...path, index], isRecord(schema) ? schema.items : undefined));
|
|
64
78
|
if (isRecord(next)) {
|
|
65
79
|
const result = isRecord(previous) ? { ...previous } : {};
|
|
66
80
|
for (const [name, value] of Object.entries(next)) {
|
|
67
81
|
Object.defineProperty(result, name, {
|
|
68
|
-
value: merge(isRecord(previous) ? previous[name] : undefined, value, [...path, name]),
|
|
82
|
+
value: merge(isRecord(previous) ? previous[name] : undefined, value, [...path, name], property(schema, name)),
|
|
69
83
|
enumerable: true,
|
|
70
84
|
configurable: true,
|
|
71
85
|
writable: true,
|
|
@@ -75,17 +89,20 @@ function merge(previous, next, path = []) {
|
|
|
75
89
|
}
|
|
76
90
|
return next;
|
|
77
91
|
}
|
|
78
|
-
function
|
|
79
|
-
|
|
92
|
+
function property(schema, name) {
|
|
93
|
+
return isRecord(schema) && isRecord(schema.properties) ? schema.properties[name] : undefined;
|
|
94
|
+
}
|
|
95
|
+
function mask(value, key = '', schema) {
|
|
96
|
+
if (typeof value === 'string' && (sensitive.has(key) || (isRecord(schema) && schema.writeOnly === true))) {
|
|
80
97
|
return value && !reference.test(value) ? placeholder : value;
|
|
81
98
|
}
|
|
82
99
|
if (Array.isArray(value))
|
|
83
|
-
return value.map((item) => mask(item));
|
|
100
|
+
return value.map((item) => mask(item, '', isRecord(schema) ? schema.items : undefined));
|
|
84
101
|
if (isRecord(value))
|
|
85
|
-
return Object.fromEntries(Object.entries(value).map(([name, item]) => [name, mask(item, name)]));
|
|
102
|
+
return Object.fromEntries(Object.entries(value).map(([name, item]) => [name, mask(item, name, property(schema, name))]));
|
|
86
103
|
return value;
|
|
87
104
|
}
|
|
88
|
-
function validate(raw) {
|
|
105
|
+
function validate(raw, app) {
|
|
89
106
|
let interpolated;
|
|
90
107
|
try {
|
|
91
108
|
interpolated = interpolateConfigValues(raw);
|
|
@@ -99,29 +116,15 @@ function validate(raw) {
|
|
|
99
116
|
if (!result.success) {
|
|
100
117
|
throw new SettingsValidationError(result.error.issues.map((issue) => ({ path: formatIssuePath(issue.path), message: issue.message })));
|
|
101
118
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if (
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
113
|
-
const adapter = definition.configSchema.safeParse(value);
|
|
114
|
-
if (adapter.success)
|
|
115
|
-
parsed[name] = adapter.data;
|
|
116
|
-
else
|
|
117
|
-
issues.push(...adapter.error.issues.map((issue) => ({
|
|
118
|
-
path: formatIssuePath(['adapters', name, ...issue.path]),
|
|
119
|
-
message: issue.message,
|
|
120
|
-
})));
|
|
121
|
-
}
|
|
122
|
-
if (issues.length)
|
|
123
|
-
throw new SettingsValidationError(issues);
|
|
124
|
-
return { config: result.data, adapters: parsed };
|
|
119
|
+
try {
|
|
120
|
+
app.connections.validate(result.data.connections);
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
if (error instanceof ConfigError)
|
|
124
|
+
throw new SettingsValidationError([{ path: 'connections', message: error.message }]);
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
return { config: result.data };
|
|
125
128
|
}
|
|
126
129
|
export class SettingsService {
|
|
127
130
|
app;
|
|
@@ -144,23 +147,12 @@ export class SettingsService {
|
|
|
144
147
|
}
|
|
145
148
|
async get() {
|
|
146
149
|
const raw = await this.raw();
|
|
147
|
-
const { config } = validate(raw);
|
|
148
|
-
const configured = isRecord(raw.adapters) ? raw.adapters : {};
|
|
150
|
+
const { config } = validate(raw, this.app);
|
|
149
151
|
const { overrides: _overrides, ...command } = config.command;
|
|
150
152
|
return {
|
|
151
153
|
superusers: config.superusers,
|
|
152
154
|
timezone: config.timezone,
|
|
153
155
|
command,
|
|
154
|
-
adapters: Object.fromEntries(Object.entries(configured).map(([name, value]) => {
|
|
155
|
-
const definition = adapters[name];
|
|
156
|
-
if (!definition)
|
|
157
|
-
throw new Error('Validated adapter is missing from registry');
|
|
158
|
-
return [
|
|
159
|
-
name,
|
|
160
|
-
{ value: mask(value), schema: z.toJSONSchema(definition.configSchema, { io: 'input' }) },
|
|
161
|
-
];
|
|
162
|
-
})),
|
|
163
|
-
available: Object.keys(adapters),
|
|
164
156
|
restartRequired: config.timezone === this.app.config.timezone ? [] : ['timezone'],
|
|
165
157
|
};
|
|
166
158
|
}
|
|
@@ -168,17 +160,10 @@ export class SettingsService {
|
|
|
168
160
|
return this.app.plugins.serial(async () => {
|
|
169
161
|
const previous = await this.raw();
|
|
170
162
|
const next = merge(previous, input);
|
|
171
|
-
|
|
172
|
-
const configured = next.adapters;
|
|
173
|
-
for (const [name, value] of Object.entries(input.adapters))
|
|
174
|
-
if (value === null)
|
|
175
|
-
delete configured[name];
|
|
176
|
-
}
|
|
177
|
-
const validated = validate(next);
|
|
163
|
+
const validated = validate(next, this.app);
|
|
178
164
|
await this.persist(previous, next);
|
|
179
165
|
this.app.config.superusers = validated.config.superusers;
|
|
180
166
|
this.app.config.command = validated.config.command;
|
|
181
|
-
await this.app.reconfigureAdapters(validated.adapters);
|
|
182
167
|
return this.get();
|
|
183
168
|
});
|
|
184
169
|
}
|
|
@@ -189,17 +174,129 @@ export class SettingsService {
|
|
|
189
174
|
const previous = await this.raw();
|
|
190
175
|
const next = merge(previous, {
|
|
191
176
|
superusers: input.superusers,
|
|
192
|
-
...(input.adapters === undefined ? {} : { adapters: input.adapters }),
|
|
193
177
|
});
|
|
194
178
|
next.server = { ...(isRecord(next.server) ? next.server : {}), panelPassword: input.password };
|
|
195
|
-
const validated = validate(next);
|
|
179
|
+
const validated = validate(next, this.app);
|
|
196
180
|
await this.persist(previous, next);
|
|
197
181
|
this.app.config.server.panelPassword = validated.config.server.panelPassword;
|
|
198
182
|
this.app.config.superusers = validated.config.superusers;
|
|
199
|
-
await this.app.reconfigureAdapters(validated.adapters);
|
|
200
183
|
this.app.setupCode = undefined;
|
|
201
184
|
});
|
|
202
185
|
}
|
|
186
|
+
async connections() {
|
|
187
|
+
const raw = await this.raw();
|
|
188
|
+
const records = z.array(connectionSchema).parse(raw.connections ?? []);
|
|
189
|
+
return this.app.connections.list().map((state) => {
|
|
190
|
+
const config = records.find((record) => record.id === state.id);
|
|
191
|
+
const definition = this.app.connections.definition(state.adapter);
|
|
192
|
+
if (definition.setup)
|
|
193
|
+
return { ...state, config: {} };
|
|
194
|
+
const schema = this.app.connections
|
|
195
|
+
.catalog()
|
|
196
|
+
.find((adapter) => adapter.id === state.adapter)?.configSchema;
|
|
197
|
+
return { ...state, config: mask(config?.config, '', schema) };
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
saveConnection(input) {
|
|
201
|
+
return this.app.plugins.serial(async () => {
|
|
202
|
+
const previous = await this.raw();
|
|
203
|
+
const records = z.array(connectionSchema).parse(previous.connections ?? []);
|
|
204
|
+
const old = records.find((record) => record.id === input.id);
|
|
205
|
+
const setup = this.app.connections.definition(input.adapter).setup;
|
|
206
|
+
const oldSetup = old && this.app.connections.definition(old.adapter).setup;
|
|
207
|
+
if ((setup || oldSetup) && (old?.adapter !== input.adapter || !isDeepStrictEqual(input.config, {})))
|
|
208
|
+
throw new ConnectionSetupError(409, '此连接的授权只能通过扫码设置');
|
|
209
|
+
const schema = this.app.connections
|
|
210
|
+
.catalog()
|
|
211
|
+
.find((adapter) => adapter.id === input.adapter)?.configSchema;
|
|
212
|
+
const nextRecord = {
|
|
213
|
+
...input,
|
|
214
|
+
config: setup
|
|
215
|
+
? old?.config
|
|
216
|
+
: merge(old?.adapter === input.adapter ? old.config : undefined, input.config, [], schema),
|
|
217
|
+
};
|
|
218
|
+
const next = {
|
|
219
|
+
...previous,
|
|
220
|
+
connections: old
|
|
221
|
+
? records.map((record) => (record.id === input.id ? nextRecord : record))
|
|
222
|
+
: [...records, nextRecord],
|
|
223
|
+
};
|
|
224
|
+
const { config } = validate(next, this.app);
|
|
225
|
+
await this.persist(previous, next);
|
|
226
|
+
this.app.config.connections = config.connections;
|
|
227
|
+
await this.app.connections.apply(config.connections);
|
|
228
|
+
return this.connections();
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
saveConnectionSetup(input) {
|
|
232
|
+
return this.app.plugins.serial(async () => {
|
|
233
|
+
const previous = await this.raw();
|
|
234
|
+
const records = z.array(connectionSchema).parse(previous.connections ?? []);
|
|
235
|
+
const current = validate(previous, this.app).config.connections;
|
|
236
|
+
const { id, adapter, previousAccount, account, options, signal } = input;
|
|
237
|
+
const definition = this.app.connections.definition(adapter);
|
|
238
|
+
const existing = current.find((record) => record.id === id);
|
|
239
|
+
const sameTarget = previousAccount
|
|
240
|
+
? existing?.adapter === adapter &&
|
|
241
|
+
isDeepStrictEqual(definition.configSchema.parse(existing.config), previousAccount.config)
|
|
242
|
+
: !existing;
|
|
243
|
+
if (!sameTarget)
|
|
244
|
+
throw new ConnectionSetupError(409, '连接已变更,请重新扫码');
|
|
245
|
+
if (previousAccount && previousAccount.selfId !== account.selfId && !options.replace)
|
|
246
|
+
throw new ConnectionSetupError(409, '请确认替换已绑定的机器人');
|
|
247
|
+
if (current.some((record) => {
|
|
248
|
+
if (record.id === id)
|
|
249
|
+
return false;
|
|
250
|
+
const other = this.app.connections.definition(record.adapter);
|
|
251
|
+
return other.platform === definition.platform && other.accountId?.(record.config) === account.selfId;
|
|
252
|
+
}))
|
|
253
|
+
throw new ConnectionSetupError(409, '该机器人已绑定到另一条连接');
|
|
254
|
+
const connection = connectionSchema.parse({
|
|
255
|
+
id,
|
|
256
|
+
adapter,
|
|
257
|
+
name: existing?.name ?? options.name ?? definition.name,
|
|
258
|
+
enabled: existing?.enabled ?? options.enabled ?? true,
|
|
259
|
+
config: account.config,
|
|
260
|
+
});
|
|
261
|
+
const next = {
|
|
262
|
+
...previous,
|
|
263
|
+
connections: existing
|
|
264
|
+
? records.map((record) => (record.id === id ? connection : record))
|
|
265
|
+
: [...records, connection],
|
|
266
|
+
};
|
|
267
|
+
const { config } = validate(next, this.app);
|
|
268
|
+
if (signal.aborted)
|
|
269
|
+
throw new ConnectionSetupError(409, '扫码已结束,请重新扫码');
|
|
270
|
+
await this.persist(previous, next);
|
|
271
|
+
if (account.adminUserId && account.selfId !== previousAccount?.selfId) {
|
|
272
|
+
const bot = { platform: definition.platform, selfId: account.selfId };
|
|
273
|
+
try {
|
|
274
|
+
this.app.policies.setAdmins(bot, [...this.app.policies.admins(bot), account.adminUserId]);
|
|
275
|
+
}
|
|
276
|
+
catch (error) {
|
|
277
|
+
await this.persist(next, previous);
|
|
278
|
+
throw error;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
this.app.config.connections = config.connections;
|
|
282
|
+
await this.app.connections.apply(config.connections);
|
|
283
|
+
if (existing?.enabled &&
|
|
284
|
+
isDeepStrictEqual(definition.configSchema.parse(existing.config), definition.configSchema.parse(account.config)))
|
|
285
|
+
await this.app.connections.reconnect(id);
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
removeConnection(id) {
|
|
289
|
+
return this.app.plugins.serial(async () => {
|
|
290
|
+
const previous = await this.raw();
|
|
291
|
+
const records = z.array(connectionSchema).parse(previous.connections ?? []);
|
|
292
|
+
const next = { ...previous, connections: records.filter((record) => record.id !== id) };
|
|
293
|
+
const { config } = validate(next, this.app);
|
|
294
|
+
await this.persist(previous, next);
|
|
295
|
+
this.app.config.connections = config.connections;
|
|
296
|
+
await this.app.connections.apply(config.connections);
|
|
297
|
+
return this.connections();
|
|
298
|
+
});
|
|
299
|
+
}
|
|
203
300
|
async persist(previous, next) {
|
|
204
301
|
if (this.app.configPath)
|
|
205
302
|
await writeConfigValue(this.app.configPath, [], next, { previousValue: previous });
|
package/dist/server/views.d.ts
CHANGED
|
@@ -1,17 +1,8 @@
|
|
|
1
1
|
import type { App } from '../core/app.ts';
|
|
2
|
-
import { type
|
|
2
|
+
import { type BotAccount } from '../core/bot-accounts.ts';
|
|
3
3
|
import type { CommandDefinition } from '../core/command/builder.ts';
|
|
4
|
+
import { type BotIdentity } from '../core/identity.ts';
|
|
4
5
|
import type { PluginState } from '../core/plugin/types.ts';
|
|
5
|
-
export declare function botView(app: App, bot: Bot): {
|
|
6
|
-
platform: string;
|
|
7
|
-
selfId: string;
|
|
8
|
-
nickname?: string | undefined;
|
|
9
|
-
avatar?: string | undefined;
|
|
10
|
-
status: "connecting" | "offline" | "online";
|
|
11
|
-
capabilities: import("../core/bot.ts").BotCapabilities;
|
|
12
|
-
connection: string;
|
|
13
|
-
url?: string | undefined;
|
|
14
|
-
};
|
|
15
6
|
export declare function commandView(app: App, command: CommandDefinition): {
|
|
16
7
|
owner: string;
|
|
17
8
|
path: string;
|
|
@@ -30,7 +21,7 @@ export declare function commandView(app: App, command: CommandDefinition): {
|
|
|
30
21
|
override?: {
|
|
31
22
|
enabled?: boolean | undefined;
|
|
32
23
|
aliases?: string[] | undefined;
|
|
33
|
-
permission?: "everyone" | "group.admin" | "group.owner" | "superuser" | undefined;
|
|
24
|
+
permission?: "bot.admin" | "everyone" | "group.admin" | "group.owner" | "superuser" | undefined;
|
|
34
25
|
cooldown?: false | {
|
|
35
26
|
duration: number;
|
|
36
27
|
scope?: "global" | "group" | "user" | undefined;
|
|
@@ -44,12 +35,15 @@ export declare function pluginView(app: App, state: PluginState): {
|
|
|
44
35
|
name: string;
|
|
45
36
|
version?: string;
|
|
46
37
|
description?: string;
|
|
38
|
+
depends?: string[];
|
|
47
39
|
source: 'builtin' | 'file' | 'package';
|
|
48
40
|
path?: string;
|
|
49
41
|
state: 'loading' | 'active' | 'failed' | 'disabled';
|
|
50
42
|
error?: string;
|
|
51
43
|
enabled: boolean;
|
|
52
44
|
configSchema?: Record<string, unknown>;
|
|
45
|
+
capabilities?: import("../core/capabilities.ts").CapabilityRequirements;
|
|
46
|
+
defaultOnNewBots: boolean;
|
|
53
47
|
provides: string[];
|
|
54
48
|
inject: {
|
|
55
49
|
name: string;
|
|
@@ -75,7 +69,7 @@ export declare function pluginView(app: App, state: PluginState): {
|
|
|
75
69
|
override?: {
|
|
76
70
|
enabled?: boolean | undefined;
|
|
77
71
|
aliases?: string[] | undefined;
|
|
78
|
-
permission?: "everyone" | "group.admin" | "group.owner" | "superuser" | undefined;
|
|
72
|
+
permission?: "bot.admin" | "everyone" | "group.admin" | "group.owner" | "superuser" | undefined;
|
|
79
73
|
cooldown?: false | {
|
|
80
74
|
duration: number;
|
|
81
75
|
scope?: "global" | "group" | "user" | undefined;
|
|
@@ -102,12 +96,13 @@ export declare function overview(app: App): {
|
|
|
102
96
|
bots: {
|
|
103
97
|
platform: string;
|
|
104
98
|
selfId: string;
|
|
105
|
-
|
|
106
|
-
|
|
99
|
+
connectionId?: string;
|
|
100
|
+
lastOnlineAt?: number;
|
|
101
|
+
nickname: string | undefined;
|
|
102
|
+
avatar: string | undefined;
|
|
107
103
|
status: "connecting" | "offline" | "online";
|
|
108
|
-
capabilities: import("../core/bot.ts").BotCapabilities;
|
|
109
104
|
connection: string;
|
|
110
|
-
|
|
105
|
+
capabilities: import("../core/bot.ts").BotCapabilities | undefined;
|
|
111
106
|
}[];
|
|
112
107
|
plugins: {
|
|
113
108
|
active: number;
|
|
@@ -119,3 +114,86 @@ export declare function overview(app: App): {
|
|
|
119
114
|
sent: number;
|
|
120
115
|
};
|
|
121
116
|
};
|
|
117
|
+
export declare function panelBot(app: App, identity: BotIdentity): {
|
|
118
|
+
platform: string;
|
|
119
|
+
selfId: string;
|
|
120
|
+
connectionId?: string;
|
|
121
|
+
lastOnlineAt?: number;
|
|
122
|
+
nickname: string | undefined;
|
|
123
|
+
avatar: string | undefined;
|
|
124
|
+
status: "connecting" | "offline" | "online";
|
|
125
|
+
connection: string;
|
|
126
|
+
capabilities: import("../core/bot.ts").BotCapabilities | undefined;
|
|
127
|
+
} | undefined;
|
|
128
|
+
export declare function panelBots(app: App): {
|
|
129
|
+
platform: string;
|
|
130
|
+
selfId: string;
|
|
131
|
+
connectionId?: string;
|
|
132
|
+
lastOnlineAt?: number;
|
|
133
|
+
nickname: string | undefined;
|
|
134
|
+
avatar: string | undefined;
|
|
135
|
+
status: "connecting" | "offline" | "online";
|
|
136
|
+
connection: string;
|
|
137
|
+
capabilities: import("../core/bot.ts").BotCapabilities | undefined;
|
|
138
|
+
}[];
|
|
139
|
+
export declare function botPluginView(app: App, bot: BotAccount, name: string): {
|
|
140
|
+
name: string;
|
|
141
|
+
version?: string;
|
|
142
|
+
description?: string;
|
|
143
|
+
depends?: string[];
|
|
144
|
+
source: 'builtin' | 'file' | 'package';
|
|
145
|
+
path?: string;
|
|
146
|
+
state: 'loading' | 'active' | 'failed' | 'disabled';
|
|
147
|
+
error?: string;
|
|
148
|
+
enabled: boolean;
|
|
149
|
+
configSchema?: Record<string, unknown>;
|
|
150
|
+
capabilities?: import("../core/capabilities.ts").CapabilityRequirements;
|
|
151
|
+
defaultOnNewBots: boolean;
|
|
152
|
+
provides: string[];
|
|
153
|
+
inject: {
|
|
154
|
+
name: string;
|
|
155
|
+
optional: boolean;
|
|
156
|
+
}[];
|
|
157
|
+
removable: boolean;
|
|
158
|
+
package?: import("../core/plugin/packages.ts").PackageInfo | undefined;
|
|
159
|
+
commands: {
|
|
160
|
+
owner: string;
|
|
161
|
+
path: string;
|
|
162
|
+
usage: string;
|
|
163
|
+
description: string;
|
|
164
|
+
aliases: string[];
|
|
165
|
+
options: {
|
|
166
|
+
flags: string[];
|
|
167
|
+
description?: string | undefined;
|
|
168
|
+
default?: {} | null | undefined;
|
|
169
|
+
}[];
|
|
170
|
+
examples: string[];
|
|
171
|
+
permission: boolean;
|
|
172
|
+
scope: "all" | "group" | "private";
|
|
173
|
+
hidden: boolean;
|
|
174
|
+
override?: {
|
|
175
|
+
enabled?: boolean | undefined;
|
|
176
|
+
aliases?: string[] | undefined;
|
|
177
|
+
permission?: "bot.admin" | "everyone" | "group.admin" | "group.owner" | "superuser" | undefined;
|
|
178
|
+
cooldown?: false | {
|
|
179
|
+
duration: number;
|
|
180
|
+
scope?: "global" | "group" | "user" | undefined;
|
|
181
|
+
} | undefined;
|
|
182
|
+
hidden?: boolean | undefined;
|
|
183
|
+
} | undefined;
|
|
184
|
+
disabledGroups: string[];
|
|
185
|
+
overrideIssues: string[];
|
|
186
|
+
}[];
|
|
187
|
+
config?: {} | null | undefined;
|
|
188
|
+
page?: {
|
|
189
|
+
title: string;
|
|
190
|
+
icon?: string | undefined;
|
|
191
|
+
} | undefined;
|
|
192
|
+
botEnabled: boolean;
|
|
193
|
+
botDefaultEnabled: boolean;
|
|
194
|
+
overrideEnabled: boolean | undefined;
|
|
195
|
+
override: Record<string, unknown> | undefined;
|
|
196
|
+
effectiveConfig: unknown;
|
|
197
|
+
configSources: Record<string, string>;
|
|
198
|
+
compatibility: import("../core/capabilities.ts").Compatibility | undefined;
|
|
199
|
+
};
|
package/dist/server/views.js
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
import { readFileSync } from 'node:fs';
|
|
2
|
-
import {
|
|
2
|
+
import { botAvatar } from '../core/bot-accounts.js';
|
|
3
|
+
import { compatibility } from '../core/capabilities.js';
|
|
3
4
|
import { formatUsage } from '../core/command/parse.js';
|
|
5
|
+
import { botKey } from '../core/identity.js';
|
|
4
6
|
const version = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf8')).version;
|
|
5
|
-
|
|
6
|
-
const info = app.getBotInfo(
|
|
7
|
-
const avatar = supports(bot, 'getAvatarUrl') ? bot.getAvatarUrl?.(bot.selfId) : undefined;
|
|
7
|
+
function botView(app, account, live) {
|
|
8
|
+
const info = live && app.getBotInfo(live);
|
|
8
9
|
return {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
capabilities:
|
|
15
|
-
connection: info?.connection ?? '',
|
|
16
|
-
...(info?.url === undefined ? {} : { url: info.url }),
|
|
10
|
+
...account,
|
|
11
|
+
nickname: live?.nickname || account.nickname,
|
|
12
|
+
avatar: (live && botAvatar(live)) ?? account.avatar,
|
|
13
|
+
status: live?.status ?? 'offline',
|
|
14
|
+
connection: info?.connection ?? account.connection ?? '',
|
|
15
|
+
capabilities: live?.capabilities ?? account.capabilities,
|
|
17
16
|
};
|
|
18
17
|
}
|
|
19
18
|
export function commandView(app, command) {
|
|
@@ -55,6 +54,7 @@ export function pluginView(app, state) {
|
|
|
55
54
|
}
|
|
56
55
|
return {
|
|
57
56
|
...state,
|
|
57
|
+
defaultOnNewBots: app.config.plugins[state.name]?.defaultOnNewBots ?? false,
|
|
58
58
|
provides: [...state.provides],
|
|
59
59
|
inject: [...state.inject],
|
|
60
60
|
removable: state.source !== 'builtin',
|
|
@@ -74,7 +74,7 @@ export function overview(app) {
|
|
|
74
74
|
node: process.version,
|
|
75
75
|
startedAt: app.startedAt,
|
|
76
76
|
memory: { rss: memory.rss, heapUsed: memory.heapUsed },
|
|
77
|
-
bots:
|
|
77
|
+
bots: panelBots(app),
|
|
78
78
|
plugins: {
|
|
79
79
|
active: states.filter((item) => item.state === 'active').length,
|
|
80
80
|
failed: states.filter((item) => item.state === 'failed').length,
|
|
@@ -83,3 +83,38 @@ export function overview(app) {
|
|
|
83
83
|
today: app.stats.today(),
|
|
84
84
|
};
|
|
85
85
|
}
|
|
86
|
+
export function panelBot(app, identity) {
|
|
87
|
+
const { platform, selfId } = identity;
|
|
88
|
+
const live = app.getBot(platform, selfId);
|
|
89
|
+
const account = app.botAccounts.get(identity);
|
|
90
|
+
if (live || account)
|
|
91
|
+
return botView(app, account ?? { platform, selfId }, live);
|
|
92
|
+
}
|
|
93
|
+
export function panelBots(app) {
|
|
94
|
+
const accounts = new Map(app.botAccounts.list().map((account) => [botKey(account), account]));
|
|
95
|
+
for (const bot of app.bots.list()) {
|
|
96
|
+
if (!accounts.has(botKey(bot)))
|
|
97
|
+
accounts.set(botKey(bot), { platform: bot.platform, selfId: bot.selfId });
|
|
98
|
+
}
|
|
99
|
+
return [...accounts.values()].map((account) => botView(app, account, app.getBot(account.platform, account.selfId)));
|
|
100
|
+
}
|
|
101
|
+
export function botPluginView(app, bot, name) {
|
|
102
|
+
const state = app.plugins.get(name);
|
|
103
|
+
if (!state)
|
|
104
|
+
throw new Error('Unknown plugin');
|
|
105
|
+
const policy = app.policies.plugin(bot, name);
|
|
106
|
+
return {
|
|
107
|
+
...pluginView(app, state),
|
|
108
|
+
botEnabled: app.plugins.isEnabledOnBot(name, bot),
|
|
109
|
+
botDefaultEnabled: app.policies.defaultPluginEnabled(bot, name),
|
|
110
|
+
overrideEnabled: policy.enabled,
|
|
111
|
+
override: policy.config,
|
|
112
|
+
effectiveConfig: state.configSchema && app.plugins.getConfig(name) !== undefined
|
|
113
|
+
? app.plugins.configFor(name, bot)
|
|
114
|
+
: undefined,
|
|
115
|
+
configSources: state.configSchema && app.plugins.getConfig(name) !== undefined
|
|
116
|
+
? app.plugins.configSources(name, bot)
|
|
117
|
+
: {},
|
|
118
|
+
compatibility: bot.capabilities && compatibility(state.capabilities, bot.capabilities),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
@@ -16,13 +16,13 @@ export async function createTestApp(options = {}) {
|
|
|
16
16
|
const dataDir = await mkdtemp(join(tmpdir(), 'asabot-test-'));
|
|
17
17
|
let app;
|
|
18
18
|
try {
|
|
19
|
-
if (options.config?.
|
|
19
|
+
if (options.config?.connections && options.config.connections.length > 0) {
|
|
20
20
|
throw new Error('createTestApp uses the virtual adapter; external adapters cannot be configured');
|
|
21
21
|
}
|
|
22
22
|
const config = configSchema.parse({
|
|
23
23
|
...options.config,
|
|
24
24
|
dataDir,
|
|
25
|
-
|
|
25
|
+
connections: [],
|
|
26
26
|
pluginLoader: {
|
|
27
27
|
hotReload: false,
|
|
28
28
|
...options.config?.pluginLoader,
|
|
@@ -25,7 +25,8 @@ export function dispatchFixture(config = {}) {
|
|
|
25
25
|
userId,
|
|
26
26
|
message: typeof message === 'string' ? [seg.text(message)] : message,
|
|
27
27
|
sender: { userId, nickname: userId, ...(options.role === undefined ? {} : { role: options.role }) },
|
|
28
|
-
isSuperuser: options.superuser ?? app.isSuperuser(userId),
|
|
28
|
+
isSuperuser: options.superuser ?? app.isSuperuser({ platform: bot.platform, selfId: bot.selfId, userId }),
|
|
29
|
+
isBotAdmin: app.isBotAdmin({ platform: bot.platform, selfId: bot.selfId, userId }),
|
|
29
30
|
}, app.sessionServices);
|
|
30
31
|
};
|
|
31
32
|
const say = (message, options) => app.dispatcher.dispatch(session(message, options));
|
|
@@ -9,7 +9,7 @@ export interface FakeSend {
|
|
|
9
9
|
/** 用于轻量测试的机器人,记录发送内容并模拟部分 API。 */
|
|
10
10
|
export declare class FakeBot implements Bot {
|
|
11
11
|
readonly platform = "fake";
|
|
12
|
-
readonly selfId
|
|
12
|
+
readonly selfId: string;
|
|
13
13
|
readonly status = "online";
|
|
14
14
|
readonly capabilities: {
|
|
15
15
|
segments: string[];
|
|
@@ -17,6 +17,7 @@ export declare class FakeBot implements Bot {
|
|
|
17
17
|
actions: readonly ["sendPrivate", "sendGroup", "sendForward", "getForward", "deleteMessage", "getMessage", "getSelf", "getGroupList", "getGroupMember", "getGroupMembers", "muteMember", "kickMember", "setMemberCard", "getFriendList", "call"];
|
|
18
18
|
};
|
|
19
19
|
readonly sends: FakeSend[];
|
|
20
|
+
constructor(selfId?: string);
|
|
20
21
|
sendPrivate(userId: string, content: Sendable): Promise<SendResult>;
|
|
21
22
|
sendGroup(groupId: string, content: Sendable): Promise<SendResult>;
|
|
22
23
|
sendForward(_target: MessageTarget, _nodes: ForwardNode[]): Promise<SendResult>;
|
package/dist/testing/fake-bot.js
CHANGED
|
@@ -4,10 +4,13 @@ import { normalizeSendable } from '../core/message/index.js';
|
|
|
4
4
|
/** 用于轻量测试的机器人,记录发送内容并模拟部分 API。 */
|
|
5
5
|
export class FakeBot {
|
|
6
6
|
platform = 'fake';
|
|
7
|
-
selfId
|
|
7
|
+
selfId;
|
|
8
8
|
status = 'online';
|
|
9
9
|
capabilities = { segments: ['text'], textStyles: [], actions: botActions };
|
|
10
10
|
sends = [];
|
|
11
|
+
constructor(selfId = '10000') {
|
|
12
|
+
this.selfId = selfId;
|
|
13
|
+
}
|
|
11
14
|
async sendPrivate(userId, content) {
|
|
12
15
|
this.sends.push({ type: 'private', target: userId, message: normalizeSendable(content) });
|
|
13
16
|
return { messageId: String(this.sends.length) };
|