koishi-plugin-chatluna 1.1.2 → 1.1.3
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 +1 -1
- package/lib/index.cjs +35 -33
- package/lib/index.mjs +35 -33
- package/lib/llm-core/agent/openai/index.d.ts +1 -1
- package/lib/llm-core/chain/prompt.cjs +7 -1
- package/lib/llm-core/chain/prompt.mjs +7 -1
- package/lib/llm-core/memory/message/database_history.d.ts +1 -0
- package/lib/llm-core/memory/message/index.cjs +10 -4
- package/lib/llm-core/memory/message/index.mjs +14 -4
- package/lib/services/chat.cjs +14 -2
- package/lib/services/chat.mjs +14 -2
- package/lib/utils/string.cjs +29 -0
- package/lib/utils/string.d.ts +6 -0
- package/lib/utils/string.mjs +26 -0
- package/package.json +1 -1
package/lib/chains/chain.d.ts
CHANGED
|
@@ -43,7 +43,7 @@ export declare class ChainMiddleware {
|
|
|
43
43
|
constructor(name: string, execute: ChainMiddlewareFunction, graph: ChatChainDependencyGraph);
|
|
44
44
|
before<T extends keyof ChainMiddlewareName>(name: T): this;
|
|
45
45
|
after<T extends keyof ChainMiddlewareName>(name: T): this;
|
|
46
|
-
run(session: Session, options: ChainMiddlewareContext): Promise<string | h[] |
|
|
46
|
+
run(session: Session, options: ChainMiddlewareContext): Promise<string | h[] | h[][] | ChainMiddlewareRunStatus>;
|
|
47
47
|
}
|
|
48
48
|
export interface ChainMiddlewareContext {
|
|
49
49
|
config: Config;
|
package/lib/index.cjs
CHANGED
|
@@ -594,42 +594,44 @@ __name(apply2, "apply");
|
|
|
594
594
|
|
|
595
595
|
// src/commands/memory.ts
|
|
596
596
|
function apply3(ctx, config, chain) {
|
|
597
|
-
ctx.
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
597
|
+
ctx.inject(["chatluna_long_memory"], (ctx2) => {
|
|
598
|
+
ctx2.command("chatluna.memory", { authority: 1 });
|
|
599
|
+
ctx2.command("chatluna.memory.search <query:string>").option("type", "-t <type:string>").option("limit", "-l <limit:number>").option("page", "-p <page:number>").option("view", "-v <view:string>").action(async ({ options, session }, query) => {
|
|
600
|
+
await chain.receiveCommand(session, "search_memory", {
|
|
601
|
+
type: options.type,
|
|
602
|
+
page: options.page ?? 1,
|
|
603
|
+
limit: options.limit ?? 6,
|
|
604
|
+
view: options.view,
|
|
605
|
+
query
|
|
606
|
+
});
|
|
605
607
|
});
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
608
|
+
ctx2.command("chatluna.memory.delete <...ids>").option("type", "-t <type:string>").option("view", "-v <view:string>").action(async ({ session, options }, ...ids) => {
|
|
609
|
+
await chain.receiveCommand(session, "delete_memory", {
|
|
610
|
+
ids,
|
|
611
|
+
type: options.type,
|
|
612
|
+
view: options.view
|
|
613
|
+
});
|
|
612
614
|
});
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
615
|
+
ctx2.command("chatluna.memory.clear").option("type", "-t <type:string>").option("view", "-v <view:string>").action(async ({ session, options }) => {
|
|
616
|
+
await chain.receiveCommand(session, "clear_memory", {
|
|
617
|
+
type: options.type,
|
|
618
|
+
view: options.view
|
|
619
|
+
});
|
|
618
620
|
});
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
621
|
+
ctx2.command("chatluna.memory.add <content:text>").option("type", "-t <type:string>").option("view", "-v <view:string>").action(async ({ session, options }, content) => {
|
|
622
|
+
await chain.receiveCommand(session, "add_memory", {
|
|
623
|
+
type: options.type,
|
|
624
|
+
view: options.view,
|
|
625
|
+
content
|
|
626
|
+
});
|
|
625
627
|
});
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
628
|
+
ctx2.command("chatluna.memory.edit <id:string>").option("type", "-t <type:string>").option("view", "-v <view:string>").action(async ({ session, options }, id, content) => {
|
|
629
|
+
await chain.receiveCommand(session, "edit_memory", {
|
|
630
|
+
memoryId: id,
|
|
631
|
+
content,
|
|
632
|
+
type: options.type,
|
|
633
|
+
view: options.view
|
|
634
|
+
});
|
|
633
635
|
});
|
|
634
636
|
});
|
|
635
637
|
}
|
|
@@ -6018,7 +6020,7 @@ var TextRenderer = class extends Renderer {
|
|
|
6018
6020
|
return (0, import_koishi12.h)("message", element);
|
|
6019
6021
|
});
|
|
6020
6022
|
}
|
|
6021
|
-
if (transformed[0]
|
|
6023
|
+
if (transformed[0]?.type === "p") {
|
|
6022
6024
|
const pElement = transformed.shift();
|
|
6023
6025
|
const pElementContent = pElement.attrs["content"];
|
|
6024
6026
|
if (pElementContent) {
|
package/lib/index.mjs
CHANGED
|
@@ -563,42 +563,44 @@ __name(apply2, "apply");
|
|
|
563
563
|
|
|
564
564
|
// src/commands/memory.ts
|
|
565
565
|
function apply3(ctx, config, chain) {
|
|
566
|
-
ctx.
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
566
|
+
ctx.inject(["chatluna_long_memory"], (ctx2) => {
|
|
567
|
+
ctx2.command("chatluna.memory", { authority: 1 });
|
|
568
|
+
ctx2.command("chatluna.memory.search <query:string>").option("type", "-t <type:string>").option("limit", "-l <limit:number>").option("page", "-p <page:number>").option("view", "-v <view:string>").action(async ({ options, session }, query) => {
|
|
569
|
+
await chain.receiveCommand(session, "search_memory", {
|
|
570
|
+
type: options.type,
|
|
571
|
+
page: options.page ?? 1,
|
|
572
|
+
limit: options.limit ?? 6,
|
|
573
|
+
view: options.view,
|
|
574
|
+
query
|
|
575
|
+
});
|
|
574
576
|
});
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
577
|
+
ctx2.command("chatluna.memory.delete <...ids>").option("type", "-t <type:string>").option("view", "-v <view:string>").action(async ({ session, options }, ...ids) => {
|
|
578
|
+
await chain.receiveCommand(session, "delete_memory", {
|
|
579
|
+
ids,
|
|
580
|
+
type: options.type,
|
|
581
|
+
view: options.view
|
|
582
|
+
});
|
|
581
583
|
});
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
584
|
+
ctx2.command("chatluna.memory.clear").option("type", "-t <type:string>").option("view", "-v <view:string>").action(async ({ session, options }) => {
|
|
585
|
+
await chain.receiveCommand(session, "clear_memory", {
|
|
586
|
+
type: options.type,
|
|
587
|
+
view: options.view
|
|
588
|
+
});
|
|
587
589
|
});
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
590
|
+
ctx2.command("chatluna.memory.add <content:text>").option("type", "-t <type:string>").option("view", "-v <view:string>").action(async ({ session, options }, content) => {
|
|
591
|
+
await chain.receiveCommand(session, "add_memory", {
|
|
592
|
+
type: options.type,
|
|
593
|
+
view: options.view,
|
|
594
|
+
content
|
|
595
|
+
});
|
|
594
596
|
});
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
597
|
+
ctx2.command("chatluna.memory.edit <id:string>").option("type", "-t <type:string>").option("view", "-v <view:string>").action(async ({ session, options }, id, content) => {
|
|
598
|
+
await chain.receiveCommand(session, "edit_memory", {
|
|
599
|
+
memoryId: id,
|
|
600
|
+
content,
|
|
601
|
+
type: options.type,
|
|
602
|
+
view: options.view
|
|
603
|
+
});
|
|
602
604
|
});
|
|
603
605
|
});
|
|
604
606
|
}
|
|
@@ -6027,7 +6029,7 @@ var TextRenderer = class extends Renderer {
|
|
|
6027
6029
|
return h5("message", element);
|
|
6028
6030
|
});
|
|
6029
6031
|
}
|
|
6030
|
-
if (transformed[0]
|
|
6032
|
+
if (transformed[0]?.type === "p") {
|
|
6031
6033
|
const pElement = transformed.shift();
|
|
6032
6034
|
const pElementContent = pElement.attrs["content"];
|
|
6033
6035
|
if (pElementContent) {
|
|
@@ -22,4 +22,4 @@ export type CreateOpenAIAgentParams = {
|
|
|
22
22
|
};
|
|
23
23
|
export declare function createOpenAIAgent({ llm, tools, prompt }: CreateOpenAIAgentParams): RunnableSequence<{
|
|
24
24
|
steps: AgentStep[];
|
|
25
|
-
}, AgentAction | AgentAction[]
|
|
25
|
+
}, AgentAction | AgentFinish | AgentAction[]>;
|
|
@@ -62,7 +62,12 @@ var ChatLunaChatPrompt = class _ChatLunaChatPrompt extends import_prompts.BaseCh
|
|
|
62
62
|
return "chatluna_chat";
|
|
63
63
|
}
|
|
64
64
|
async _countMessageTokens(message) {
|
|
65
|
-
let
|
|
65
|
+
let content = (0, import_string.getMessageContent)(message.content);
|
|
66
|
+
if (content.includes("![image]") && content.includes("base64") && message.additional_kwargs?.["images"]) {
|
|
67
|
+
content = content.replaceAll(/!\[.*?\]\(.*?\)/g, "");
|
|
68
|
+
message.content = content;
|
|
69
|
+
}
|
|
70
|
+
let result = await this.tokenCounter((0, import_string.getMessageContent)(message.content)) + await this.tokenCounter(
|
|
66
71
|
(0, import_count_tokens.messageTypeToOpenAIRole)(message.getType())
|
|
67
72
|
);
|
|
68
73
|
if (message.name) {
|
|
@@ -144,6 +149,7 @@ Your goal is to craft an insightful, engaging response that seamlessly integrate
|
|
|
144
149
|
usedTokens += await this._countMessageTokens(agentScratchpad);
|
|
145
150
|
}
|
|
146
151
|
}
|
|
152
|
+
console.log(chatHistory.length);
|
|
147
153
|
const formatResult = await this._formatWithMessagesPlaceholder(
|
|
148
154
|
chatHistory,
|
|
149
155
|
[longHistory, knowledge].concat(
|
|
@@ -53,7 +53,12 @@ var ChatLunaChatPrompt = class _ChatLunaChatPrompt extends BaseChatPromptTemplat
|
|
|
53
53
|
return "chatluna_chat";
|
|
54
54
|
}
|
|
55
55
|
async _countMessageTokens(message) {
|
|
56
|
-
let
|
|
56
|
+
let content = getMessageContent(message.content);
|
|
57
|
+
if (content.includes("![image]") && content.includes("base64") && message.additional_kwargs?.["images"]) {
|
|
58
|
+
content = content.replaceAll(/!\[.*?\]\(.*?\)/g, "");
|
|
59
|
+
message.content = content;
|
|
60
|
+
}
|
|
61
|
+
let result = await this.tokenCounter(getMessageContent(message.content)) + await this.tokenCounter(
|
|
57
62
|
messageTypeToOpenAIRole(message.getType())
|
|
58
63
|
);
|
|
59
64
|
if (message.name) {
|
|
@@ -135,6 +140,7 @@ Your goal is to craft an insightful, engaging response that seamlessly integrate
|
|
|
135
140
|
usedTokens += await this._countMessageTokens(agentScratchpad);
|
|
136
141
|
}
|
|
137
142
|
}
|
|
143
|
+
console.log(chatHistory.length);
|
|
138
144
|
const formatResult = await this._formatWithMessagesPlaceholder(
|
|
139
145
|
chatHistory,
|
|
140
146
|
[longHistory, knowledge].concat(
|
|
@@ -28,6 +28,7 @@ module.exports = __toCommonJS(message_exports);
|
|
|
28
28
|
var import_messages = require("@langchain/core/messages");
|
|
29
29
|
var import_uuid = require("uuid");
|
|
30
30
|
var import_chat_history = require("@langchain/core/chat_history");
|
|
31
|
+
var import_string = require("koishi-plugin-chatluna/utils/string");
|
|
31
32
|
var KoishiChatMessageHistory = class extends import_chat_history.BaseChatMessageHistory {
|
|
32
33
|
constructor(ctx, conversationId, _maxMessagesCount) {
|
|
33
34
|
super();
|
|
@@ -155,14 +156,16 @@ var KoishiChatMessageHistory = class extends import_chat_history.BaseChatMessage
|
|
|
155
156
|
await this.clear();
|
|
156
157
|
}
|
|
157
158
|
this._serializedChatHistory = sorted;
|
|
158
|
-
|
|
159
|
-
const
|
|
159
|
+
const promises = sorted.map(async (item) => {
|
|
160
|
+
const args = JSON.parse(
|
|
161
|
+
item.additional_kwargs_binary ? await (0, import_string.gzipDecode)(item.additional_kwargs_binary) : item.additional_kwargs ?? "{}"
|
|
162
|
+
);
|
|
160
163
|
const content = JSON.parse(item.text);
|
|
161
164
|
const fields = {
|
|
162
165
|
content,
|
|
163
166
|
id: item.rawId ?? void 0,
|
|
164
167
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
165
|
-
additional_kwargs:
|
|
168
|
+
additional_kwargs: args
|
|
166
169
|
};
|
|
167
170
|
if (item.role === "system") {
|
|
168
171
|
return new import_messages.SystemMessage(fields);
|
|
@@ -174,6 +177,7 @@ var KoishiChatMessageHistory = class extends import_chat_history.BaseChatMessage
|
|
|
174
177
|
throw new Error("Unknown role");
|
|
175
178
|
}
|
|
176
179
|
});
|
|
180
|
+
return await Promise.all(promises);
|
|
177
181
|
}
|
|
178
182
|
async _loadConversation() {
|
|
179
183
|
const conversation = (await this._ctx.database.get("chathub_conversation", {
|
|
@@ -213,7 +217,9 @@ var KoishiChatMessageHistory = class extends import_chat_history.BaseChatMessage
|
|
|
213
217
|
text: JSON.stringify(message.content),
|
|
214
218
|
parent: lastedMessage?.id ?? null,
|
|
215
219
|
role: message.getType(),
|
|
216
|
-
|
|
220
|
+
additional_kwargs_binary: additionalArgs ? await (0, import_string.gzipEncode)(JSON.stringify(additionalArgs)).then(
|
|
221
|
+
(buf) => (0, import_string.bufferToArrayBuffer)(buf)
|
|
222
|
+
) : null,
|
|
217
223
|
rawId: message.id ?? null,
|
|
218
224
|
conversation: this.conversationId
|
|
219
225
|
};
|
|
@@ -9,6 +9,11 @@ import {
|
|
|
9
9
|
} from "@langchain/core/messages";
|
|
10
10
|
import { v4 as uuidv4 } from "uuid";
|
|
11
11
|
import { BaseChatMessageHistory } from "@langchain/core/chat_history";
|
|
12
|
+
import {
|
|
13
|
+
bufferToArrayBuffer,
|
|
14
|
+
gzipDecode,
|
|
15
|
+
gzipEncode
|
|
16
|
+
} from "koishi-plugin-chatluna/utils/string";
|
|
12
17
|
var KoishiChatMessageHistory = class extends BaseChatMessageHistory {
|
|
13
18
|
constructor(ctx, conversationId, _maxMessagesCount) {
|
|
14
19
|
super();
|
|
@@ -136,14 +141,16 @@ var KoishiChatMessageHistory = class extends BaseChatMessageHistory {
|
|
|
136
141
|
await this.clear();
|
|
137
142
|
}
|
|
138
143
|
this._serializedChatHistory = sorted;
|
|
139
|
-
|
|
140
|
-
const
|
|
144
|
+
const promises = sorted.map(async (item) => {
|
|
145
|
+
const args = JSON.parse(
|
|
146
|
+
item.additional_kwargs_binary ? await gzipDecode(item.additional_kwargs_binary) : item.additional_kwargs ?? "{}"
|
|
147
|
+
);
|
|
141
148
|
const content = JSON.parse(item.text);
|
|
142
149
|
const fields = {
|
|
143
150
|
content,
|
|
144
151
|
id: item.rawId ?? void 0,
|
|
145
152
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
146
|
-
additional_kwargs:
|
|
153
|
+
additional_kwargs: args
|
|
147
154
|
};
|
|
148
155
|
if (item.role === "system") {
|
|
149
156
|
return new SystemMessage(fields);
|
|
@@ -155,6 +162,7 @@ var KoishiChatMessageHistory = class extends BaseChatMessageHistory {
|
|
|
155
162
|
throw new Error("Unknown role");
|
|
156
163
|
}
|
|
157
164
|
});
|
|
165
|
+
return await Promise.all(promises);
|
|
158
166
|
}
|
|
159
167
|
async _loadConversation() {
|
|
160
168
|
const conversation = (await this._ctx.database.get("chathub_conversation", {
|
|
@@ -194,7 +202,9 @@ var KoishiChatMessageHistory = class extends BaseChatMessageHistory {
|
|
|
194
202
|
text: JSON.stringify(message.content),
|
|
195
203
|
parent: lastedMessage?.id ?? null,
|
|
196
204
|
role: message.getType(),
|
|
197
|
-
|
|
205
|
+
additional_kwargs_binary: additionalArgs ? await gzipEncode(JSON.stringify(additionalArgs)).then(
|
|
206
|
+
(buf) => bufferToArrayBuffer(buf)
|
|
207
|
+
) : null,
|
|
198
208
|
rawId: message.id ?? null,
|
|
199
209
|
conversation: this.conversationId
|
|
200
210
|
};
|
package/lib/services/chat.cjs
CHANGED
|
@@ -711,6 +711,14 @@ var MessageTransformer = class {
|
|
|
711
711
|
model
|
|
712
712
|
);
|
|
713
713
|
}
|
|
714
|
+
} else if (element.children) {
|
|
715
|
+
await this.transform(
|
|
716
|
+
session,
|
|
717
|
+
element.children,
|
|
718
|
+
message,
|
|
719
|
+
quote,
|
|
720
|
+
model
|
|
721
|
+
);
|
|
714
722
|
}
|
|
715
723
|
}
|
|
716
724
|
if (session.quote && !quote && this._config.includeQuoteReply) {
|
|
@@ -747,7 +755,7 @@ Please consider this quote when generating your response. User's message: ${mess
|
|
|
747
755
|
new Error("text transform function already exists")
|
|
748
756
|
);
|
|
749
757
|
}
|
|
750
|
-
if (this._transformFunctions[type] != null && !["
|
|
758
|
+
if (this._transformFunctions[type] != null && !["img"].includes(type)) {
|
|
751
759
|
import_koishi_plugin_chatluna.logger?.warn(
|
|
752
760
|
`transform function for ${type} already exists. Check your installed plugins.`
|
|
753
761
|
);
|
|
@@ -808,7 +816,7 @@ var TextRenderer = class extends Renderer {
|
|
|
808
816
|
return (0, import_koishi3.h)("message", element);
|
|
809
817
|
});
|
|
810
818
|
}
|
|
811
|
-
if (transformed[0]
|
|
819
|
+
if (transformed[0]?.type === "p") {
|
|
812
820
|
const pElement = transformed.shift();
|
|
813
821
|
const pElementContent = pElement.attrs["content"];
|
|
814
822
|
if (pElementContent) {
|
|
@@ -1366,6 +1374,10 @@ var ChatLunaService = class extends import_koishi9.Service {
|
|
|
1366
1374
|
type: "text",
|
|
1367
1375
|
nullable: true
|
|
1368
1376
|
},
|
|
1377
|
+
additional_kwargs_binary: {
|
|
1378
|
+
type: "binary",
|
|
1379
|
+
nullable: true
|
|
1380
|
+
},
|
|
1369
1381
|
rawId: {
|
|
1370
1382
|
type: "char",
|
|
1371
1383
|
length: 255,
|
package/lib/services/chat.mjs
CHANGED
|
@@ -700,6 +700,14 @@ var MessageTransformer = class {
|
|
|
700
700
|
model
|
|
701
701
|
);
|
|
702
702
|
}
|
|
703
|
+
} else if (element.children) {
|
|
704
|
+
await this.transform(
|
|
705
|
+
session,
|
|
706
|
+
element.children,
|
|
707
|
+
message,
|
|
708
|
+
quote,
|
|
709
|
+
model
|
|
710
|
+
);
|
|
703
711
|
}
|
|
704
712
|
}
|
|
705
713
|
if (session.quote && !quote && this._config.includeQuoteReply) {
|
|
@@ -736,7 +744,7 @@ Please consider this quote when generating your response. User's message: ${mess
|
|
|
736
744
|
new Error("text transform function already exists")
|
|
737
745
|
);
|
|
738
746
|
}
|
|
739
|
-
if (this._transformFunctions[type] != null && !["
|
|
747
|
+
if (this._transformFunctions[type] != null && !["img"].includes(type)) {
|
|
740
748
|
logger2?.warn(
|
|
741
749
|
`transform function for ${type} already exists. Check your installed plugins.`
|
|
742
750
|
);
|
|
@@ -800,7 +808,7 @@ var TextRenderer = class extends Renderer {
|
|
|
800
808
|
return h2("message", element);
|
|
801
809
|
});
|
|
802
810
|
}
|
|
803
|
-
if (transformed[0]
|
|
811
|
+
if (transformed[0]?.type === "p") {
|
|
804
812
|
const pElement = transformed.shift();
|
|
805
813
|
const pElementContent = pElement.attrs["content"];
|
|
806
814
|
if (pElementContent) {
|
|
@@ -1358,6 +1366,10 @@ var ChatLunaService = class extends Service {
|
|
|
1358
1366
|
type: "text",
|
|
1359
1367
|
nullable: true
|
|
1360
1368
|
},
|
|
1369
|
+
additional_kwargs_binary: {
|
|
1370
|
+
type: "binary",
|
|
1371
|
+
nullable: true
|
|
1372
|
+
},
|
|
1361
1373
|
rawId: {
|
|
1362
1374
|
type: "char",
|
|
1363
1375
|
length: 255,
|
package/lib/utils/string.cjs
CHANGED
|
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var string_exports = {};
|
|
22
22
|
__export(string_exports, {
|
|
23
23
|
PresetPostHandler: () => PresetPostHandler,
|
|
24
|
+
bufferToArrayBuffer: () => bufferToArrayBuffer,
|
|
24
25
|
fuzzyQuery: () => fuzzyQuery,
|
|
25
26
|
getCurrentWeekday: () => getCurrentWeekday,
|
|
26
27
|
getMessageContent: () => getMessageContent,
|
|
@@ -28,11 +29,17 @@ __export(string_exports, {
|
|
|
28
29
|
getTimeDiff: () => getTimeDiff,
|
|
29
30
|
getTimeDiffFormat: () => getTimeDiffFormat,
|
|
30
31
|
getTimeInUTC: () => getTimeInUTC,
|
|
32
|
+
gzipDecode: () => gzipDecode,
|
|
33
|
+
gzipEncode: () => gzipEncode,
|
|
31
34
|
rollDice: () => rollDice,
|
|
32
35
|
selectFromList: () => selectFromList
|
|
33
36
|
});
|
|
34
37
|
module.exports = __toCommonJS(string_exports);
|
|
35
38
|
var import_koishi = require("koishi");
|
|
39
|
+
var import_zlib = require("zlib");
|
|
40
|
+
var import_util = require("util");
|
|
41
|
+
var gzipAsync = (0, import_util.promisify)(import_zlib.gzip);
|
|
42
|
+
var gunzipAsync = (0, import_util.promisify)(import_zlib.gunzip);
|
|
36
43
|
function fuzzyQuery(source, keywords) {
|
|
37
44
|
for (const keyword of keywords) {
|
|
38
45
|
const match = source.includes(keyword);
|
|
@@ -185,9 +192,29 @@ var PresetPostHandler = class {
|
|
|
185
192
|
}
|
|
186
193
|
}
|
|
187
194
|
};
|
|
195
|
+
async function gzipEncode(text, encoding = "buffer") {
|
|
196
|
+
const buffer = await gzipAsync(text);
|
|
197
|
+
return encoding === "buffer" ? buffer : buffer.toString(encoding);
|
|
198
|
+
}
|
|
199
|
+
__name(gzipEncode, "gzipEncode");
|
|
200
|
+
async function gzipDecode(data, inputEncoding = "base64") {
|
|
201
|
+
const buffer = typeof data === "string" ? Buffer.from(data, inputEncoding) : data;
|
|
202
|
+
return (await gunzipAsync(buffer)).toString("utf8");
|
|
203
|
+
}
|
|
204
|
+
__name(gzipDecode, "gzipDecode");
|
|
205
|
+
function bufferToArrayBuffer(buffer) {
|
|
206
|
+
const arrayBuffer = new ArrayBuffer(buffer.length);
|
|
207
|
+
const view = new Uint8Array(arrayBuffer);
|
|
208
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
209
|
+
view[i] = buffer[i];
|
|
210
|
+
}
|
|
211
|
+
return arrayBuffer;
|
|
212
|
+
}
|
|
213
|
+
__name(bufferToArrayBuffer, "bufferToArrayBuffer");
|
|
188
214
|
// Annotate the CommonJS export names for ESM import in node:
|
|
189
215
|
0 && (module.exports = {
|
|
190
216
|
PresetPostHandler,
|
|
217
|
+
bufferToArrayBuffer,
|
|
191
218
|
fuzzyQuery,
|
|
192
219
|
getCurrentWeekday,
|
|
193
220
|
getMessageContent,
|
|
@@ -195,6 +222,8 @@ var PresetPostHandler = class {
|
|
|
195
222
|
getTimeDiff,
|
|
196
223
|
getTimeDiffFormat,
|
|
197
224
|
getTimeInUTC,
|
|
225
|
+
gzipDecode,
|
|
226
|
+
gzipEncode,
|
|
198
227
|
rollDice,
|
|
199
228
|
selectFromList
|
|
200
229
|
});
|
package/lib/utils/string.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { BaseMessage } from '@langchain/core/messages';
|
|
|
2
2
|
import type { HandlerResult, PostHandler } from './types';
|
|
3
3
|
import { Context, Session } from 'koishi';
|
|
4
4
|
import { Config } from 'koishi-plugin-chatluna';
|
|
5
|
+
type Encoding = 'buffer' | 'base64' | 'hex';
|
|
6
|
+
type BufferType<T extends Encoding> = T extends 'buffer' ? Buffer : T extends 'base64' ? string : T extends 'hex' ? string : never;
|
|
5
7
|
export declare function fuzzyQuery(source: string, keywords: string[]): boolean;
|
|
6
8
|
export declare function getMessageContent(message: BaseMessage['content']): string;
|
|
7
9
|
export declare function getNotEmptyString(...texts: (string | undefined)[]): string;
|
|
@@ -23,3 +25,7 @@ export declare class PresetPostHandler implements PostHandler {
|
|
|
23
25
|
handler(session: Session, data: string): Promise<HandlerResult>;
|
|
24
26
|
private _compileVariables;
|
|
25
27
|
}
|
|
28
|
+
export declare function gzipEncode<T extends Encoding = 'buffer'>(text: string, encoding?: T): Promise<BufferType<T>>;
|
|
29
|
+
export declare function gzipDecode(data: ArrayBuffer | Buffer | string, inputEncoding?: Encoding): Promise<string>;
|
|
30
|
+
export declare function bufferToArrayBuffer(buffer: Buffer): ArrayBuffer;
|
|
31
|
+
export {};
|
package/lib/utils/string.mjs
CHANGED
|
@@ -3,6 +3,10 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
|
|
|
3
3
|
|
|
4
4
|
// src/utils/string.ts
|
|
5
5
|
import { h } from "koishi";
|
|
6
|
+
import { gunzip, gzip } from "zlib";
|
|
7
|
+
import { promisify } from "util";
|
|
8
|
+
var gzipAsync = promisify(gzip);
|
|
9
|
+
var gunzipAsync = promisify(gunzip);
|
|
6
10
|
function fuzzyQuery(source, keywords) {
|
|
7
11
|
for (const keyword of keywords) {
|
|
8
12
|
const match = source.includes(keyword);
|
|
@@ -155,8 +159,28 @@ var PresetPostHandler = class {
|
|
|
155
159
|
}
|
|
156
160
|
}
|
|
157
161
|
};
|
|
162
|
+
async function gzipEncode(text, encoding = "buffer") {
|
|
163
|
+
const buffer = await gzipAsync(text);
|
|
164
|
+
return encoding === "buffer" ? buffer : buffer.toString(encoding);
|
|
165
|
+
}
|
|
166
|
+
__name(gzipEncode, "gzipEncode");
|
|
167
|
+
async function gzipDecode(data, inputEncoding = "base64") {
|
|
168
|
+
const buffer = typeof data === "string" ? Buffer.from(data, inputEncoding) : data;
|
|
169
|
+
return (await gunzipAsync(buffer)).toString("utf8");
|
|
170
|
+
}
|
|
171
|
+
__name(gzipDecode, "gzipDecode");
|
|
172
|
+
function bufferToArrayBuffer(buffer) {
|
|
173
|
+
const arrayBuffer = new ArrayBuffer(buffer.length);
|
|
174
|
+
const view = new Uint8Array(arrayBuffer);
|
|
175
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
176
|
+
view[i] = buffer[i];
|
|
177
|
+
}
|
|
178
|
+
return arrayBuffer;
|
|
179
|
+
}
|
|
180
|
+
__name(bufferToArrayBuffer, "bufferToArrayBuffer");
|
|
158
181
|
export {
|
|
159
182
|
PresetPostHandler,
|
|
183
|
+
bufferToArrayBuffer,
|
|
160
184
|
fuzzyQuery,
|
|
161
185
|
getCurrentWeekday,
|
|
162
186
|
getMessageContent,
|
|
@@ -164,6 +188,8 @@ export {
|
|
|
164
188
|
getTimeDiff,
|
|
165
189
|
getTimeDiffFormat,
|
|
166
190
|
getTimeInUTC,
|
|
191
|
+
gzipDecode,
|
|
192
|
+
gzipEncode,
|
|
167
193
|
rollDice,
|
|
168
194
|
selectFromList
|
|
169
195
|
};
|