koishi-plugin-chatluna 1.0.0-beta.20
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/README.MD +130 -0
- package/lib/authorization/service.d.ts +24 -0
- package/lib/authorization/service.js +342 -0
- package/lib/authorization/types.d.ts +36 -0
- package/lib/authorization/types.js +2 -0
- package/lib/cache.d.ts +18 -0
- package/lib/cache.js +27 -0
- package/lib/chains/chain.d.ts +66 -0
- package/lib/chains/chain.js +465 -0
- package/lib/chains/rooms.d.ts +24 -0
- package/lib/chains/rooms.js +486 -0
- package/lib/command.d.ts +3 -0
- package/lib/command.js +21 -0
- package/lib/commands/auth.d.ts +4 -0
- package/lib/commands/auth.js +92 -0
- package/lib/commands/chat.d.ts +4 -0
- package/lib/commands/chat.js +51 -0
- package/lib/commands/model.d.ts +4 -0
- package/lib/commands/model.js +18 -0
- package/lib/commands/preset.d.ts +4 -0
- package/lib/commands/preset.js +47 -0
- package/lib/commands/providers.d.ts +4 -0
- package/lib/commands/providers.js +44 -0
- package/lib/commands/room.d.ts +4 -0
- package/lib/commands/room.js +169 -0
- package/lib/config.d.ts +36 -0
- package/lib/config.js +151 -0
- package/lib/index.d.ts +11 -0
- package/lib/index.js +101 -0
- package/lib/llm-core/chain/base.d.ts +79 -0
- package/lib/llm-core/chain/base.js +111 -0
- package/lib/llm-core/chain/browsing_chat_chain.d.ts +33 -0
- package/lib/llm-core/chain/browsing_chat_chain.js +183 -0
- package/lib/llm-core/chain/chat_chain.d.ts +24 -0
- package/lib/llm-core/chain/chat_chain.js +109 -0
- package/lib/llm-core/chain/function_calling_browsing_chain.d.ts +31 -0
- package/lib/llm-core/chain/function_calling_browsing_chain.js +149 -0
- package/lib/llm-core/chain/out_parsers.d.ts +11 -0
- package/lib/llm-core/chain/out_parsers.js +34 -0
- package/lib/llm-core/chain/plugin_chat_chain.d.ts +29 -0
- package/lib/llm-core/chain/plugin_chat_chain.js +133 -0
- package/lib/llm-core/chain/prompt.d.ts +85 -0
- package/lib/llm-core/chain/prompt.js +437 -0
- package/lib/llm-core/chain/wrapper_chain.d.ts +21 -0
- package/lib/llm-core/chain/wrapper_chain.js +61 -0
- package/lib/llm-core/chat/app.d.ts +40 -0
- package/lib/llm-core/chat/app.js +281 -0
- package/lib/llm-core/chat/default.d.ts +3 -0
- package/lib/llm-core/chat/default.js +119 -0
- package/lib/llm-core/memory/message/database_memory.d.ts +50 -0
- package/lib/llm-core/memory/message/database_memory.js +184 -0
- package/lib/llm-core/model/base.d.ts +18 -0
- package/lib/llm-core/model/base.js +35 -0
- package/lib/llm-core/model/in_memory.d.ts +15 -0
- package/lib/llm-core/model/in_memory.js +32 -0
- package/lib/llm-core/platform/api.d.ts +59 -0
- package/lib/llm-core/platform/api.js +15 -0
- package/lib/llm-core/platform/client.d.ts +25 -0
- package/lib/llm-core/platform/client.js +46 -0
- package/lib/llm-core/platform/config.d.ts +40 -0
- package/lib/llm-core/platform/config.js +93 -0
- package/lib/llm-core/platform/model.d.ts +105 -0
- package/lib/llm-core/platform/model.js +334 -0
- package/lib/llm-core/platform/service.d.ts +54 -0
- package/lib/llm-core/platform/service.js +216 -0
- package/lib/llm-core/platform/types.d.ts +51 -0
- package/lib/llm-core/platform/types.js +9 -0
- package/lib/llm-core/prompt/index.d.ts +1 -0
- package/lib/llm-core/prompt/index.js +17 -0
- package/lib/llm-core/prompt/preset_prompt_parse.d.ts +19 -0
- package/lib/llm-core/prompt/preset_prompt_parse.js +103 -0
- package/lib/llm-core/utils/chunk.d.ts +1 -0
- package/lib/llm-core/utils/chunk.js +11 -0
- package/lib/llm-core/utils/count_tokens.d.ts +13 -0
- package/lib/llm-core/utils/count_tokens.js +138 -0
- package/lib/llm-core/utils/tiktoken.d.ts +10 -0
- package/lib/llm-core/utils/tiktoken.js +40 -0
- package/lib/middleware.d.ts +3 -0
- package/lib/middleware.js +21 -0
- package/lib/middlewares/add_preset.d.ts +12 -0
- package/lib/middlewares/add_preset.js +46 -0
- package/lib/middlewares/add_room_to_group.d.ts +9 -0
- package/lib/middlewares/add_room_to_group.js +36 -0
- package/lib/middlewares/add_user_to_auth_group.d.ts +9 -0
- package/lib/middlewares/add_user_to_auth_group.js +20 -0
- package/lib/middlewares/allow_reply.d.ts +9 -0
- package/lib/middlewares/allow_reply.js +41 -0
- package/lib/middlewares/black_list.d.ts +9 -0
- package/lib/middlewares/black_list.js +21 -0
- package/lib/middlewares/censor.d.ts +9 -0
- package/lib/middlewares/censor.js +17 -0
- package/lib/middlewares/chat_time_limit_check.d.ts +25 -0
- package/lib/middlewares/chat_time_limit_check.js +93 -0
- package/lib/middlewares/chat_time_limit_save.d.ts +9 -0
- package/lib/middlewares/chat_time_limit_save.js +32 -0
- package/lib/middlewares/check_room.d.ts +9 -0
- package/lib/middlewares/check_room.js +37 -0
- package/lib/middlewares/clear_balance.d.ts +9 -0
- package/lib/middlewares/clear_balance.js +20 -0
- package/lib/middlewares/clear_room.d.ts +9 -0
- package/lib/middlewares/clear_room.js +36 -0
- package/lib/middlewares/clone_preset.d.ts +15 -0
- package/lib/middlewares/clone_preset.js +44 -0
- package/lib/middlewares/cooldown_time.d.ts +9 -0
- package/lib/middlewares/cooldown_time.js +23 -0
- package/lib/middlewares/create_auth_group.d.ts +20 -0
- package/lib/middlewares/create_auth_group.js +253 -0
- package/lib/middlewares/create_room.d.ts +21 -0
- package/lib/middlewares/create_room.js +257 -0
- package/lib/middlewares/delete_preset.d.ts +12 -0
- package/lib/middlewares/delete_preset.js +60 -0
- package/lib/middlewares/delete_room.d.ts +9 -0
- package/lib/middlewares/delete_room.js +45 -0
- package/lib/middlewares/invite_room.d.ts +14 -0
- package/lib/middlewares/invite_room.js +33 -0
- package/lib/middlewares/join_room.d.ts +9 -0
- package/lib/middlewares/join_room.js +65 -0
- package/lib/middlewares/kick_member.d.ts +9 -0
- package/lib/middlewares/kick_member.js +33 -0
- package/lib/middlewares/kick_user_form_auth_group.d.ts +9 -0
- package/lib/middlewares/kick_user_form_auth_group.js +20 -0
- package/lib/middlewares/leave_room.d.ts +9 -0
- package/lib/middlewares/leave_room.js +45 -0
- package/lib/middlewares/lifecycle.d.ts +29 -0
- package/lib/middlewares/lifecycle.js +31 -0
- package/lib/middlewares/list_all_embeddings.d.ts +9 -0
- package/lib/middlewares/list_all_embeddings.js +28 -0
- package/lib/middlewares/list_all_model.d.ts +13 -0
- package/lib/middlewares/list_all_model.js +28 -0
- package/lib/middlewares/list_all_preset.d.ts +9 -0
- package/lib/middlewares/list_all_preset.js +39 -0
- package/lib/middlewares/list_all_vectorstore.d.ts +9 -0
- package/lib/middlewares/list_all_vectorstore.js +27 -0
- package/lib/middlewares/list_auth_group.d.ts +14 -0
- package/lib/middlewares/list_auth_group.js +39 -0
- package/lib/middlewares/list_room.d.ts +11 -0
- package/lib/middlewares/list_room.js +43 -0
- package/lib/middlewares/mute_user.d.ts +9 -0
- package/lib/middlewares/mute_user.js +38 -0
- package/lib/middlewares/query_balance.d.ts +9 -0
- package/lib/middlewares/query_balance.js +19 -0
- package/lib/middlewares/read_chat_message.d.ts +9 -0
- package/lib/middlewares/read_chat_message.js +51 -0
- package/lib/middlewares/render_message.d.ts +14 -0
- package/lib/middlewares/render_message.js +30 -0
- package/lib/middlewares/request_model.d.ts +15 -0
- package/lib/middlewares/request_model.js +207 -0
- package/lib/middlewares/resolve_model.d.ts +9 -0
- package/lib/middlewares/resolve_model.js +21 -0
- package/lib/middlewares/resolve_room.d.ts +14 -0
- package/lib/middlewares/resolve_room.js +84 -0
- package/lib/middlewares/room_info.d.ts +9 -0
- package/lib/middlewares/room_info.js +37 -0
- package/lib/middlewares/room_permission.d.ts +9 -0
- package/lib/middlewares/room_permission.js +49 -0
- package/lib/middlewares/set_auto_update_room.d.ts +12 -0
- package/lib/middlewares/set_auto_update_room.js +42 -0
- package/lib/middlewares/set_balance.d.ts +13 -0
- package/lib/middlewares/set_balance.js +20 -0
- package/lib/middlewares/set_default_embeddings.d.ts +16 -0
- package/lib/middlewares/set_default_embeddings.js +46 -0
- package/lib/middlewares/set_default_vectorstore.d.ts +12 -0
- package/lib/middlewares/set_default_vectorstore.js +45 -0
- package/lib/middlewares/set_preset.d.ts +12 -0
- package/lib/middlewares/set_preset.js +38 -0
- package/lib/middlewares/set_room.d.ts +9 -0
- package/lib/middlewares/set_room.js +194 -0
- package/lib/middlewares/switch_room.d.ts +9 -0
- package/lib/middlewares/switch_room.js +18 -0
- package/lib/middlewares/thinking_message_recall.d.ts +9 -0
- package/lib/middlewares/thinking_message_recall.js +16 -0
- package/lib/middlewares/thinking_message_send.d.ts +18 -0
- package/lib/middlewares/thinking_message_send.js +53 -0
- package/lib/middlewares/transfer_room.d.ts +9 -0
- package/lib/middlewares/transfer_room.js +46 -0
- package/lib/middlewares/wipe.d.ts +13 -0
- package/lib/middlewares/wipe.js +91 -0
- package/lib/preset.d.ts +19 -0
- package/lib/preset.js +116 -0
- package/lib/render.d.ts +18 -0
- package/lib/render.js +58 -0
- package/lib/renders/image.d.ts +14 -0
- package/lib/renders/image.js +104 -0
- package/lib/renders/mixed-image.d.ts +16 -0
- package/lib/renders/mixed-image.js +191 -0
- package/lib/renders/mixed-voice.d.ts +14 -0
- package/lib/renders/mixed-voice.js +89 -0
- package/lib/renders/raw.d.ts +5 -0
- package/lib/renders/raw.js +12 -0
- package/lib/renders/text.d.ts +7 -0
- package/lib/renders/text.js +38 -0
- package/lib/renders/voice.d.ts +7 -0
- package/lib/renders/voice.js +58 -0
- package/lib/services/chat.d.ts +104 -0
- package/lib/services/chat.js +586 -0
- package/lib/services/message_transform.d.ts +9 -0
- package/lib/services/message_transform.js +35 -0
- package/lib/services/types.d.ts +23 -0
- package/lib/services/types.js +2 -0
- package/lib/types.d.ts +53 -0
- package/lib/types.js +2 -0
- package/lib/utils/error.d.ts +47 -0
- package/lib/utils/error.js +71 -0
- package/lib/utils/flow.d.ts +11 -0
- package/lib/utils/flow.js +52 -0
- package/lib/utils/lock.d.ts +9 -0
- package/lib/utils/lock.js +46 -0
- package/lib/utils/logger.d.ts +4 -0
- package/lib/utils/logger.js +25 -0
- package/lib/utils/pagination.d.ts +18 -0
- package/lib/utils/pagination.js +42 -0
- package/lib/utils/promise.d.ts +7 -0
- package/lib/utils/promise.js +20 -0
- package/lib/utils/queue.d.ts +9 -0
- package/lib/utils/queue.js +56 -0
- package/lib/utils/request.d.ts +18 -0
- package/lib/utils/request.js +121 -0
- package/lib/utils/sse.d.ts +2 -0
- package/lib/utils/sse.js +63 -0
- package/lib/utils/stream.d.ts +1 -0
- package/lib/utils/stream.js +45 -0
- package/lib/utils/string.d.ts +1 -0
- package/lib/utils/string.js +17 -0
- package/lib/utils/types.d.ts +1 -0
- package/lib/utils/types.js +2 -0
- package/package.json +102 -0
- package/resources/css/code.css +118 -0
- package/resources/css/main.css +412 -0
- package/resources/out.html +64 -0
- package/resources/presets/catgirl.yml +23 -0
- package/resources/presets/chatgpt.yml +10 -0
- package/resources/template.html +63 -0
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ChainMiddlewareRunStatus = exports.ChainMiddleware = exports.ChatChain = void 0;
|
|
4
|
+
const koishi_1 = require("koishi");
|
|
5
|
+
const logger_1 = require("../utils/logger");
|
|
6
|
+
const lifecycle_1 = require("../middlewares/lifecycle");
|
|
7
|
+
const events_1 = require("events");
|
|
8
|
+
const error_1 = require("../utils/error");
|
|
9
|
+
let logger;
|
|
10
|
+
/**
|
|
11
|
+
* ChatChain为消息的发送和接收提供了一个统一的中间提供交互
|
|
12
|
+
*/
|
|
13
|
+
class ChatChain {
|
|
14
|
+
ctx;
|
|
15
|
+
config;
|
|
16
|
+
_graph;
|
|
17
|
+
_senders;
|
|
18
|
+
constructor(ctx, config) {
|
|
19
|
+
this.ctx = ctx;
|
|
20
|
+
this.config = config;
|
|
21
|
+
logger = (0, logger_1.createLogger)(ctx);
|
|
22
|
+
this._graph = new ChatChainDependencyGraph();
|
|
23
|
+
this._senders = [];
|
|
24
|
+
console.log(ctx.runtime.inject);
|
|
25
|
+
const defaultChatChainSender = new DefaultChatChainSender(config);
|
|
26
|
+
this._senders.push((session, messages) => defaultChatChainSender.send(session, messages));
|
|
27
|
+
}
|
|
28
|
+
async receiveMessage(session, ctx) {
|
|
29
|
+
const context = {
|
|
30
|
+
config: this.config,
|
|
31
|
+
message: session.content,
|
|
32
|
+
ctx: ctx ?? this.ctx,
|
|
33
|
+
session,
|
|
34
|
+
options: {},
|
|
35
|
+
send: (message) => this.sendMessage(session, message),
|
|
36
|
+
recallThinkingMessage: async () => { }
|
|
37
|
+
};
|
|
38
|
+
context.recallThinkingMessage = async () => {
|
|
39
|
+
if (context.options?.thinkingTimeoutObject) {
|
|
40
|
+
clearTimeout(context.options.thinkingTimeoutObject.timeout);
|
|
41
|
+
if (context.options.thinkingTimeoutObject.autoRecallTimeout) {
|
|
42
|
+
clearTimeout(context.options.thinkingTimeoutObject.autoRecallTimeout);
|
|
43
|
+
}
|
|
44
|
+
if (context.options.thinkingTimeoutObject.recallFunc) {
|
|
45
|
+
await context.options.thinkingTimeoutObject.recallFunc();
|
|
46
|
+
}
|
|
47
|
+
if (context.options?.thinkingTimeoutObject?.timeout) {
|
|
48
|
+
context.options.thinkingTimeoutObject.timeout = null;
|
|
49
|
+
}
|
|
50
|
+
context.options.thinkingTimeoutObject = undefined;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
const result = await this._runMiddleware(session, context);
|
|
54
|
+
await context.recallThinkingMessage();
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
async receiveCommand(session, command, options = {}) {
|
|
58
|
+
const context = {
|
|
59
|
+
config: this.config,
|
|
60
|
+
message: options?.message ?? session.content,
|
|
61
|
+
ctx: this.ctx,
|
|
62
|
+
session,
|
|
63
|
+
command,
|
|
64
|
+
send: (message) => this.sendMessage(session, message),
|
|
65
|
+
recallThinkingMessage: async () => { },
|
|
66
|
+
options
|
|
67
|
+
};
|
|
68
|
+
context.recallThinkingMessage = async () => {
|
|
69
|
+
if (context.options.thinkingTimeoutObject) {
|
|
70
|
+
clearTimeout(context.options.thinkingTimeoutObject.timeout);
|
|
71
|
+
if (context.options.thinkingTimeoutObject.recallFunc) {
|
|
72
|
+
await context.options.thinkingTimeoutObject.recallFunc();
|
|
73
|
+
}
|
|
74
|
+
if (context.options?.thinkingTimeoutObject?.timeout) {
|
|
75
|
+
context.options.thinkingTimeoutObject.timeout = null;
|
|
76
|
+
}
|
|
77
|
+
context.options.thinkingTimeoutObject = undefined;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const result = await this._runMiddleware(session, context);
|
|
81
|
+
await context.recallThinkingMessage();
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
middleware(name, middleware, ctx = this.ctx) {
|
|
85
|
+
const result = new ChainMiddleware(name, middleware, this._graph);
|
|
86
|
+
this._graph.addNode(result);
|
|
87
|
+
ctx.on('dispose', () => {
|
|
88
|
+
this._graph.removeNode(name);
|
|
89
|
+
});
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
sender(sender) {
|
|
93
|
+
this._senders.push(sender);
|
|
94
|
+
}
|
|
95
|
+
async _runMiddleware(session, context) {
|
|
96
|
+
// 手动 polyfill,呃呃呃呃呃
|
|
97
|
+
if (session.isDirect == null) {
|
|
98
|
+
session.isDirect = session.subtype === 'private';
|
|
99
|
+
}
|
|
100
|
+
const originMessage = context.message;
|
|
101
|
+
const runList = this._graph.build();
|
|
102
|
+
if (runList.length === 0) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
let isOutputLog = false;
|
|
106
|
+
for (const middleware of runList) {
|
|
107
|
+
let result;
|
|
108
|
+
let executedTime = Date.now();
|
|
109
|
+
try {
|
|
110
|
+
result = await middleware.run(session, context);
|
|
111
|
+
executedTime = Date.now() - executedTime;
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
if (error instanceof error_1.ChatLunaError) {
|
|
115
|
+
await this.sendMessage(session, error.message);
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
logger.error(`chat-chain: ${middleware.name} error ${error}`);
|
|
119
|
+
logger.error(error);
|
|
120
|
+
if (error.cause) {
|
|
121
|
+
logger.error(error.cause);
|
|
122
|
+
}
|
|
123
|
+
logger.debug('-'.repeat(20) + '\n');
|
|
124
|
+
await this.sendMessage(session, `执行 ${middleware.name} 时出现错误: ${error.message}`);
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
if (!middleware.name.startsWith('lifecycle-') &&
|
|
128
|
+
ChainMiddlewareRunStatus.SKIPPED !== result &&
|
|
129
|
+
middleware.name !== 'allow_reply' &&
|
|
130
|
+
executedTime > 10) {
|
|
131
|
+
logger.debug(`middleware %c executed in %d ms`, middleware.name, executedTime);
|
|
132
|
+
isOutputLog = true;
|
|
133
|
+
}
|
|
134
|
+
if (result === ChainMiddlewareRunStatus.STOP) {
|
|
135
|
+
// 中间件说这里不要继续执行了
|
|
136
|
+
if (context.message != null &&
|
|
137
|
+
context.message !== originMessage) {
|
|
138
|
+
// 消息被修改了
|
|
139
|
+
await this.sendMessage(session, context.message);
|
|
140
|
+
}
|
|
141
|
+
if (isOutputLog) {
|
|
142
|
+
logger.debug('-'.repeat(20) + '\n');
|
|
143
|
+
}
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
else if (result instanceof Array || typeof result === 'string') {
|
|
147
|
+
context.message = result;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (isOutputLog) {
|
|
151
|
+
logger.debug('-'.repeat(20) + '\n');
|
|
152
|
+
}
|
|
153
|
+
if (context.message != null && context.message !== originMessage) {
|
|
154
|
+
// 消息被修改了
|
|
155
|
+
await this.sendMessage(session, context.message);
|
|
156
|
+
}
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
async sendMessage(session, message) {
|
|
160
|
+
// check if message is a two-dimensional array
|
|
161
|
+
const messages = message instanceof Array ? message : [message];
|
|
162
|
+
for (const sender of this._senders) {
|
|
163
|
+
await sender(session, messages);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
exports.ChatChain = ChatChain;
|
|
168
|
+
// 定义一个有向无环图类,包含节点集合和邻接表
|
|
169
|
+
class ChatChainDependencyGraph {
|
|
170
|
+
_tasks = [];
|
|
171
|
+
_dependencies = new Map();
|
|
172
|
+
_eventEmitter = new events_1.EventEmitter();
|
|
173
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
174
|
+
_listeners = new Map();
|
|
175
|
+
constructor() {
|
|
176
|
+
this._eventEmitter.on('build_node', () => {
|
|
177
|
+
for (const [name, listeners] of this._listeners.entries()) {
|
|
178
|
+
for (const listener of listeners) {
|
|
179
|
+
listener(name);
|
|
180
|
+
}
|
|
181
|
+
listeners.length = 0;
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
// Add a task to the DAG.
|
|
186
|
+
addNode(middleware) {
|
|
187
|
+
this._tasks.push({
|
|
188
|
+
name: middleware.name,
|
|
189
|
+
middleware
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
removeNode(name) {
|
|
193
|
+
const index = this._tasks.findIndex((task) => task.name === name);
|
|
194
|
+
if (index !== -1) {
|
|
195
|
+
this._tasks.splice(index, 1);
|
|
196
|
+
}
|
|
197
|
+
// remove dependencies
|
|
198
|
+
for (const [, dependencies] of this._dependencies.entries()) {
|
|
199
|
+
if (dependencies.has(name)) {
|
|
200
|
+
dependencies.delete(name);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (this._dependencies[name]) {
|
|
204
|
+
delete this._dependencies[name];
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
208
|
+
once(name, listener) {
|
|
209
|
+
if (this._listeners.has(name)) {
|
|
210
|
+
this._listeners.get(name).push(listener);
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
this._listeners.set(name, [listener]);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
// Set a dependency between two tasks
|
|
217
|
+
before(taskA, taskB) {
|
|
218
|
+
if (taskA instanceof ChainMiddleware) {
|
|
219
|
+
taskA = taskA.name;
|
|
220
|
+
}
|
|
221
|
+
if (taskB instanceof ChainMiddleware) {
|
|
222
|
+
taskB = taskB.name;
|
|
223
|
+
}
|
|
224
|
+
if (taskA && taskB) {
|
|
225
|
+
// Add taskB to the dependencies of taskA
|
|
226
|
+
const dependencies = this._dependencies.get(taskA) ?? new Set();
|
|
227
|
+
dependencies.add(taskB);
|
|
228
|
+
this._dependencies.set(taskA, dependencies);
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
throw new Error('Invalid tasks');
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
// Set a reverse dependency between two tasks
|
|
235
|
+
after(taskA, taskB) {
|
|
236
|
+
if (taskA instanceof ChainMiddleware) {
|
|
237
|
+
taskA = taskA.name;
|
|
238
|
+
}
|
|
239
|
+
if (taskB instanceof ChainMiddleware) {
|
|
240
|
+
taskB = taskB.name;
|
|
241
|
+
}
|
|
242
|
+
if (taskA && taskB) {
|
|
243
|
+
// Add taskB to the dependencies of taskA
|
|
244
|
+
const dependencies = this._dependencies.get(taskB) ?? new Set();
|
|
245
|
+
dependencies.add(taskA);
|
|
246
|
+
this._dependencies.set(taskB, dependencies);
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
throw new Error('Invalid tasks');
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
// Get dependencies of a task
|
|
253
|
+
getDependencies(task) {
|
|
254
|
+
return this._dependencies.get(task);
|
|
255
|
+
}
|
|
256
|
+
// Get dependents of a task
|
|
257
|
+
getDependents(task) {
|
|
258
|
+
const dependents = [];
|
|
259
|
+
for (const [key, value] of this._dependencies.entries()) {
|
|
260
|
+
if ([...value].includes(task)) {
|
|
261
|
+
dependents.push(key);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return dependents;
|
|
265
|
+
}
|
|
266
|
+
// Build a two-dimensional array of tasks based on their dependencies
|
|
267
|
+
build() {
|
|
268
|
+
this._eventEmitter.emit('build_node');
|
|
269
|
+
// Create an array to store the result
|
|
270
|
+
const result = [];
|
|
271
|
+
// Create a map to store the indegree of each task
|
|
272
|
+
const indegree = new Map();
|
|
273
|
+
// Initialize the indegree map with zero for each task
|
|
274
|
+
for (const task of this._tasks) {
|
|
275
|
+
indegree.set(task.name, 0);
|
|
276
|
+
}
|
|
277
|
+
// Iterate over the tasks and increment the indegree of their dependencies
|
|
278
|
+
for (const [, dependencies] of this._dependencies.entries()) {
|
|
279
|
+
for (const dependency of dependencies) {
|
|
280
|
+
indegree.set(dependency, indegree.get(dependency) + 1);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
// Create a queue to store the tasks with zero indegree
|
|
284
|
+
const queue = [];
|
|
285
|
+
// Enqueue the tasks with zero indegree
|
|
286
|
+
for (const [task, degree] of indegree.entries()) {
|
|
287
|
+
if (degree === 0) {
|
|
288
|
+
queue.push(task);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
// While the queue is not empty
|
|
292
|
+
while (queue.length > 0) {
|
|
293
|
+
// Create an array to store the current level of tasks
|
|
294
|
+
// Dequeue all the tasks in the queue and add them to the level
|
|
295
|
+
while (queue.length > 0) {
|
|
296
|
+
const task = queue.shift();
|
|
297
|
+
result.push(this._tasks.find((t) => t.name === task).middleware);
|
|
298
|
+
// For each dependency of the dequeued task
|
|
299
|
+
for (const dep of this._dependencies.get(task) ?? []) {
|
|
300
|
+
// Decrement its indegree by one
|
|
301
|
+
indegree.set(dep, indegree.get(dep) - 1);
|
|
302
|
+
// If its indegree becomes zero, enqueue it to the queue
|
|
303
|
+
if (indegree.get(dep) === 0) {
|
|
304
|
+
queue.push(dep);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
// Return the result
|
|
310
|
+
return result;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
class ChainMiddleware {
|
|
314
|
+
name;
|
|
315
|
+
execute;
|
|
316
|
+
graph;
|
|
317
|
+
constructor(name, execute, graph) {
|
|
318
|
+
this.name = name;
|
|
319
|
+
this.execute = execute;
|
|
320
|
+
this.graph = graph;
|
|
321
|
+
}
|
|
322
|
+
before(name) {
|
|
323
|
+
this.graph.before(this.name, name);
|
|
324
|
+
if (this.name.startsWith('lifecycle-')) {
|
|
325
|
+
return this;
|
|
326
|
+
}
|
|
327
|
+
const lifecycleName = lifecycle_1.lifecycleNames;
|
|
328
|
+
// 现在我们需要基于当前添加的依赖,去寻找这个依赖锚定的生命周期
|
|
329
|
+
// 如果当前添加的依赖是生命周期,那么我们需要找到这个生命周期的下一个生命周期
|
|
330
|
+
if (lifecycleName.includes(name)) {
|
|
331
|
+
const lastLifecycleName = lifecycleName[lifecycleName.indexOf(name) - 1];
|
|
332
|
+
if (lastLifecycleName) {
|
|
333
|
+
this.graph.after(this.name, lastLifecycleName);
|
|
334
|
+
}
|
|
335
|
+
return this;
|
|
336
|
+
}
|
|
337
|
+
// 如果不是的话,我们就需要寻找依赖锚定的生命周期
|
|
338
|
+
this.graph.once('build_node', () => {
|
|
339
|
+
const beforeMiddlewares = [
|
|
340
|
+
...this.graph.getDependencies(name)
|
|
341
|
+
].filter((name) => name.startsWith('lifecycle-'));
|
|
342
|
+
const afterMiddlewares = this.graph
|
|
343
|
+
.getDependents(name)
|
|
344
|
+
.filter((name) => name.startsWith('lifecycle-'));
|
|
345
|
+
for (const before of beforeMiddlewares) {
|
|
346
|
+
this.graph.before(this.name, before);
|
|
347
|
+
}
|
|
348
|
+
for (const after of afterMiddlewares) {
|
|
349
|
+
this.graph.after(this.name, after);
|
|
350
|
+
}
|
|
351
|
+
});
|
|
352
|
+
return this;
|
|
353
|
+
}
|
|
354
|
+
after(name) {
|
|
355
|
+
this.graph.after(this.name, name);
|
|
356
|
+
if (this.name.startsWith('lifecycle-')) {
|
|
357
|
+
return this;
|
|
358
|
+
}
|
|
359
|
+
const lifecycleName = lifecycle_1.lifecycleNames;
|
|
360
|
+
// 现在我们需要基于当前添加的依赖,去寻找这个依赖锚定的生命周期
|
|
361
|
+
// 如果当前添加的依赖是生命周期,那么我们需要找到这个生命周期的下一个生命周期
|
|
362
|
+
if (lifecycleName.includes(name)) {
|
|
363
|
+
const nextLifecycleName = lifecycleName[lifecycleName.indexOf(name) + 1];
|
|
364
|
+
if (nextLifecycleName) {
|
|
365
|
+
this.graph.before(this.name, nextLifecycleName);
|
|
366
|
+
}
|
|
367
|
+
return this;
|
|
368
|
+
}
|
|
369
|
+
// 如果不是的话,我们就需要寻找依赖锚定的生命周期
|
|
370
|
+
this.graph.once('build_node', () => {
|
|
371
|
+
const beforeMiddlewares = [
|
|
372
|
+
...this.graph.getDependencies(name)
|
|
373
|
+
].filter((name) => name.startsWith('lifecycle-'));
|
|
374
|
+
const afterMiddlewares = this.graph
|
|
375
|
+
.getDependents(name)
|
|
376
|
+
.filter((name) => name.startsWith('lifecycle-'));
|
|
377
|
+
for (const before of beforeMiddlewares) {
|
|
378
|
+
this.graph.before(this.name, before);
|
|
379
|
+
}
|
|
380
|
+
for (const after of afterMiddlewares) {
|
|
381
|
+
this.graph.after(this.name, after);
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
return this;
|
|
385
|
+
}
|
|
386
|
+
run(session, options) {
|
|
387
|
+
return this.execute(session, options);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
exports.ChainMiddleware = ChainMiddleware;
|
|
391
|
+
class DefaultChatChainSender {
|
|
392
|
+
config;
|
|
393
|
+
constructor(config) {
|
|
394
|
+
this.config = config;
|
|
395
|
+
}
|
|
396
|
+
async send(session, messages) {
|
|
397
|
+
if (this.config.isForwardMsg) {
|
|
398
|
+
const sendMessages = [];
|
|
399
|
+
if (messages[0] instanceof Array) {
|
|
400
|
+
// h[][]
|
|
401
|
+
for (const message of messages) {
|
|
402
|
+
sendMessages.push((0, koishi_1.h)('message', ...message));
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
else if (messages[0] instanceof Object) {
|
|
406
|
+
// h | h[]
|
|
407
|
+
sendMessages.push((0, koishi_1.h)('message', ...messages));
|
|
408
|
+
}
|
|
409
|
+
else if (typeof messages[0] === 'string') {
|
|
410
|
+
// string
|
|
411
|
+
sendMessages.push(koishi_1.h.text(messages[0]));
|
|
412
|
+
}
|
|
413
|
+
else {
|
|
414
|
+
throw new Error(`unknown message type: ${typeof messages[0]}`);
|
|
415
|
+
}
|
|
416
|
+
await session.sendQueued((0, koishi_1.h)('message', {
|
|
417
|
+
forward: true
|
|
418
|
+
}, ...sendMessages));
|
|
419
|
+
}
|
|
420
|
+
else {
|
|
421
|
+
for (const message of messages) {
|
|
422
|
+
let messageFragment;
|
|
423
|
+
if (this.config.isReplyWithAt && session.isDirect === false) {
|
|
424
|
+
messageFragment = [(0, koishi_1.h)('quote', { id: session.messageId })];
|
|
425
|
+
if (message instanceof Array) {
|
|
426
|
+
messageFragment = messageFragment.concat(message);
|
|
427
|
+
}
|
|
428
|
+
else if (typeof message === 'string') {
|
|
429
|
+
messageFragment.push(koishi_1.h.text(message));
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
messageFragment.push(message);
|
|
433
|
+
}
|
|
434
|
+
for (const element of messageFragment) {
|
|
435
|
+
// 语音,消息 不能引用
|
|
436
|
+
if (element.type === 'audio' ||
|
|
437
|
+
element.type === 'message') {
|
|
438
|
+
messageFragment.shift();
|
|
439
|
+
break;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
else {
|
|
444
|
+
if (message instanceof Array) {
|
|
445
|
+
messageFragment = message;
|
|
446
|
+
}
|
|
447
|
+
else if (typeof message === 'string') {
|
|
448
|
+
messageFragment = [koishi_1.h.text(message)];
|
|
449
|
+
}
|
|
450
|
+
else {
|
|
451
|
+
// 你就说是不是 element 吧
|
|
452
|
+
messageFragment = [message];
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
await session.sendQueued(messageFragment);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
var ChainMiddlewareRunStatus;
|
|
461
|
+
(function (ChainMiddlewareRunStatus) {
|
|
462
|
+
ChainMiddlewareRunStatus[ChainMiddlewareRunStatus["SKIPPED"] = 0] = "SKIPPED";
|
|
463
|
+
ChainMiddlewareRunStatus[ChainMiddlewareRunStatus["STOP"] = 1] = "STOP";
|
|
464
|
+
ChainMiddlewareRunStatus[ChainMiddlewareRunStatus["CONTINUE"] = 2] = "CONTINUE";
|
|
465
|
+
})(ChainMiddlewareRunStatus || (exports.ChainMiddlewareRunStatus = ChainMiddlewareRunStatus = {}));
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Context, Session } from 'koishi';
|
|
2
|
+
import { ConversationRoom } from '../types';
|
|
3
|
+
import { Config } from '../config';
|
|
4
|
+
export declare function queryJoinedConversationRoom(ctx: Context, session: Session, name?: string): Promise<ConversationRoom>;
|
|
5
|
+
export declare function queryPublicConversationRoom(ctx: Context, session: Session): Promise<ConversationRoom>;
|
|
6
|
+
export declare function checkConversationRoomAvailability(ctx: Context, room: ConversationRoom): Promise<boolean>;
|
|
7
|
+
export declare function fixConversationRoomAvailability(ctx: Context, config: Config, room: ConversationRoom): Promise<void>;
|
|
8
|
+
export declare function getTemplateConversationRoom(ctx: Context, config: Config): Promise<ConversationRoom>;
|
|
9
|
+
export declare function getConversationRoomCount(ctx: Context): Promise<number>;
|
|
10
|
+
export declare function transferConversationRoom(ctx: Context, session: Session, room: ConversationRoom, userId: string): Promise<void>;
|
|
11
|
+
export declare function switchConversationRoom(ctx: Context, session: Session, id: string | number): Promise<ConversationRoom>;
|
|
12
|
+
export declare function getAllJoinedConversationRoom(ctx: Context, session: Session, queryAll?: boolean): Promise<ConversationRoom[]>;
|
|
13
|
+
export declare function leaveConversationRoom(ctx: Context, session: Session, room: ConversationRoom): Promise<void>;
|
|
14
|
+
export declare function queryConversationRoom(ctx: Context, session: Session, name: string): Promise<ConversationRoom>;
|
|
15
|
+
export declare function resolveConversationRoom(ctx: Context, roomId: number): Promise<ConversationRoom>;
|
|
16
|
+
export declare function deleteConversationRoom(ctx: Context, session: Session, room: ConversationRoom): Promise<void>;
|
|
17
|
+
export declare function joinConversationRoom(ctx: Context, session: Session, roomId: number | ConversationRoom, isDirect?: boolean, userId?: string): Promise<void>;
|
|
18
|
+
export declare function getConversationRoomUser(ctx: Context, session: Session, roomId: number | ConversationRoom, userId?: string): Promise<Pick<import("../types").ConversationRoomMemberInfo, import("koishi").Keys<import("../types").ConversationRoomMemberInfo, any>>>;
|
|
19
|
+
export declare function setUserPermission(ctx: Context, session: Session, roomId: number | ConversationRoom, permission: 'member' | 'admin', userId?: string): Promise<void>;
|
|
20
|
+
export declare function addConversationRoomToGroup(ctx: Context, session: Session, roomId: number | ConversationRoom, groupId?: string): Promise<void>;
|
|
21
|
+
export declare function muteUserFromConversationRoom(ctx: Context, session: Session, roomId: number | ConversationRoom, userId: string): Promise<void>;
|
|
22
|
+
export declare function kickUserFromConversationRoom(ctx: Context, session: Session, roomId: number | ConversationRoom, userId: string): Promise<void>;
|
|
23
|
+
export declare function checkAdmin(session: Session): Promise<number>;
|
|
24
|
+
export declare function createConversationRoom(ctx: Context, session: Session, room: ConversationRoom): Promise<void>;
|