@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/cron.ts
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { CronExpressionParser, CronExpression } from 'cron-parser';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Cron 定时任务类
|
|
5
|
+
* 基于 cron-parser 实现的定时任务调度器
|
|
6
|
+
*/
|
|
7
|
+
export class Cron {
|
|
8
|
+
private expression: CronExpression;
|
|
9
|
+
private callback: () => void | Promise<void>;
|
|
10
|
+
private timeoutId?: NodeJS.Timeout;
|
|
11
|
+
private isRunning = false;
|
|
12
|
+
private isDisposed = false;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 创建一个新的 Cron 实例
|
|
16
|
+
* @param cronExpression - Cron 表达式 (例如: '0 0 * * *' 表示每天午夜执行)
|
|
17
|
+
* @param callback - 要执行的回调函数
|
|
18
|
+
*/
|
|
19
|
+
constructor(cronExpression: string, callback: () => void | Promise<void>) {
|
|
20
|
+
try {
|
|
21
|
+
this.expression = CronExpressionParser.parse(cronExpression);
|
|
22
|
+
this.callback = callback;
|
|
23
|
+
} catch (error) {
|
|
24
|
+
throw new Error(`Invalid cron expression "${cronExpression}": ${(error as Error).message}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 启动定时任务
|
|
30
|
+
*/
|
|
31
|
+
run(): void {
|
|
32
|
+
if (this.isDisposed) {
|
|
33
|
+
throw new Error('Cannot run a disposed cron job');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (this.isRunning) {
|
|
37
|
+
return; // 已经在运行中
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
this.isRunning = true;
|
|
41
|
+
this.scheduleNext();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 停止定时任务
|
|
46
|
+
*/
|
|
47
|
+
stop(): void {
|
|
48
|
+
if (this.timeoutId) {
|
|
49
|
+
clearTimeout(this.timeoutId);
|
|
50
|
+
this.timeoutId = undefined;
|
|
51
|
+
}
|
|
52
|
+
this.isRunning = false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 销毁定时任务,释放资源
|
|
57
|
+
*/
|
|
58
|
+
dispose(): void {
|
|
59
|
+
this.stop();
|
|
60
|
+
this.isDisposed = true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 获取下一次执行时间
|
|
65
|
+
*/
|
|
66
|
+
getNextExecutionTime(): Date {
|
|
67
|
+
if (this.isDisposed) {
|
|
68
|
+
throw new Error('Cannot get next execution time for a disposed cron job');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// 重置表达式到当前时间
|
|
72
|
+
this.expression.reset();
|
|
73
|
+
return this.expression.next().toDate();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 检查任务是否正在运行
|
|
78
|
+
*/
|
|
79
|
+
get running(): boolean {
|
|
80
|
+
return this.isRunning;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 检查任务是否已被销毁
|
|
85
|
+
*/
|
|
86
|
+
get disposed(): boolean {
|
|
87
|
+
return this.isDisposed;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 获取原始的 cron 表达式字符串
|
|
92
|
+
*/
|
|
93
|
+
get cronExpression(): string {
|
|
94
|
+
return this.expression.stringify();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 调度下一次执行
|
|
99
|
+
*/
|
|
100
|
+
private scheduleNext(): void {
|
|
101
|
+
if (!this.isRunning || this.isDisposed) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
try {
|
|
106
|
+
// 重置到当前时间并获取下一次执行时间
|
|
107
|
+
this.expression.reset();
|
|
108
|
+
const nextDate = this.expression.next().toDate();
|
|
109
|
+
const now = new Date();
|
|
110
|
+
const delay = nextDate.getTime() - now.getTime();
|
|
111
|
+
|
|
112
|
+
// 如果延迟时间为负数或0,说明应该立即执行
|
|
113
|
+
if (delay <= 0) {
|
|
114
|
+
this.executeCallback();
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// 设置定时器
|
|
119
|
+
this.timeoutId = setTimeout(() => {
|
|
120
|
+
this.executeCallback();
|
|
121
|
+
}, delay);
|
|
122
|
+
|
|
123
|
+
} catch (error) {
|
|
124
|
+
console.error(`Error scheduling next cron execution: ${(error as Error).message}`);
|
|
125
|
+
// 如果出错,停止任务
|
|
126
|
+
this.stop();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 执行回调函数并调度下一次执行
|
|
132
|
+
*/
|
|
133
|
+
private async executeCallback(): Promise<void> {
|
|
134
|
+
if (!this.isRunning || this.isDisposed) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
// 执行回调函数
|
|
140
|
+
await this.callback();
|
|
141
|
+
} catch (error) {
|
|
142
|
+
console.error(`Error executing cron callback: ${(error as Error).message}`);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// 调度下一次执行
|
|
146
|
+
this.scheduleNext();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Cron 表达式格式说明:
|
|
152
|
+
*
|
|
153
|
+
* 标准格式: "秒 分 时 日 月 周"
|
|
154
|
+
*
|
|
155
|
+
* 字段说明:
|
|
156
|
+
* - 秒: 0-59
|
|
157
|
+
* - 分: 0-59
|
|
158
|
+
* - 时: 0-23
|
|
159
|
+
* - 日: 1-31
|
|
160
|
+
* - 月: 1-12 (或 JAN-DEC)
|
|
161
|
+
* - 周: 0-7 (0和7都表示周日,或 SUN-SAT)
|
|
162
|
+
*
|
|
163
|
+
* 特殊字符:
|
|
164
|
+
* - 星号: 匹配任意值
|
|
165
|
+
* - 问号: 用于日和周字段,表示不指定值
|
|
166
|
+
* - 横线: 表示范围,如 1-5
|
|
167
|
+
* - 逗号: 表示列表,如 1,3,5
|
|
168
|
+
* - 斜杠: 表示步长,如 0/15 表示每15分钟
|
|
169
|
+
*
|
|
170
|
+
* 常用示例:
|
|
171
|
+
* - "0 0 0 * * *": 每天午夜执行
|
|
172
|
+
* - "0 0/15 * * * *": 每15分钟执行
|
|
173
|
+
* - "0 0 12 * * *": 每天中午12点执行
|
|
174
|
+
* - "0 0 0 1 * *": 每月1号午夜执行
|
|
175
|
+
* - "0 0 0 * * 0": 每周日午夜执行
|
|
176
|
+
*/
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// 错误处理系统
|
|
3
|
+
// ============================================================================
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 基础错误类,所有自定义错误都应该继承此类
|
|
7
|
+
*/
|
|
8
|
+
export class ZhinError extends Error {
|
|
9
|
+
public readonly code: string
|
|
10
|
+
public readonly timestamp: Date
|
|
11
|
+
public readonly context?: Record<string, any>
|
|
12
|
+
|
|
13
|
+
constructor(message: string, code: string = 'ZHIN_ERROR', context?: Record<string, any>) {
|
|
14
|
+
super(message)
|
|
15
|
+
this.name = this.constructor.name
|
|
16
|
+
this.code = code
|
|
17
|
+
this.timestamp = new Date()
|
|
18
|
+
this.context = context
|
|
19
|
+
|
|
20
|
+
// 确保错误堆栈正确显示
|
|
21
|
+
if (Error.captureStackTrace) {
|
|
22
|
+
Error.captureStackTrace(this, this.constructor)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 转换为JSON格式
|
|
28
|
+
*/
|
|
29
|
+
toJSON() {
|
|
30
|
+
return {
|
|
31
|
+
name: this.name,
|
|
32
|
+
message: this.message,
|
|
33
|
+
code: this.code,
|
|
34
|
+
timestamp: this.timestamp.toISOString(),
|
|
35
|
+
context: this.context,
|
|
36
|
+
stack: this.stack
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 转换为用户友好的格式
|
|
42
|
+
*/
|
|
43
|
+
toUserString(): string {
|
|
44
|
+
return `[${this.code}] ${this.message}`
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 配置相关错误
|
|
50
|
+
*/
|
|
51
|
+
export class ConfigError extends ZhinError {
|
|
52
|
+
constructor(message: string, context?: Record<string, any>) {
|
|
53
|
+
super(message, 'CONFIG_ERROR', context)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 插件相关错误
|
|
59
|
+
*/
|
|
60
|
+
export class PluginError extends ZhinError {
|
|
61
|
+
public readonly pluginName: string
|
|
62
|
+
|
|
63
|
+
constructor(message: string, pluginName: string, context?: Record<string, any>) {
|
|
64
|
+
super(message, 'PLUGIN_ERROR', { ...context, pluginName })
|
|
65
|
+
this.pluginName = pluginName
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 适配器相关错误
|
|
71
|
+
*/
|
|
72
|
+
export class AdapterError extends ZhinError {
|
|
73
|
+
public readonly adapterName: string
|
|
74
|
+
public readonly botName?: string
|
|
75
|
+
|
|
76
|
+
constructor(message: string, adapterName: string, botName?: string, context?: Record<string, any>) {
|
|
77
|
+
super(message, 'ADAPTER_ERROR', { ...context, adapterName, botName })
|
|
78
|
+
this.adapterName = adapterName
|
|
79
|
+
this.botName = botName
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 连接相关错误
|
|
85
|
+
*/
|
|
86
|
+
export class ConnectionError extends ZhinError {
|
|
87
|
+
public readonly retryable: boolean
|
|
88
|
+
|
|
89
|
+
constructor(message: string, retryable: boolean = true, context?: Record<string, any>) {
|
|
90
|
+
super(message, 'CONNECTION_ERROR', { ...context, retryable })
|
|
91
|
+
this.retryable = retryable
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 消息处理错误
|
|
97
|
+
*/
|
|
98
|
+
export class MessageError extends ZhinError {
|
|
99
|
+
public readonly messageId?: string
|
|
100
|
+
public readonly channelId?: string
|
|
101
|
+
|
|
102
|
+
constructor(message: string, messageId?: string, channelId?: string, context?: Record<string, any>) {
|
|
103
|
+
super(message, 'MESSAGE_ERROR', { ...context, messageId, channelId })
|
|
104
|
+
this.messageId = messageId
|
|
105
|
+
this.channelId = channelId
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 上下文相关错误
|
|
111
|
+
*/
|
|
112
|
+
export class ContextError extends ZhinError {
|
|
113
|
+
public readonly contextName: string
|
|
114
|
+
|
|
115
|
+
constructor(message: string, contextName: string, context?: Record<string, any>) {
|
|
116
|
+
super(message, 'CONTEXT_ERROR', { ...context, contextName })
|
|
117
|
+
this.contextName = contextName
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 验证错误
|
|
123
|
+
*/
|
|
124
|
+
export class ValidationError extends ZhinError {
|
|
125
|
+
public readonly field?: string
|
|
126
|
+
public readonly value?: any
|
|
127
|
+
|
|
128
|
+
constructor(message: string, field?: string, value?: any, context?: Record<string, any>) {
|
|
129
|
+
super(message, 'VALIDATION_ERROR', { ...context, field, value })
|
|
130
|
+
this.field = field
|
|
131
|
+
this.value = value
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 权限错误
|
|
137
|
+
*/
|
|
138
|
+
export class PermissionError extends ZhinError {
|
|
139
|
+
public readonly userId?: string
|
|
140
|
+
public readonly requiredPermission?: string
|
|
141
|
+
|
|
142
|
+
constructor(message: string, userId?: string, requiredPermission?: string, context?: Record<string, any>) {
|
|
143
|
+
super(message, 'PERMISSION_ERROR', { ...context, userId, requiredPermission })
|
|
144
|
+
this.userId = userId
|
|
145
|
+
this.requiredPermission = requiredPermission
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 超时错误
|
|
151
|
+
*/
|
|
152
|
+
export class TimeoutError extends ZhinError {
|
|
153
|
+
public readonly timeoutMs: number
|
|
154
|
+
|
|
155
|
+
constructor(message: string, timeoutMs: number, context?: Record<string, any>) {
|
|
156
|
+
super(message, 'TIMEOUT_ERROR', { ...context, timeoutMs })
|
|
157
|
+
this.timeoutMs = timeoutMs
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 错误处理器接口
|
|
163
|
+
*/
|
|
164
|
+
export interface ErrorHandler {
|
|
165
|
+
(error: Error, context?: Record<string, any>): void | Promise<void>
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* 错误管理器
|
|
170
|
+
*/
|
|
171
|
+
export class ErrorManager {
|
|
172
|
+
private handlers: Map<string, ErrorHandler[]> = new Map()
|
|
173
|
+
private globalHandlers: ErrorHandler[] = []
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* 注册错误处理器
|
|
177
|
+
*/
|
|
178
|
+
register(errorType: string, handler: ErrorHandler): void {
|
|
179
|
+
if (!this.handlers.has(errorType)) {
|
|
180
|
+
this.handlers.set(errorType, [])
|
|
181
|
+
}
|
|
182
|
+
this.handlers.get(errorType)!.push(handler)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* 注册全局错误处理器
|
|
187
|
+
*/
|
|
188
|
+
registerGlobal(handler: ErrorHandler): void {
|
|
189
|
+
this.globalHandlers.push(handler)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* 处理错误
|
|
194
|
+
*/
|
|
195
|
+
async handle(error: Error, context?: Record<string, any>): Promise<void> {
|
|
196
|
+
// 首先调用全局处理器
|
|
197
|
+
for (const handler of this.globalHandlers) {
|
|
198
|
+
try {
|
|
199
|
+
await handler(error, context)
|
|
200
|
+
} catch (handlerError) {
|
|
201
|
+
// console.error 已替换为注释
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// 然后调用特定类型的处理器
|
|
206
|
+
const errorType = error.constructor.name
|
|
207
|
+
const handlers = this.handlers.get(errorType) || []
|
|
208
|
+
|
|
209
|
+
for (const handler of handlers) {
|
|
210
|
+
try {
|
|
211
|
+
await handler(error, context)
|
|
212
|
+
} catch (handlerError) {
|
|
213
|
+
// console.error 已替换为注释
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* 移除错误处理器
|
|
220
|
+
*/
|
|
221
|
+
unregister(errorType: string, handler: ErrorHandler): boolean {
|
|
222
|
+
const handlers = this.handlers.get(errorType)
|
|
223
|
+
if (handlers) {
|
|
224
|
+
const index = handlers.indexOf(handler)
|
|
225
|
+
if (index !== -1) {
|
|
226
|
+
handlers.splice(index, 1)
|
|
227
|
+
return true
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return false
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* 清理所有处理器
|
|
235
|
+
*/
|
|
236
|
+
clear(): void {
|
|
237
|
+
this.handlers.clear()
|
|
238
|
+
this.globalHandlers.length = 0
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* 错误重试工具
|
|
244
|
+
*/
|
|
245
|
+
export class RetryManager {
|
|
246
|
+
/**
|
|
247
|
+
* 执行重试逻辑
|
|
248
|
+
*/
|
|
249
|
+
static async retry<T>(
|
|
250
|
+
fn: () => Promise<T>,
|
|
251
|
+
options: {
|
|
252
|
+
maxRetries?: number
|
|
253
|
+
delay?: number
|
|
254
|
+
exponentialBackoff?: boolean
|
|
255
|
+
retryCondition?: (error: Error) => boolean
|
|
256
|
+
} = {}
|
|
257
|
+
): Promise<T> {
|
|
258
|
+
const {
|
|
259
|
+
maxRetries = 3,
|
|
260
|
+
delay = 1000,
|
|
261
|
+
exponentialBackoff = true,
|
|
262
|
+
retryCondition = () => true
|
|
263
|
+
} = options
|
|
264
|
+
|
|
265
|
+
let lastError: Error
|
|
266
|
+
|
|
267
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
268
|
+
try {
|
|
269
|
+
return await fn()
|
|
270
|
+
} catch (error) {
|
|
271
|
+
lastError = error as Error
|
|
272
|
+
|
|
273
|
+
// 如果是最后一次尝试或不满足重试条件,直接抛出错误
|
|
274
|
+
if (attempt === maxRetries || !retryCondition(lastError)) {
|
|
275
|
+
throw lastError
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// 计算延迟时间
|
|
279
|
+
const currentDelay = exponentialBackoff
|
|
280
|
+
? delay * Math.pow(2, attempt)
|
|
281
|
+
: delay
|
|
282
|
+
|
|
283
|
+
// 等待后重试
|
|
284
|
+
await new Promise(resolve => setTimeout(resolve, currentDelay))
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
throw lastError!
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* 断路器模式实现
|
|
294
|
+
*/
|
|
295
|
+
export class CircuitBreaker {
|
|
296
|
+
private failures: number = 0
|
|
297
|
+
private lastFailureTime: number = 0
|
|
298
|
+
private state: 'CLOSED' | 'OPEN' | 'HALF_OPEN' = 'CLOSED'
|
|
299
|
+
|
|
300
|
+
constructor(
|
|
301
|
+
private failureThreshold: number = 5,
|
|
302
|
+
private timeoutMs: number = 60000,
|
|
303
|
+
private monitoringPeriodMs: number = 10000
|
|
304
|
+
) {}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* 执行受保护的操作
|
|
308
|
+
*/
|
|
309
|
+
async execute<T>(fn: () => Promise<T>): Promise<T> {
|
|
310
|
+
if (this.state === 'OPEN') {
|
|
311
|
+
if (Date.now() - this.lastFailureTime > this.timeoutMs) {
|
|
312
|
+
this.state = 'HALF_OPEN'
|
|
313
|
+
} else {
|
|
314
|
+
throw new Error('Circuit breaker is OPEN')
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
try {
|
|
319
|
+
const result = await fn()
|
|
320
|
+
this.onSuccess()
|
|
321
|
+
return result
|
|
322
|
+
} catch (error) {
|
|
323
|
+
this.onFailure()
|
|
324
|
+
throw error
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
private onSuccess(): void {
|
|
329
|
+
this.failures = 0
|
|
330
|
+
this.state = 'CLOSED'
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private onFailure(): void {
|
|
334
|
+
this.failures++
|
|
335
|
+
this.lastFailureTime = Date.now()
|
|
336
|
+
|
|
337
|
+
if (this.failures >= this.failureThreshold) {
|
|
338
|
+
this.state = 'OPEN'
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* 获取断路器状态
|
|
344
|
+
*/
|
|
345
|
+
getState(): 'CLOSED' | 'OPEN' | 'HALF_OPEN' {
|
|
346
|
+
return this.state
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* 重置断路器
|
|
351
|
+
*/
|
|
352
|
+
reset(): void {
|
|
353
|
+
this.failures = 0
|
|
354
|
+
this.lastFailureTime = 0
|
|
355
|
+
this.state = 'CLOSED'
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// 默认错误管理器实例
|
|
360
|
+
export const errorManager = new ErrorManager()
|
|
361
|
+
|
|
362
|
+
// 默认错误处理器
|
|
363
|
+
errorManager.registerGlobal((error, context) => {
|
|
364
|
+
// Default error handler - logs to console
|
|
365
|
+
})
|
package/src/index.ts
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export
|
|
7
|
-
export { loadConfig, saveConfig, createDefaultConfig } from './config.js';
|
|
8
|
-
export * from './types.js';
|
|
9
|
-
export * from './config.js';
|
|
1
|
+
// Core exports
|
|
2
|
+
export * from './app.js'
|
|
3
|
+
export * from './bot.js'
|
|
4
|
+
export * from './plugin.js'
|
|
5
|
+
export * from './command.js'
|
|
6
|
+
export * from './component.js'
|
|
10
7
|
export * from './adapter.js'
|
|
11
|
-
export * from './
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export * from '
|
|
8
|
+
export * from './config.js'
|
|
9
|
+
export * from './message.js'
|
|
10
|
+
// Logger moved to @zhin.js/logger package
|
|
11
|
+
export * from './types.js'
|
|
12
|
+
export * from './utils.js'
|
|
13
|
+
export * from './errors.js' // 导出错误处理系统
|
|
14
|
+
export * from './cron.js'
|
|
15
|
+
export * from '@zhin.js/database'
|
|
16
|
+
|
|
17
|
+
export { Dependency } from '@zhin.js/hmr'
|
package/src/jsx.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { SendContent } from './types.js';
|
|
2
|
+
import { Component,ComponentContext } from './component.js';
|
|
3
|
+
import { MessageElement } from './types.js';
|
|
4
|
+
|
|
5
|
+
declare global {
|
|
6
|
+
namespace JSX {
|
|
7
|
+
interface Element {
|
|
8
|
+
type: string | Component<any>;
|
|
9
|
+
data: Record<string, any>;
|
|
10
|
+
children?: JSXChildren;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface ElementClass {
|
|
14
|
+
render(props: any, context?: ComponentContext): MaybePromise<SendContent>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface ElementAttributesProperty {
|
|
18
|
+
data: {};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface ElementChildrenAttribute {
|
|
22
|
+
children: {};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface IntrinsicElements {
|
|
26
|
+
// 简化的组件元素
|
|
27
|
+
fetch: JSXFetchElement;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// 简化的组件属性接口
|
|
33
|
+
interface JSXBaseElement {
|
|
34
|
+
children?: JSXChildren;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface JSXFetchElement extends JSXBaseElement {
|
|
38
|
+
url?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// JSX 子元素类型
|
|
42
|
+
type JSXChildren = MessageElement | string | number | boolean | null | undefined | JSXChildren[];
|
|
43
|
+
|
|
44
|
+
// JSX 元素类型
|
|
45
|
+
type JSXElement = {
|
|
46
|
+
type: string | Component<any>;
|
|
47
|
+
data: Record<string, any>;
|
|
48
|
+
children?: JSXChildren;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// 类型辅助
|
|
52
|
+
type MaybePromise<T> = T | Promise<T>;
|