@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/CHANGELOG.md
ADDED
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
|
|
3
|
+
Zhin 机器人框架核心包,基于 HMR(热模块替换)系统构建的现代化机器人开发框架。
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## 核心特性
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- 🔥 **热模块替换**: 基于 @zhin.js/hmr 的热更新系统
|
|
8
|
+
- 🔌 **插件化架构**: 完整的插件生命周期管理
|
|
9
|
+
- 🤖 **多平台适配**: 支持多种聊天平台适配器
|
|
10
|
+
- 🎯 **命令系统**: 基于 segment-matcher 的智能消息匹配
|
|
11
|
+
- 🧩 **组件系统**: 支持模板渲染、属性绑定、插槽的组件化开发
|
|
12
|
+
- ⚡ **中间件链**: 灵活的消息处理中间件机制
|
|
13
|
+
- 🔧 **TypeScript**: 完整的类型支持
|
|
8
14
|
|
|
9
|
-
|
|
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: ['
|
|
27
|
+
plugins: ['my-plugin'],
|
|
22
28
|
bots: [{
|
|
23
|
-
context: '
|
|
24
|
-
name: 'bot
|
|
25
|
-
|
|
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
|
-
|
|
40
|
+
**主要功能**:
|
|
41
|
+
- 插件生命周期管理
|
|
42
|
+
- 配置文件加载和管理
|
|
43
|
+
- 适配器和机器人实例管理
|
|
44
|
+
- 消息路由和分发
|
|
45
|
+
- 热更新监听
|
|
33
46
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
- 定时任务
|
|
38
|
-
- 上下文管理
|
|
39
|
-
- 消息处理
|
|
47
|
+
### Plugin 类
|
|
48
|
+
|
|
49
|
+
插件基类,继承自 `Dependency<Plugin>`:
|
|
40
50
|
|
|
41
51
|
```typescript
|
|
42
|
-
import { usePlugin,
|
|
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(
|
|
58
|
+
console.log(`[${plugin.name}] 收到消息:`, message.raw)
|
|
48
59
|
await next()
|
|
49
60
|
})
|
|
50
61
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
62
|
+
// 添加命令
|
|
63
|
+
addCommand(new MessageCommand('hello')
|
|
64
|
+
.action(async (message) => {
|
|
65
|
+
return '你好!'
|
|
66
|
+
})
|
|
67
|
+
)
|
|
54
68
|
```
|
|
55
69
|
|
|
56
|
-
|
|
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
|
|
63
|
-
constructor() {
|
|
64
|
-
|
|
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
|
-
###
|
|
104
|
+
### MessageCommand 类
|
|
105
|
+
|
|
106
|
+
基于 `SegmentMatcher` 的命令处理器:
|
|
70
107
|
|
|
71
|
-
机器人接口定义:
|
|
72
108
|
```typescript
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
161
|
+
// 获取插件日志器
|
|
162
|
+
const logger = useLogger()
|
|
163
|
+
```
|
|
91
164
|
|
|
92
|
-
|
|
93
|
-
|
|
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(
|
|
180
|
+
onPrivateMessage(async (message) => {
|
|
181
|
+
await message.reply('私聊回复')
|
|
182
|
+
})
|
|
97
183
|
|
|
98
|
-
//
|
|
99
|
-
|
|
184
|
+
// 自定义事件监听
|
|
185
|
+
onEvent('custom.event', (data) => {
|
|
186
|
+
console.log('自定义事件:', data)
|
|
187
|
+
})
|
|
188
|
+
```
|
|
100
189
|
|
|
101
|
-
|
|
102
|
-
|
|
190
|
+
### 生命周期
|
|
191
|
+
```typescript
|
|
192
|
+
// 插件挂载时
|
|
193
|
+
onMounted(async (plugin) => {
|
|
194
|
+
console.log('插件已挂载:', plugin.name)
|
|
195
|
+
})
|
|
103
196
|
|
|
104
|
-
//
|
|
105
|
-
|
|
197
|
+
// 插件销毁时
|
|
198
|
+
onDispose(() => {
|
|
199
|
+
console.log('插件正在销毁')
|
|
200
|
+
})
|
|
106
201
|
```
|
|
107
202
|
|
|
108
|
-
###
|
|
203
|
+
### 上下文管理
|
|
109
204
|
```typescript
|
|
110
205
|
// 注册上下文
|
|
111
206
|
register({
|
|
112
|
-
name: '
|
|
207
|
+
name: 'database',
|
|
113
208
|
async mounted(plugin) {
|
|
114
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
281
|
+
disable_dependencies?: string[]
|
|
147
282
|
debug?: boolean
|
|
148
283
|
}
|
|
149
284
|
|
|
150
285
|
interface BotConfig {
|
|
151
|
-
context: string
|
|
152
286
|
name: string
|
|
153
|
-
|
|
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
|
package/lib/adapter.d.ts
ADDED
|
@@ -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 @@
|
|
|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S,KAAK,CAAC,CAAA;IAC9I,KAAY,UAAU,CAAC,CAAC,SAAS,GAAG,IAAI,iBAAiB,CAAC,CAAC,CAAC,GAAC,UAAU,CAAC,CAAC,CAAC,CAAA;CAC7E"}
|
package/{dist → lib}/adapter.js
RENAMED
|
@@ -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
|
|
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
|
|
61
|
+
await bot.$disconnect();
|
|
44
62
|
plugin.logger.info(`bot ${name} of adapter ${this.name} disconnected`);
|
|
45
63
|
this.bots.delete(name);
|
|
46
64
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.js","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,OAAO,OAAO;IASG;IARnB,8BAA8B;IACvB,IAAI,GAAe,IAAI,GAAG,EAAa,CAAA;IAC9C,WAAW,CAAsB;IACjC;;;;OAIG;IACH,YAAmB,IAAW,EAAC,UAAgC;QAA5C,SAAI,GAAJ,IAAI,CAAO;QAC1B,IAAI,CAAC,WAAW,GAAC,UAAU,CAAA;IAC/B,CAAC;IACD;;;OAGG;IACH,KAAK,CAAC,KAAK,CAAC,MAAa;QACrB,MAAM,OAAO,GAAC,MAAM,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAA,EAAE,CAAA,CAAC,CAAC,OAAO,KAAG,IAAI,CAAC,IAAI,CAAC,CAAA;QAC3E,IAAG,CAAC,OAAO,EAAE,MAAM;YAAE,OAAM;QAC3B,IAAI,CAAC;YACD,KAAI,MAAM,MAAM,IAAI,OAAO,EAAC,CAAC;gBACzB,IAAI,GAAM,CAAA;gBACV,IAAI,OAAO,CAAC,gBAAgB,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC;oBAC7C,GAAG,GAAG,IAAI,IAAI,CAAC,WAAW,CAAC,MAAM,EAAC,MAAM,CAAM,CAAA;gBAClD,CAAC;qBAAM,CAAC;oBACJ,GAAG,GAAG,IAAI,CAAC,WAAW,CAAC,MAAM,EAAC,MAAM,CAAM,CAAA;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