koishi-plugin-chatluna 1.4.0-alpha.9 → 1.4.0
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/lib/chains/chain.d.ts +10 -22
- package/lib/chains/index.cjs +50 -26
- package/lib/chains/index.mjs +50 -26
- package/lib/commands/conversation.d.ts +5 -0
- package/lib/config.d.ts +4 -1
- package/lib/conversation_types.d.ts +206 -0
- package/lib/index.cjs +2532 -1867
- package/lib/index.mjs +2402 -1714
- package/lib/invocation/runtime.d.ts +37 -0
- package/lib/llm-core/agent/agent.d.ts +5 -3
- package/lib/llm-core/agent/index.cjs +1269 -727
- package/lib/llm-core/agent/index.mjs +1279 -733
- package/lib/llm-core/agent/legacy-executor.d.ts +3 -5
- package/lib/llm-core/agent/openai/index.d.ts +1 -1
- package/lib/llm-core/agent/react/index.d.ts +3 -2
- package/lib/llm-core/agent/react/prompt.d.ts +1 -1
- package/lib/llm-core/agent/sub-agent/executor.d.ts +22 -0
- package/lib/llm-core/agent/sub-agent/runtime.d.ts +2 -0
- package/lib/llm-core/agent/sub-agent/tool.d.ts +61 -0
- package/lib/llm-core/agent/sub-agent/types.d.ts +145 -0
- package/lib/llm-core/agent/sub-agent/utils.d.ts +11 -0
- package/lib/llm-core/agent/sub-agent.d.ts +4 -95
- package/lib/llm-core/agent/tool-observation.d.ts +18 -0
- package/lib/llm-core/agent/types.d.ts +10 -10
- package/lib/llm-core/agent/wakeup.d.ts +3 -0
- package/lib/llm-core/chain/agent_chat_chain.d.ts +1 -1
- package/lib/llm-core/chain/base.cjs +17 -16
- package/lib/llm-core/chain/base.d.ts +4 -2
- package/lib/llm-core/chain/base.mjs +17 -16
- package/lib/llm-core/chain/chat_chain.d.ts +1 -1
- package/lib/llm-core/chain/infinite_context_chain.d.ts +2 -25
- package/lib/llm-core/chain/prompt.cjs +16 -3
- package/lib/llm-core/chain/prompt.mjs +16 -3
- package/lib/llm-core/chat/app.cjs +307 -365
- package/lib/llm-core/chat/app.d.ts +6 -6
- package/lib/llm-core/chat/app.mjs +313 -372
- package/lib/llm-core/chat/helper.d.ts +4 -3
- package/lib/llm-core/chat/infinite_context.d.ts +12 -11
- package/lib/llm-core/chat/runtime.d.ts +35 -0
- package/lib/llm-core/memory/message/database_history.d.ts +3 -1
- package/lib/llm-core/memory/message/index.cjs +60 -16
- package/lib/llm-core/memory/message/index.mjs +57 -10
- package/lib/llm-core/platform/api.cjs +144 -3
- package/lib/llm-core/platform/api.d.ts +18 -1
- package/lib/llm-core/platform/api.mjs +142 -4
- package/lib/llm-core/platform/client.cjs +48 -10
- package/lib/llm-core/platform/client.d.ts +10 -3
- package/lib/llm-core/platform/client.mjs +47 -9
- package/lib/llm-core/platform/config.cjs +1 -0
- package/lib/llm-core/platform/config.mjs +1 -0
- package/lib/llm-core/platform/metrics.d.ts +32 -0
- package/lib/llm-core/platform/model.cjs +427 -136
- package/lib/llm-core/platform/model.d.ts +13 -4
- package/lib/llm-core/platform/model.mjs +433 -137
- package/lib/llm-core/platform/rerank.cjs +178 -0
- package/lib/llm-core/platform/rerank.d.ts +36 -0
- package/lib/llm-core/platform/rerank.mjs +162 -0
- package/lib/llm-core/platform/service.cjs +24 -10
- package/lib/llm-core/platform/service.d.ts +5 -3
- package/lib/llm-core/platform/service.mjs +28 -12
- package/lib/llm-core/platform/types.cjs +1 -0
- package/lib/llm-core/platform/types.d.ts +5 -7
- package/lib/llm-core/platform/types.mjs +1 -0
- package/lib/llm-core/platform/usage.cjs +168 -0
- package/lib/llm-core/platform/usage.d.ts +69 -0
- package/lib/llm-core/platform/usage.mjs +140 -0
- package/lib/llm-core/prompt/index.cjs +34 -50
- package/lib/llm-core/prompt/index.mjs +34 -47
- package/lib/llm-core/prompt/system_prompts.d.ts +2 -3
- package/lib/llm-core/prompt/type.d.ts +2 -0
- package/lib/llm-core/utils/count_tokens.cjs +38 -1
- package/lib/llm-core/utils/count_tokens.d.ts +7 -1
- package/lib/llm-core/utils/count_tokens.mjs +36 -1
- package/lib/llm-core/vectorstores/index.cjs +2 -0
- package/lib/llm-core/vectorstores/index.mjs +2 -0
- package/lib/middlewares/chat/message_delay.d.ts +1 -0
- package/lib/middlewares/chat/thinking_message_send.d.ts +2 -0
- package/lib/middlewares/conversation/request_conversation.d.ts +0 -1
- package/lib/preset.cjs +24 -1
- package/lib/preset.d.ts +1 -0
- package/lib/preset.mjs +25 -1
- package/lib/render/base.d.ts +26 -0
- package/lib/render/index.d.ts +24 -0
- package/lib/render/koishi-element.d.ts +11 -0
- package/lib/render/mixed-voice.d.ts +14 -0
- package/lib/render/pure-text.d.ts +12 -0
- package/lib/render/raw.d.ts +10 -0
- package/lib/render/sentence.d.ts +3 -0
- package/lib/render/split.d.ts +25 -0
- package/lib/render/stream.d.ts +37 -0
- package/lib/render/text.d.ts +11 -0
- package/lib/render/types.d.ts +27 -0
- package/lib/render/voice.d.ts +12 -0
- package/lib/services/agent_wakeup.d.ts +3 -0
- package/lib/services/chat.cjs +1923 -1157
- package/lib/services/chat.d.ts +9 -2
- package/lib/services/chat.mjs +1900 -1123
- package/lib/services/conversation.d.ts +33 -5
- package/lib/services/conversation_runtime.d.ts +8 -5
- package/lib/services/conversation_types.d.ts +26 -0
- package/lib/services/invocation_runtime.d.ts +37 -0
- package/lib/services/types.d.ts +10 -10
- package/lib/types.d.ts +280 -3
- package/lib/utils/archive.d.ts +1 -1
- package/lib/utils/conversation.d.ts +2 -1
- package/lib/utils/error.cjs +72 -1
- package/lib/utils/error.d.ts +33 -1
- package/lib/utils/error.mjs +67 -1
- package/lib/utils/koishi.cjs +91 -0
- package/lib/utils/koishi.d.ts +7 -1
- package/lib/utils/koishi.mjs +89 -0
- package/lib/utils/langchain.cjs +19 -0
- package/lib/utils/langchain.d.ts +3 -1
- package/lib/utils/langchain.mjs +17 -0
- package/lib/utils/logger.cjs +44 -46
- package/lib/utils/logger.d.ts +1 -1
- package/lib/utils/logger.mjs +44 -46
- package/lib/utils/model.d.ts +2 -0
- package/lib/utils/pagination.cjs +1 -0
- package/lib/utils/pagination.mjs +1 -0
- package/lib/utils/queue.cjs +5 -1
- package/lib/utils/queue.mjs +7 -2
- package/lib/utils/schema.cjs +25 -4
- package/lib/utils/schema.d.ts +7 -0
- package/lib/utils/schema.mjs +22 -0
- package/lib/utils/sse.cjs +46 -21
- package/lib/utils/sse.d.ts +5 -3
- package/lib/utils/sse.mjs +49 -21
- package/lib/utils/string.cjs +8 -15
- package/lib/utils/string.d.ts +4 -3
- package/lib/utils/string.mjs +8 -15
- package/lib/utils/usage_source.cjs +79 -0
- package/lib/utils/usage_source.d.ts +1 -0
- package/lib/utils/usage_source.mjs +47 -0
- package/lib/utils/virtual_session.cjs +66 -0
- package/lib/utils/virtual_session.d.ts +15 -0
- package/lib/utils/virtual_session.mjs +44 -0
- package/package.json +24 -2
- package/resources/presets/sydney.yml +2 -4
package/lib/chains/chain.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { Context, h, Session } from 'koishi';
|
|
2
2
|
import { Config } from '../config';
|
|
3
|
-
import type { ConversationResolution } from '../
|
|
3
|
+
import type { ChatInvocationContext, ConversationResolution } from '../types';
|
|
4
4
|
import '../middlewares/system/lifecycle';
|
|
5
|
-
import type { UsageMetadata } from '@langchain/core/messages';
|
|
6
|
-
import type { ToolMask } from '../llm-core/agent';
|
|
7
5
|
export declare class ChatChain {
|
|
8
6
|
private readonly ctx;
|
|
9
7
|
private readonly config;
|
|
@@ -14,6 +12,10 @@ export declare class ChatChain {
|
|
|
14
12
|
private _createRecallThinkingMessage;
|
|
15
13
|
receiveMessage(session: Session, ctx?: Context): Promise<boolean>;
|
|
16
14
|
receiveCommand(session: Session, command: string, options?: ChainMiddlewareContextOptions, ctx?: Context): Promise<boolean>;
|
|
15
|
+
runCommand(session: Session, command: string, options?: ChainMiddlewareContextOptions, ctx?: Context): Promise<{
|
|
16
|
+
ok: boolean;
|
|
17
|
+
context: ChainMiddlewareContext;
|
|
18
|
+
}>;
|
|
17
19
|
middleware<T extends keyof ChainMiddlewareName>(name: T, middleware: ChainMiddlewareFunction, ctx?: Context): ChainMiddleware;
|
|
18
20
|
sender(sender: ChatChainSender): void;
|
|
19
21
|
private _runMiddleware;
|
|
@@ -48,7 +50,7 @@ export declare class ChainMiddleware {
|
|
|
48
50
|
constructor(name: string, execute: ChainMiddlewareFunction, graph: ChatChainDependencyGraph);
|
|
49
51
|
before<T extends keyof ChainMiddlewareName>(name: T): this;
|
|
50
52
|
after<T extends keyof ChainMiddlewareName>(name: T): this;
|
|
51
|
-
run(session: Session, options: ChainMiddlewareContext): Promise<string |
|
|
53
|
+
run(session: Session, options: ChainMiddlewareContext): Promise<string | h[] | ChainMiddlewareRunStatus | h[][]>;
|
|
52
54
|
}
|
|
53
55
|
export interface ChainMiddlewareContext {
|
|
54
56
|
config: Config;
|
|
@@ -60,26 +62,12 @@ export interface ChainMiddlewareContext {
|
|
|
60
62
|
recallThinkingMessage?: () => Promise<void>;
|
|
61
63
|
send: (message: h[][] | h[] | h | string) => Promise<void>;
|
|
62
64
|
}
|
|
63
|
-
export interface TriggerWakeupContext {
|
|
64
|
-
requestId: string;
|
|
65
|
-
replyTo?: 'channel' | 'user' | 'silent' | 'callback';
|
|
66
|
-
source: {
|
|
67
|
-
kind: string;
|
|
68
|
-
taskId?: number;
|
|
69
|
-
providerKind?: string;
|
|
70
|
-
detail?: unknown;
|
|
71
|
-
};
|
|
72
|
-
chatMode?: string;
|
|
73
|
-
signal?: AbortSignal;
|
|
74
|
-
toolMask?: ToolMask;
|
|
75
|
-
variables?: Record<string, unknown>;
|
|
76
|
-
state?: {
|
|
77
|
-
tokens?: UsageMetadata;
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
65
|
export interface ChainMiddlewareContextOptions {
|
|
81
66
|
conversation?: ConversationResolution;
|
|
82
|
-
|
|
67
|
+
invocation?: ChatInvocationContext;
|
|
68
|
+
deliverySession?: Session;
|
|
69
|
+
messageName?: string;
|
|
70
|
+
error?: Error;
|
|
83
71
|
[key: string]: any;
|
|
84
72
|
}
|
|
85
73
|
export interface ChainMiddlewareName {
|
package/lib/chains/index.cjs
CHANGED
|
@@ -74,6 +74,8 @@ var ChatChain = class {
|
|
|
74
74
|
(session, messages, context) => defaultChatChainSender.send(session, messages, context)
|
|
75
75
|
);
|
|
76
76
|
}
|
|
77
|
+
ctx;
|
|
78
|
+
config;
|
|
77
79
|
static {
|
|
78
80
|
__name(this, "ChatChain");
|
|
79
81
|
}
|
|
@@ -86,6 +88,7 @@ var ChatChain = class {
|
|
|
86
88
|
const timeoutObj = context.options.thinkingTimeoutObject;
|
|
87
89
|
clearTimeout(timeoutObj.timeout);
|
|
88
90
|
timeoutObj.autoRecallTimeout && clearTimeout(timeoutObj.autoRecallTimeout);
|
|
91
|
+
timeoutObj.setQueueCount(0);
|
|
89
92
|
timeoutObj.recallFunc && await timeoutObj.recallFunc();
|
|
90
93
|
timeoutObj.timeout = null;
|
|
91
94
|
context.options.thinkingTimeoutObject = void 0;
|
|
@@ -106,18 +109,33 @@ var ChatChain = class {
|
|
|
106
109
|
)
|
|
107
110
|
};
|
|
108
111
|
context.recallThinkingMessage = this._createRecallThinkingMessage(context);
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
+
try {
|
|
113
|
+
return await this._runMiddleware(session, context);
|
|
114
|
+
} finally {
|
|
115
|
+
await context.options.completeMessageTurn?.();
|
|
116
|
+
await context.recallThinkingMessage();
|
|
117
|
+
}
|
|
112
118
|
}
|
|
113
119
|
async receiveCommand(session, command, options = {}, ctx = this.ctx) {
|
|
120
|
+
return (await this.runCommand(session, command, options, ctx)).ok;
|
|
121
|
+
}
|
|
122
|
+
async runCommand(session, command, options = {}, ctx = this.ctx) {
|
|
114
123
|
const context = {
|
|
115
124
|
config: this.config,
|
|
116
125
|
message: options?.message ?? session.content,
|
|
117
126
|
ctx,
|
|
118
127
|
session,
|
|
119
128
|
command,
|
|
120
|
-
send: /* @__PURE__ */ __name((message) =>
|
|
129
|
+
send: /* @__PURE__ */ __name(async (message) => {
|
|
130
|
+
if (options.invocation?.delivery === "silent" || options.invocation?.delivery === "capture") {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
await this.sendMessage(
|
|
134
|
+
options.deliverySession ?? session,
|
|
135
|
+
message,
|
|
136
|
+
context
|
|
137
|
+
);
|
|
138
|
+
}, "send"),
|
|
121
139
|
recallThinkingMessage: this._createRecallThinkingMessage(
|
|
122
140
|
{}
|
|
123
141
|
),
|
|
@@ -127,9 +145,13 @@ var ChatChain = class {
|
|
|
127
145
|
}
|
|
128
146
|
};
|
|
129
147
|
context.recallThinkingMessage = this._createRecallThinkingMessage(context);
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
148
|
+
try {
|
|
149
|
+
const ok = await this._runMiddleware(session, context);
|
|
150
|
+
return { ok, context };
|
|
151
|
+
} finally {
|
|
152
|
+
await context.options.completeMessageTurn?.();
|
|
153
|
+
await context.recallThinkingMessage();
|
|
154
|
+
}
|
|
133
155
|
}
|
|
134
156
|
middleware(name, middleware, ctx = this.ctx) {
|
|
135
157
|
const result = new ChainMiddleware(name, middleware, this._graph);
|
|
@@ -165,8 +187,10 @@ var ChatChain = class {
|
|
|
165
187
|
return false;
|
|
166
188
|
}
|
|
167
189
|
if (result.status === "error") {
|
|
190
|
+
context.options.error = result.error;
|
|
168
191
|
await this._handleMiddlewareError(
|
|
169
192
|
session,
|
|
193
|
+
context,
|
|
170
194
|
result.middlewareName,
|
|
171
195
|
result.error
|
|
172
196
|
);
|
|
@@ -184,7 +208,7 @@ var ChatChain = class {
|
|
|
184
208
|
logger.debug("-".repeat(40) + "\n");
|
|
185
209
|
}
|
|
186
210
|
if (context.message != null && context.message !== originMessage) {
|
|
187
|
-
await
|
|
211
|
+
await context.send(context.message);
|
|
188
212
|
}
|
|
189
213
|
return true;
|
|
190
214
|
}
|
|
@@ -253,7 +277,7 @@ var ChatChain = class {
|
|
|
253
277
|
const shouldLogTime = !middleware.name.startsWith("lifecycle-") && result !== 0 /* SKIPPED */ && middleware.name !== "allow_reply" && executionTime > 100;
|
|
254
278
|
if (shouldLogTime) {
|
|
255
279
|
logger.debug(
|
|
256
|
-
`
|
|
280
|
+
`[Middleware] %c completed in %c`,
|
|
257
281
|
middleware.name,
|
|
258
282
|
formatDuration(executionTime)
|
|
259
283
|
);
|
|
@@ -288,29 +312,28 @@ var ChatChain = class {
|
|
|
288
312
|
}
|
|
289
313
|
async _handleStopStatus(session, context, originMessage, isOutputLog) {
|
|
290
314
|
if (context.message != null && context.message !== originMessage) {
|
|
291
|
-
await
|
|
315
|
+
await context.send(context.message);
|
|
292
316
|
}
|
|
293
317
|
if (isOutputLog) {
|
|
294
318
|
logger.debug("-".repeat(40) + "\n");
|
|
295
319
|
}
|
|
296
320
|
}
|
|
297
|
-
async _handleMiddlewareError(session, middlewareName, error) {
|
|
321
|
+
async _handleMiddlewareError(session, context, middlewareName, error) {
|
|
322
|
+
if (context.options.invocation != null) return;
|
|
298
323
|
if (error instanceof import_error.ChatLunaError) {
|
|
299
324
|
const message = error.errorCode === import_error.ChatLunaErrorCode.ABORTED ? session.text("chatluna.aborted") : error.message;
|
|
300
|
-
await
|
|
325
|
+
await context.send(message);
|
|
301
326
|
return;
|
|
302
327
|
}
|
|
303
328
|
logger.error(`chat-chain: ${middlewareName} error ${error}`);
|
|
304
329
|
logger.error(error);
|
|
305
330
|
error.cause && logger.error(error.cause);
|
|
306
331
|
logger.debug("-".repeat(40) + "\n");
|
|
307
|
-
await
|
|
308
|
-
session,
|
|
332
|
+
await context.send(
|
|
309
333
|
session.text("chatluna.middleware_error", [
|
|
310
334
|
middlewareName,
|
|
311
335
|
error.message
|
|
312
|
-
])
|
|
313
|
-
void 0
|
|
336
|
+
])
|
|
314
337
|
);
|
|
315
338
|
}
|
|
316
339
|
};
|
|
@@ -720,6 +743,9 @@ var ChainMiddleware = class {
|
|
|
720
743
|
this.execute = execute;
|
|
721
744
|
this.graph = graph;
|
|
722
745
|
}
|
|
746
|
+
name;
|
|
747
|
+
execute;
|
|
748
|
+
graph;
|
|
723
749
|
static {
|
|
724
750
|
__name(this, "ChainMiddleware");
|
|
725
751
|
}
|
|
@@ -739,6 +765,7 @@ var DefaultChatChainSender = class {
|
|
|
739
765
|
constructor(config) {
|
|
740
766
|
this.config = config;
|
|
741
767
|
}
|
|
768
|
+
config;
|
|
742
769
|
static {
|
|
743
770
|
__name(this, "DefaultChatChainSender");
|
|
744
771
|
}
|
|
@@ -771,17 +798,14 @@ var DefaultChatChainSender = class {
|
|
|
771
798
|
}
|
|
772
799
|
async sendAsQQMarkdown(session, message) {
|
|
773
800
|
const { user } = session.event;
|
|
774
|
-
await session
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
markdown: {
|
|
781
|
-
content: message.attrs["content"]
|
|
782
|
-
}
|
|
801
|
+
await session["bot"].internal.sendPrivateMessage(user.id, {
|
|
802
|
+
msg_type: 2,
|
|
803
|
+
msg_seq: 1,
|
|
804
|
+
msg_id: session.messageId,
|
|
805
|
+
markdown: {
|
|
806
|
+
content: message.attrs["content"]
|
|
783
807
|
}
|
|
784
|
-
);
|
|
808
|
+
});
|
|
785
809
|
}
|
|
786
810
|
async sendAsForward(session, messages) {
|
|
787
811
|
const sendMessages = this.convertToForwardMessages(messages);
|
package/lib/chains/index.mjs
CHANGED
|
@@ -53,6 +53,8 @@ var ChatChain = class {
|
|
|
53
53
|
(session, messages, context) => defaultChatChainSender.send(session, messages, context)
|
|
54
54
|
);
|
|
55
55
|
}
|
|
56
|
+
ctx;
|
|
57
|
+
config;
|
|
56
58
|
static {
|
|
57
59
|
__name(this, "ChatChain");
|
|
58
60
|
}
|
|
@@ -65,6 +67,7 @@ var ChatChain = class {
|
|
|
65
67
|
const timeoutObj = context.options.thinkingTimeoutObject;
|
|
66
68
|
clearTimeout(timeoutObj.timeout);
|
|
67
69
|
timeoutObj.autoRecallTimeout && clearTimeout(timeoutObj.autoRecallTimeout);
|
|
70
|
+
timeoutObj.setQueueCount(0);
|
|
68
71
|
timeoutObj.recallFunc && await timeoutObj.recallFunc();
|
|
69
72
|
timeoutObj.timeout = null;
|
|
70
73
|
context.options.thinkingTimeoutObject = void 0;
|
|
@@ -85,18 +88,33 @@ var ChatChain = class {
|
|
|
85
88
|
)
|
|
86
89
|
};
|
|
87
90
|
context.recallThinkingMessage = this._createRecallThinkingMessage(context);
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
+
try {
|
|
92
|
+
return await this._runMiddleware(session, context);
|
|
93
|
+
} finally {
|
|
94
|
+
await context.options.completeMessageTurn?.();
|
|
95
|
+
await context.recallThinkingMessage();
|
|
96
|
+
}
|
|
91
97
|
}
|
|
92
98
|
async receiveCommand(session, command, options = {}, ctx = this.ctx) {
|
|
99
|
+
return (await this.runCommand(session, command, options, ctx)).ok;
|
|
100
|
+
}
|
|
101
|
+
async runCommand(session, command, options = {}, ctx = this.ctx) {
|
|
93
102
|
const context = {
|
|
94
103
|
config: this.config,
|
|
95
104
|
message: options?.message ?? session.content,
|
|
96
105
|
ctx,
|
|
97
106
|
session,
|
|
98
107
|
command,
|
|
99
|
-
send: /* @__PURE__ */ __name((message) =>
|
|
108
|
+
send: /* @__PURE__ */ __name(async (message) => {
|
|
109
|
+
if (options.invocation?.delivery === "silent" || options.invocation?.delivery === "capture") {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
await this.sendMessage(
|
|
113
|
+
options.deliverySession ?? session,
|
|
114
|
+
message,
|
|
115
|
+
context
|
|
116
|
+
);
|
|
117
|
+
}, "send"),
|
|
100
118
|
recallThinkingMessage: this._createRecallThinkingMessage(
|
|
101
119
|
{}
|
|
102
120
|
),
|
|
@@ -106,9 +124,13 @@ var ChatChain = class {
|
|
|
106
124
|
}
|
|
107
125
|
};
|
|
108
126
|
context.recallThinkingMessage = this._createRecallThinkingMessage(context);
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
127
|
+
try {
|
|
128
|
+
const ok = await this._runMiddleware(session, context);
|
|
129
|
+
return { ok, context };
|
|
130
|
+
} finally {
|
|
131
|
+
await context.options.completeMessageTurn?.();
|
|
132
|
+
await context.recallThinkingMessage();
|
|
133
|
+
}
|
|
112
134
|
}
|
|
113
135
|
middleware(name, middleware, ctx = this.ctx) {
|
|
114
136
|
const result = new ChainMiddleware(name, middleware, this._graph);
|
|
@@ -144,8 +166,10 @@ var ChatChain = class {
|
|
|
144
166
|
return false;
|
|
145
167
|
}
|
|
146
168
|
if (result.status === "error") {
|
|
169
|
+
context.options.error = result.error;
|
|
147
170
|
await this._handleMiddlewareError(
|
|
148
171
|
session,
|
|
172
|
+
context,
|
|
149
173
|
result.middlewareName,
|
|
150
174
|
result.error
|
|
151
175
|
);
|
|
@@ -163,7 +187,7 @@ var ChatChain = class {
|
|
|
163
187
|
logger.debug("-".repeat(40) + "\n");
|
|
164
188
|
}
|
|
165
189
|
if (context.message != null && context.message !== originMessage) {
|
|
166
|
-
await
|
|
190
|
+
await context.send(context.message);
|
|
167
191
|
}
|
|
168
192
|
return true;
|
|
169
193
|
}
|
|
@@ -232,7 +256,7 @@ var ChatChain = class {
|
|
|
232
256
|
const shouldLogTime = !middleware.name.startsWith("lifecycle-") && result !== 0 /* SKIPPED */ && middleware.name !== "allow_reply" && executionTime > 100;
|
|
233
257
|
if (shouldLogTime) {
|
|
234
258
|
logger.debug(
|
|
235
|
-
`
|
|
259
|
+
`[Middleware] %c completed in %c`,
|
|
236
260
|
middleware.name,
|
|
237
261
|
formatDuration(executionTime)
|
|
238
262
|
);
|
|
@@ -267,29 +291,28 @@ var ChatChain = class {
|
|
|
267
291
|
}
|
|
268
292
|
async _handleStopStatus(session, context, originMessage, isOutputLog) {
|
|
269
293
|
if (context.message != null && context.message !== originMessage) {
|
|
270
|
-
await
|
|
294
|
+
await context.send(context.message);
|
|
271
295
|
}
|
|
272
296
|
if (isOutputLog) {
|
|
273
297
|
logger.debug("-".repeat(40) + "\n");
|
|
274
298
|
}
|
|
275
299
|
}
|
|
276
|
-
async _handleMiddlewareError(session, middlewareName, error) {
|
|
300
|
+
async _handleMiddlewareError(session, context, middlewareName, error) {
|
|
301
|
+
if (context.options.invocation != null) return;
|
|
277
302
|
if (error instanceof ChatLunaError) {
|
|
278
303
|
const message = error.errorCode === ChatLunaErrorCode.ABORTED ? session.text("chatluna.aborted") : error.message;
|
|
279
|
-
await
|
|
304
|
+
await context.send(message);
|
|
280
305
|
return;
|
|
281
306
|
}
|
|
282
307
|
logger.error(`chat-chain: ${middlewareName} error ${error}`);
|
|
283
308
|
logger.error(error);
|
|
284
309
|
error.cause && logger.error(error.cause);
|
|
285
310
|
logger.debug("-".repeat(40) + "\n");
|
|
286
|
-
await
|
|
287
|
-
session,
|
|
311
|
+
await context.send(
|
|
288
312
|
session.text("chatluna.middleware_error", [
|
|
289
313
|
middlewareName,
|
|
290
314
|
error.message
|
|
291
|
-
])
|
|
292
|
-
void 0
|
|
315
|
+
])
|
|
293
316
|
);
|
|
294
317
|
}
|
|
295
318
|
};
|
|
@@ -699,6 +722,9 @@ var ChainMiddleware = class {
|
|
|
699
722
|
this.execute = execute;
|
|
700
723
|
this.graph = graph;
|
|
701
724
|
}
|
|
725
|
+
name;
|
|
726
|
+
execute;
|
|
727
|
+
graph;
|
|
702
728
|
static {
|
|
703
729
|
__name(this, "ChainMiddleware");
|
|
704
730
|
}
|
|
@@ -718,6 +744,7 @@ var DefaultChatChainSender = class {
|
|
|
718
744
|
constructor(config) {
|
|
719
745
|
this.config = config;
|
|
720
746
|
}
|
|
747
|
+
config;
|
|
721
748
|
static {
|
|
722
749
|
__name(this, "DefaultChatChainSender");
|
|
723
750
|
}
|
|
@@ -750,17 +777,14 @@ var DefaultChatChainSender = class {
|
|
|
750
777
|
}
|
|
751
778
|
async sendAsQQMarkdown(session, message) {
|
|
752
779
|
const { user } = session.event;
|
|
753
|
-
await session
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
markdown: {
|
|
760
|
-
content: message.attrs["content"]
|
|
761
|
-
}
|
|
780
|
+
await session["bot"].internal.sendPrivateMessage(user.id, {
|
|
781
|
+
msg_type: 2,
|
|
782
|
+
msg_seq: 1,
|
|
783
|
+
msg_id: session.messageId,
|
|
784
|
+
markdown: {
|
|
785
|
+
content: message.attrs["content"]
|
|
762
786
|
}
|
|
763
|
-
);
|
|
787
|
+
});
|
|
764
788
|
}
|
|
765
789
|
async sendAsForward(session, messages) {
|
|
766
790
|
const sendMessages = this.convertToForwardMessages(messages);
|
|
@@ -18,6 +18,7 @@ declare module '../chains/chain' {
|
|
|
18
18
|
};
|
|
19
19
|
conversation_use?: {
|
|
20
20
|
model?: string;
|
|
21
|
+
auto?: boolean;
|
|
21
22
|
preset?: string;
|
|
22
23
|
chatMode?: string;
|
|
23
24
|
};
|
|
@@ -30,6 +31,10 @@ declare module '../chains/chain' {
|
|
|
30
31
|
force?: boolean;
|
|
31
32
|
newOnly?: boolean;
|
|
32
33
|
clear?: boolean;
|
|
34
|
+
auto?: boolean;
|
|
35
|
+
};
|
|
36
|
+
conversation_compress?: {
|
|
37
|
+
instruction?: string;
|
|
33
38
|
};
|
|
34
39
|
i18n_base?: string;
|
|
35
40
|
}
|
package/lib/config.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export interface Config {
|
|
|
10
10
|
randomReplyFrequency: Computed<Awaitable<number>>;
|
|
11
11
|
includeQuoteReply: boolean;
|
|
12
12
|
attachForwardMsgIdToContext: boolean;
|
|
13
|
+
autoUpdateConversationModel: boolean;
|
|
13
14
|
isLog: boolean;
|
|
14
15
|
isReplyWithAt: boolean;
|
|
15
16
|
replyQuoteThreshold?: number;
|
|
@@ -20,7 +21,7 @@ export interface Config {
|
|
|
20
21
|
sendThinkingMessage: boolean;
|
|
21
22
|
sendThinkingMessageTimeout: number;
|
|
22
23
|
showThoughtMessage: boolean;
|
|
23
|
-
splitMessage:
|
|
24
|
+
splitMessage: 'none' | 'punctuation' | 'paragraph';
|
|
24
25
|
blackList: Computed<Awaitable<number>>;
|
|
25
26
|
censor: boolean;
|
|
26
27
|
autoArchive: boolean;
|
|
@@ -29,6 +30,7 @@ export interface Config {
|
|
|
29
30
|
autoPurgeArchiveTimeout: number;
|
|
30
31
|
messageQueue: boolean;
|
|
31
32
|
messageQueueDelay: number;
|
|
33
|
+
agentTaskAutoWakeup: boolean;
|
|
32
34
|
infiniteContext: boolean;
|
|
33
35
|
infiniteContextThreshold: number;
|
|
34
36
|
rawOnCensor: boolean;
|
|
@@ -41,6 +43,7 @@ export interface Config {
|
|
|
41
43
|
defaultChatMode: string;
|
|
42
44
|
defaultModel: string;
|
|
43
45
|
defaultPreset: string;
|
|
46
|
+
enablePresetKeywordTrigger: boolean;
|
|
44
47
|
voiceSpeakId: number;
|
|
45
48
|
enableSimilarityCheck: boolean;
|
|
46
49
|
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { AIMessage } from '@langchain/core/messages';
|
|
2
|
+
import type { Session } from 'koishi';
|
|
3
|
+
export type ConversationStatus = 'active' | 'archived' | 'deleted' | 'broken';
|
|
4
|
+
export type RouteMode = 'personal' | 'shared' | 'custom';
|
|
5
|
+
export type ConstraintManageMode = 'anyone' | 'admin';
|
|
6
|
+
export type ConstraintPrincipalType = 'user' | 'guild';
|
|
7
|
+
export type ConstraintPermission = 'view' | 'manage';
|
|
8
|
+
export type ArchiveState = 'ready' | 'restoring' | 'broken';
|
|
9
|
+
export interface ConversationCompressionRecord {
|
|
10
|
+
count?: number;
|
|
11
|
+
summary?: string;
|
|
12
|
+
compressedAt?: Date | string | null;
|
|
13
|
+
originalMessageCount?: number;
|
|
14
|
+
remainingMessageCount?: number;
|
|
15
|
+
tokenUsage?: number;
|
|
16
|
+
inputTokens?: number;
|
|
17
|
+
outputTokens?: number;
|
|
18
|
+
reducedTokens?: number;
|
|
19
|
+
reducedPercent?: number;
|
|
20
|
+
[key: string]: unknown;
|
|
21
|
+
}
|
|
22
|
+
export interface ConversationRecord {
|
|
23
|
+
id: string;
|
|
24
|
+
seq?: number;
|
|
25
|
+
bindingKey: string;
|
|
26
|
+
title: string;
|
|
27
|
+
model: string;
|
|
28
|
+
preset: string;
|
|
29
|
+
chatMode: string;
|
|
30
|
+
createdBy: string;
|
|
31
|
+
createdAt: Date;
|
|
32
|
+
updatedAt: Date;
|
|
33
|
+
lastChatAt?: Date | null;
|
|
34
|
+
status: ConversationStatus;
|
|
35
|
+
latestMessageId?: string | null;
|
|
36
|
+
additional_kwargs?: string | null;
|
|
37
|
+
compression?: string | null;
|
|
38
|
+
archivedAt?: Date | null;
|
|
39
|
+
archiveId?: string | null;
|
|
40
|
+
legacyRoomId?: number | null;
|
|
41
|
+
legacyMeta?: string | null;
|
|
42
|
+
autoTitle?: boolean | null;
|
|
43
|
+
}
|
|
44
|
+
export interface ConversationListEntry {
|
|
45
|
+
conversation: ConversationRecord;
|
|
46
|
+
displaySeq: number;
|
|
47
|
+
}
|
|
48
|
+
export interface MessageRecord {
|
|
49
|
+
id: string;
|
|
50
|
+
conversationId: string;
|
|
51
|
+
parentId?: string | null;
|
|
52
|
+
role: string;
|
|
53
|
+
text?: string | null;
|
|
54
|
+
content?: ArrayBuffer | null;
|
|
55
|
+
name?: string | null;
|
|
56
|
+
tool_call_id?: string | null;
|
|
57
|
+
tool_calls?: AIMessage['tool_calls'];
|
|
58
|
+
additional_kwargs_binary?: ArrayBuffer | null;
|
|
59
|
+
response_metadata_binary?: ArrayBuffer | null;
|
|
60
|
+
rawId?: string | null;
|
|
61
|
+
createdAt?: Date | null;
|
|
62
|
+
}
|
|
63
|
+
export interface ChatLunaMessageMeta {
|
|
64
|
+
recordId?: string;
|
|
65
|
+
createdAt?: string;
|
|
66
|
+
source?: 'user';
|
|
67
|
+
}
|
|
68
|
+
export interface BindingRecord {
|
|
69
|
+
bindingKey: string;
|
|
70
|
+
activeConversationId?: string | null;
|
|
71
|
+
lastConversationId?: string | null;
|
|
72
|
+
updatedAt: Date;
|
|
73
|
+
}
|
|
74
|
+
export interface ConstraintRecord {
|
|
75
|
+
id?: number;
|
|
76
|
+
name: string;
|
|
77
|
+
enabled: boolean;
|
|
78
|
+
priority: number;
|
|
79
|
+
createdBy: string;
|
|
80
|
+
createdAt: Date;
|
|
81
|
+
updatedAt: Date;
|
|
82
|
+
platform?: string | null;
|
|
83
|
+
selfId?: string | null;
|
|
84
|
+
guildId?: string | null;
|
|
85
|
+
channelId?: string | null;
|
|
86
|
+
direct?: boolean | null;
|
|
87
|
+
users?: string | null;
|
|
88
|
+
excludeUsers?: string | null;
|
|
89
|
+
routeMode?: RouteMode | null;
|
|
90
|
+
routeKey?: string | null;
|
|
91
|
+
activePresetLane?: string | null;
|
|
92
|
+
defaultModel?: string | null;
|
|
93
|
+
defaultPreset?: string | null;
|
|
94
|
+
defaultChatMode?: string | null;
|
|
95
|
+
fixedModel?: string | null;
|
|
96
|
+
fixedPreset?: string | null;
|
|
97
|
+
fixedChatMode?: string | null;
|
|
98
|
+
lockConversation?: boolean | null;
|
|
99
|
+
allowNew?: boolean | null;
|
|
100
|
+
allowSwitch?: boolean | null;
|
|
101
|
+
allowArchive?: boolean | null;
|
|
102
|
+
allowExport?: boolean | null;
|
|
103
|
+
manageMode?: ConstraintManageMode | null;
|
|
104
|
+
}
|
|
105
|
+
export interface ArchiveRecord {
|
|
106
|
+
id: string;
|
|
107
|
+
conversationId: string;
|
|
108
|
+
path: string;
|
|
109
|
+
formatVersion: number;
|
|
110
|
+
messageCount: number;
|
|
111
|
+
checksum?: string | null;
|
|
112
|
+
size: number;
|
|
113
|
+
state: ArchiveState;
|
|
114
|
+
createdAt: Date;
|
|
115
|
+
restoredAt?: Date | null;
|
|
116
|
+
}
|
|
117
|
+
export interface ACLRecord {
|
|
118
|
+
conversationId: string;
|
|
119
|
+
principalType: ConstraintPrincipalType;
|
|
120
|
+
principalId: string;
|
|
121
|
+
permission: ConstraintPermission;
|
|
122
|
+
}
|
|
123
|
+
export interface MetaRecord {
|
|
124
|
+
key: string;
|
|
125
|
+
value?: string | null;
|
|
126
|
+
updatedAt: Date;
|
|
127
|
+
}
|
|
128
|
+
export interface ResolvedConstraint {
|
|
129
|
+
routeMode: RouteMode;
|
|
130
|
+
bindingKey: string;
|
|
131
|
+
baseKey: string;
|
|
132
|
+
constraints: ConstraintRecord[];
|
|
133
|
+
activePresetLane?: string | null;
|
|
134
|
+
defaultModel?: string | null;
|
|
135
|
+
defaultPreset?: string | null;
|
|
136
|
+
defaultChatMode?: string | null;
|
|
137
|
+
fixedModel?: string | null;
|
|
138
|
+
fixedPreset?: string | null;
|
|
139
|
+
fixedChatMode?: string | null;
|
|
140
|
+
lockConversation: boolean;
|
|
141
|
+
allowNew: boolean;
|
|
142
|
+
allowSwitch: boolean;
|
|
143
|
+
allowArchive: boolean;
|
|
144
|
+
allowExport: boolean;
|
|
145
|
+
manageMode: ConstraintManageMode;
|
|
146
|
+
}
|
|
147
|
+
export interface ResolvedConversationContext {
|
|
148
|
+
bindingKey: string;
|
|
149
|
+
presetLane?: string;
|
|
150
|
+
conversation?: ConversationRecord | null;
|
|
151
|
+
binding?: BindingRecord | null;
|
|
152
|
+
effectiveModel?: string | null;
|
|
153
|
+
effectivePreset?: string | null;
|
|
154
|
+
effectiveChatMode?: string | null;
|
|
155
|
+
constraint: ResolvedConstraint;
|
|
156
|
+
}
|
|
157
|
+
export type ConversationResolveMode = 'context' | 'active' | 'target';
|
|
158
|
+
export type ConversationResolutionErrorCode = 'ambiguous_target' | 'target_outside_route';
|
|
159
|
+
export declare class ConversationResolutionError extends Error {
|
|
160
|
+
readonly code: ConversationResolutionErrorCode;
|
|
161
|
+
constructor(code: ConversationResolutionErrorCode);
|
|
162
|
+
}
|
|
163
|
+
export declare class ConversationNotFoundError extends Error {
|
|
164
|
+
constructor();
|
|
165
|
+
}
|
|
166
|
+
export type ConstraintAction = 'create' | 'switch' | 'rename' | 'delete' | 'archive' | 'restore' | 'export' | 'update' | 'compress';
|
|
167
|
+
export declare class ConstraintLockedError extends Error {
|
|
168
|
+
readonly action: ConstraintAction;
|
|
169
|
+
constructor(action: ConstraintAction);
|
|
170
|
+
}
|
|
171
|
+
export declare class ConstraintDisabledError extends Error {
|
|
172
|
+
readonly action: ConstraintAction;
|
|
173
|
+
constructor(action: ConstraintAction);
|
|
174
|
+
}
|
|
175
|
+
export type ConstraintFixedField = 'model' | 'preset' | 'chatMode';
|
|
176
|
+
export declare class ConstraintFixedError extends Error {
|
|
177
|
+
readonly field: ConstraintFixedField;
|
|
178
|
+
readonly value: string;
|
|
179
|
+
constructor(field: ConstraintFixedField, value: string);
|
|
180
|
+
}
|
|
181
|
+
export declare class InvalidChatModeError extends Error {
|
|
182
|
+
readonly mode: string;
|
|
183
|
+
constructor(mode: string);
|
|
184
|
+
}
|
|
185
|
+
export declare class AdminRequiredError extends Error {
|
|
186
|
+
constructor();
|
|
187
|
+
}
|
|
188
|
+
export interface ResolveConversationOptions {
|
|
189
|
+
mode?: ConversationResolveMode;
|
|
190
|
+
presetLane?: string;
|
|
191
|
+
conversationId?: string;
|
|
192
|
+
bindingKey?: string;
|
|
193
|
+
useRoutePresetLane?: boolean;
|
|
194
|
+
targetConversation?: string;
|
|
195
|
+
includeArchived?: boolean;
|
|
196
|
+
permission?: ConstraintPermission;
|
|
197
|
+
allPresetLanes?: boolean;
|
|
198
|
+
}
|
|
199
|
+
export interface ConversationResolution extends ResolvedConversationContext {
|
|
200
|
+
mode: ConversationResolveMode;
|
|
201
|
+
conversationId: string | null;
|
|
202
|
+
}
|
|
203
|
+
export declare function getBaseBindingKey(bindingKey: string): string;
|
|
204
|
+
export declare function getPresetLane(bindingKey: string): string;
|
|
205
|
+
export declare function computeBaseBindingKey(session: Session, routeMode: RouteMode, routeKey?: string | null): string;
|
|
206
|
+
export declare function applyPresetLane(bindingKey: string, presetLane?: string): string;
|