@jsbot-tellus/core 2.0.3 → 2.1.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.
Files changed (78) hide show
  1. package/README.md +13 -17
  2. package/dist/app.js +12 -40
  3. package/dist/app.js.map +1 -1
  4. package/dist/core/config/schema.d.ts +0 -2
  5. package/dist/core/config/schema.d.ts.map +1 -1
  6. package/dist/core/config/schema.js +0 -1
  7. package/dist/core/config/schema.js.map +1 -1
  8. package/dist/core/dispatch/command.d.ts +24 -2
  9. package/dist/core/dispatch/command.d.ts.map +1 -1
  10. package/dist/core/dispatch/command.js +297 -60
  11. package/dist/core/dispatch/command.js.map +1 -1
  12. package/dist/core/dispatch/router.d.ts +17 -3
  13. package/dist/core/dispatch/router.d.ts.map +1 -1
  14. package/dist/core/dispatch/router.js +38 -2
  15. package/dist/core/dispatch/router.js.map +1 -1
  16. package/dist/core/index.d.ts +1 -1
  17. package/dist/core/index.d.ts.map +1 -1
  18. package/dist/core/plugin/command-params.d.ts +15 -0
  19. package/dist/core/plugin/command-params.d.ts.map +1 -0
  20. package/dist/core/plugin/command-params.js +427 -0
  21. package/dist/core/plugin/command-params.js.map +1 -0
  22. package/dist/core/plugin/context.d.ts.map +1 -1
  23. package/dist/core/plugin/context.js +27 -0
  24. package/dist/core/plugin/context.js.map +1 -1
  25. package/dist/core/plugin/install-dir.d.ts +6 -9
  26. package/dist/core/plugin/install-dir.d.ts.map +1 -1
  27. package/dist/core/plugin/install-dir.js +99 -79
  28. package/dist/core/plugin/install-dir.js.map +1 -1
  29. package/dist/core/plugin/install-validate.d.ts +2 -1
  30. package/dist/core/plugin/install-validate.d.ts.map +1 -1
  31. package/dist/core/plugin/install-validate.js +25 -17
  32. package/dist/core/plugin/install-validate.js.map +1 -1
  33. package/dist/core/plugin/installer.d.ts +3 -3
  34. package/dist/core/plugin/installer.d.ts.map +1 -1
  35. package/dist/core/plugin/installer.js +143 -140
  36. package/dist/core/plugin/installer.js.map +1 -1
  37. package/dist/core/plugin/loader.d.ts +1 -0
  38. package/dist/core/plugin/loader.d.ts.map +1 -1
  39. package/dist/core/plugin/loader.js +24 -2
  40. package/dist/core/plugin/loader.js.map +1 -1
  41. package/dist/core/plugin/types.d.ts +11 -3
  42. package/dist/core/plugin/types.d.ts.map +1 -1
  43. package/dist/core/plugin/types.js.map +1 -1
  44. package/dist/plugins/admin/index.d.ts.map +1 -1
  45. package/dist/plugins/admin/index.js +119 -86
  46. package/dist/plugins/admin/index.js.map +1 -1
  47. package/dist/plugins/core/index.d.ts.map +1 -1
  48. package/dist/plugins/core/index.js +4 -1
  49. package/dist/plugins/core/index.js.map +1 -1
  50. package/docs/architecture.md +22 -24
  51. package/docs/configuration.md +19 -23
  52. package/docs/feature-parity.md +4 -4
  53. package/docs/migration.md +13 -13
  54. package/docs/plugin-api.md +156 -26
  55. package/docs/plugin-development.md +390 -0
  56. package/docs/release.md +2 -2
  57. package/examples/plugins/hello/README.md +10 -5
  58. package/examples/plugins/hello/index.js +3 -6
  59. package/package.json +2 -1
  60. package/src/app.ts +16 -55
  61. package/src/core/config/schema.ts +0 -1
  62. package/src/core/dispatch/command.ts +474 -70
  63. package/src/core/dispatch/router.ts +77 -3
  64. package/src/core/index.ts +2 -0
  65. package/src/core/plugin/command-params.ts +595 -0
  66. package/src/core/plugin/context.ts +47 -2
  67. package/src/core/plugin/install-dir.ts +117 -111
  68. package/src/core/plugin/install-validate.ts +36 -34
  69. package/src/core/plugin/installer.ts +228 -184
  70. package/src/core/plugin/loader.ts +36 -2
  71. package/src/core/plugin/types.ts +23 -3
  72. package/src/plugins/admin/index.ts +302 -233
  73. package/src/plugins/core/index.ts +5 -1
  74. package/dist/core/plugin/installed.d.ts +0 -9
  75. package/dist/core/plugin/installed.d.ts.map +0 -1
  76. package/dist/core/plugin/installed.js +0 -113
  77. package/dist/core/plugin/installed.js.map +0 -1
  78. package/src/core/plugin/installed.ts +0 -165
@@ -1,13 +1,16 @@
1
1
  import type {
2
2
  EventNext,
3
+ OneBotMessageEvent,
3
4
  SnowLumaEvent,
4
5
  SnowLumaEventContextWithQuote,
5
6
  } from '@snowluma/sdk'
6
7
  import { isMessageEvent, isNoticeEvent, isRequestEvent } from '@snowluma/sdk'
7
8
  import type { Logger } from '../logging/logger.js'
8
9
  import type { MessageSender } from '../messaging/sender.js'
10
+ import { CommandParamsError } from '../plugin/command-params.js'
9
11
  import type { PluginGate } from '../plugin/gating.js'
10
12
  import type {
13
+ CommandGroupRegistrar,
11
14
  CommandHandler,
12
15
  CommandInfo,
13
16
  EventType,
@@ -22,7 +25,11 @@ import type {
22
25
  import type { RoleService } from '../security/roles.js'
23
26
  import { PermissionRefuser } from '../security/refuse.js'
24
27
  import type { Permission } from '../security/permission.js'
25
- import { CommandRegistry, type CommandRegistration } from './command.js'
28
+ import {
29
+ CommandRegistry,
30
+ type CommandGroupRegistration,
31
+ type CommandRegistration,
32
+ } from './command.js'
26
33
  import {
27
34
  createEventContext,
28
35
  eventTypeOf,
@@ -48,6 +55,8 @@ export interface RouterDeps {
48
55
  logger: Logger
49
56
  }
50
57
 
58
+ type AnyCommandHandler = CommandHandler<OneBotMessageEvent, any[]>
59
+
51
60
  export interface RegisterCommandOptions {
52
61
  pluginId: string
53
62
  name: string
@@ -55,7 +64,19 @@ export interface RegisterCommandOptions {
55
64
  prefixes?: string[] | undefined
56
65
  permission?: Permission | undefined
57
66
  description?: string | undefined
58
- handler: CommandHandler
67
+ params?: string | undefined
68
+ handler: AnyCommandHandler
69
+ }
70
+
71
+ export interface RegisterCommandGroupOptions {
72
+ pluginId: string
73
+ name: string
74
+ aliases?: string[] | undefined
75
+ prefixes?: string[] | undefined
76
+ permission?: Permission | undefined
77
+ description?: string | undefined
78
+ params?: string | undefined
79
+ handler?: AnyCommandHandler | undefined
59
80
  }
60
81
 
61
82
  export interface RegisterNoticeOptions extends NoticeOptions {
@@ -153,6 +174,35 @@ export class Router {
153
174
  this.sender,
154
175
  found.match
155
176
  )
177
+ let args: unknown[] = []
178
+
179
+ if (found.registration.paramsParser) {
180
+ try {
181
+ const parsed =
182
+ found.registration.paramsParser.parse(
183
+ found.match.rest
184
+ )
185
+
186
+ args = parsed.args
187
+
188
+ if (parsed.ignored.length > 0) {
189
+ this.logger.debug(
190
+ `ignored extra arguments for ${found.registration.name}`,
191
+ parsed.ignored.join(' ')
192
+ )
193
+ }
194
+ } catch (error) {
195
+ if (error instanceof CommandParamsError) {
196
+ await commandContext.send(
197
+ `${error.message}\n用法:${found.match.prefix}${found.registration.name} ${error.usage}`,
198
+ { quote: false }
199
+ )
200
+ return
201
+ }
202
+
203
+ throw error
204
+ }
205
+ }
156
206
 
157
207
  await this.safeRun(
158
208
  found.registration.pluginId,
@@ -161,7 +211,8 @@ export class Router {
161
211
  found.registration.handler(
162
212
  event,
163
213
  commandContext,
164
- found.match
214
+ found.match,
215
+ ...args
165
216
  )
166
217
  )
167
218
 
@@ -193,12 +244,35 @@ export class Router {
193
244
  ...(options.description !== undefined
194
245
  ? { description: options.description }
195
246
  : {}),
247
+ ...(options.params !== undefined ? { params: options.params } : {}),
196
248
  handler: options.handler,
197
249
  }
198
250
 
199
251
  return this.commands.register(registration)
200
252
  }
201
253
 
254
+ registerCommandGroup(
255
+ options: RegisterCommandGroupOptions,
256
+ setup: (group: CommandGroupRegistrar) => void
257
+ ): () => void {
258
+ const registration: CommandGroupRegistration = {
259
+ pluginId: options.pluginId,
260
+ name: options.name,
261
+ aliases: options.aliases ?? [],
262
+ prefixes: options.prefixes ?? ['/'],
263
+ permission: options.permission ?? 'User',
264
+ ...(options.description !== undefined
265
+ ? { description: options.description }
266
+ : {}),
267
+ ...(options.params !== undefined ? { params: options.params } : {}),
268
+ ...(options.handler !== undefined
269
+ ? { handler: options.handler }
270
+ : {}),
271
+ }
272
+
273
+ return this.commands.group(registration, setup)
274
+ }
275
+
202
276
  registerMessage(pluginId: string, handler: MessageHandler): () => void {
203
277
  return this.handlers.registerMessage(pluginId, handler)
204
278
  }
package/src/core/index.ts CHANGED
@@ -17,6 +17,8 @@ export type { PluginKV } from './plugin/kv.js'
17
17
  export { definePlugin } from './plugin/types.js'
18
18
  export type {
19
19
  AnyPluginDefinition,
20
+ CommandGroupOptions,
21
+ CommandGroupRegistrar,
20
22
  CommandHandler,
21
23
  CommandInfo,
22
24
  CommandMatch,