@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/jsx.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { MaybePromise } from '@zhin.js/types';
|
|
2
|
+
import { SendContent,MessageElement } from './types.js';
|
|
3
|
+
import { MessageComponent } from './message.js';
|
|
4
|
+
import { Component, ComponentContext } from './component.js';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
// JSX 子元素类型
|
|
8
|
+
export type JSXChildren = MessageElement | string | number | boolean | null | undefined | JSXChildren[];
|
|
9
|
+
|
|
10
|
+
// JSX 元素类型
|
|
11
|
+
export type JSXElementType = string | Component<any> ;
|
|
12
|
+
|
|
13
|
+
// JSX 属性类型
|
|
14
|
+
export type JSXProps = Record<string, any> & {
|
|
15
|
+
children?: JSXChildren;
|
|
16
|
+
};
|
|
17
|
+
export {Fragment} from './component.js'
|
|
18
|
+
// 全局 JSX 命名空间
|
|
19
|
+
declare global {
|
|
20
|
+
namespace JSX {
|
|
21
|
+
interface Element extends MessageComponent<any> {}
|
|
22
|
+
interface ElementClass {
|
|
23
|
+
render(props: any, context?: ComponentContext): MaybePromise<SendContent>;
|
|
24
|
+
}
|
|
25
|
+
interface ElementAttributesProperty {
|
|
26
|
+
data: {};
|
|
27
|
+
}
|
|
28
|
+
interface ElementChildrenAttribute {
|
|
29
|
+
children: {};
|
|
30
|
+
}
|
|
31
|
+
interface IntrinsicElements {
|
|
32
|
+
[elemName: string]: any;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// JSX 运行时函数
|
|
38
|
+
export function jsx(type: JSXElementType, data: JSXProps): MessageElement {
|
|
39
|
+
return {
|
|
40
|
+
type,
|
|
41
|
+
data,
|
|
42
|
+
} as MessageElement;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// JSX Fragment 支持
|
|
46
|
+
export function jsxs(type: JSXElementType, props: JSXProps): MessageElement {
|
|
47
|
+
return jsx(type, props);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
// JSX 渲染函数
|
|
52
|
+
export async function renderJSX(element: MessageComponent<any>, context?: ComponentContext): Promise<SendContent> {
|
|
53
|
+
if (typeof element.type === 'string') {
|
|
54
|
+
if (element.type === 'Fragment') {
|
|
55
|
+
return await renderChildren(element.data.children, context);
|
|
56
|
+
}
|
|
57
|
+
// 其他内置组件处理
|
|
58
|
+
return await renderChildren(element.data.children, context);
|
|
59
|
+
} else if (typeof element.type === 'function') {
|
|
60
|
+
// 函数组件
|
|
61
|
+
const component = element.type as Component<any>;
|
|
62
|
+
return await component(element.data, context || {} as ComponentContext);
|
|
63
|
+
} else {
|
|
64
|
+
// 类组件或其他类型
|
|
65
|
+
const component = element.type as Component<any>;
|
|
66
|
+
return await component(element.data, context || {} as ComponentContext);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 渲染子元素
|
|
71
|
+
async function renderChildren(children: JSXChildren, context?: ComponentContext): Promise<SendContent> {
|
|
72
|
+
if (children == null) return '';
|
|
73
|
+
if (typeof children === 'string' || typeof children === 'number' || typeof children === 'boolean') {
|
|
74
|
+
return String(children);
|
|
75
|
+
}
|
|
76
|
+
if (Array.isArray(children)) {
|
|
77
|
+
const results = await Promise.all(children.map(async child => {
|
|
78
|
+
if (typeof child === 'string' || typeof child === 'number' || typeof child === 'boolean') {
|
|
79
|
+
return String(child);
|
|
80
|
+
}
|
|
81
|
+
if (child && typeof child === 'object' && 'type' in child) {
|
|
82
|
+
return await renderJSX(child as MessageComponent<any>, context);
|
|
83
|
+
}
|
|
84
|
+
return '';
|
|
85
|
+
}));
|
|
86
|
+
return results.join('');
|
|
87
|
+
}
|
|
88
|
+
if (children && typeof children === 'object' && 'type' in children) {
|
|
89
|
+
return await renderJSX(children as MessageComponent<any>, context);
|
|
90
|
+
}
|
|
91
|
+
return '';
|
|
92
|
+
}
|
package/src/message.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {MessageElement, MessageSender, SendContent} from "./types";
|
|
2
|
+
import { Component } from "./component.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 消息组件类型:用于自定义消息结构
|
|
6
|
+
*/
|
|
7
|
+
export type MessageComponent<T extends object>={
|
|
8
|
+
type:Component<T&{children?:SendContent}>
|
|
9
|
+
data:T
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 消息频道信息
|
|
13
|
+
*/
|
|
14
|
+
export interface MessageChannel{
|
|
15
|
+
id: string;
|
|
16
|
+
type: MessageType;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* 消息类型枚举
|
|
20
|
+
*/
|
|
21
|
+
export type MessageType = 'group' | 'private' | 'channel'
|
|
22
|
+
/**
|
|
23
|
+
* 消息基础结构
|
|
24
|
+
*/
|
|
25
|
+
export interface MessageBase {
|
|
26
|
+
$id: string;
|
|
27
|
+
$adapter:string
|
|
28
|
+
$bot:string
|
|
29
|
+
$content: MessageElement[];
|
|
30
|
+
$sender: MessageSender;
|
|
31
|
+
$reply(content:SendContent,quote?:boolean|string):Promise<void>
|
|
32
|
+
$channel: MessageChannel;
|
|
33
|
+
$timestamp: number;
|
|
34
|
+
$raw: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 完整消息类型,支持扩展
|
|
38
|
+
*/
|
|
39
|
+
export type Message<T extends object={}>=MessageBase&T;
|
|
40
|
+
export namespace Message{
|
|
41
|
+
/**
|
|
42
|
+
* 工具方法:合并自定义字段与基础消息结构
|
|
43
|
+
*/
|
|
44
|
+
export function from<T extends object>(input:T,format:MessageBase):Message<T>{
|
|
45
|
+
return Object.assign(input,format)
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/plugin.ts
CHANGED
|
@@ -1,52 +1,123 @@
|
|
|
1
1
|
// ============================================================================
|
|
2
|
-
//
|
|
2
|
+
// 插件类型定义(定义插件中间件、生命周期、命令、组件等核心类型)
|
|
3
3
|
// ============================================================================
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
import {MaybePromise} from '@zhin.js/types'
|
|
7
|
-
import {
|
|
7
|
+
import {AdapterMessage, BeforeSendHandler, RegisteredAdapter, SendOptions} from "./types.js";
|
|
8
|
+
import {Message} from './message.js'
|
|
8
9
|
import {Dependency, Logger,} from "@zhin.js/hmr";
|
|
9
10
|
import {App} from "./app";
|
|
11
|
+
import {MessageCommand} from "./command.js";
|
|
12
|
+
import {Component, renderComponents} from "./component.js";
|
|
13
|
+
import { PluginError, MessageError, errorManager } from './errors.js';
|
|
14
|
+
import {remove} from "./utils.js";
|
|
15
|
+
import {Prompt} from "./prompt.js";
|
|
16
|
+
import { Schema } from '@zhin.js/database';
|
|
17
|
+
import { Cron} from './cron.js';
|
|
10
18
|
|
|
11
19
|
/** 消息中间件函数 */
|
|
12
|
-
export type MessageMiddleware = (message: Message
|
|
13
|
-
|
|
14
|
-
/** 事件监听器函数 */
|
|
15
|
-
export type EventListener<T = any> = (data: T) => void | Promise<void>;
|
|
16
|
-
|
|
17
|
-
/** 定时任务配置 */
|
|
18
|
-
export interface CronJob {
|
|
19
|
-
name: string;
|
|
20
|
-
schedule: string; // cron 表达式
|
|
21
|
-
handler: () => void | Promise<void>;
|
|
22
|
-
enabled?: boolean;
|
|
23
|
-
}
|
|
20
|
+
export type MessageMiddleware<P extends RegisteredAdapter=RegisteredAdapter> = (message: Message<AdapterMessage<P>>, next: () => Promise<void>) => MaybePromise<void>;
|
|
24
21
|
|
|
25
22
|
|
|
26
23
|
// ============================================================================
|
|
27
|
-
// Plugin
|
|
24
|
+
// Plugin 类(插件的生命周期、命令/中间件/组件/定时任务等管理)
|
|
28
25
|
// ============================================================================
|
|
29
26
|
|
|
30
27
|
/**
|
|
31
|
-
* 插件类:继承自Dependency
|
|
28
|
+
* 插件类:继承自 Dependency,提供机器人特定功能与生命周期管理。
|
|
29
|
+
* 支持命令注册、中间件、组件、定时任务、模型等。
|
|
32
30
|
*/
|
|
33
31
|
export class Plugin extends Dependency<Plugin> {
|
|
34
|
-
middlewares: MessageMiddleware[] = [];
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
32
|
+
middlewares: MessageMiddleware<any>[] = [];
|
|
33
|
+
components: Map<string, Component<any>> = new Map();
|
|
34
|
+
schemas: Map<string,Schema<any>>=new Map();
|
|
35
|
+
commands:MessageCommand[]=[];
|
|
36
|
+
crons:Cron[]=[];
|
|
38
37
|
#logger?:Logger
|
|
38
|
+
/**
|
|
39
|
+
* 构造函数:初始化插件,注册消息事件、命令中间件、资源清理等
|
|
40
|
+
* @param parent 所属 App 实例
|
|
41
|
+
* @param name 插件名
|
|
42
|
+
* @param filePath 插件文件路径
|
|
43
|
+
*/
|
|
39
44
|
constructor(parent: Dependency<Plugin>, name: string, filePath: string) {
|
|
40
45
|
super(parent, name, filePath);
|
|
46
|
+
// 绑定消息事件,自动分发到命令和中间件
|
|
41
47
|
this.on('message.receive',this.#handleMessage.bind(this))
|
|
48
|
+
// 注册命令处理为默认中间件
|
|
49
|
+
this.addMiddleware(async (message,next)=>{
|
|
50
|
+
for(const command of this.commands){
|
|
51
|
+
const result=await command.handle(message);
|
|
52
|
+
if(result) message.$reply(result);
|
|
53
|
+
}
|
|
54
|
+
return next()
|
|
55
|
+
});
|
|
56
|
+
// 发送前渲染组件
|
|
57
|
+
this.beforeSend((options)=>renderComponents(this.components,options))
|
|
58
|
+
// 资源清理:卸载时清空模型、定时任务等
|
|
59
|
+
this.on('dispose',()=>{
|
|
60
|
+
for(const name of this.schemas.keys()){
|
|
61
|
+
this.app.database?.models.delete(name);
|
|
62
|
+
}
|
|
63
|
+
this.schemas.clear();
|
|
64
|
+
for(const cron of this.crons){
|
|
65
|
+
cron.dispose();
|
|
66
|
+
}
|
|
67
|
+
this.crons.length = 0;
|
|
68
|
+
});
|
|
69
|
+
// 挂载时启动定时任务
|
|
70
|
+
this.on('mounted',()=>{
|
|
71
|
+
for(const cron of this.crons){
|
|
72
|
+
cron.run();
|
|
73
|
+
}
|
|
74
|
+
});
|
|
42
75
|
}
|
|
43
|
-
#handleMessage(message:Message){
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
76
|
+
async #handleMessage(message: Message) {
|
|
77
|
+
try {
|
|
78
|
+
await this.#runMiddlewares(message, 0)
|
|
79
|
+
} catch (error) {
|
|
80
|
+
const messageError = new MessageError(
|
|
81
|
+
`消息处理失败: ${(error as Error).message}`,
|
|
82
|
+
message.$id,
|
|
83
|
+
message.$channel.id,
|
|
84
|
+
{ pluginName: this.name, originalError: error }
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
await errorManager.handle(messageError)
|
|
88
|
+
|
|
89
|
+
// 可选:发送错误回复给用户
|
|
90
|
+
try {
|
|
91
|
+
await message.$reply('抱歉,处理您的消息时出现了错误。')
|
|
92
|
+
} catch (replyError) {
|
|
93
|
+
// 静默处理回复错误,避免错误循环
|
|
94
|
+
// console.error 已替换为注释
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
cron(cronExpression:string,callback:()=>void){
|
|
99
|
+
const cronJob = new Cron(cronExpression,callback);
|
|
100
|
+
this.crons.push(cronJob);
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
async #runMiddlewares(message: Message, index: number): Promise<void> {
|
|
104
|
+
if (index >= this.middlewares.length) return
|
|
105
|
+
|
|
106
|
+
const middleware = this.middlewares[index]
|
|
107
|
+
|
|
108
|
+
try {
|
|
109
|
+
await middleware(message, () => this.#runMiddlewares(message, index + 1))
|
|
110
|
+
} catch (error) {
|
|
111
|
+
throw new PluginError(
|
|
112
|
+
`中间件执行失败: ${(error as Error).message}`,
|
|
113
|
+
this.name,
|
|
114
|
+
{ middlewareIndex: index, originalError: error }
|
|
115
|
+
)
|
|
48
116
|
}
|
|
49
|
-
|
|
117
|
+
}
|
|
118
|
+
defineModel<S extends Record<string,any>>(name:string,schema:Schema<S>){
|
|
119
|
+
this.schemas.set(name,schema);
|
|
120
|
+
return this;
|
|
50
121
|
}
|
|
51
122
|
beforeSend(handler:BeforeSendHandler){
|
|
52
123
|
this.before('message.send',handler)
|
|
@@ -60,63 +131,74 @@ export class Plugin extends Dependency<Plugin> {
|
|
|
60
131
|
}
|
|
61
132
|
get logger(): Logger {
|
|
62
133
|
if(this.#logger) return this.#logger
|
|
63
|
-
const names = [
|
|
134
|
+
const names = [];
|
|
64
135
|
let temp=this as Dependency<Plugin>
|
|
65
136
|
while(temp.parent){
|
|
66
|
-
names.unshift(temp.
|
|
137
|
+
names.unshift(temp.name)
|
|
67
138
|
temp=temp.parent
|
|
68
139
|
}
|
|
69
|
-
return
|
|
140
|
+
return temp.getLogger(names.join('/'))
|
|
141
|
+
}
|
|
142
|
+
/** 添加组件 */
|
|
143
|
+
addComponent<T=any>(component:Component<T>){
|
|
144
|
+
this.components.set(component.name,component);
|
|
70
145
|
}
|
|
71
|
-
|
|
72
146
|
/** 添加中间件 */
|
|
73
|
-
|
|
147
|
+
addCommand(command:MessageCommand){
|
|
148
|
+
this.commands.push(command);
|
|
149
|
+
this.dispatch('command.add',command);
|
|
150
|
+
}
|
|
151
|
+
/** 添加中间件 */
|
|
152
|
+
addMiddleware<T extends RegisteredAdapter>(middleware: MessageMiddleware<T>) {
|
|
74
153
|
this.middlewares.push(middleware);
|
|
75
154
|
this.dispatch('middleware.add',middleware)
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
/** 添加事件监听器 */
|
|
79
|
-
addEventListener<T = any>(event: string, listener: EventListener<T>): void {
|
|
80
|
-
if (!this.eventListeners.has(event)) {
|
|
81
|
-
this.eventListeners.set(event, []);
|
|
155
|
+
return ()=>{
|
|
156
|
+
remove(this.middlewares,middleware)
|
|
82
157
|
}
|
|
83
|
-
this.eventListeners.get(event)!.push(listener);
|
|
84
|
-
this.dispatch('listener.add',event,listener)
|
|
85
158
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
addCronJob(job: CronJob): void {
|
|
89
|
-
this.cronJobs.set(job.name, job);
|
|
90
|
-
this.dispatch('cron-job.add',job)
|
|
159
|
+
prompt<P extends RegisteredAdapter>(message:Message<AdapterMessage<P>>){
|
|
160
|
+
return new Prompt<P>(this,message)
|
|
91
161
|
}
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
92
165
|
/** 发送消息 */
|
|
93
|
-
async sendMessage(options:SendOptions): Promise<void> {
|
|
94
|
-
|
|
166
|
+
async sendMessage(options: SendOptions): Promise<void> {
|
|
167
|
+
try {
|
|
168
|
+
await this.app.sendMessage(options);
|
|
169
|
+
} catch (error) {
|
|
170
|
+
const messageError = new MessageError(
|
|
171
|
+
`发送消息失败: ${(error as Error).message}`,
|
|
172
|
+
undefined,
|
|
173
|
+
(options as any).channel_id,
|
|
174
|
+
{ pluginName: this.name, sendOptions: options, originalError: error }
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
await errorManager.handle(messageError)
|
|
178
|
+
throw messageError
|
|
179
|
+
}
|
|
95
180
|
}
|
|
96
181
|
|
|
97
182
|
/** 销毁插件 */
|
|
98
183
|
dispose(): void {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
this.middlewares = []
|
|
104
|
-
|
|
105
|
-
// 移除所有事件监听器
|
|
106
|
-
for (const [event, listeners] of this.eventListeners) {
|
|
107
|
-
for (const listener of listeners) {
|
|
108
|
-
this.dispatch('listener.remove', event, listener)
|
|
184
|
+
try {
|
|
185
|
+
// 移除所有中间件
|
|
186
|
+
for (const middleware of this.middlewares) {
|
|
187
|
+
this.dispatch('middleware.remove', middleware)
|
|
109
188
|
}
|
|
189
|
+
this.middlewares = []
|
|
190
|
+
|
|
191
|
+
// 调用父类的dispose方法
|
|
192
|
+
super.dispose()
|
|
193
|
+
} catch (error) {
|
|
194
|
+
const pluginError = new PluginError(
|
|
195
|
+
`插件销毁失败: ${(error as Error).message}`,
|
|
196
|
+
this.name,
|
|
197
|
+
{ originalError: error }
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
errorManager.handle(pluginError).catch(console.error)
|
|
201
|
+
throw pluginError
|
|
110
202
|
}
|
|
111
|
-
this.eventListeners.clear()
|
|
112
|
-
|
|
113
|
-
// 移除所有定时任务
|
|
114
|
-
for (const [name, job] of this.cronJobs) {
|
|
115
|
-
this.dispatch('cron-job.remove', job)
|
|
116
|
-
}
|
|
117
|
-
this.cronJobs.clear()
|
|
118
|
-
|
|
119
|
-
// 调用父类的dispose方法
|
|
120
|
-
super.dispose()
|
|
121
203
|
}
|
|
122
204
|
}
|
package/src/prompt.ts
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import {AdapterMessage, Dict, RegisteredAdapter} from './types.js';
|
|
2
|
+
import { MessageMiddleware,Plugin } from './plugin.js';
|
|
3
|
+
import { Message } from './message.js';
|
|
4
|
+
import { Schema } from './schema.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Prompt类:用于实现机器人与用户的交互式提问与输入收集。
|
|
8
|
+
* 支持文本、数字、确认、列表、选项、Schema等多种输入类型,自动处理超时、默认值、格式化等。
|
|
9
|
+
* 典型用法:await new Prompt(plugin, event).text('请输入内容')
|
|
10
|
+
* @template P 适配器类型
|
|
11
|
+
*/
|
|
12
|
+
export class Prompt<P extends RegisteredAdapter> {
|
|
13
|
+
/**
|
|
14
|
+
* 构造函数
|
|
15
|
+
* @param plugin 所属插件实例
|
|
16
|
+
* @param event 当前消息事件
|
|
17
|
+
*/
|
|
18
|
+
constructor(private plugin:Plugin,private event: Message<AdapterMessage<P>>) {}
|
|
19
|
+
/**
|
|
20
|
+
* 获取当前会话唯一标识(适配器-机器人-频道-用户)
|
|
21
|
+
*/
|
|
22
|
+
private getChannelAddress<P2 extends RegisteredAdapter>(event: Message<AdapterMessage<P2>>) {
|
|
23
|
+
return `${event.$adapter}-${event.$bot}-${event.$channel.type}:${event.$channel.id}-${event.$sender.id}`;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 通用提问方法,支持自定义格式化、超时、默认值等
|
|
27
|
+
* @param config 提问配置
|
|
28
|
+
*/
|
|
29
|
+
private prompt<T = any>(config: Prompt.Config<T>) {
|
|
30
|
+
return new Promise<T>((resolve, reject) => {
|
|
31
|
+
this.event.$reply(config.tips);
|
|
32
|
+
this.middleware(
|
|
33
|
+
input => {
|
|
34
|
+
if (input instanceof Error) {
|
|
35
|
+
this.event.$reply(input.message);
|
|
36
|
+
if (config.defaultValue) resolve(config.defaultValue);
|
|
37
|
+
else reject(input);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
resolve(config.format(input));
|
|
41
|
+
},
|
|
42
|
+
config.timeout,
|
|
43
|
+
config.timeoutText,
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* 注册一次性消息中间件,等待用户输入或超时
|
|
49
|
+
* @param callback 输入回调
|
|
50
|
+
* @param timeout 超时时间(默认3分钟)
|
|
51
|
+
* @param timeoutText 超时提示
|
|
52
|
+
*/
|
|
53
|
+
middleware(callback: (input: string | Error) => any, timeout: number = 3 * 60 * 1000, timeoutText = '输入超时') {
|
|
54
|
+
const middleware: MessageMiddleware<P> = (event, next) => {
|
|
55
|
+
if (this.getChannelAddress<P>(event) !== this.getChannelAddress<P>(this.event)) return next();
|
|
56
|
+
callback(event.$raw);
|
|
57
|
+
dispose();
|
|
58
|
+
clearTimeout(timer);
|
|
59
|
+
};
|
|
60
|
+
const dispose = this.plugin.addMiddleware(middleware);
|
|
61
|
+
const timer = setTimeout(() => {
|
|
62
|
+
dispose();
|
|
63
|
+
callback(new Error(timeoutText));
|
|
64
|
+
}, timeout);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* 文本输入
|
|
68
|
+
*/
|
|
69
|
+
async text(tips: string, timeout?: number, defaultValue = '', timeoutText?: string): Promise<string> {
|
|
70
|
+
return this.prompt<string>({
|
|
71
|
+
tips,
|
|
72
|
+
defaultValue,
|
|
73
|
+
timeoutText,
|
|
74
|
+
timeout,
|
|
75
|
+
format: (input: string) => input,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 任意输入
|
|
80
|
+
*/
|
|
81
|
+
async any(tips: string, timeout?: number, defaultValue = '', timeoutText?: string) {
|
|
82
|
+
return this.prompt<string>({
|
|
83
|
+
tips,
|
|
84
|
+
defaultValue,
|
|
85
|
+
timeoutText,
|
|
86
|
+
timeout,
|
|
87
|
+
format: (input: string) => input,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 数字输入
|
|
92
|
+
*/
|
|
93
|
+
async number(tips: string, timeout?: number, defaultValue = 0, timeoutText?: string): Promise<number> {
|
|
94
|
+
return this.prompt<number>({
|
|
95
|
+
tips,
|
|
96
|
+
defaultValue,
|
|
97
|
+
timeoutText,
|
|
98
|
+
timeout,
|
|
99
|
+
format: (input: string) => +input,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* 确认输入(如 yes/no)
|
|
104
|
+
*/
|
|
105
|
+
async confirm(
|
|
106
|
+
tips: string,
|
|
107
|
+
condition: string = 'yes',
|
|
108
|
+
timeout?: number,
|
|
109
|
+
defaultValue = false,
|
|
110
|
+
timeoutText?: string,
|
|
111
|
+
): Promise<boolean> {
|
|
112
|
+
return this.prompt<boolean>({
|
|
113
|
+
tips: `${tips}\n输入“${condition}”以确认`,
|
|
114
|
+
defaultValue,
|
|
115
|
+
timeout,
|
|
116
|
+
timeoutText,
|
|
117
|
+
format: (input: string) => input === condition,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* 列表输入,支持多值分隔
|
|
122
|
+
*/
|
|
123
|
+
async list<T extends Prompt.SingleType = 'text'>(
|
|
124
|
+
tips: string,
|
|
125
|
+
config: Prompt.ListConfig<T> = { type: 'text' as T },
|
|
126
|
+
timeoutText?: string,
|
|
127
|
+
): Promise<Prompt.Result<T>[]> {
|
|
128
|
+
const separator = config.separator || ',';
|
|
129
|
+
return this.prompt<Prompt.Result<T>[]>({
|
|
130
|
+
tips: `${tips}\n值之间使用“${separator}”分隔`,
|
|
131
|
+
defaultValue: config.defaultValue || [],
|
|
132
|
+
timeout: config.timeout,
|
|
133
|
+
timeoutText,
|
|
134
|
+
format: (input: string) =>
|
|
135
|
+
input.split(separator).map(v => {
|
|
136
|
+
switch (config.type) {
|
|
137
|
+
case 'boolean':
|
|
138
|
+
return Boolean(v);
|
|
139
|
+
case 'number':
|
|
140
|
+
return +v;
|
|
141
|
+
case 'text':
|
|
142
|
+
return v;
|
|
143
|
+
}
|
|
144
|
+
}) as Prompt.Result<T>[],
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* 返回常量值(用于Schema)
|
|
149
|
+
*/
|
|
150
|
+
async const<T = any>(value: T): Promise<T> {
|
|
151
|
+
return value;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* 选项选择,支持单选/多选
|
|
155
|
+
*/
|
|
156
|
+
async pick<T extends Prompt.SingleType, M extends boolean = false>(
|
|
157
|
+
tips: string,
|
|
158
|
+
config: Prompt.PickConfig<T, M>,
|
|
159
|
+
timeoutText?: string,
|
|
160
|
+
): Promise<Prompt.PickResult<T, M>> {
|
|
161
|
+
const moreTextArr = config.options.map((o, idx) => {
|
|
162
|
+
return `${idx + 1}.${o.label}`;
|
|
163
|
+
});
|
|
164
|
+
const separator = config.separator || ',';
|
|
165
|
+
if (config.multiple) moreTextArr.push(`多选请用“${separator}”分隔`);
|
|
166
|
+
return this.prompt<Prompt.PickResult<T, M>>({
|
|
167
|
+
tips: `${tips}\n${moreTextArr.join('\n')}`,
|
|
168
|
+
defaultValue: config.defaultValue,
|
|
169
|
+
timeout: config.timeout,
|
|
170
|
+
timeoutText,
|
|
171
|
+
format: (input: string) => {
|
|
172
|
+
if (!config.multiple)
|
|
173
|
+
return config.options.find((o, idx) => {
|
|
174
|
+
return idx + 1 === +input;
|
|
175
|
+
})?.value as Prompt.PickResult<T, M>;
|
|
176
|
+
const pickIdx = input.split(separator).map(Number);
|
|
177
|
+
return config.options
|
|
178
|
+
.filter((o, idx) => {
|
|
179
|
+
return pickIdx.includes(idx + 1);
|
|
180
|
+
})
|
|
181
|
+
.map(o => o.value) as Prompt.PickResult<T, M>;
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* 基于Schema的选项选择
|
|
187
|
+
*/
|
|
188
|
+
async pickValueWithSchema<T extends Schema>(schema: T): Promise<Schema.Types<T>> {
|
|
189
|
+
return this.pick(schema.meta.description, {
|
|
190
|
+
type: '' as any,
|
|
191
|
+
options: schema.meta.options!.map(o => ({
|
|
192
|
+
label: o.label,
|
|
193
|
+
value: o.value,
|
|
194
|
+
})),
|
|
195
|
+
multiple: schema.meta.multiple,
|
|
196
|
+
defaultValue: schema.meta.default,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* 批量Schema输入
|
|
201
|
+
*/
|
|
202
|
+
async getValueWithSchemas<T extends Record<string, Schema>>(schemas: T): Promise<Schema.RecordTypes<T>> {
|
|
203
|
+
const result: Dict = {};
|
|
204
|
+
for (const key of Object.keys(schemas)) {
|
|
205
|
+
const schema = schemas[key];
|
|
206
|
+
result[key] = await this.getValueWithSchema(schema);
|
|
207
|
+
}
|
|
208
|
+
return result as Schema.RecordTypes<T>;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* 单个Schema输入,自动分发到不同类型
|
|
212
|
+
*/
|
|
213
|
+
async getValueWithSchema<T extends Schema>(schema: T): Promise<Schema.Types<T>> {
|
|
214
|
+
if (schema.meta.options) return this.pickValueWithSchema(schema);
|
|
215
|
+
switch (schema.meta.type) {
|
|
216
|
+
case 'number':
|
|
217
|
+
return (await this.number(schema.meta.description)) as Schema.Types<T>;
|
|
218
|
+
case 'string':
|
|
219
|
+
return (await this.text(schema.meta.description)) as Schema.Types<T>;
|
|
220
|
+
case 'boolean':
|
|
221
|
+
return (await this.confirm(schema.meta.description)) as Schema.Types<T>;
|
|
222
|
+
case 'object':
|
|
223
|
+
if (schema.meta.description) await this.event.$reply(schema.meta.description);
|
|
224
|
+
return (await this.getValueWithSchemas(schema.options.object!)) as Schema.Types<T>;
|
|
225
|
+
case 'date':
|
|
226
|
+
return await this.prompt({
|
|
227
|
+
tips: schema.meta.description,
|
|
228
|
+
defaultValue: schema.meta.default || new Date(),
|
|
229
|
+
format: (input: string) => new Date(input) as Schema.Types<T>,
|
|
230
|
+
});
|
|
231
|
+
case 'regexp':
|
|
232
|
+
return await this.prompt({
|
|
233
|
+
tips: schema.meta.description,
|
|
234
|
+
defaultValue: schema.meta.default || '',
|
|
235
|
+
format: (input: string) => new RegExp(input) as Schema.Types<T>,
|
|
236
|
+
});
|
|
237
|
+
case 'const':
|
|
238
|
+
return await this.const(schema.meta.default!);
|
|
239
|
+
case 'list':
|
|
240
|
+
const inner = schema.options.inner!;
|
|
241
|
+
if (!['string', 'boolean', 'number'].includes(inner.meta.type))
|
|
242
|
+
throw new Error(`unsupported inner type :${inner.meta.type}`);
|
|
243
|
+
return (await this.list(schema.meta.description, {
|
|
244
|
+
type: inner.meta.type === 'string' ? 'text' : (inner.meta.type as Prompt.SingleType),
|
|
245
|
+
defaultValue: schema.meta.default,
|
|
246
|
+
})) as Schema.Types<T>;
|
|
247
|
+
case 'dict':
|
|
248
|
+
default:
|
|
249
|
+
throw new Error(`Unsupported schema input type: ${schema.meta.type}`);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Prompt命名空间:类型辅助定义
|
|
255
|
+
*/
|
|
256
|
+
export namespace Prompt {
|
|
257
|
+
interface SingleMap {
|
|
258
|
+
text: string;
|
|
259
|
+
number: number;
|
|
260
|
+
boolean: boolean;
|
|
261
|
+
}
|
|
262
|
+
export interface ListConfig<T extends SingleType> {
|
|
263
|
+
type: T;
|
|
264
|
+
defaultValue?: SingleMap[T][];
|
|
265
|
+
separator?: string;
|
|
266
|
+
timeout?: number;
|
|
267
|
+
}
|
|
268
|
+
export interface PickConfig<T extends SingleType = SingleType, M extends boolean = false> {
|
|
269
|
+
type: T;
|
|
270
|
+
defaultValue?: PickResult<T, M>;
|
|
271
|
+
separator?: string;
|
|
272
|
+
timeout?: number;
|
|
273
|
+
options: PickOption<T>[];
|
|
274
|
+
multiple?: M;
|
|
275
|
+
}
|
|
276
|
+
export type PickOption<T extends SingleType = 'text'> = {
|
|
277
|
+
label: string;
|
|
278
|
+
value: SingleMap[T];
|
|
279
|
+
};
|
|
280
|
+
export type PickResult<T extends SingleType, M extends boolean> = M extends true ? Result<T>[] : Result<T>;
|
|
281
|
+
export type SingleType = keyof SingleMap;
|
|
282
|
+
export type Result<T extends SingleType> = SingleMap[T];
|
|
283
|
+
export type Config<R = any> = {
|
|
284
|
+
tips: string;
|
|
285
|
+
defaultValue?: R;
|
|
286
|
+
timeout?: number;
|
|
287
|
+
timeoutText?: string;
|
|
288
|
+
format: (input: string) => R;
|
|
289
|
+
};
|
|
290
|
+
}
|