@zhin.js/core 1.0.0 → 1.0.2

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 (133) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/LICENSE +21 -0
  3. package/README.md +295 -74
  4. package/lib/adapter.d.ts +39 -0
  5. package/lib/adapter.d.ts.map +1 -0
  6. package/{dist → lib}/adapter.js +20 -2
  7. package/lib/adapter.js.map +1 -0
  8. package/lib/app.d.ts +115 -0
  9. package/lib/app.d.ts.map +1 -0
  10. package/{dist → lib}/app.js +148 -78
  11. package/lib/app.js.map +1 -0
  12. package/lib/bot.d.ts +31 -0
  13. package/lib/bot.d.ts.map +1 -0
  14. package/lib/command.d.ts +32 -0
  15. package/lib/command.d.ts.map +1 -0
  16. package/lib/command.js +46 -0
  17. package/lib/command.js.map +1 -0
  18. package/lib/component.d.ts +27 -0
  19. package/lib/component.d.ts.map +1 -0
  20. package/lib/component.js +469 -0
  21. package/lib/component.js.map +1 -0
  22. package/{dist → lib}/config.d.ts.map +1 -1
  23. package/{dist → lib}/config.js +6 -9
  24. package/lib/config.js.map +1 -0
  25. package/lib/cron.d.ts +81 -0
  26. package/lib/cron.d.ts.map +1 -0
  27. package/lib/cron.js +159 -0
  28. package/lib/cron.js.map +1 -0
  29. package/lib/errors.d.ts +165 -0
  30. package/lib/errors.d.ts.map +1 -0
  31. package/lib/errors.js +306 -0
  32. package/lib/errors.js.map +1 -0
  33. package/lib/index.d.ts +15 -0
  34. package/lib/index.d.ts.map +1 -0
  35. package/lib/index.js +17 -0
  36. package/lib/index.js.map +1 -0
  37. package/lib/jsx-runtime.d.ts +12 -0
  38. package/lib/jsx-runtime.d.ts.map +1 -0
  39. package/lib/jsx-runtime.js +11 -0
  40. package/lib/jsx-runtime.js.map +1 -0
  41. package/lib/jsx.d.ts +32 -0
  42. package/lib/jsx.d.ts.map +1 -0
  43. package/lib/jsx.js +57 -0
  44. package/lib/jsx.js.map +1 -0
  45. package/lib/message.d.ts +47 -0
  46. package/lib/message.d.ts.map +1 -0
  47. package/lib/message.js +11 -0
  48. package/lib/message.js.map +1 -0
  49. package/lib/plugin.d.ts +50 -0
  50. package/lib/plugin.d.ts.map +1 -0
  51. package/lib/plugin.js +170 -0
  52. package/lib/plugin.js.map +1 -0
  53. package/lib/prompt.d.ts +116 -0
  54. package/lib/prompt.d.ts.map +1 -0
  55. package/lib/prompt.js +240 -0
  56. package/lib/prompt.js.map +1 -0
  57. package/lib/schema.d.ts +83 -0
  58. package/lib/schema.d.ts.map +1 -0
  59. package/lib/schema.js +245 -0
  60. package/lib/schema.js.map +1 -0
  61. package/{dist → lib}/types-generator.d.ts.map +1 -1
  62. package/{dist → lib}/types-generator.js +6 -3
  63. package/lib/types-generator.js.map +1 -0
  64. package/lib/types.d.ts +121 -0
  65. package/lib/types.d.ts.map +1 -0
  66. package/lib/utils.d.ts +52 -0
  67. package/lib/utils.d.ts.map +1 -0
  68. package/lib/utils.js +340 -0
  69. package/lib/utils.js.map +1 -0
  70. package/package.json +23 -9
  71. package/src/adapter.ts +25 -9
  72. package/src/app.ts +363 -258
  73. package/src/bot.ts +29 -8
  74. package/src/command.ts +50 -0
  75. package/src/component.ts +561 -0
  76. package/src/config.ts +9 -12
  77. package/src/cron.ts +176 -0
  78. package/src/errors.ts +365 -0
  79. package/src/index.ts +16 -13
  80. package/src/jsx-runtime.ts +12 -0
  81. package/src/jsx.d.ts +52 -0
  82. package/src/jsx.ts +92 -0
  83. package/src/message.ts +47 -0
  84. package/src/plugin.ts +148 -66
  85. package/src/prompt.ts +290 -0
  86. package/src/schema.ts +273 -0
  87. package/src/types-generator.ts +7 -3
  88. package/src/types.ts +80 -31
  89. package/src/utils.ts +313 -0
  90. package/tests/adapter.test.ts +36 -22
  91. package/tests/app.test.ts +30 -0
  92. package/tests/command.test.ts +545 -0
  93. package/tests/component-new.test.ts +348 -0
  94. package/tests/config.test.ts +1 -1
  95. package/tests/errors.test.ts +311 -0
  96. package/tests/expression-evaluation.test.ts +258 -0
  97. package/tests/message.test.ts +402 -0
  98. package/tests/plugin.test.ts +284 -143
  99. package/tests/utils.test.ts +80 -0
  100. package/tsconfig.json +3 -4
  101. package/dist/adapter.d.ts +0 -22
  102. package/dist/adapter.d.ts.map +0 -1
  103. package/dist/adapter.js.map +0 -1
  104. package/dist/app.d.ts +0 -69
  105. package/dist/app.d.ts.map +0 -1
  106. package/dist/app.js.map +0 -1
  107. package/dist/bot.d.ts +0 -9
  108. package/dist/bot.d.ts.map +0 -1
  109. package/dist/config.js.map +0 -1
  110. package/dist/index.d.ts +0 -9
  111. package/dist/index.d.ts.map +0 -1
  112. package/dist/index.js +0 -12
  113. package/dist/index.js.map +0 -1
  114. package/dist/logger.d.ts +0 -3
  115. package/dist/logger.d.ts.map +0 -1
  116. package/dist/logger.js +0 -3
  117. package/dist/logger.js.map +0 -1
  118. package/dist/plugin.d.ts +0 -41
  119. package/dist/plugin.d.ts.map +0 -1
  120. package/dist/plugin.js +0 -95
  121. package/dist/plugin.js.map +0 -1
  122. package/dist/types-generator.js.map +0 -1
  123. package/dist/types.d.ts +0 -69
  124. package/dist/types.d.ts.map +0 -1
  125. package/src/logger.ts +0 -3
  126. package/tests/logger.test.ts +0 -170
  127. package/tsconfig.tsbuildinfo +0 -1
  128. /package/{dist → lib}/bot.js +0 -0
  129. /package/{dist → lib}/bot.js.map +0 -0
  130. /package/{dist → lib}/config.d.ts +0 -0
  131. /package/{dist → lib}/types-generator.d.ts +0 -0
  132. /package/{dist → lib}/types.js +0 -0
  133. /package/{dist → lib}/types.js.map +0 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # @zhin.js/core
2
+
3
+ ## 1.0.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 15fc934: fix: 支持 jsx
8
+ - 3ecd487: fix: 函数式组件,更新文档
9
+
10
+ ## 1.0.1
11
+
12
+ ### Patch Changes
13
+
14
+ - efdd58a: fix: init
15
+ - Updated dependencies [efdd58a]
16
+ - @zhin.js/hmr@1.0.1
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 凉菜
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,159 +1,380 @@
1
1
  # @zhin.js/core
2
2
 
3
- Zhin Bot Framework的核心包,基于HMR系统构建的机器人框架核心实现。
3
+ Zhin 机器人框架核心包,基于 HMR(热模块替换)系统构建的现代化机器人开发框架。
4
4
 
5
- ## 主要组件
5
+ ## 核心特性
6
6
 
7
- ### App
7
+ - 🔥 **热模块替换**: 基于 @zhin.js/hmr 的热更新系统
8
+ - 🔌 **插件化架构**: 完整的插件生命周期管理
9
+ - 🤖 **多平台适配**: 支持多种聊天平台适配器
10
+ - 🎯 **命令系统**: 基于 segment-matcher 的智能消息匹配
11
+ - 🧩 **组件系统**: 支持模板渲染、属性绑定、插槽的组件化开发
12
+ - ⚡ **中间件链**: 灵活的消息处理中间件机制
13
+ - 🔧 **TypeScript**: 完整的类型支持
8
14
 
9
- 继承自HMR的应用核心类,提供:
10
- - 插件管理
11
- - 配置管理
12
- - 适配器管理
13
- - 消息路由
14
- - 热更新支持
15
+ ## 核心组件
16
+
17
+ ### App 类
18
+
19
+ 应用核心类,继承自 `HMR<Plugin>`:
15
20
 
16
21
  ```typescript
17
22
  import { createApp } from '@zhin.js/core'
18
23
 
24
+ // 创建应用实例
19
25
  const app = await createApp({
20
26
  plugin_dirs: ['./plugins'],
21
- plugins: ['plugin1'],
27
+ plugins: ['my-plugin'],
22
28
  bots: [{
23
- context: 'adapter-name',
24
- name: 'bot-name',
25
- config: {}
26
- }]
29
+ context: 'onebot11',
30
+ name: 'my-bot',
31
+ url: 'ws://localhost:8080'
32
+ }],
33
+ debug: true
27
34
  })
28
35
 
36
+ // 启动应用
29
37
  await app.start()
30
38
  ```
31
39
 
32
- ### Plugin
40
+ **主要功能**:
41
+ - 插件生命周期管理
42
+ - 配置文件加载和管理
43
+ - 适配器和机器人实例管理
44
+ - 消息路由和分发
45
+ - 热更新监听
33
46
 
34
- 继承自Dependency的插件基类,提供:
35
- - 中间件系统
36
- - 事件监听
37
- - 定时任务
38
- - 上下文管理
39
- - 消息处理
47
+ ### Plugin 类
48
+
49
+ 插件基类,继承自 `Dependency<Plugin>`:
40
50
 
41
51
  ```typescript
42
- import { usePlugin, onMessage, addMiddleware } from '@zhin.js/core'
52
+ import { usePlugin, addMiddleware, addCommand, addComponent } from '@zhin.js/core'
43
53
 
44
54
  const plugin = usePlugin()
45
55
 
56
+ // 添加中间件
46
57
  addMiddleware(async (message, next) => {
47
- console.log('收到消息:', message)
58
+ console.log(`[${plugin.name}] 收到消息:`, message.raw)
48
59
  await next()
49
60
  })
50
61
 
51
- onMessage(async (message) => {
52
- // 处理消息
53
- })
62
+ // 添加命令
63
+ addCommand(new MessageCommand('hello')
64
+ .action(async (message) => {
65
+ return '你好!'
66
+ })
67
+ )
54
68
  ```
55
69
 
56
- ### Adapter
70
+ **主要功能**:
71
+ - 中间件链管理
72
+ - 命令注册和处理
73
+ - 组件管理
74
+ - 事件监听和分发
75
+ - 消息发送前处理 (beforeSend)
76
+
77
+ ### Adapter 类
78
+
79
+ 适配器基类,用于连接不同聊天平台:
57
80
 
58
- 适配器基类,用于实现不同平台的机器人适配:
59
81
  ```typescript
60
- import { Adapter, Bot } from '@zhin.js/core'
82
+ import { Adapter, Bot, Plugin } from '@zhin.js/core'
61
83
 
62
- class MyAdapter extends Adapter {
63
- constructor() {
64
- super('my-adapter', (plugin, config) => new MyBot(plugin, config))
84
+ class MyBot implements Bot {
85
+ constructor(public plugin: Plugin, public config: BotConfig) {}
86
+
87
+ async connect() {
88
+ // 连接逻辑
89
+ }
90
+
91
+ async disconnect() {
92
+ // 断开连接逻辑
93
+ }
94
+
95
+ async sendMessage(options: SendOptions) {
96
+ // 发送消息逻辑
65
97
  }
66
98
  }
99
+
100
+ // 注册适配器
101
+ const adapter = new Adapter('my-platform', MyBot)
67
102
  ```
68
103
 
69
- ### Bot
104
+ ### MessageCommand 类
105
+
106
+ 基于 `SegmentMatcher` 的命令处理器:
70
107
 
71
- 机器人接口定义:
72
108
  ```typescript
73
- export interface Bot<T extends BotConfig=BotConfig> {
74
- config: T
75
- connected?: boolean
76
- connect(): Promise<void>
77
- disconnect(): Promise<void>
78
- sendMessage(options: SendOptions): Promise<void>
79
- }
109
+ import { MessageCommand } from '@zhin.js/core'
110
+
111
+ const command = new MessageCommand('echo <content:text>')
112
+ .action(async (message, result) => {
113
+ const content = result.args.content
114
+ return `你说了:${content}`
115
+ })
116
+
117
+ addCommand(command)
118
+ ```
119
+
120
+ ### Component 系统
121
+
122
+ 强大的组件渲染系统:
123
+
124
+ ```typescript
125
+ import { defineComponent, addComponent } from '@zhin.js/core'
126
+
127
+ // 定义组件
128
+ const MyComponent = defineComponent({
129
+ name: 'my-comp',
130
+ props: {
131
+ title: String,
132
+ count: { type: Number, default: 0 }
133
+ },
134
+ data(this: { title: string, count: number }) {
135
+ return {
136
+ message: `${this.title}: ${this.count}`
137
+ }
138
+ },
139
+ render(props, context) {
140
+ return `<text>${context.message}</text>`
141
+ }
142
+ })
143
+
144
+ // 注册组件
145
+ addComponent(MyComponent)
146
+
147
+ // 在消息中使用
148
+ // <my-comp title="计数器" :count="5"/>
80
149
  ```
81
150
 
82
151
  ## Hooks API
83
152
 
84
- ### 插件相关
153
+ ### 应用和插件
85
154
  ```typescript
86
- // 获取插件实例
155
+ // 获取应用实例
156
+ const app = useApp()
157
+
158
+ // 获取当前插件
87
159
  const plugin = usePlugin()
88
160
 
89
- // 监听事件
90
- onEvent('event.name', handler)
161
+ // 获取插件日志器
162
+ const logger = useLogger()
163
+ ```
91
164
 
92
- // 监听群消息
93
- onGroupMessage(handler)
165
+ ### 事件监听
166
+ ```typescript
167
+ // 监听所有消息
168
+ onMessage(async (message) => {
169
+ console.log('收到消息:', message.raw)
170
+ })
171
+
172
+ // 监听群聊消息
173
+ onGroupMessage(async (message) => {
174
+ if (message.raw.includes('帮助')) {
175
+ await message.reply('这里是帮助信息')
176
+ }
177
+ })
94
178
 
95
179
  // 监听私聊消息
96
- onPrivateMessage(handler)
180
+ onPrivateMessage(async (message) => {
181
+ await message.reply('私聊回复')
182
+ })
97
183
 
98
- // 监听所有消息
99
- onMessage(handler)
184
+ // 自定义事件监听
185
+ onEvent('custom.event', (data) => {
186
+ console.log('自定义事件:', data)
187
+ })
188
+ ```
100
189
 
101
- // 添加中间件
102
- addMiddleware(middleware)
190
+ ### 生命周期
191
+ ```typescript
192
+ // 插件挂载时
193
+ onMounted(async (plugin) => {
194
+ console.log('插件已挂载:', plugin.name)
195
+ })
103
196
 
104
- // 添加定时任务
105
- addCronJob(job)
197
+ // 插件销毁时
198
+ onDispose(() => {
199
+ console.log('插件正在销毁')
200
+ })
106
201
  ```
107
202
 
108
- ### 上下文相关
203
+ ### 上下文管理
109
204
  ```typescript
110
205
  // 注册上下文
111
206
  register({
112
- name: 'myContext',
207
+ name: 'database',
113
208
  async mounted(plugin) {
114
- return { /* context data */ }
209
+ const db = new Database()
210
+ await db.connect()
211
+ return db
212
+ },
213
+ async dispose(db) {
214
+ await db.disconnect()
115
215
  }
116
216
  })
117
217
 
118
- // 使用上下文
119
- const ctx = use('myContext')
120
-
121
- // 声明上下文依赖
122
- useContext('ctx1', 'ctx2', async (ctx1, ctx2) => {
123
- // 使用上下文
218
+ // 使用上下文依赖
219
+ useContext('database', async (db) => {
220
+ const users = await db.getUsers()
221
+ console.log('用户列表:', users)
124
222
  })
125
223
  ```
126
224
 
127
- ### 生命周期
225
+ ### 适配器注册
128
226
  ```typescript
129
- // 挂载时
130
- onMounted(async (plugin) => {
131
- // 初始化
227
+ import { registerAdapter } from '@zhin.js/core'
228
+
229
+ registerAdapter(new Adapter('my-platform', MyBot))
230
+ ```
231
+
232
+ ### 消息处理
233
+ ```typescript
234
+ // 发送消息前处理
235
+ beforeSend(async (options) => {
236
+ console.log('即将发送消息:', options)
237
+ // 可以修改消息内容
238
+ return options
132
239
  })
133
240
 
134
- // 销毁时
135
- onDispose(() => {
136
- // 清理
241
+ // 发送消息
242
+ await sendMessage({
243
+ type: 'group',
244
+ id: '123456',
245
+ context: 'onebot11',
246
+ bot: 'my-bot',
247
+ content: '你好世界!'
137
248
  })
138
249
  ```
139
250
 
140
- ## 配置
251
+ ## 类型定义
252
+
253
+ ### 消息相关
254
+ ```typescript
255
+ interface Message {
256
+ id: string
257
+ adapter: string
258
+ bot: string
259
+ content: MessageSegment[]
260
+ sender: MessageSender
261
+ channel: MessageChannel
262
+ timestamp: number
263
+ raw: string
264
+ reply(content: SendContent, quote?: boolean | string): Promise<void>
265
+ }
266
+
267
+ interface MessageSegment {
268
+ type: string
269
+ data: Record<string, any>
270
+ }
271
+
272
+ type SendContent = string | MessageSegment | (string | MessageSegment)[]
273
+ ```
141
274
 
275
+ ### 配置相关
142
276
  ```typescript
143
277
  interface AppConfig {
278
+ bots?: BotConfig[]
144
279
  plugin_dirs?: string[]
145
280
  plugins?: string[]
146
- bots?: BotConfig[]
281
+ disable_dependencies?: string[]
147
282
  debug?: boolean
148
283
  }
149
284
 
150
285
  interface BotConfig {
151
- context: string
152
286
  name: string
153
- config: any
287
+ context: string
288
+ [key: string]: any
289
+ }
290
+ ```
291
+
292
+ ### 机器人接口
293
+ ```typescript
294
+ interface Bot<T extends BotConfig = BotConfig> {
295
+ config: T
296
+ connected?: boolean
297
+ connect(): Promise<void>
298
+ disconnect(): Promise<void>
299
+ sendMessage(options: SendOptions): Promise<void>
154
300
  }
155
301
  ```
156
302
 
303
+ ## 中间件系统
304
+
305
+ ```typescript
306
+ type MessageMiddleware = (
307
+ message: Message,
308
+ next: () => Promise<void>
309
+ ) => Promise<void> | void
310
+
311
+ // 添加身份验证中间件
312
+ addMiddleware(async (message, next) => {
313
+ if (isAdmin(message.sender.id)) {
314
+ await next()
315
+ } else {
316
+ await message.reply('权限不足')
317
+ }
318
+ })
319
+
320
+ // 添加日志中间件
321
+ addMiddleware(async (message, next) => {
322
+ const start = Date.now()
323
+ await next()
324
+ const duration = Date.now() - start
325
+ console.log(`处理消息耗时: ${duration}ms`)
326
+ })
327
+ ```
328
+
329
+ ## 配置文件支持
330
+
331
+ 支持多种配置文件格式:
332
+ - `zhin.config.ts` - JavaScript 配置
333
+ - `zhin.config.ts` - TypeScript 配置
334
+ - `zhin.config.json` - JSON 配置
335
+ - `zhin.config.yaml` - YAML 配置
336
+ - `zhin.config.toml` - TOML 配置
337
+
338
+ ```typescript
339
+ // zhin.config.ts
340
+ export default {
341
+ plugin_dirs: ['./plugins', './node_modules'],
342
+ plugins: ['my-plugin'],
343
+ bots: [
344
+ {
345
+ context: 'onebot11',
346
+ name: 'bot1',
347
+ url: 'ws://localhost:8080',
348
+ access_token: 'your-token'
349
+ }
350
+ ],
351
+ debug: process.env.NODE_ENV === 'development'
352
+ }
353
+ ```
354
+
355
+ ## 开发工具
356
+
357
+ ### 类型生成
358
+ ```bash
359
+ # 自动生成环境类型定义
360
+ npx zhin dev # 开发时自动生成
361
+ ```
362
+
363
+ ### 热更新
364
+ ```bash
365
+ # 开发模式启动,支持热更新
366
+ npx zhin dev
367
+ ```
368
+
369
+ ## 依赖项
370
+
371
+ - `@zhin.js/hmr` - 热模块替换系统
372
+ - `segment-matcher` - 消息片段匹配器
373
+ - `yaml` - YAML 配置文件支持
374
+ - `toml` - TOML 配置文件支持
375
+ - `dotenv` - 环境变量支持
376
+
157
377
  ## 许可证
158
378
 
379
+ MIT License
159
380
  MIT License
@@ -0,0 +1,39 @@
1
+ import { Bot, BotConfig } from "./bot.js";
2
+ import { Plugin } from "./plugin.js";
3
+ /**
4
+ * Adapter类:适配器抽象,管理多平台Bot实例。
5
+ * 负责根据配置启动/关闭各平台机器人,统一异常处理。
6
+ */
7
+ export declare class Adapter<R extends Bot = Bot> {
8
+ #private;
9
+ name: string;
10
+ /** 当前适配器下所有Bot实例,key为bot名称 */
11
+ bots: Map<string, R>;
12
+ /**
13
+ * 构造函数
14
+ * @param name 适配器名称(如 'process'、'qq' 等)
15
+ * @param botFactory Bot工厂函数或构造器
16
+ */
17
+ constructor(name: string, botFactory: Adapter.BotFactory<R>);
18
+ /**
19
+ * 启动适配器,自动根据配置创建并连接所有Bot
20
+ * @param plugin 所属插件实例
21
+ */
22
+ start(plugin: Plugin): Promise<void>;
23
+ /**
24
+ * 停止适配器,断开并移除所有Bot实例
25
+ * @param plugin 所属插件实例
26
+ */
27
+ stop(plugin: Plugin): Promise<void>;
28
+ }
29
+ export declare namespace Adapter {
30
+ type BotBotConstructor<T extends Bot> = T extends Bot<infer F, infer S> ? {
31
+ new (plugin: Plugin, config: S): T;
32
+ } : {
33
+ new (plugin: Plugin, config: BotConfig): T;
34
+ };
35
+ function isBotConstructor<T extends Bot>(fn: BotFactory<T>): fn is BotBotConstructor<T>;
36
+ type BotCreator<T extends Bot> = T extends Bot<infer F, infer S> ? (plugin: Plugin, config: S) => T : (plugin: Plugin, config: BotConfig) => T;
37
+ type BotFactory<T extends Bot> = BotBotConstructor<T> | BotCreator<T>;
38
+ }
39
+ //# sourceMappingURL=adapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAC,SAAS,EAAC,MAAM,UAAU,CAAC;AACvC,OAAO,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AACnC;;;GAGG;AACH,qBAAa,OAAO,CAAC,CAAC,SAAS,GAAG,GAAC,GAAG;;IASf,IAAI,EAAC,MAAM;IAR9B,8BAA8B;IACvB,IAAI,EAAC,GAAG,CAAC,MAAM,EAAC,CAAC,CAAC,CAAqB;IAE9C;;;;OAIG;gBACgB,IAAI,EAAC,MAAM,EAAC,UAAU,EAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC;IAG/D;;;OAGG;IACG,KAAK,CAAC,MAAM,EAAC,MAAM;IA2BzB;;;OAGG;IACG,IAAI,CAAC,MAAM,EAAC,MAAM;CAkB3B;AACD,yBAAiB,OAAO,CAAC;IACrB,KAAY,iBAAiB,CAAC,CAAC,SAAS,GAAG,IAAE,CAAC,SAAS,GAAG,CAAC,MAAM,CAAC,EAAC,MAAM,CAAC,CAAC,GAAG;QAC1E,KAAI,MAAM,EAAC,MAAM,EAAC,MAAM,EAAC,CAAC,GAAE,CAAC,CAAA;KAChC,GAAE;QACC,KAAI,MAAM,EAAC,MAAM,EAAC,MAAM,EAAC,SAAS,GAAE,CAAC,CAAA;KACxC,CAAA;IACD,SAAgB,gBAAgB,CAAC,CAAC,SAAS,GAAG,EAAE,EAAE,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,EAAE,IAAI,iBAAiB,CAAC,CAAC,CAAC,CAG7F;IACD,KAAY,UAAU,CAAC,CAAC,SAAS,GAAG,IAAE,CAAC,SAAS,GAAG,CAAC,MAAM,CAAC,EAAC,MAAM,CAAC,CAAC,GAAG,CAAC,MAAM,EAAC,MAAM,EAAC,MAAM,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,EAAC,MAAM,EAAC,MAAM,EAAE,SAAS,KAAK,CAAC,CAAA;IAC9I,KAAY,UAAU,CAAC,CAAC,SAAS,GAAG,IAAI,iBAAiB,CAAC,CAAC,CAAC,GAAC,UAAU,CAAC,CAAC,CAAC,CAAA;CAC7E"}
@@ -1,11 +1,25 @@
1
+ /**
2
+ * Adapter类:适配器抽象,管理多平台Bot实例。
3
+ * 负责根据配置启动/关闭各平台机器人,统一异常处理。
4
+ */
1
5
  export class Adapter {
2
6
  name;
7
+ /** 当前适配器下所有Bot实例,key为bot名称 */
3
8
  bots = new Map();
4
9
  #botFactory;
10
+ /**
11
+ * 构造函数
12
+ * @param name 适配器名称(如 'process'、'qq' 等)
13
+ * @param botFactory Bot工厂函数或构造器
14
+ */
5
15
  constructor(name, botFactory) {
6
16
  this.name = name;
7
17
  this.#botFactory = botFactory;
8
18
  }
19
+ /**
20
+ * 启动适配器,自动根据配置创建并连接所有Bot
21
+ * @param plugin 所属插件实例
22
+ */
9
23
  async start(plugin) {
10
24
  const configs = plugin.app.getConfig().bots?.filter(c => c.context === this.name);
11
25
  if (!configs?.length)
@@ -20,7 +34,7 @@ export class Adapter {
20
34
  bot = this.#botFactory(plugin, config);
21
35
  }
22
36
  try {
23
- await bot.connect();
37
+ await bot.$connect();
24
38
  plugin.logger.info(`bot ${config.name} of adapter ${this.name} connected`);
25
39
  this.bots.set(config.name, bot);
26
40
  }
@@ -36,11 +50,15 @@ export class Adapter {
36
50
  throw error;
37
51
  }
38
52
  }
53
+ /**
54
+ * 停止适配器,断开并移除所有Bot实例
55
+ * @param plugin 所属插件实例
56
+ */
39
57
  async stop(plugin) {
40
58
  try {
41
59
  for (const [name, bot] of this.bots) {
42
60
  try {
43
- await bot.disconnect();
61
+ await bot.$disconnect();
44
62
  plugin.logger.info(`bot ${name} of adapter ${this.name} disconnected`);
45
63
  this.bots.delete(name);
46
64
  }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