koishi-plugin-satori-ai 1.3.6 → 1.3.7
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 +51 -21
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -284,11 +284,11 @@ function filterResponse(prompt, words) {
|
|
|
284
284
|
const lastAnswer5 = answerMatches5 ? answerMatches5[answerMatches5.length - 1] : null;
|
|
285
285
|
const answerContent = lastAnswer || lastDoubao || lastAnswer3 || lastAnswer4 || lastAnswer5;
|
|
286
286
|
if (!answerContent) {
|
|
287
|
-
return { content: "
|
|
287
|
+
return { content: "有点问题,请重置对话", error: true };
|
|
288
288
|
}
|
|
289
289
|
const regex = /<p>|<\/p>|<doubaothinking>|<\/doubaothinking>|<\/p|<answer>|<\/answer>|<br>|<\/br>|<br\/>/g;
|
|
290
290
|
const cleanedContent = answerContent.replace(regex, "");
|
|
291
|
-
return cleanedContent ? { content: cleanedContent, error: false } : { content: "
|
|
291
|
+
return cleanedContent ? { content: cleanedContent, error: false } : { content: "有点问题,请重置对话", error: true };
|
|
292
292
|
}
|
|
293
293
|
__name(filterResponse, "filterResponse");
|
|
294
294
|
function addOutputCensor(session, word, baseURL) {
|
|
@@ -311,6 +311,45 @@ async function updateUserPWithTicket(ctx, user, adjustment) {
|
|
|
311
311
|
}
|
|
312
312
|
}
|
|
313
313
|
__name(updateUserPWithTicket, "updateUserPWithTicket");
|
|
314
|
+
function findLongestCommonSubstring(str1, str2) {
|
|
315
|
+
if (!str1 || !str2)
|
|
316
|
+
return 0;
|
|
317
|
+
const m = str1.length;
|
|
318
|
+
const n = str2.length;
|
|
319
|
+
const dp = Array(m + 1).fill(null).map(() => Array(n + 1).fill(0));
|
|
320
|
+
let maxLength = 0;
|
|
321
|
+
for (let i = 1; i <= m; i++) {
|
|
322
|
+
for (let j = 1; j <= n; j++) {
|
|
323
|
+
if (str1[i - 1] === str2[j - 1]) {
|
|
324
|
+
dp[i][j] = dp[i - 1][j - 1] + 1;
|
|
325
|
+
maxLength = Math.max(maxLength, dp[i][j]);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return maxLength;
|
|
330
|
+
}
|
|
331
|
+
__name(findLongestCommonSubstring, "findLongestCommonSubstring");
|
|
332
|
+
function countCommonChars(str1, str2) {
|
|
333
|
+
const chars1 = /* @__PURE__ */ new Map();
|
|
334
|
+
const chars2 = /* @__PURE__ */ new Map();
|
|
335
|
+
for (const char of str1) {
|
|
336
|
+
if (/[\u4e00-\u9fff]/.test(char)) {
|
|
337
|
+
chars1.set(char, (chars1.get(char) || 0) + 1);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
for (const char of str2) {
|
|
341
|
+
if (/[\u4e00-\u9fff]/.test(char)) {
|
|
342
|
+
chars2.set(char, (chars2.get(char) || 0) + 1);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
let commonCount = 0;
|
|
346
|
+
for (const [char, count1] of chars1) {
|
|
347
|
+
const count2 = chars2.get(char) || 0;
|
|
348
|
+
commonCount += Math.min(count1, count2);
|
|
349
|
+
}
|
|
350
|
+
return commonCount;
|
|
351
|
+
}
|
|
352
|
+
__name(countCommonChars, "countCommonChars");
|
|
314
353
|
|
|
315
354
|
// src/api.ts
|
|
316
355
|
var logger2 = new import_koishi2.Logger("satori-api");
|
|
@@ -702,22 +741,12 @@ var MemoryManager = class {
|
|
|
702
741
|
calculateMatchScore(content, keywords) {
|
|
703
742
|
if (keywords.length === 0)
|
|
704
743
|
return { score: 0, count: 0 };
|
|
705
|
-
const
|
|
706
|
-
|
|
707
|
-
const
|
|
708
|
-
const
|
|
709
|
-
|
|
710
|
-
const
|
|
711
|
-
let count = 0;
|
|
712
|
-
matches.forEach((match) => {
|
|
713
|
-
const chineseCount = (match.match(chineseRegex) || []).length;
|
|
714
|
-
const englishCount = match.length - chineseCount;
|
|
715
|
-
count += chineseCount * 2 + englishCount;
|
|
716
|
-
});
|
|
717
|
-
const totalChinese = (content.match(chineseRegex) || []).length;
|
|
718
|
-
const totalEnglish = content.length - totalChinese;
|
|
719
|
-
const totalWeight = totalChinese * 2 + totalEnglish;
|
|
720
|
-
const ratio = totalWeight > 0 ? count / totalWeight : 0;
|
|
744
|
+
const regex = new RegExp(`[${this.charactersToRemove.join("")}]`, "g");
|
|
745
|
+
content = content.replace(regex, "");
|
|
746
|
+
const Keyword = keywords.map((k) => escapeRegExp(k)).join("");
|
|
747
|
+
const length = findLongestCommonSubstring(content, Keyword);
|
|
748
|
+
const count = countCommonChars(content, Keyword);
|
|
749
|
+
const ratio = (length * length + count) / content.length;
|
|
721
750
|
return { score: ratio, count };
|
|
722
751
|
}
|
|
723
752
|
// 格式化匹配结果
|
|
@@ -732,7 +761,8 @@ var MemoryManager = class {
|
|
|
732
761
|
matched = matched.slice(0, topN < matched.length ? topN : matched.length);
|
|
733
762
|
if (type === "common") {
|
|
734
763
|
const result = `${prefixMap[type]}{
|
|
735
|
-
${matched.map((entry) => entry.content).join("\n")}
|
|
764
|
+
${matched.map((entry) => entry.content).join("\n")}
|
|
765
|
+
${date}
|
|
736
766
|
}
|
|
737
767
|
`;
|
|
738
768
|
return result;
|
|
@@ -3664,7 +3694,7 @@ ${payload}`);
|
|
|
3664
3694
|
getThinkingPrompt(user, prompt) {
|
|
3665
3695
|
const reasonerPrompt = this.config.reasoner_prompt;
|
|
3666
3696
|
const promptForNoReasoner = `#请你在回复时先进行分析思考,并且模仿思维链的模式输出思考内容,${reasonerPrompt};
|
|
3667
|
-
#你在思考时必须以 "<think>" 开头, "</think>"
|
|
3697
|
+
#你在思考时必须以 "<think>" 开头, "</think>" 结尾。仔细揣摩用户意图,**完整输出思考内容后**再输出正式的回复内容;
|
|
3668
3698
|
#注意:你的正式回复内容必须使用“<p>”开头,使用“</p>”结尾,并且无论如何都要把标签输出完整
|
|
3669
3699
|
`;
|
|
3670
3700
|
const promptForReasoner = `#你在思考时必须以 "嗯" 开头。仔细揣摩用户意图,思考结束后返回符合要求的回复。
|
|
@@ -3766,7 +3796,7 @@ ${useNoReasoner ? promptForNoReasoner : promptForReasoner}
|
|
|
3766
3796
|
const filePath = path7.join(this.config.dataDir, "common_sense.txt");
|
|
3767
3797
|
await this.memoryManager.saveLongTermMemory(session, [{
|
|
3768
3798
|
role: "user",
|
|
3769
|
-
content
|
|
3799
|
+
content: content.replace(/\"/g, "'").trim()
|
|
3770
3800
|
}], filePath);
|
|
3771
3801
|
return session.text("commands.sat.common_sense.messages.succeed", [content]);
|
|
3772
3802
|
}
|