@zhin.js/core 1.0.0 → 1.0.1

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 (121) hide show
  1. package/CHANGELOG.md +9 -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 +107 -0
  19. package/lib/component.d.ts.map +1 -0
  20. package/lib/component.js +273 -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/message.d.ts +44 -0
  38. package/lib/message.d.ts.map +1 -0
  39. package/lib/message.js +11 -0
  40. package/lib/message.js.map +1 -0
  41. package/lib/plugin.d.ts +50 -0
  42. package/lib/plugin.d.ts.map +1 -0
  43. package/lib/plugin.js +170 -0
  44. package/lib/plugin.js.map +1 -0
  45. package/lib/prompt.d.ts +116 -0
  46. package/lib/prompt.d.ts.map +1 -0
  47. package/lib/prompt.js +240 -0
  48. package/lib/prompt.js.map +1 -0
  49. package/lib/schema.d.ts +83 -0
  50. package/lib/schema.d.ts.map +1 -0
  51. package/lib/schema.js +245 -0
  52. package/lib/schema.js.map +1 -0
  53. package/{dist → lib}/types-generator.d.ts.map +1 -1
  54. package/{dist → lib}/types-generator.js +6 -3
  55. package/lib/types-generator.js.map +1 -0
  56. package/lib/types.d.ts +119 -0
  57. package/lib/types.d.ts.map +1 -0
  58. package/lib/utils.d.ts +52 -0
  59. package/lib/utils.d.ts.map +1 -0
  60. package/lib/utils.js +338 -0
  61. package/lib/utils.js.map +1 -0
  62. package/package.json +15 -9
  63. package/src/adapter.ts +25 -9
  64. package/src/app.ts +363 -258
  65. package/src/bot.ts +29 -8
  66. package/src/command.ts +50 -0
  67. package/src/component.ts +318 -0
  68. package/src/config.ts +9 -12
  69. package/src/cron.ts +176 -0
  70. package/src/errors.ts +365 -0
  71. package/src/index.ts +16 -13
  72. package/src/message.ts +44 -0
  73. package/src/plugin.ts +148 -66
  74. package/src/prompt.ts +290 -0
  75. package/src/schema.ts +273 -0
  76. package/src/types-generator.ts +7 -3
  77. package/src/types.ts +77 -30
  78. package/src/utils.ts +312 -0
  79. package/tests/adapter.test.ts +36 -22
  80. package/tests/app.test.ts +30 -0
  81. package/tests/command.test.ts +545 -0
  82. package/tests/component.test.ts +656 -0
  83. package/tests/config.test.ts +1 -1
  84. package/tests/errors.test.ts +311 -0
  85. package/tests/message.test.ts +402 -0
  86. package/tests/plugin.test.ts +275 -143
  87. package/tests/utils.test.ts +80 -0
  88. package/tsconfig.json +3 -4
  89. package/dist/adapter.d.ts +0 -22
  90. package/dist/adapter.d.ts.map +0 -1
  91. package/dist/adapter.js.map +0 -1
  92. package/dist/app.d.ts +0 -69
  93. package/dist/app.d.ts.map +0 -1
  94. package/dist/app.js.map +0 -1
  95. package/dist/bot.d.ts +0 -9
  96. package/dist/bot.d.ts.map +0 -1
  97. package/dist/config.js.map +0 -1
  98. package/dist/index.d.ts +0 -9
  99. package/dist/index.d.ts.map +0 -1
  100. package/dist/index.js +0 -12
  101. package/dist/index.js.map +0 -1
  102. package/dist/logger.d.ts +0 -3
  103. package/dist/logger.d.ts.map +0 -1
  104. package/dist/logger.js +0 -3
  105. package/dist/logger.js.map +0 -1
  106. package/dist/plugin.d.ts +0 -41
  107. package/dist/plugin.d.ts.map +0 -1
  108. package/dist/plugin.js +0 -95
  109. package/dist/plugin.js.map +0 -1
  110. package/dist/types-generator.js.map +0 -1
  111. package/dist/types.d.ts +0 -69
  112. package/dist/types.d.ts.map +0 -1
  113. package/src/logger.ts +0 -3
  114. package/tests/logger.test.ts +0 -170
  115. package/tsconfig.tsbuildinfo +0 -1
  116. /package/{dist → lib}/bot.js +0 -0
  117. /package/{dist → lib}/bot.js.map +0 -0
  118. /package/{dist → lib}/config.d.ts +0 -0
  119. /package/{dist → lib}/types-generator.d.ts +0 -0
  120. /package/{dist → lib}/types.js +0 -0
  121. /package/{dist → lib}/types.js.map +0 -0
package/src/app.ts CHANGED
@@ -1,170 +1,248 @@
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';
1
+ import path from "path";
2
+ import { SideEffect, GlobalContext, Models } from "@zhin.js/types";
5
3
  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";
4
+ HMR,
5
+ Context,
6
+ Logger,
7
+ getCallerFile,
8
+ getCallerFiles,
9
+ } from "@zhin.js/hmr";
10
+ import {
11
+ AdapterMessage,
12
+ AppConfig,
13
+ BeforeSendHandler,
14
+ RegisteredAdapter,
15
+ SendOptions,
16
+ } from "./types.js";
17
+ import { Message } from "./message.js";
18
+ import { fileURLToPath } from "url";
19
+ import { generateEnvTypes } from "./types-generator.js";
20
+ import logger, { setName } from "@zhin.js/logger";
21
+ import { sleep } from "./utils.js";
22
+
23
+ // 创建静态logger用于配置加载等静态操作
24
+ setName("Zhin");
25
+ import { MessageMiddleware, Plugin } from "./plugin.js";
26
+ import { Adapter } from "./adapter";
27
+ import { MessageCommand } from "./command";
28
+ import { Component } from "./component";
29
+ import { RelatedDatabase,DocumentDatabase,KeyValueDatabase,Schema,Registry} from "@zhin.js/database";
15
30
 
16
31
  // ============================================================================
17
- // App
32
+ // App 类(Zhin.js 应用主入口,负责插件热重载、配置管理、消息分发等)
18
33
  // ============================================================================
19
34
  /**
20
- * App类:继承自HMR,提供热更新的机器人框架
35
+ * App类:Zhin.js 应用主入口,负责插件热重载、配置管理、消息分发等。
36
+ * 继承自 HMR,支持插件生命周期、适配器管理、数据库集成等。
21
37
  */
22
38
  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
39
+ static currentPlugin: Plugin;
40
+ private config: AppConfig;
41
+ adapters: string[] = [];
42
+ database?: RelatedDatabase<any,Models>|DocumentDatabase<any,Models>|KeyValueDatabase<any,Models>;
43
+ /**
44
+ * 构造函数:初始化应用,加载配置,注册全局异常处理
45
+ * @param config 可选的应用配置,若为空则自动查找配置文件
46
+ */
47
+ constructor(config?: Partial<AppConfig>) {
48
+ // 如果没有传入配置或配置为空对象,尝试自动加载配置文件
49
+ let finalConfig: AppConfig;
50
+ if (!config || Object.keys(config).length === 0) {
51
+ try {
52
+ // 异步加载配置,这里需要改为同步初始化
53
+ logger.info("🔍 正在查找配置文件...");
54
+ finalConfig = App.loadConfigSync();
55
+ logger.info("✅ 配置文件加载成功");
56
+ } catch (error) {
57
+ logger.warn("⚠️ 配置文件加载失败,使用默认配置", {
58
+ error: error instanceof Error ? error.message : String(error),
51
59
  });
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() 方法');
60
+ finalConfig = Object.assign({}, App.defaultConfig);
61
+ }
62
+ } else {
63
+ // 合并默认配置和传入的配置
64
+ finalConfig = Object.assign({}, App.defaultConfig, config);
74
65
  }
75
66
 
76
- /** 创建插件依赖 */
77
- createDependency(name: string, filePath: string): Plugin {
78
- return new Plugin(this, name, filePath);
79
- }
67
+ // 调用父类构造函数
68
+ super("Zhin", {
69
+ logger,
70
+ dirs: finalConfig.plugin_dirs || [],
71
+ extensions: new Set([".js", ".ts"]),
72
+ debug: finalConfig.debug,
73
+ });
74
+ this.on("message.send", this.sendMessage.bind(this));
75
+ process.on("uncaughtException", (e) => {
76
+ this.logger.error(e);
77
+ });
78
+ process.on("unhandledRejection", (e) => {
79
+ this.logger.error(e);
80
+ });
81
+ this.config = finalConfig;
82
+ }
83
+ /** 默认配置 */
84
+ /**
85
+ * 默认配置
86
+ * - plugin_dirs: 插件目录
87
+ * - plugins: 启用插件
88
+ * - bots: 机器人配置
89
+ * - debug: 是否调试模式
90
+ */
91
+ static defaultConfig: AppConfig = {
92
+ plugin_dirs: ["./plugins"],
93
+ plugins: [],
94
+ bots: [],
95
+ debug: false,
96
+ };
97
+ /**
98
+ * 发送消息到指定适配器和机器人
99
+ * @param options 消息发送参数(包含 context、bot、内容等)
100
+ * @throws 找不到适配器或机器人时抛出异常
101
+ */
102
+ async sendMessage(options: SendOptions) {
103
+ const adapter = this.getContext<Adapter>(options.context);
104
+ if (!adapter)
105
+ throw new Error(`can't find adapter for name ${options.context}`);
106
+ const bot = adapter.bots.get(options.bot);
107
+ if (!bot)
108
+ throw new Error(
109
+ `can't find bot ${options.bot} for adapter ${options.context}`
110
+ );
111
+ return bot.$sendMessage(options);
112
+ }
113
+ /** 同步加载配置文件 */
114
+ /**
115
+ * 同步加载配置文件(暂不支持,建议使用异步创建)
116
+ * @throws 始终抛出异常,提示使用异步方法
117
+ */
118
+ static loadConfigSync(): AppConfig {
119
+ // 由于loadConfig是异步的,我们需要创建一个同步版本
120
+ // 或者在这里简化处理,让用户使用异步创建方法
121
+ throw new Error("同步加载配置暂不支持,请使用 App.createAsync() 方法");
122
+ }
80
123
 
81
- /** 获取App配置 */
82
- getConfig(): Readonly<AppConfig> {
83
- return { ...this.config };
84
- }
124
+ /** 创建插件依赖 */
125
+ /**
126
+ * 创建插件依赖
127
+ * @param name 插件名
128
+ * @param filePath 插件文件路径
129
+ */
130
+ createDependency(name: string, filePath: string): Plugin {
131
+ return new Plugin(this, name, filePath);
132
+ }
85
133
 
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
- }
134
+ /** 获取App配置 */
135
+ /**
136
+ * 获取App配置(只读)
137
+ */
138
+ getConfig(): Readonly<AppConfig> {
139
+ return { ...this.config };
140
+ }
113
141
 
114
- /** 使用插件 */
115
- use(filePath: string): void {
116
- this.emit('internal.add', filePath);
117
- }
142
+ /** 更新App配置 */
143
+ /**
144
+ * 更新App配置
145
+ * @param config 部分配置项,将与现有配置合并
146
+ */
147
+ updateConfig(config: Partial<AppConfig>): void {
148
+ this.config = { ...this.config, ...config };
149
+
150
+ // 更新HMR配置
151
+ if (config.plugin_dirs) {
152
+ // 动态更新监听目录
153
+ const currentDirs = this.getWatchDirs();
154
+ const newDirs = config.plugin_dirs;
118
155
 
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);
156
+ // 移除不再需要的目录
157
+ for (const dir of currentDirs) {
158
+ if (!newDirs.includes(dir)) {
159
+ this.removeWatchDir(dir);
125
160
  }
126
- // 等待所有插件就绪
127
- await this.waitForReady();
128
- this.logger.info('started successfully');
161
+ }
162
+
163
+ // 添加新的目录
164
+ for (const dir of newDirs) {
165
+ if (!currentDirs.includes(dir)) {
166
+ this.addWatchDir(dir);
167
+ }
168
+ }
129
169
  }
130
170
 
131
- /** 停止App */
132
- async stop(): Promise<void> {
133
- this.logger.info('Stopping app...');
134
- // 销毁所有插件
135
- this.dispose();
171
+ this.logger.info("App configuration updated", this.config);
172
+ }
173
+ get schemas(){
174
+ return this.dependencyList.reduce((result, plugin) => {
175
+ plugin.schemas.forEach((schema, name) => {
176
+ result.set(name, schema);
177
+ });
178
+ return result;
179
+ }, new Map<string,Schema<any>>());
180
+ }
181
+ /** 使用插件 */
182
+ use(filePath: string): void {
183
+ this.emit("internal.add", filePath);
184
+ }
136
185
 
137
- this.logger.info('App stopped');
186
+ /** 启动App */
187
+ async start(mode: "dev" | "prod" = "prod"): Promise<void> {
188
+ await generateEnvTypes(process.cwd());
189
+ // 加载插件
190
+ for (const pluginName of this.config.plugins || []) {
191
+ this.use(pluginName);
138
192
  }
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}`)
193
+ await sleep(200);
194
+ const schemas:Record<string,Schema>={};
195
+ for (const [name, schema] of this.schemas) {
196
+ schemas[name]=schema;
152
197
  }
198
+ if (this.config.database) {
199
+ this.database=Registry.create((this.config.database as any).dialect,this.config.database,schemas);
200
+ await this.database?.start();
201
+ this.logger.info(`database init success`);
202
+ } else {
203
+ this.logger.info(`database not configured, skipping database init`);
204
+ }
205
+ this.dispatch("database.ready",this.database);
206
+ // 等待所有插件就绪
207
+ await this.waitForReady();
208
+ this.logger.info("started successfully");
209
+ this.dispatch("app.ready");
210
+ }
211
+
212
+ /** 停止App */
213
+ async stop(): Promise<void> {
214
+ this.logger.info("Stopping app...");
215
+ // 销毁所有插件
216
+ this.dispose();
153
217
 
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
218
+ this.logger.info("App stopped");
219
+ }
220
+
221
+ getContext<T>(name: string): T {
222
+ for (const dep of this.dependencyList) {
223
+ if (dep.contexts.has(name)) {
224
+ const context = dep.contexts.get(name)!;
225
+ // 如果上下文还没有挂载,等待挂载完成
226
+ if (!context.value) {
227
+ throw new Error(`Context ${name} is not mounted yet`);
162
228
  }
163
- return options
229
+ return context.value;
230
+ }
164
231
  }
165
- getLogger(...names: string[]): Logger {
166
- return new ConsoleLogger(`[${[...names].join('/')}]`, process.env.NODE_ENV === 'development');
232
+ throw new Error(`can't find Context of ${name}`);
233
+ }
234
+
235
+ async handleBeforeSend(options: SendOptions) {
236
+ const handlers = this.dependencyList.reduce((result, plugin) => {
237
+ result.push(...plugin.listeners("before-message.send"));
238
+ return result;
239
+ }, [] as Function[]);
240
+ for (const handler of handlers) {
241
+ const result = await handler(options);
242
+ if (result) options = result;
167
243
  }
244
+ return options;
245
+ }
168
246
  }
169
247
 
170
248
  // ============================================================================
@@ -172,168 +250,195 @@ export class App extends HMR<Plugin> {
172
250
  // ============================================================================
173
251
 
174
252
  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
253
+ const name = path.basename(filename).replace(path.extname(filename), "");
254
+
255
+ // 尝试从当前依赖中查找插件
256
+ const childPlugin = hmr.findChild(filename);
257
+ if (childPlugin) {
258
+ return childPlugin;
259
+ }
260
+ const parent = hmr.findParent(
261
+ filename,
262
+ getCallerFiles(fileURLToPath(import.meta.url))
263
+ );
264
+ // 创建新的插件实例
265
+ const newPlugin = new Plugin(parent, name, filename);
266
+
267
+ // 添加到当前依赖的子依赖中
268
+ parent.dependencies.set(filename, newPlugin);
269
+
270
+ return newPlugin;
217
271
  }
218
272
  /** 获取App实例 */
219
273
  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;
274
+ const hmr = HMR.currentHMR;
275
+ if (!hmr) throw new Error("useApp must be called within a App Context");
276
+ return hmr as unknown as App;
277
+ }
278
+ export function defineModel<T extends Record<string, any>>(
279
+ name: string,
280
+ schema: Schema<T>,
281
+ ) {
282
+ const plugin = usePlugin();
283
+ return plugin.defineModel(name, schema);
223
284
  }
224
285
 
225
286
  /** 获取当前插件实例 */
226
287
  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;
288
+ const hmr = HMR.currentHMR;
289
+ if (!hmr) throw new Error("usePlugin must be called within a App Context");
290
+
291
+ try {
292
+ const currentFile = getCallerFile(import.meta.url);
293
+ return getPlugin(hmr as unknown as HMR<Plugin>, currentFile);
294
+ } catch (error) {
295
+ // 如果无法获取当前文件,尝试从当前依赖获取
296
+ if (HMR.currentDependency) {
297
+ return HMR.currentDependency as unknown as Plugin;
239
298
  }
299
+ throw error;
300
+ }
240
301
  }
241
- export function beforeSend(handler:BeforeSendHandler){
242
- const plugin = usePlugin();
243
- return plugin.beforeSend(handler);
302
+ export function beforeSend(handler: BeforeSendHandler) {
303
+ const plugin = usePlugin();
304
+ return plugin.beforeSend(handler);
244
305
  }
245
306
  /** 创建Context */
246
- export function register<T>(context: Context<T,Plugin>): Context<T,Plugin> {
247
- const plugin = usePlugin();
248
- return plugin.register(context);
307
+ export function register<T>(context: Context<T, Plugin>): Context<T, Plugin> {
308
+ const plugin = usePlugin();
309
+ return plugin.register(context);
249
310
  }
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
- })
311
+ export function registerAdapter<T extends Adapter>(adapter: T) {
312
+ const plugin = usePlugin();
313
+ plugin.app.adapters.push(adapter.name);
314
+ plugin.register({
315
+ name: adapter.name,
316
+ description: `adapter for ${adapter.name}`,
317
+ async mounted(plugin) {
318
+ await adapter.start(plugin);
319
+ return adapter;
320
+ },
321
+ dispose() {
322
+ return adapter.stop(plugin);
323
+ },
324
+ });
262
325
  }
263
326
 
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
327
 
271
328
  /** 标记必需的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);
329
+ export function useContext<T extends (keyof GlobalContext)[]>(
330
+ ...args: [...T, sideEffect: SideEffect<T>]
331
+ ): void {
332
+ const plugin = usePlugin();
333
+ plugin.useContext(...(args as any));
275
334
  }
276
335
 
277
336
  /** 添加中间件 */
278
337
  export function addMiddleware(middleware: MessageMiddleware): void {
279
- const plugin = usePlugin();
280
- plugin.addMiddleware(middleware);
338
+ const plugin = usePlugin();
339
+ plugin.addMiddleware(middleware);
340
+ }
341
+ export function onDatabaseReady(callback: (database: RelatedDatabase<any,Models>|DocumentDatabase<any,Models>|KeyValueDatabase<any,Models>) => PromiseLike<void>) {
342
+ const plugin = usePlugin();
343
+ if (plugin.app.database?.isStarted) callback(plugin.app.database);
344
+ plugin.on("database.ready", callback);
345
+ }
346
+ export function useDatabase() {
347
+ const plugin = usePlugin();
348
+ return plugin.app.database;
349
+ }
350
+ export function onAppReady(callback: () => PromiseLike<void>) {
351
+ const plugin = usePlugin();
352
+ if (plugin.app.isReady) callback();
353
+ plugin.on("app.ready", callback);
354
+ }
355
+ /** 添加指令 */
356
+ export function addCommand(command: MessageCommand): void {
357
+ const plugin = usePlugin();
358
+ plugin.addCommand(command);
359
+ }
360
+
361
+ /** 添加组件 */
362
+ export function addComponent<T = {}, D = {}, P = Component.Props<T>>(
363
+ component: Component<T, D, P>
364
+ ): void {
365
+ const plugin = usePlugin();
366
+ plugin.addComponent(component);
281
367
  }
282
368
 
283
369
  /** 监听事件 */
284
- export function onEvent<T = any>(event: string, listener: EventListener<T>): void {
285
- const plugin = usePlugin();
286
- plugin.on(event, listener);
370
+ export function onEvent(
371
+ event: string,
372
+ listener: (...args: any[]) => any
373
+ ): void {
374
+ const plugin = usePlugin();
375
+ plugin.on(event, listener);
287
376
  }
288
377
 
289
378
  /** 监听群组消息 */
290
- export function onGroupMessage(handler: (message: Message) => void | Promise<void>): void {
291
- onEvent('message.group.receive', handler);
379
+ export function onGroupMessage(
380
+ handler: (message: Message) => void | Promise<void>
381
+ ): void {
382
+ onEvent("message.group.receive", handler);
292
383
  }
293
384
 
294
385
  /** 监听私聊消息 */
295
- export function onPrivateMessage(handler: (message: Message) => void | Promise<void>): void {
296
- onEvent('message.private.receive', handler);
386
+ export function onPrivateMessage(
387
+ handler: (message: Message) => void | Promise<void>
388
+ ): void {
389
+ onEvent("message.private.receive", handler);
297
390
  }
298
391
 
299
392
  /** 监听所有消息 */
300
- export function onMessage(handler: (message: Message) => void | Promise<void>): void {
301
- onEvent('message.receive', handler);
393
+ export function onMessage<T extends RegisteredAdapter>(
394
+ handler: (message: Message<AdapterMessage<T>>) => void | Promise<void>
395
+ ): void {
396
+ onEvent("message.receive", handler);
397
+ }
398
+ /** 获取下一条消息 */
399
+ export function usePrompt<P extends RegisteredAdapter>(
400
+ message: Message<AdapterMessage<P>>
401
+ ) {
402
+ const plugin = usePlugin();
403
+ return plugin.prompt<P>(message);
302
404
  }
303
405
 
304
406
  /** 监听插件挂载事件 */
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);
407
+ export function onMounted(
408
+ hook: (plugin: Plugin) => Promise<void> | void
409
+ ): void {
410
+ const plugin = usePlugin();
411
+ if (plugin.isReady) hook(plugin);
412
+ plugin.on("self.mounted", hook);
309
413
  }
310
414
 
311
415
  /** 监听插件销毁事件 */
312
416
  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);
417
+ const plugin = usePlugin();
418
+ if (plugin.isDispose) hook();
419
+ plugin.on("self.dispose", hook);
322
420
  }
323
421
 
324
422
  /** 发送消息 */
325
- export async function sendMessage(options:SendOptions): Promise<void> {
326
- const app = useApp();
327
- await app.sendMessage(options);
423
+ export async function sendMessage(options: SendOptions): Promise<void> {
424
+ const app = useApp();
425
+ await app.sendMessage(options);
328
426
  }
329
427
 
330
428
  /** 获取App实例(用于高级操作) */
331
429
  export function getAppInstance(): App {
332
- return useApp();
430
+ return useApp();
333
431
  }
334
432
 
335
433
  /** 获取插件日志记录器 */
336
434
  export function useLogger(): Logger {
337
- const plugin = usePlugin();
338
- return plugin.logger;
339
- }
435
+ const plugin = usePlugin();
436
+ return plugin.logger;
437
+ }
438
+
439
+ /** 创建App实例的工厂函数 */
440
+ export async function createApp(config?: Partial<AppConfig>): Promise<App> {
441
+ const app = new App(config);
442
+ await app.start();
443
+ return app;
444
+ }