@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.
- package/CHANGELOG.md +16 -0
- package/LICENSE +21 -0
- package/README.md +295 -74
- package/lib/adapter.d.ts +39 -0
- package/lib/adapter.d.ts.map +1 -0
- package/{dist → lib}/adapter.js +20 -2
- package/lib/adapter.js.map +1 -0
- package/lib/app.d.ts +115 -0
- package/lib/app.d.ts.map +1 -0
- package/{dist → lib}/app.js +148 -78
- package/lib/app.js.map +1 -0
- package/lib/bot.d.ts +31 -0
- package/lib/bot.d.ts.map +1 -0
- package/lib/command.d.ts +32 -0
- package/lib/command.d.ts.map +1 -0
- package/lib/command.js +46 -0
- package/lib/command.js.map +1 -0
- package/lib/component.d.ts +27 -0
- package/lib/component.d.ts.map +1 -0
- package/lib/component.js +469 -0
- package/lib/component.js.map +1 -0
- package/{dist → lib}/config.d.ts.map +1 -1
- package/{dist → lib}/config.js +6 -9
- package/lib/config.js.map +1 -0
- package/lib/cron.d.ts +81 -0
- package/lib/cron.d.ts.map +1 -0
- package/lib/cron.js +159 -0
- package/lib/cron.js.map +1 -0
- package/lib/errors.d.ts +165 -0
- package/lib/errors.d.ts.map +1 -0
- package/lib/errors.js +306 -0
- package/lib/errors.js.map +1 -0
- package/lib/index.d.ts +15 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +17 -0
- package/lib/index.js.map +1 -0
- package/lib/jsx-runtime.d.ts +12 -0
- package/lib/jsx-runtime.d.ts.map +1 -0
- package/lib/jsx-runtime.js +11 -0
- package/lib/jsx-runtime.js.map +1 -0
- package/lib/jsx.d.ts +32 -0
- package/lib/jsx.d.ts.map +1 -0
- package/lib/jsx.js +57 -0
- package/lib/jsx.js.map +1 -0
- package/lib/message.d.ts +47 -0
- package/lib/message.d.ts.map +1 -0
- package/lib/message.js +11 -0
- package/lib/message.js.map +1 -0
- package/lib/plugin.d.ts +50 -0
- package/lib/plugin.d.ts.map +1 -0
- package/lib/plugin.js +170 -0
- package/lib/plugin.js.map +1 -0
- package/lib/prompt.d.ts +116 -0
- package/lib/prompt.d.ts.map +1 -0
- package/lib/prompt.js +240 -0
- package/lib/prompt.js.map +1 -0
- package/lib/schema.d.ts +83 -0
- package/lib/schema.d.ts.map +1 -0
- package/lib/schema.js +245 -0
- package/lib/schema.js.map +1 -0
- package/{dist → lib}/types-generator.d.ts.map +1 -1
- package/{dist → lib}/types-generator.js +6 -3
- package/lib/types-generator.js.map +1 -0
- package/lib/types.d.ts +121 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/utils.d.ts +52 -0
- package/lib/utils.d.ts.map +1 -0
- package/lib/utils.js +340 -0
- package/lib/utils.js.map +1 -0
- package/package.json +23 -9
- package/src/adapter.ts +25 -9
- package/src/app.ts +363 -258
- package/src/bot.ts +29 -8
- package/src/command.ts +50 -0
- package/src/component.ts +561 -0
- package/src/config.ts +9 -12
- package/src/cron.ts +176 -0
- package/src/errors.ts +365 -0
- package/src/index.ts +16 -13
- package/src/jsx-runtime.ts +12 -0
- package/src/jsx.d.ts +52 -0
- package/src/jsx.ts +92 -0
- package/src/message.ts +47 -0
- package/src/plugin.ts +148 -66
- package/src/prompt.ts +290 -0
- package/src/schema.ts +273 -0
- package/src/types-generator.ts +7 -3
- package/src/types.ts +80 -31
- package/src/utils.ts +313 -0
- package/tests/adapter.test.ts +36 -22
- package/tests/app.test.ts +30 -0
- package/tests/command.test.ts +545 -0
- package/tests/component-new.test.ts +348 -0
- package/tests/config.test.ts +1 -1
- package/tests/errors.test.ts +311 -0
- package/tests/expression-evaluation.test.ts +258 -0
- package/tests/message.test.ts +402 -0
- package/tests/plugin.test.ts +284 -143
- package/tests/utils.test.ts +80 -0
- package/tsconfig.json +3 -4
- package/dist/adapter.d.ts +0 -22
- package/dist/adapter.d.ts.map +0 -1
- package/dist/adapter.js.map +0 -1
- package/dist/app.d.ts +0 -69
- package/dist/app.d.ts.map +0 -1
- package/dist/app.js.map +0 -1
- package/dist/bot.d.ts +0 -9
- package/dist/bot.d.ts.map +0 -1
- package/dist/config.js.map +0 -1
- package/dist/index.d.ts +0 -9
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -12
- package/dist/index.js.map +0 -1
- package/dist/logger.d.ts +0 -3
- package/dist/logger.d.ts.map +0 -1
- package/dist/logger.js +0 -3
- package/dist/logger.js.map +0 -1
- package/dist/plugin.d.ts +0 -41
- package/dist/plugin.d.ts.map +0 -1
- package/dist/plugin.js +0 -95
- package/dist/plugin.js.map +0 -1
- package/dist/types-generator.js.map +0 -1
- package/dist/types.d.ts +0 -69
- package/dist/types.d.ts.map +0 -1
- package/src/logger.ts +0 -3
- package/tests/logger.test.ts +0 -170
- package/tsconfig.tsbuildinfo +0 -1
- /package/{dist → lib}/bot.js +0 -0
- /package/{dist → lib}/bot.js.map +0 -0
- /package/{dist → lib}/config.d.ts +0 -0
- /package/{dist → lib}/types-generator.d.ts +0 -0
- /package/{dist → lib}/types.js +0 -0
- /package/{dist → lib}/types.js.map +0 -0
package/src/adapter.ts
CHANGED
|
@@ -1,13 +1,25 @@
|
|
|
1
|
-
import {BotConfig} from "./
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import {Bot,BotConfig} from "./bot.js";
|
|
2
|
+
import {Plugin} from "./plugin.js";
|
|
3
|
+
/**
|
|
4
|
+
* Adapter类:适配器抽象,管理多平台Bot实例。
|
|
5
|
+
* 负责根据配置启动/关闭各平台机器人,统一异常处理。
|
|
6
|
+
*/
|
|
5
7
|
export class Adapter<R extends Bot=Bot>{
|
|
8
|
+
/** 当前适配器下所有Bot实例,key为bot名称 */
|
|
6
9
|
public bots:Map<string,R>=new Map<string, R>()
|
|
7
10
|
#botFactory:Adapter.BotFactory<R>
|
|
11
|
+
/**
|
|
12
|
+
* 构造函数
|
|
13
|
+
* @param name 适配器名称(如 'process'、'qq' 等)
|
|
14
|
+
* @param botFactory Bot工厂函数或构造器
|
|
15
|
+
*/
|
|
8
16
|
constructor(public name:string,botFactory:Adapter.BotFactory<R>) {
|
|
9
17
|
this.#botFactory=botFactory
|
|
10
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* 启动适配器,自动根据配置创建并连接所有Bot
|
|
21
|
+
* @param plugin 所属插件实例
|
|
22
|
+
*/
|
|
11
23
|
async start(plugin:Plugin){
|
|
12
24
|
const configs=plugin.app.getConfig().bots?.filter(c=>c.context===this.name)
|
|
13
25
|
if(!configs?.length) return
|
|
@@ -20,7 +32,7 @@ export class Adapter<R extends Bot=Bot>{
|
|
|
20
32
|
bot = this.#botFactory(plugin,config) as R
|
|
21
33
|
}
|
|
22
34
|
try {
|
|
23
|
-
await bot
|
|
35
|
+
await bot.$connect()
|
|
24
36
|
plugin.logger.info(`bot ${config.name} of adapter ${this.name} connected`)
|
|
25
37
|
this.bots.set(config.name,bot)
|
|
26
38
|
} catch (error) {
|
|
@@ -35,11 +47,15 @@ export class Adapter<R extends Bot=Bot>{
|
|
|
35
47
|
throw error
|
|
36
48
|
}
|
|
37
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* 停止适配器,断开并移除所有Bot实例
|
|
52
|
+
* @param plugin 所属插件实例
|
|
53
|
+
*/
|
|
38
54
|
async stop(plugin:Plugin){
|
|
39
55
|
try {
|
|
40
56
|
for(const [name,bot] of this.bots){
|
|
41
57
|
try {
|
|
42
|
-
await bot
|
|
58
|
+
await bot.$disconnect()
|
|
43
59
|
plugin.logger.info(`bot ${name} of adapter ${this.name} disconnected`)
|
|
44
60
|
this.bots.delete(name)
|
|
45
61
|
} catch (error) {
|
|
@@ -55,8 +71,8 @@ export class Adapter<R extends Bot=Bot>{
|
|
|
55
71
|
}
|
|
56
72
|
}
|
|
57
73
|
export namespace Adapter {
|
|
58
|
-
export type BotBotConstructor<T extends Bot>=T extends Bot<infer
|
|
59
|
-
new(plugin:Plugin,config:
|
|
74
|
+
export type BotBotConstructor<T extends Bot>=T extends Bot<infer F,infer S> ? {
|
|
75
|
+
new(plugin:Plugin,config:S):T
|
|
60
76
|
}: {
|
|
61
77
|
new(plugin:Plugin,config:BotConfig):T
|
|
62
78
|
}
|
|
@@ -64,6 +80,6 @@ export namespace Adapter {
|
|
|
64
80
|
return fn.prototype &&
|
|
65
81
|
fn.prototype.constructor === fn
|
|
66
82
|
}
|
|
67
|
-
export type BotCreator<T extends Bot>=T extends Bot<infer
|
|
83
|
+
export type BotCreator<T extends Bot>=T extends Bot<infer F,infer S> ? (plugin:Plugin,config: S) => T : (plugin:Plugin,config: BotConfig) => T
|
|
68
84
|
export type BotFactory<T extends Bot> = BotBotConstructor<T>|BotCreator<T>
|
|
69
85
|
}
|