koishi-plugin-chatluna-toolbox 0.0.2 → 0.0.5
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/index.js +61 -8
- package/package.json +1 -6
package/lib/index.js
CHANGED
|
@@ -607,9 +607,12 @@ function createXmlInterceptor(deps) {
|
|
|
607
607
|
let currentGuildId = null;
|
|
608
608
|
let lastSession = null;
|
|
609
609
|
const pendingSessions = [];
|
|
610
|
+
let responseSeq = 0;
|
|
611
|
+
const processedResponses = /* @__PURE__ */ new Set();
|
|
610
612
|
let monitorHandle = null;
|
|
611
613
|
let fastRetryHandle = null;
|
|
612
614
|
let startupHandle = null;
|
|
615
|
+
const responseRetryHandles = /* @__PURE__ */ new Map();
|
|
613
616
|
let activeService = null;
|
|
614
617
|
let activeLogger = null;
|
|
615
618
|
let originalDebug = null;
|
|
@@ -658,6 +661,45 @@ function createXmlInterceptor(deps) {
|
|
|
658
661
|
}
|
|
659
662
|
return resolved.key ? sessionMap.get(resolved.key) || resolved.session : resolved.session;
|
|
660
663
|
};
|
|
664
|
+
const markProcessed = (responseId) => {
|
|
665
|
+
processedResponses.add(responseId);
|
|
666
|
+
if (processedResponses.size > 100) {
|
|
667
|
+
const first = processedResponses.values().next().value;
|
|
668
|
+
if (first) processedResponses.delete(first);
|
|
669
|
+
}
|
|
670
|
+
};
|
|
671
|
+
const runResponse = (responseId, response, session) => {
|
|
672
|
+
const handled = onResponse(response, session);
|
|
673
|
+
if (handled) {
|
|
674
|
+
markProcessed(responseId);
|
|
675
|
+
const retryHandle = responseRetryHandles.get(responseId);
|
|
676
|
+
if (retryHandle) {
|
|
677
|
+
retryHandle();
|
|
678
|
+
responseRetryHandles.delete(responseId);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
return handled;
|
|
682
|
+
};
|
|
683
|
+
const retryResponse = (responseId, response) => {
|
|
684
|
+
if (responseRetryHandles.has(responseId) || processedResponses.has(responseId))
|
|
685
|
+
return;
|
|
686
|
+
const handle = ctx.setTimeout(() => {
|
|
687
|
+
responseRetryHandles.delete(responseId);
|
|
688
|
+
if (processedResponses.has(responseId)) return;
|
|
689
|
+
const session = resolveSession();
|
|
690
|
+
if (!session) {
|
|
691
|
+
if (config.debugLogging) {
|
|
692
|
+
log?.("warn", "\u62E6\u622A\u5230\u539F\u59CB\u8F93\u51FA\u4F46\u7F3A\u5C11\u4F1A\u8BDD\u4E0A\u4E0B\u6587\uFF0CXML \u5DE5\u5177\u4E0D\u4F1A\u6267\u884C", {
|
|
693
|
+
length: response.length
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
runResponse(responseId, response, null);
|
|
697
|
+
return;
|
|
698
|
+
}
|
|
699
|
+
runResponse(responseId, response, session);
|
|
700
|
+
}, 200);
|
|
701
|
+
responseRetryHandles.set(responseId, handle);
|
|
702
|
+
};
|
|
661
703
|
const attach = () => {
|
|
662
704
|
const characterService = ctx.chatluna_character;
|
|
663
705
|
if (!characterService) return false;
|
|
@@ -685,17 +727,20 @@ function createXmlInterceptor(deps) {
|
|
|
685
727
|
const wrapped = (...args) => {
|
|
686
728
|
raw(...args);
|
|
687
729
|
const message = args[0];
|
|
688
|
-
if (typeof message !== "string"
|
|
689
|
-
|
|
690
|
-
|
|
730
|
+
if (typeof message !== "string") return;
|
|
731
|
+
const prefix = ["model response: ", "model response:\n"].find(
|
|
732
|
+
(item) => message.startsWith(item)
|
|
733
|
+
);
|
|
734
|
+
if (!prefix) return;
|
|
735
|
+
const response = message.substring(prefix.length);
|
|
691
736
|
if (!response) return;
|
|
737
|
+
const responseId = `${Date.now()}:${responseSeq++}`;
|
|
692
738
|
const session = resolveSession();
|
|
693
|
-
if (!session
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
});
|
|
739
|
+
if (!session) {
|
|
740
|
+
retryResponse(responseId, response);
|
|
741
|
+
return;
|
|
697
742
|
}
|
|
698
|
-
|
|
743
|
+
runResponse(responseId, response, session);
|
|
699
744
|
};
|
|
700
745
|
wrapped[RAW_INTERCEPTOR_TAG] = true;
|
|
701
746
|
loggerService.debug = wrapped;
|
|
@@ -767,6 +812,9 @@ function createXmlInterceptor(deps) {
|
|
|
767
812
|
}
|
|
768
813
|
stopFastRetry();
|
|
769
814
|
stopMonitor();
|
|
815
|
+
for (const handle of responseRetryHandles.values()) handle();
|
|
816
|
+
responseRetryHandles.clear();
|
|
817
|
+
processedResponses.clear();
|
|
770
818
|
restore();
|
|
771
819
|
sessionMap.clear();
|
|
772
820
|
pendingSessions.length = 0;
|
|
@@ -806,10 +854,12 @@ function registerXmlTools(deps) {
|
|
|
806
854
|
log,
|
|
807
855
|
onResponse: (response, session) => {
|
|
808
856
|
const actions = parseXmlActions(response);
|
|
857
|
+
let handled = false;
|
|
809
858
|
if (config.enablePokeXmlTool && actions.pokeUserIds.length > 0) {
|
|
810
859
|
if (!session) {
|
|
811
860
|
log?.("warn", "\u68C0\u6D4B\u5230\u6233\u4E00\u6233\u6807\u8BB0\u4F46\u7F3A\u5C11\u4F1A\u8BDD\u4E0A\u4E0B\u6587");
|
|
812
861
|
} else {
|
|
862
|
+
handled = true;
|
|
813
863
|
for (const userId of actions.pokeUserIds) {
|
|
814
864
|
void sendPoke({ session, userId, protocol, log }).catch((error) => {
|
|
815
865
|
log?.("warn", "XML \u89E6\u53D1 poke \u5931\u8D25", error);
|
|
@@ -821,6 +871,7 @@ function registerXmlTools(deps) {
|
|
|
821
871
|
if (!session) {
|
|
822
872
|
log?.("warn", "\u68C0\u6D4B\u5230\u8868\u60C5\u6807\u8BB0\u4F46\u7F3A\u5C11\u4F1A\u8BDD\u4E0A\u4E0B\u6587");
|
|
823
873
|
} else {
|
|
874
|
+
handled = true;
|
|
824
875
|
for (const item of actions.emojis) {
|
|
825
876
|
void sendMsgEmoji({
|
|
826
877
|
session,
|
|
@@ -838,6 +889,7 @@ function registerXmlTools(deps) {
|
|
|
838
889
|
if (!session) {
|
|
839
890
|
log?.("warn", "\u68C0\u6D4B\u5230\u64A4\u56DE\u6807\u8BB0\u4F46\u7F3A\u5C11\u4F1A\u8BDD\u4E0A\u4E0B\u6587");
|
|
840
891
|
} else {
|
|
892
|
+
handled = true;
|
|
841
893
|
for (const messageId of actions.deleteMessageIds) {
|
|
842
894
|
void sendDeleteMessage({ session, messageId, log }).catch(
|
|
843
895
|
(error) => {
|
|
@@ -847,6 +899,7 @@ function registerXmlTools(deps) {
|
|
|
847
899
|
}
|
|
848
900
|
}
|
|
849
901
|
}
|
|
902
|
+
return handled;
|
|
850
903
|
}
|
|
851
904
|
});
|
|
852
905
|
return {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koishi-plugin-chatluna-toolbox",
|
|
3
3
|
"description": "为 ChatLuna 提供更多原生工具、XML 工具与变量。",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.5",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
7
7
|
"files": [
|
|
@@ -18,11 +18,6 @@
|
|
|
18
18
|
"plugin",
|
|
19
19
|
"chatluna"
|
|
20
20
|
],
|
|
21
|
-
"koishi": {
|
|
22
|
-
"description": {
|
|
23
|
-
"zh": "ChatLuna 工具箱插件"
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
21
|
"peerDependencies": {
|
|
27
22
|
"koishi": "^4.18.7",
|
|
28
23
|
"koishi-plugin-chatluna": "*",
|