@zhin.js/core 1.0.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 (57) hide show
  1. package/README.md +159 -0
  2. package/dist/adapter.d.ts +22 -0
  3. package/dist/adapter.d.ts.map +1 -0
  4. package/dist/adapter.js +67 -0
  5. package/dist/adapter.js.map +1 -0
  6. package/dist/app.d.ts +69 -0
  7. package/dist/app.d.ts.map +1 -0
  8. package/dist/app.js +307 -0
  9. package/dist/app.js.map +1 -0
  10. package/dist/bot.d.ts +9 -0
  11. package/dist/bot.d.ts.map +1 -0
  12. package/dist/bot.js +2 -0
  13. package/dist/bot.js.map +1 -0
  14. package/dist/config.d.ts +24 -0
  15. package/dist/config.d.ts.map +1 -0
  16. package/dist/config.js +242 -0
  17. package/dist/config.js.map +1 -0
  18. package/dist/index.d.ts +9 -0
  19. package/dist/index.d.ts.map +1 -0
  20. package/dist/index.js +12 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/logger.d.ts +3 -0
  23. package/dist/logger.d.ts.map +1 -0
  24. package/dist/logger.js +3 -0
  25. package/dist/logger.js.map +1 -0
  26. package/dist/plugin.d.ts +41 -0
  27. package/dist/plugin.d.ts.map +1 -0
  28. package/dist/plugin.js +95 -0
  29. package/dist/plugin.js.map +1 -0
  30. package/dist/types-generator.d.ts +6 -0
  31. package/dist/types-generator.d.ts.map +1 -0
  32. package/dist/types-generator.js +69 -0
  33. package/dist/types-generator.js.map +1 -0
  34. package/dist/types.d.ts +69 -0
  35. package/dist/types.d.ts.map +1 -0
  36. package/dist/types.js +2 -0
  37. package/dist/types.js.map +1 -0
  38. package/package.json +29 -0
  39. package/src/adapter.ts +69 -0
  40. package/src/app.ts +339 -0
  41. package/src/bot.ts +9 -0
  42. package/src/config.ts +276 -0
  43. package/src/index.ts +14 -0
  44. package/src/logger.ts +3 -0
  45. package/src/plugin.ts +122 -0
  46. package/src/types-generator.ts +74 -0
  47. package/src/types.ts +74 -0
  48. package/tests/adapter.test.ts +187 -0
  49. package/tests/app.test.ts +207 -0
  50. package/tests/bot.test.ts +132 -0
  51. package/tests/config.test.ts +328 -0
  52. package/tests/logger.test.ts +170 -0
  53. package/tests/plugin.test.ts +226 -0
  54. package/tests/test-utils.ts +59 -0
  55. package/tests/types.test.ts +162 -0
  56. package/tsconfig.json +25 -0
  57. package/tsconfig.tsbuildinfo +1 -0
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@zhin.js/core",
3
+ "version": "1.0.0",
4
+ "description": "Zhin机器人核心框架",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "dependencies": {
15
+ "ws": "^8.14.2",
16
+ "yaml": "^2.3.4",
17
+ "toml": "^3.0.0",
18
+ "dotenv": "^16.3.1",
19
+ "@zhin.js/hmr": "1.0.0"
20
+ },
21
+ "devDependencies": {
22
+ "typescript": "^5.3.0",
23
+ "@zhin.js/types": "1.0.0"
24
+ },
25
+ "scripts": {
26
+ "build": "tsc",
27
+ "clean": "rm -rf dist"
28
+ }
29
+ }
package/src/adapter.ts ADDED
@@ -0,0 +1,69 @@
1
+ import {BotConfig} from "./types";
2
+ import {Bot} from "./bot";
3
+ import {Plugin} from "./plugin";
4
+
5
+ export class Adapter<R extends Bot=Bot>{
6
+ public bots:Map<string,R>=new Map<string, R>()
7
+ #botFactory:Adapter.BotFactory<R>
8
+ constructor(public name:string,botFactory:Adapter.BotFactory<R>) {
9
+ this.#botFactory=botFactory
10
+ }
11
+ async start(plugin:Plugin){
12
+ const configs=plugin.app.getConfig().bots?.filter(c=>c.context===this.name)
13
+ if(!configs?.length) return
14
+ try {
15
+ for(const config of configs){
16
+ let bot: R
17
+ if (Adapter.isBotConstructor(this.#botFactory)) {
18
+ bot = new this.#botFactory(plugin,config) as R
19
+ } else {
20
+ bot = this.#botFactory(plugin,config) as R
21
+ }
22
+ try {
23
+ await bot.connect()
24
+ plugin.logger.info(`bot ${config.name} of adapter ${this.name} connected`)
25
+ this.bots.set(config.name,bot)
26
+ } catch (error) {
27
+ // 如果连接失败,确保错误正确传播
28
+ throw error
29
+ }
30
+ }
31
+
32
+ plugin.logger.info(`adapter ${this.name} started`)
33
+ } catch (error) {
34
+ // 确保错误正确传播
35
+ throw error
36
+ }
37
+ }
38
+ async stop(plugin:Plugin){
39
+ try {
40
+ for(const [name,bot] of this.bots){
41
+ try {
42
+ await bot.disconnect()
43
+ plugin.logger.info(`bot ${name} of adapter ${this.name} disconnected`)
44
+ this.bots.delete(name)
45
+ } catch (error) {
46
+ // 如果断开连接失败,确保错误正确传播
47
+ throw error
48
+ }
49
+ }
50
+ plugin.logger.info(`adapter ${this.name} stopped`)
51
+ } catch (error) {
52
+ // 确保错误正确传播
53
+ throw error
54
+ }
55
+ }
56
+ }
57
+ export namespace Adapter {
58
+ export type BotBotConstructor<T extends Bot>=T extends Bot<infer R> ? {
59
+ new(plugin:Plugin,config:R):T
60
+ }: {
61
+ new(plugin:Plugin,config:BotConfig):T
62
+ }
63
+ export function isBotConstructor<T extends Bot>(fn: BotFactory<T>): fn is BotBotConstructor<T> {
64
+ return fn.prototype &&
65
+ fn.prototype.constructor === fn
66
+ }
67
+ export type BotCreator<T extends Bot>=T extends Bot<infer R> ? (plugin:Plugin,config: R) => T : (plugin:Plugin,config: BotConfig) => T
68
+ export type BotFactory<T extends Bot> = BotBotConstructor<T>|BotCreator<T>
69
+ }
package/src/app.ts ADDED
@@ -0,0 +1,339 @@
1
+ import path from 'path';
2
+ import * as fs from 'fs'
3
+ import {SideEffect, GlobalContext} from '@zhin.js/types'
4
+ import { HMR, Context, Logger, ConsoleLogger,getCallerFile, getCallerFiles } from '@zhin.js/hmr';
5
+ import {
6
+ AppConfig,
7
+ Message, BeforeSendHandler,SendOptions,
8
+ } from './types.js';
9
+ import { loadConfig } from './config.js';
10
+ import { fileURLToPath } from 'url';
11
+ import { generateEnvTypes } from './types-generator.js';
12
+ import { logger } from './logger.js';
13
+ import {CronJob, EventListener, MessageMiddleware, Plugin} from "./plugin.js";
14
+ import {Adapter} from "./adapter";
15
+
16
+ // ============================================================================
17
+ // App 类
18
+ // ============================================================================
19
+ /**
20
+ * App类:继承自HMR,提供热更新的机器人框架
21
+ */
22
+ export class App extends HMR<Plugin> {
23
+ static currentPlugin: Plugin;
24
+ private config: AppConfig;
25
+
26
+ constructor(config?: Partial<AppConfig>) {
27
+ // 如果没有传入配置或配置为空对象,尝试自动加载配置文件
28
+ let finalConfig: AppConfig;
29
+
30
+ if (!config || Object.keys(config).length === 0) {
31
+ try {
32
+ // 异步加载配置,这里需要改为同步初始化
33
+ logger.info('🔍 正在查找配置文件...');
34
+ finalConfig = App.loadConfigSync();
35
+ logger.info('✅ 配置文件加载成功');
36
+ } catch (error) {
37
+ logger.warn('⚠️ 配置文件加载失败,使用默认配置:', error instanceof Error ? error.message : error);
38
+ finalConfig = Object.assign({}, App.defaultConfig);
39
+ }
40
+ } else {
41
+ // 合并默认配置和传入的配置
42
+ finalConfig = Object.assign({}, App.defaultConfig, config);
43
+ }
44
+
45
+ // 调用父类构造函数
46
+ super('Zhin',{
47
+ logger: new ConsoleLogger('[Zhin]'),
48
+ dirs: finalConfig.plugin_dirs || [],
49
+ extensions: new Set(['.js', '.ts']),
50
+ debug: finalConfig.debug
51
+ });
52
+ this.on('message.send',this.sendMessage.bind(this))
53
+ this.config = finalConfig;
54
+ }
55
+ /** 默认配置 */
56
+ static defaultConfig: AppConfig = {
57
+ plugin_dirs: ['./plugins'],
58
+ plugins: [],
59
+ bots: [],
60
+ debug: false,
61
+ };
62
+ async sendMessage(options:SendOptions){
63
+ const adapter=this.getContext<Adapter>(options.context)
64
+ if(!adapter) throw new Error(`can't find adapter for name ${options.context}`)
65
+ const bot=adapter.bots.get(options.bot)
66
+ if(!bot) throw new Error(`can't find bot ${options.bot} for adapter ${options.bot}`)
67
+ return bot.sendMessage(options)
68
+ }
69
+ /** 同步加载配置文件 */
70
+ static loadConfigSync(): AppConfig {
71
+ // 由于loadConfig是异步的,我们需要创建一个同步版本
72
+ // 或者在这里简化处理,让用户使用异步创建方法
73
+ throw new Error('同步加载配置暂不支持,请使用 App.createAsync() 方法');
74
+ }
75
+
76
+ /** 创建插件依赖 */
77
+ createDependency(name: string, filePath: string): Plugin {
78
+ return new Plugin(this, name, filePath);
79
+ }
80
+
81
+ /** 获取App配置 */
82
+ getConfig(): Readonly<AppConfig> {
83
+ return { ...this.config };
84
+ }
85
+
86
+ /** 更新App配置 */
87
+ updateConfig(config: Partial<AppConfig>): void {
88
+ this.config = { ...this.config, ...config };
89
+
90
+ // 更新HMR配置
91
+ if (config.plugin_dirs) {
92
+ // 动态更新监听目录
93
+ const currentDirs = this.getWatchDirs();
94
+ const newDirs = config.plugin_dirs;
95
+
96
+ // 移除不再需要的目录
97
+ for (const dir of currentDirs) {
98
+ if (!newDirs.includes(dir)) {
99
+ this.removeWatchDir(dir);
100
+ }
101
+ }
102
+
103
+ // 添加新的目录
104
+ for (const dir of newDirs) {
105
+ if (!currentDirs.includes(dir)) {
106
+ this.addWatchDir(dir);
107
+ }
108
+ }
109
+ }
110
+
111
+ this.logger.info('App configuration updated', this.config);
112
+ }
113
+
114
+ /** 使用插件 */
115
+ use(filePath: string): void {
116
+ this.emit('internal.add', filePath);
117
+ }
118
+
119
+ /** 启动App */
120
+ async start(mode: 'dev' | 'prod' = 'prod'): Promise<void> {
121
+ await generateEnvTypes(process.cwd());
122
+ // 加载插件
123
+ for (const pluginName of this.config.plugins || []) {
124
+ this.use(pluginName);
125
+ }
126
+ // 等待所有插件就绪
127
+ await this.waitForReady();
128
+ this.logger.info('started successfully');
129
+ }
130
+
131
+ /** 停止App */
132
+ async stop(): Promise<void> {
133
+ this.logger.info('Stopping app...');
134
+ // 销毁所有插件
135
+ this.dispose();
136
+
137
+ this.logger.info('App stopped');
138
+ }
139
+
140
+ getContext<T>(name:string):T{
141
+ for(const dep of this.dependencyList){
142
+ if(dep.contexts.has(name)) {
143
+ const context = dep.contexts.get(name)!
144
+ // 如果上下文还没有挂载,等待挂载完成
145
+ if (!context.value) {
146
+ throw new Error(`Context ${name} is not mounted yet`)
147
+ }
148
+ return context.value
149
+ }
150
+ }
151
+ throw new Error(`can't find Context of ${name}`)
152
+ }
153
+
154
+ async handleBeforeSend(options:SendOptions){
155
+ const handlers=this.dependencyList.reduce((result,plugin)=>{
156
+ result.push(...plugin.listeners('before-message.send'))
157
+ return result
158
+ },[] as Function[])
159
+ for(const handler of handlers){
160
+ const result=await handler(options)
161
+ if(result) options=result
162
+ }
163
+ return options
164
+ }
165
+ getLogger(...names: string[]): Logger {
166
+ return new ConsoleLogger(`[${[...names].join('/')}]`, process.env.NODE_ENV === 'development');
167
+ }
168
+ }
169
+
170
+ // ============================================================================
171
+ // Hooks API
172
+ // ============================================================================
173
+
174
+ function getPlugin(hmr: HMR<Plugin>, filename: string): Plugin {
175
+ const name = path.basename(filename).replace(path.extname(filename), '');
176
+
177
+ // 尝试从当前依赖中查找插件
178
+ const childPlugin = hmr.findChild(filename);
179
+ if (childPlugin) {
180
+ return childPlugin;
181
+ }
182
+ const parent=hmr.findParent(filename,getCallerFiles(fileURLToPath(import.meta.url)))
183
+ // 创建新的插件实例
184
+ const newPlugin = new Plugin(parent, name, filename);
185
+
186
+ // 添加到当前依赖的子依赖中
187
+ parent.dependencies.set(filename, newPlugin);
188
+
189
+ return newPlugin;
190
+ }
191
+ export async function createApp(config?: Partial<AppConfig>): Promise<App> {
192
+ let finalConfig: AppConfig,configPath:string='';
193
+ const envFiles=['.env',`.env.${process.env.NODE_ENV}`]
194
+ .filter(filename=>fs.existsSync(path.join(process.cwd(),filename)))
195
+ if (!config || Object.keys(config).length === 0) {
196
+ try {
197
+ logger.info('🔍 正在查找配置文件...');
198
+ [configPath,finalConfig] = await loadConfig();
199
+ logger.info('✅ 配置文件加载成功');
200
+ } catch (error) {
201
+ logger.warn('⚠️ 配置文件加载失败,使用默认配置:', error instanceof Error ? error.message : error);
202
+ finalConfig = Object.assign({}, App.defaultConfig);
203
+ }
204
+ } else {
205
+ finalConfig = Object.assign({}, App.defaultConfig, config);
206
+ }
207
+ const app= new App(finalConfig);
208
+ app.watching(envFiles,()=>{
209
+ process.exit(51)
210
+ })
211
+ if(configPath){
212
+ app.watching(configPath,()=>{
213
+ process.exit(51);
214
+ })
215
+ }
216
+ return app
217
+ }
218
+ /** 获取App实例 */
219
+ export function useApp(): App {
220
+ const hmr = HMR.currentHMR;
221
+ if (!hmr) throw new Error('useApp must be called within a App Context');
222
+ return hmr as unknown as App;
223
+ }
224
+
225
+ /** 获取当前插件实例 */
226
+ export function usePlugin(): Plugin {
227
+ const hmr = HMR.currentHMR;
228
+ if (!hmr) throw new Error('usePlugin must be called within a App Context');
229
+
230
+ try {
231
+ const currentFile = getCallerFile(import.meta.url);
232
+ return getPlugin(hmr as unknown as HMR<Plugin>, currentFile);
233
+ } catch (error) {
234
+ // 如果无法获取当前文件,尝试从当前依赖获取
235
+ if (HMR.currentDependency) {
236
+ return HMR.currentDependency as unknown as Plugin;
237
+ }
238
+ throw error;
239
+ }
240
+ }
241
+ export function beforeSend(handler:BeforeSendHandler){
242
+ const plugin = usePlugin();
243
+ return plugin.beforeSend(handler);
244
+ }
245
+ /** 创建Context */
246
+ export function register<T>(context: Context<T,Plugin>): Context<T,Plugin> {
247
+ const plugin = usePlugin();
248
+ return plugin.register(context);
249
+ }
250
+ export function registerAdapter<T extends Adapter>(adapter:T){
251
+ const plugin = usePlugin();
252
+ plugin.register({
253
+ name:adapter.name,
254
+ async mounted(plugin){
255
+ await adapter.start(plugin)
256
+ return adapter
257
+ },
258
+ dispose(){
259
+ return adapter.stop(plugin)
260
+ }
261
+ })
262
+ }
263
+
264
+ export function use<T extends keyof GlobalContext>(name: T): GlobalContext[T]
265
+ export function use<T>(name: string): T
266
+ export function use(name: string){
267
+ const plugin = usePlugin();
268
+ return plugin.use(name);
269
+ }
270
+
271
+ /** 标记必需的Context */
272
+ export function useContext<T extends (keyof GlobalContext)[]>(...args:[...T,sideEffect:SideEffect<T>]): void {
273
+ const plugin = usePlugin();
274
+ plugin.useContext(...args as any);
275
+ }
276
+
277
+ /** 添加中间件 */
278
+ export function addMiddleware(middleware: MessageMiddleware): void {
279
+ const plugin = usePlugin();
280
+ plugin.addMiddleware(middleware);
281
+ }
282
+
283
+ /** 监听事件 */
284
+ export function onEvent<T = any>(event: string, listener: EventListener<T>): void {
285
+ const plugin = usePlugin();
286
+ plugin.on(event, listener);
287
+ }
288
+
289
+ /** 监听群组消息 */
290
+ export function onGroupMessage(handler: (message: Message) => void | Promise<void>): void {
291
+ onEvent('message.group.receive', handler);
292
+ }
293
+
294
+ /** 监听私聊消息 */
295
+ export function onPrivateMessage(handler: (message: Message) => void | Promise<void>): void {
296
+ onEvent('message.private.receive', handler);
297
+ }
298
+
299
+ /** 监听所有消息 */
300
+ export function onMessage(handler: (message: Message) => void | Promise<void>): void {
301
+ onEvent('message.receive', handler);
302
+ }
303
+
304
+ /** 监听插件挂载事件 */
305
+ export function onMounted(hook: (plugin: Plugin) => Promise<void> | void): void {
306
+ const plugin = usePlugin();
307
+ if(plugin.isReady) hook(plugin)
308
+ plugin.on('self.mounted', hook);
309
+ }
310
+
311
+ /** 监听插件销毁事件 */
312
+ export function onDispose(hook: () => void): void {
313
+ const plugin = usePlugin();
314
+ if(plugin.isDispose) hook()
315
+ plugin.on('self.dispose', hook);
316
+ }
317
+
318
+ /** 添加定时任务 */
319
+ export function addCronJob(job: CronJob): void {
320
+ const plugin = usePlugin();
321
+ plugin.addCronJob(job);
322
+ }
323
+
324
+ /** 发送消息 */
325
+ export async function sendMessage(options:SendOptions): Promise<void> {
326
+ const app = useApp();
327
+ await app.sendMessage(options);
328
+ }
329
+
330
+ /** 获取App实例(用于高级操作) */
331
+ export function getAppInstance(): App {
332
+ return useApp();
333
+ }
334
+
335
+ /** 获取插件日志记录器 */
336
+ export function useLogger(): Logger {
337
+ const plugin = usePlugin();
338
+ return plugin.logger;
339
+ }
package/src/bot.ts ADDED
@@ -0,0 +1,9 @@
1
+ import type {BotConfig, SendOptions} from "./types";
2
+
3
+ export interface Bot<T extends BotConfig=BotConfig> {
4
+ config: T;
5
+ connected?: boolean;
6
+ connect():Promise<void>
7
+ disconnect():Promise<void>
8
+ sendMessage(options: SendOptions): Promise<void>
9
+ }