@jsbot-tellus/core 2.6.0 → 2.7.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 (46) hide show
  1. package/README.md +16 -4
  2. package/dist/core/dispatch/command.d.ts +5 -2
  3. package/dist/core/dispatch/command.d.ts.map +1 -1
  4. package/dist/core/dispatch/command.js +33 -7
  5. package/dist/core/dispatch/command.js.map +1 -1
  6. package/dist/core/dispatch/handler.d.ts +5 -3
  7. package/dist/core/dispatch/handler.d.ts.map +1 -1
  8. package/dist/core/dispatch/handler.js +6 -1
  9. package/dist/core/dispatch/handler.js.map +1 -1
  10. package/dist/core/dispatch/router.d.ts +4 -2
  11. package/dist/core/dispatch/router.d.ts.map +1 -1
  12. package/dist/core/dispatch/router.js +9 -5
  13. package/dist/core/dispatch/router.js.map +1 -1
  14. package/dist/core/index.d.ts +1 -1
  15. package/dist/core/index.d.ts.map +1 -1
  16. package/dist/core/kernel.d.ts +1 -1
  17. package/dist/core/kernel.d.ts.map +1 -1
  18. package/dist/core/kernel.js +4 -1
  19. package/dist/core/kernel.js.map +1 -1
  20. package/dist/core/plugin/context.d.ts.map +1 -1
  21. package/dist/core/plugin/context.js +12 -0
  22. package/dist/core/plugin/context.js.map +1 -1
  23. package/dist/core/plugin/loader.d.ts +1 -1
  24. package/dist/core/plugin/loader.d.ts.map +1 -1
  25. package/dist/core/plugin/loader.js +31 -46
  26. package/dist/core/plugin/loader.js.map +1 -1
  27. package/dist/core/plugin/types.d.ts +9 -1
  28. package/dist/core/plugin/types.d.ts.map +1 -1
  29. package/dist/core/plugin/types.js.map +1 -1
  30. package/docs/architecture.md +3 -3
  31. package/docs/configuration.md +3 -3
  32. package/docs/docker.md +166 -0
  33. package/docs/feature-parity.md +2 -2
  34. package/docs/migration.md +1 -1
  35. package/docs/plugin-api.md +20 -5
  36. package/docs/plugin-development.md +30 -27
  37. package/docs/release.md +50 -8
  38. package/package.json +2 -2
  39. package/src/core/dispatch/command.ts +53 -1
  40. package/src/core/dispatch/handler.ts +19 -1
  41. package/src/core/dispatch/router.ts +20 -3
  42. package/src/core/index.ts +4 -0
  43. package/src/core/kernel.ts +8 -1
  44. package/src/core/plugin/context.ts +21 -0
  45. package/src/core/plugin/loader.ts +41 -66
  46. package/src/core/plugin/types.ts +20 -0
@@ -1,4 +1,4 @@
1
- import type { SnowLumaEvent } from '@snowluma/sdk'
1
+ import { isMessageEvent, type SnowLumaEvent } from '@snowluma/sdk'
2
2
  import type { BotClient } from './client.js'
3
3
  import type { AppConfig } from './config/schema.js'
4
4
  import { createBotApi, type BotApi } from './api.js'
@@ -93,6 +93,13 @@ export class Kernel {
93
93
  .listCommands()
94
94
  .filter((command) =>
95
95
  gate.isEnabled(command.pluginId, event)
96
+ )
97
+ .filter(
98
+ (command) =>
99
+ !isMessageEvent(event) ||
100
+ command.messageTypes.includes(
101
+ event.message_type
102
+ )
96
103
  ),
97
104
  },
98
105
  plugins: {
@@ -12,6 +12,7 @@ import type {
12
12
  CommandHandler,
13
13
  CommandOptions,
14
14
  CoreServices,
15
+ MessageHandler,
15
16
  PluginContext,
16
17
  PluginDefinition,
17
18
  } from './types.js'
@@ -63,6 +64,9 @@ export function createPluginContext<TConfig>(
63
64
  ...(options.permission !== undefined
64
65
  ? { permission: options.permission }
65
66
  : {}),
67
+ ...(options.messageType !== undefined
68
+ ? { messageType: options.messageType }
69
+ : {}),
66
70
  ...(options.description !== undefined
67
71
  ? { description: options.description }
68
72
  : {}),
@@ -89,6 +93,9 @@ export function createPluginContext<TConfig>(
89
93
  ...(options.permission !== undefined
90
94
  ? { permission: options.permission }
91
95
  : {}),
96
+ ...(options.messageType !== undefined
97
+ ? { messageType: options.messageType }
98
+ : {}),
92
99
  ...(options.description !== undefined
93
100
  ? { description: options.description }
94
101
  : {}),
@@ -111,6 +118,20 @@ export function createPluginContext<TConfig>(
111
118
  onMessage(handler) {
112
119
  return deps.router.registerMessage(id, handler)
113
120
  },
121
+ onGroupMessage(handler) {
122
+ return deps.router.registerMessage(
123
+ id,
124
+ handler as MessageHandler,
125
+ 'group'
126
+ )
127
+ },
128
+ onPrivateMessage(handler) {
129
+ return deps.router.registerMessage(
130
+ id,
131
+ handler as MessageHandler,
132
+ 'private'
133
+ )
134
+ },
114
135
  onNotice(options, handler) {
115
136
  return deps.router.registerNotice({
116
137
  pluginId: id,
@@ -4,7 +4,7 @@ import { pathToFileURL } from 'node:url'
4
4
  import fg from 'fast-glob'
5
5
  import type { Logger } from '../logging/logger.js'
6
6
  import { collectPluginExports } from './exports.js'
7
- import { readInstallState, readJsonFile } from './install-dir.js'
7
+ import { readJsonFile } from './install-dir.js'
8
8
  import { NpmCliRunner, type NpmRunner } from './npm-runner.js'
9
9
  import type { AnyPluginDefinition } from './types.js'
10
10
 
@@ -13,7 +13,7 @@ type LoaderLogger = Pick<Logger, 'warn' | 'error'> & {
13
13
  }
14
14
 
15
15
  export interface LocalPluginLoaderOptions {
16
- /** Build local plugins that have a build script but no dist. Defaults to true. */
16
+ /** Rebuild local plugins that have a build script on every load. Defaults to true. */
17
17
  autoBuild?: boolean | undefined
18
18
  /** Timeout for a single local plugin build. Defaults to 120 seconds. */
19
19
  buildTimeoutMs?: number | undefined
@@ -49,19 +49,7 @@ async function readBuildScript(directory: string): Promise<string | undefined> {
49
49
  : undefined
50
50
  }
51
51
 
52
- async function readManagedDirectories(
53
- root: string
54
- ): Promise<Set<string> | null> {
55
- try {
56
- const state = await readInstallState(root)
57
-
58
- return new Set(
59
- Object.values(state.packages).map((record) => record.dir)
60
- )
61
- } catch {
62
- return null
63
- }
64
- }
52
+ type BuildOutcome = 'built' | 'missing-node-modules' | 'failed'
65
53
 
66
54
  async function buildLocalPlugin(
67
55
  directory: string,
@@ -69,22 +57,15 @@ async function buildLocalPlugin(
69
57
  logger: LoaderLogger | undefined,
70
58
  runner: NpmRunner,
71
59
  timeoutMs: number
72
- ): Promise<boolean> {
73
- if ((await readBuildScript(directory)) === undefined) {
74
- logger?.warn(
75
- `skipping local plugin ${name}: dist directory not found and package.json has no build script`
76
- )
77
- return false
78
- }
79
-
60
+ ): Promise<BuildOutcome> {
80
61
  if (!(await directoryExists(join(directory, 'node_modules')))) {
81
62
  logger?.warn(
82
- `skipping local plugin ${name}: dist directory not found and node_modules is missing; run npm install --prefix ${directory}`
63
+ `cannot build local plugin ${name}: node_modules is missing; run npm install --prefix ${directory}`
83
64
  )
84
- return false
65
+ return 'missing-node-modules'
85
66
  }
86
67
 
87
- logger?.info?.(`building local plugin ${name}: dist directory not found`)
68
+ logger?.info?.(`building local plugin ${name}`)
88
69
 
89
70
  try {
90
71
  await runner.run(['run', 'build'], {
@@ -93,13 +74,12 @@ async function buildLocalPlugin(
93
74
  })
94
75
  } catch (error) {
95
76
  logger?.error(`failed to build local plugin ${name}`, error)
96
- logger?.warn(`skipping local plugin ${name}: build failed`)
97
- return false
77
+ return 'failed'
98
78
  }
99
79
 
100
80
  logger?.info?.(`built local plugin ${name}`)
101
81
 
102
- return true
82
+ return 'built'
103
83
  }
104
84
 
105
85
  export async function loadPluginsFromDirectory(
@@ -127,7 +107,14 @@ export async function loadPluginsFromDirectory(
127
107
  let imported: unknown
128
108
 
129
109
  try {
130
- imported = await import(pathToFileURL(file).href)
110
+ const info = await stat(file).catch(() => null)
111
+ const url = pathToFileURL(file)
112
+
113
+ if (info !== null) {
114
+ url.searchParams.set('v', String(info.mtimeMs))
115
+ }
116
+
117
+ imported = await import(url.href)
131
118
  } catch (error) {
132
119
  logger?.error(`failed to import plugin file: ${file}`, error)
133
120
  invalid.push(
@@ -170,22 +157,7 @@ export async function loadLocalPlugins(
170
157
  )
171
158
  const plugins: AnyPluginDefinition[] = []
172
159
 
173
- let autoBuild = options.autoBuild !== false
174
- let managed = new Set<string>()
175
-
176
- if (autoBuild) {
177
- const directories = await readManagedDirectories(absolute)
178
-
179
- if (directories === null) {
180
- logger?.warn(
181
- 'cannot read plugin install state; automatic local plugin build is disabled'
182
- )
183
- autoBuild = false
184
- } else {
185
- managed = directories
186
- }
187
- }
188
-
160
+ const autoBuild = options.autoBuild !== false
189
161
  const runner = options.runner ?? new NpmCliRunner()
190
162
  const timeoutMs = options.buildTimeoutMs ?? DEFAULT_BUILD_TIMEOUT_MS
191
163
 
@@ -195,24 +167,11 @@ export async function loadLocalPlugins(
195
167
 
196
168
  const directory = join(absolute, entry.name)
197
169
  const dist = join(directory, 'dist')
170
+ const hasBuildScript = (await readBuildScript(directory)) !== undefined
198
171
  let distInfo = await stat(dist).catch(() => null)
199
172
 
200
- if (!distInfo?.isDirectory()) {
201
- if (!autoBuild) {
202
- logger?.warn(
203
- `skipping local plugin ${entry.name}: dist directory not found`
204
- )
205
- continue
206
- }
207
-
208
- if (managed.has(entry.name)) {
209
- logger?.warn(
210
- `skipping local plugin ${entry.name}: dist directory not found (installed package is expected to include dist)`
211
- )
212
- continue
213
- }
214
-
215
- const built = await buildLocalPlugin(
173
+ if (autoBuild && hasBuildScript) {
174
+ const outcome = await buildLocalPlugin(
216
175
  directory,
217
176
  entry.name,
218
177
  logger,
@@ -220,16 +179,32 @@ export async function loadLocalPlugins(
220
179
  timeoutMs
221
180
  )
222
181
 
223
- if (!built) continue
224
-
225
182
  distInfo = await stat(dist).catch(() => null)
226
183
 
227
184
  if (!distInfo?.isDirectory()) {
185
+ const reason =
186
+ outcome === 'built'
187
+ ? 'build finished but dist directory not found'
188
+ : outcome === 'missing-node-modules'
189
+ ? 'node_modules is missing and dist directory not found'
190
+ : 'build failed and dist directory not found'
191
+
192
+ logger?.warn(`skipping local plugin ${entry.name}: ${reason}`)
193
+ continue
194
+ }
195
+
196
+ if (outcome !== 'built') {
228
197
  logger?.warn(
229
- `skipping local plugin ${entry.name}: build finished but dist directory not found`
198
+ `using existing dist for local plugin ${entry.name}: rebuild failed`
230
199
  )
231
- continue
232
200
  }
201
+ } else if (!distInfo?.isDirectory()) {
202
+ logger?.warn(
203
+ hasBuildScript
204
+ ? `skipping local plugin ${entry.name}: dist directory not found and automatic build is disabled`
205
+ : `skipping local plugin ${entry.name}: dist directory not found and package.json has no build script`
206
+ )
207
+ continue
233
208
  }
234
209
 
235
210
  plugins.push(...(await loadPluginsFromDirectory(dist, logger)))
@@ -1,6 +1,8 @@
1
1
  import type {
2
+ OneBotGroupMessageEvent,
2
3
  OneBotMessageEvent,
3
4
  OneBotNoticeEvent,
5
+ OneBotPrivateMessageEvent,
4
6
  OneBotRequestEvent,
5
7
  OutgoingMessage,
6
8
  SnowLumaEvent,
@@ -17,6 +19,10 @@ import type { CommandParamArgs } from './command-params.js'
17
19
 
18
20
  export type EventType = 'message' | 'notice' | 'request' | 'meta'
19
21
 
22
+ export type MessageType = 'group' | 'private'
23
+
24
+ export type MessageTypeFilter = MessageType | readonly MessageType[]
25
+
20
26
  export interface CommandMatch {
21
27
  command: string
22
28
  prefix: string
@@ -28,6 +34,7 @@ export interface CommandInfo {
28
34
  name: string
29
35
  aliases: string[]
30
36
  permission: Permission
37
+ messageTypes: MessageType[]
31
38
  description?: string | undefined
32
39
  usage?: string | undefined
33
40
  pluginId: string
@@ -59,6 +66,16 @@ export type MessageHandler = (
59
66
  context: EventContext<OneBotMessageEvent>
60
67
  ) => void | Promise<void>
61
68
 
69
+ export type GroupMessageHandler = (
70
+ event: OneBotGroupMessageEvent,
71
+ context: EventContext<OneBotGroupMessageEvent>
72
+ ) => void | Promise<void>
73
+
74
+ export type PrivateMessageHandler = (
75
+ event: OneBotPrivateMessageEvent,
76
+ context: EventContext<OneBotPrivateMessageEvent>
77
+ ) => void | Promise<void>
78
+
62
79
  export type NoticeHandler = (
63
80
  event: OneBotNoticeEvent,
64
81
  context: EventContext<OneBotNoticeEvent>
@@ -80,6 +97,7 @@ export interface CommandOptions<Declaration extends string = string> {
80
97
  aliases?: string[] | undefined
81
98
  prefixes?: string[] | undefined
82
99
  permission?: Permission | undefined
100
+ messageType?: MessageTypeFilter | undefined
83
101
  description?: string | undefined
84
102
  params?: Declaration | undefined
85
103
  }
@@ -127,6 +145,8 @@ export interface CommandRegistrar extends CommandGroupRegistrar {
127
145
 
128
146
  export interface EventRegistrar {
129
147
  onMessage(handler: MessageHandler): () => void
148
+ onGroupMessage(handler: GroupMessageHandler): () => void
149
+ onPrivateMessage(handler: PrivateMessageHandler): () => void
130
150
  onNotice(options: NoticeOptions, handler: NoticeHandler): () => void
131
151
  onRequest(options: RequestOptions, handler: RequestHandler): () => void
132
152
  }