koishi-plugin-best-cave 2.7.22 → 2.7.24
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 +78 -73
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -103,9 +103,9 @@ var FileManager = class {
|
|
|
103
103
|
});
|
|
104
104
|
await this.s3Client.send(command);
|
|
105
105
|
} else {
|
|
106
|
-
await fs.mkdir(this.resourceDir, { recursive: true }).catch((
|
|
107
|
-
this.logger.error(`创建资源目录失败 ${this.resourceDir}:`,
|
|
108
|
-
throw
|
|
106
|
+
await fs.mkdir(this.resourceDir, { recursive: true }).catch((error2) => {
|
|
107
|
+
this.logger.error(`创建资源目录失败 ${this.resourceDir}:`, error2);
|
|
108
|
+
throw error2;
|
|
109
109
|
});
|
|
110
110
|
const filePath = path.join(this.resourceDir, fileName);
|
|
111
111
|
await this.withLock(filePath, () => fs.writeFile(filePath, data));
|
|
@@ -139,9 +139,9 @@ var FileManager = class {
|
|
|
139
139
|
const filePath = path.join(this.resourceDir, fileIdentifier);
|
|
140
140
|
await this.withLock(filePath, () => fs.unlink(filePath));
|
|
141
141
|
}
|
|
142
|
-
} catch (
|
|
143
|
-
if (
|
|
144
|
-
this.logger.warn(`删除文件 ${fileIdentifier} 失败:`,
|
|
142
|
+
} catch (error2) {
|
|
143
|
+
if (error2.code !== "ENOENT" && error2.name !== "NoSuchKey") {
|
|
144
|
+
this.logger.warn(`删除文件 ${fileIdentifier} 失败:`, error2);
|
|
145
145
|
}
|
|
146
146
|
}
|
|
147
147
|
}
|
|
@@ -238,8 +238,8 @@ async function buildCaveMessage(cave, config, fileManager, logger2, platform, pr
|
|
|
238
238
|
return resultNodes;
|
|
239
239
|
}));
|
|
240
240
|
return (0, import_koishi.h)("message", { forward: true }, messageNodes.flat());
|
|
241
|
-
} catch (
|
|
242
|
-
logger2.warn(`解析回声洞(${cave.id})合并转发内容失败:`,
|
|
241
|
+
} catch (error2) {
|
|
242
|
+
logger2.warn(`解析回声洞(${cave.id})合并转发内容失败:`, error2);
|
|
243
243
|
return import_koishi.h.text("[合并转发]");
|
|
244
244
|
}
|
|
245
245
|
}
|
|
@@ -252,8 +252,8 @@ async function buildCaveMessage(cave, config, fileManager, logger2, platform, pr
|
|
|
252
252
|
const data2 = await fileManager.readFile(fileName);
|
|
253
253
|
const mimeType = mimeTypeMap[path2.extname(fileName).toLowerCase()] || "application/octet-stream";
|
|
254
254
|
return (0, import_koishi.h)(el.type, { ...el, src: `data:${mimeType};base64,${data2.toString("base64")}` });
|
|
255
|
-
} catch (
|
|
256
|
-
logger2.warn(`转换文件 ${fileName} 为 Base64 失败:`,
|
|
255
|
+
} catch (error2) {
|
|
256
|
+
logger2.warn(`转换文件 ${fileName} 为 Base64 失败:`, error2);
|
|
257
257
|
return (0, import_koishi.h)("p", {}, `[${el.type}]`);
|
|
258
258
|
}
|
|
259
259
|
}
|
|
@@ -332,8 +332,8 @@ async function cleanupPendingDeletions(ctx, config, fileManager, logger2, reusab
|
|
|
332
332
|
await ctx.database.remove("cave", { id: { $in: idsToDelete } });
|
|
333
333
|
await ctx.database.remove("cave_hash", { cave: { $in: idsToDelete } });
|
|
334
334
|
if (config.enableAI) await ctx.database.remove("cave_meta", { cave: { $in: idsToDelete } });
|
|
335
|
-
} catch (
|
|
336
|
-
logger2.error("清理回声洞时发生错误:",
|
|
335
|
+
} catch (error2) {
|
|
336
|
+
logger2.error("清理回声洞时发生错误:", error2);
|
|
337
337
|
}
|
|
338
338
|
}
|
|
339
339
|
__name(cleanupPendingDeletions, "cleanupPendingDeletions");
|
|
@@ -481,8 +481,8 @@ async function performSimilarityChecks(ctx, config, hashManager, logger2, finalE
|
|
|
481
481
|
}
|
|
482
482
|
}
|
|
483
483
|
return { duplicate: false, textHashesToStore, imageHashesToStore };
|
|
484
|
-
} catch (
|
|
485
|
-
logger2.warn("相似度比较失败:",
|
|
484
|
+
} catch (error2) {
|
|
485
|
+
logger2.warn("相似度比较失败:", error2);
|
|
486
486
|
return { duplicate: false, textHashesToStore: [], imageHashesToStore: [] };
|
|
487
487
|
}
|
|
488
488
|
}
|
|
@@ -578,9 +578,9 @@ var DataManager = class {
|
|
|
578
578
|
try {
|
|
579
579
|
await session.send("正在处理,请稍候...");
|
|
580
580
|
return await action();
|
|
581
|
-
} catch (
|
|
582
|
-
this.logger.error("数据操作时发生错误:",
|
|
583
|
-
return `操作失败: ${
|
|
581
|
+
} catch (error2) {
|
|
582
|
+
this.logger.error("数据操作时发生错误:", error2);
|
|
583
|
+
return `操作失败: ${error2.message}`;
|
|
584
584
|
}
|
|
585
585
|
}, "commandAction");
|
|
586
586
|
cave.subcommand(".export", "导出回声洞数据", { hidden: true, authority: 4 }).usage("将所有回声洞数据导出到 cave.json 中。").action(commandAction(() => this.exportData()));
|
|
@@ -607,8 +607,8 @@ var DataManager = class {
|
|
|
607
607
|
const fileContent = await this.fileManager.readFile(fileName);
|
|
608
608
|
importedCaves = JSON.parse(fileContent.toString("utf-8"));
|
|
609
609
|
if (!Array.isArray(importedCaves) || !importedCaves.length) throw new Error("导入文件格式无效或为空");
|
|
610
|
-
} catch (
|
|
611
|
-
throw new Error(`读取导入文件失败: ${
|
|
610
|
+
} catch (error2) {
|
|
611
|
+
throw new Error(`读取导入文件失败: ${error2.message}`);
|
|
612
612
|
}
|
|
613
613
|
const allDbCaves = await this.ctx.database.get("cave", {}, { fields: ["id"] });
|
|
614
614
|
const existingIds = new Set(allDbCaves.map((c) => c.id));
|
|
@@ -703,9 +703,9 @@ ${pendingCaves.map((c) => c.id).join("|")}`;
|
|
|
703
703
|
await this.ctx.database.upsert("cave", processedIds.map((id) => ({ id, status: targetStatus })));
|
|
704
704
|
if (targetStatus === "delete") cleanupPendingDeletions(this.ctx, this.config, this.fileManager, this.logger, this.reusableIds);
|
|
705
705
|
return `已${actionText}回声洞(${processedIds.join("|")})`;
|
|
706
|
-
} catch (
|
|
707
|
-
this.logger.error(`审核操作失败:`,
|
|
708
|
-
return `操作失败: ${
|
|
706
|
+
} catch (error2) {
|
|
707
|
+
this.logger.error(`审核操作失败:`, error2);
|
|
708
|
+
return `操作失败: ${error2.message}`;
|
|
709
709
|
}
|
|
710
710
|
}, "createPendAction");
|
|
711
711
|
pend.subcommand(".Y [...ids:posint]", "通过审核").usage("通过一个或多个指定 ID 的回声洞审核。若不指定 ID,则通过所有待审核的回声洞。").action(createPendAction("approve"));
|
|
@@ -725,8 +725,8 @@ ${pendingCaves.map((c) => c.id).join("|")}`;
|
|
|
725
725
|
const [platform] = this.config.adminChannel.split(":", 1);
|
|
726
726
|
const caveMessages = await buildCaveMessage(cave, this.config, this.fileManager, this.logger, platform, "待审核");
|
|
727
727
|
for (const message of caveMessages) if (message.length > 0) await this.ctx.broadcast([this.config.adminChannel], import_koishi2.h.normalize(message));
|
|
728
|
-
} catch (
|
|
729
|
-
this.logger.error(`发送回声洞(${cave.id})审核消息失败:`,
|
|
728
|
+
} catch (error2) {
|
|
729
|
+
this.logger.error(`发送回声洞(${cave.id})审核消息失败:`, error2);
|
|
730
730
|
}
|
|
731
731
|
}
|
|
732
732
|
};
|
|
@@ -809,17 +809,17 @@ var HashManager = class {
|
|
|
809
809
|
const newHashesForCave = tempHashes;
|
|
810
810
|
if (newHashesForCave.length > 0) hashesToInsert.push(...newHashesForCave);
|
|
811
811
|
if (hashesToInsert.length >= 100) await flushBatch();
|
|
812
|
-
} catch (
|
|
812
|
+
} catch (error2) {
|
|
813
813
|
errorCount++;
|
|
814
|
-
this.logger.warn(`补全回声洞(${cave2.id})哈希时出错: ${
|
|
814
|
+
this.logger.warn(`补全回声洞(${cave2.id})哈希时出错: ${error2.message}`);
|
|
815
815
|
}
|
|
816
816
|
}
|
|
817
817
|
await flushBatch();
|
|
818
818
|
const successCount = processedCaveCount - errorCount;
|
|
819
819
|
return `已补全 ${successCount} 个回声洞的 ${totalHashesGenerated} 条哈希(失败 ${errorCount} 条)`;
|
|
820
|
-
} catch (
|
|
821
|
-
this.logger.error("补全哈希失败:",
|
|
822
|
-
return `操作失败: ${
|
|
820
|
+
} catch (error2) {
|
|
821
|
+
this.logger.error("补全哈希失败:", error2);
|
|
822
|
+
return `操作失败: ${error2.message}`;
|
|
823
823
|
}
|
|
824
824
|
});
|
|
825
825
|
cave.subcommand(".check", "检查相似度", { hidden: true }).usage("检查所有回声洞,找出相似度过高的内容。").option("textThreshold", "-t <threshold:number> 文本相似度阈值 (%)").option("imageThreshold", "-i <threshold:number> 图片相似度阈值 (%)").action(async ({ session, options }) => {
|
|
@@ -903,9 +903,9 @@ ${textResult.reportLines.join("\n")}`;
|
|
|
903
903
|
${imageResult.reportLines.join("\n")}`;
|
|
904
904
|
}
|
|
905
905
|
return report.trim();
|
|
906
|
-
} catch (
|
|
907
|
-
this.logger.error("检查相似度失败:",
|
|
908
|
-
return `检查失败: ${
|
|
906
|
+
} catch (error2) {
|
|
907
|
+
this.logger.error("检查相似度失败:", error2);
|
|
908
|
+
return `检查失败: ${error2.message}`;
|
|
909
909
|
}
|
|
910
910
|
});
|
|
911
911
|
cave.subcommand(".fix [...ids:posint]", "修复回声洞", { hidden: true, authority: 3 }).usage("扫描并修复回声洞中的图片,可指定一个或多个 ID。").action(async ({ session }, ...ids) => {
|
|
@@ -931,18 +931,18 @@ ${imageResult.reportLines.join("\n")}`;
|
|
|
931
931
|
await this.fileManager.saveFile(element.file, sanitizedBuffer);
|
|
932
932
|
fixedFiles++;
|
|
933
933
|
}
|
|
934
|
-
} catch (
|
|
935
|
-
if (
|
|
936
|
-
this.logger.warn(`无法修复回声洞(${cave2.id})的图片(${element.file}):`,
|
|
934
|
+
} catch (error2) {
|
|
935
|
+
if (error2.code !== "ENOENT" && error2.name !== "NoSuchKey") {
|
|
936
|
+
this.logger.warn(`无法修复回声洞(${cave2.id})的图片(${element.file}):`, error2);
|
|
937
937
|
errorCount++;
|
|
938
938
|
}
|
|
939
939
|
}
|
|
940
940
|
}
|
|
941
941
|
}
|
|
942
942
|
return `已修复 ${cavesToProcess.length} 个回声洞的 ${fixedFiles} 张图片(失败 ${errorCount} 条)`;
|
|
943
|
-
} catch (
|
|
944
|
-
this.logger.error("修复图像文件时发生严重错误:",
|
|
945
|
-
return `操作失败: ${
|
|
943
|
+
} catch (error2) {
|
|
944
|
+
this.logger.error("修复图像文件时发生严重错误:", error2);
|
|
945
|
+
return `操作失败: ${error2.message}`;
|
|
946
946
|
}
|
|
947
947
|
});
|
|
948
948
|
}
|
|
@@ -1083,6 +1083,7 @@ ${imageResult.reportLines.join("\n")}`;
|
|
|
1083
1083
|
|
|
1084
1084
|
// src/AIManager.ts
|
|
1085
1085
|
var path3 = __toESM(require("path"));
|
|
1086
|
+
var import_console = require("console");
|
|
1086
1087
|
var AIManager = class {
|
|
1087
1088
|
/**
|
|
1088
1089
|
* @constructor
|
|
@@ -1165,9 +1166,9 @@ var AIManager = class {
|
|
|
1165
1166
|
}
|
|
1166
1167
|
}
|
|
1167
1168
|
return `已分析 ${successCount} 个回声洞`;
|
|
1168
|
-
} catch (
|
|
1169
|
-
this.logger.error("分析回声洞失败:",
|
|
1170
|
-
return `操作失败: ${
|
|
1169
|
+
} catch (error2) {
|
|
1170
|
+
this.logger.error("分析回声洞失败:", error2);
|
|
1171
|
+
return `操作失败: ${error2.message}`;
|
|
1171
1172
|
}
|
|
1172
1173
|
});
|
|
1173
1174
|
cave.subcommand(".compare", "比较重复性", { hidden: true }).usage("检查回声洞,找出可能重复的内容。").action(async ({ session }) => {
|
|
@@ -1212,9 +1213,9 @@ var AIManager = class {
|
|
|
1212
1213
|
- ${sortedCluster.join("|")}`;
|
|
1213
1214
|
});
|
|
1214
1215
|
return report;
|
|
1215
|
-
} catch (
|
|
1216
|
-
this.logger.error("检查重复性失败:",
|
|
1217
|
-
return `检查失败: ${
|
|
1216
|
+
} catch (error2) {
|
|
1217
|
+
this.logger.error("检查重复性失败:", error2);
|
|
1218
|
+
return `检查失败: ${error2.message}`;
|
|
1218
1219
|
}
|
|
1219
1220
|
});
|
|
1220
1221
|
}
|
|
@@ -1240,8 +1241,8 @@ var AIManager = class {
|
|
|
1240
1241
|
});
|
|
1241
1242
|
const duplicateIds = (await Promise.all(comparisonPromises)).filter((id) => id !== null);
|
|
1242
1243
|
return { duplicate: duplicateIds.length > 0, ids: duplicateIds };
|
|
1243
|
-
} catch (
|
|
1244
|
-
this.logger.error("查重回声洞出错:",
|
|
1244
|
+
} catch (error2) {
|
|
1245
|
+
this.logger.error("查重回声洞出错:", error2);
|
|
1245
1246
|
return { duplicate: false };
|
|
1246
1247
|
}
|
|
1247
1248
|
}
|
|
@@ -1264,8 +1265,8 @@ var AIManager = class {
|
|
|
1264
1265
|
type: "image_url",
|
|
1265
1266
|
image_url: { url: `data:${mimeType};base64,${buffer.toString("base64")}` }
|
|
1266
1267
|
};
|
|
1267
|
-
} catch (
|
|
1268
|
-
this.logger.warn(`读取文件(${el.file})失败:`,
|
|
1268
|
+
} catch (error2) {
|
|
1269
|
+
this.logger.warn(`读取文件(${el.file})失败:`, error2);
|
|
1269
1270
|
}
|
|
1270
1271
|
})
|
|
1271
1272
|
);
|
|
@@ -1305,8 +1306,8 @@ ${combinedText}` }, ...images];
|
|
|
1305
1306
|
const userMessage = { role: "user", content: JSON.stringify(userMessageContent) };
|
|
1306
1307
|
const response = await this.requestAI([userMessage], this.DUPLICATE_CHECK_SYSTEM_PROMPT);
|
|
1307
1308
|
return response?.duplicate || false;
|
|
1308
|
-
} catch (
|
|
1309
|
-
this.logger.error(`比较回声洞(${caveA.id})与(${caveB.id})失败:`,
|
|
1309
|
+
} catch (error2) {
|
|
1310
|
+
this.logger.error(`比较回声洞(${caveA.id})与(${caveB.id})失败:`, error2);
|
|
1310
1311
|
return false;
|
|
1311
1312
|
}
|
|
1312
1313
|
}
|
|
@@ -1345,18 +1346,22 @@ ${combinedText}` }, ...images];
|
|
|
1345
1346
|
"Content-Type": "application/json",
|
|
1346
1347
|
"Authorization": `Bearer ${this.config.aiApiKey}`
|
|
1347
1348
|
};
|
|
1348
|
-
const response = await this.http.post(fullUrl, payload, { headers, timeout:
|
|
1349
|
+
const response = await this.http.post(fullUrl, payload, { headers, timeout: 6e5 });
|
|
1349
1350
|
const content = response?.choices?.[0]?.message?.content;
|
|
1350
|
-
if (
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1351
|
+
if (!content?.trim()) throw import_console.error;
|
|
1352
|
+
const candidates = [];
|
|
1353
|
+
const jsonBlockMatch = content.match(/```json\s*([\s\S]*?)\s*```/i);
|
|
1354
|
+
if (jsonBlockMatch && jsonBlockMatch[1]) candidates.push(jsonBlockMatch[1]);
|
|
1355
|
+
candidates.push(content);
|
|
1356
|
+
const firstBrace = content.indexOf("{");
|
|
1357
|
+
const lastBrace = content.lastIndexOf("}");
|
|
1358
|
+
if (firstBrace !== -1 && lastBrace > firstBrace) candidates.push(content.substring(firstBrace, lastBrace + 1));
|
|
1359
|
+
for (const candidate of [...new Set(candidates)]) try {
|
|
1360
|
+
return JSON.parse(candidate);
|
|
1361
|
+
} catch {
|
|
1359
1362
|
}
|
|
1363
|
+
this.logger.error("解析失败:", import_console.error, "原始响应:", JSON.stringify(response, null, 2));
|
|
1364
|
+
throw import_console.error;
|
|
1360
1365
|
}
|
|
1361
1366
|
};
|
|
1362
1367
|
|
|
@@ -1435,8 +1440,8 @@ function apply(ctx, config) {
|
|
|
1435
1440
|
await ctx.database.upsert("cave", idsToMark);
|
|
1436
1441
|
await cleanupPendingDeletions(ctx, config, fileManager, logger, reusableIds);
|
|
1437
1442
|
}
|
|
1438
|
-
} catch (
|
|
1439
|
-
logger.error("清理残留回声洞时发生错误:",
|
|
1443
|
+
} catch (error2) {
|
|
1444
|
+
logger.error("清理残留回声洞时发生错误:", error2);
|
|
1440
1445
|
}
|
|
1441
1446
|
});
|
|
1442
1447
|
const cave = ctx.command("cave", "回声洞").option("add", "-a <content:text> 添加回声洞").option("view", "-g <id:posint> 查看指定回声洞").option("delete", "-r <id:posint> 删除指定回声洞").option("list", "-l 查询投稿统计").usage("随机抽取一条已添加的回声洞。").action(async ({ session, options }) => {
|
|
@@ -1452,8 +1457,8 @@ function apply(ctx, config) {
|
|
|
1452
1457
|
const [randomCave] = await ctx.database.get("cave", { ...query, id: randomId });
|
|
1453
1458
|
const messages = await buildCaveMessage(randomCave, config, fileManager, logger, session.platform);
|
|
1454
1459
|
for (const message of messages) if (message.length > 0) await session.send(import_koishi3.h.normalize(message));
|
|
1455
|
-
} catch (
|
|
1456
|
-
logger.error("随机获取回声洞失败:",
|
|
1460
|
+
} catch (error2) {
|
|
1461
|
+
logger.error("随机获取回声洞失败:", error2);
|
|
1457
1462
|
return "随机获取回声洞失败";
|
|
1458
1463
|
}
|
|
1459
1464
|
});
|
|
@@ -1529,11 +1534,11 @@ function apply(ctx, config) {
|
|
|
1529
1534
|
if (allHashesToInsert.length > 0) await ctx.database.upsert("cave_hash", allHashesToInsert);
|
|
1530
1535
|
}
|
|
1531
1536
|
if (finalStatus === "pending" && reviewManager) reviewManager.sendForPend(newCave);
|
|
1532
|
-
} catch (
|
|
1533
|
-
logger.error(`回声洞(${newId})处理失败:`,
|
|
1537
|
+
} catch (error2) {
|
|
1538
|
+
logger.error(`回声洞(${newId})处理失败:`, error2);
|
|
1534
1539
|
await ctx.database.upsert("cave", [{ id: newId, status: "delete" }]);
|
|
1535
1540
|
await cleanupPendingDeletions(ctx, config, fileManager, logger, reusableIds);
|
|
1536
|
-
await session.send(`回声洞(${newId})处理失败: ${
|
|
1541
|
+
await session.send(`回声洞(${newId})处理失败: ${error2.message}`);
|
|
1537
1542
|
}
|
|
1538
1543
|
})();
|
|
1539
1544
|
});
|
|
@@ -1544,8 +1549,8 @@ function apply(ctx, config) {
|
|
|
1544
1549
|
if (!targetCave) return `回声洞(${id})不存在`;
|
|
1545
1550
|
const messages = await buildCaveMessage(targetCave, config, fileManager, logger, session.platform);
|
|
1546
1551
|
for (const message of messages) if (message.length > 0) await session.send(import_koishi3.h.normalize(message));
|
|
1547
|
-
} catch (
|
|
1548
|
-
logger.error(`查看回声洞(${id})失败:`,
|
|
1552
|
+
} catch (error2) {
|
|
1553
|
+
logger.error(`查看回声洞(${id})失败:`, error2);
|
|
1549
1554
|
return "查看失败,请稍后再试";
|
|
1550
1555
|
}
|
|
1551
1556
|
});
|
|
@@ -1561,8 +1566,8 @@ function apply(ctx, config) {
|
|
|
1561
1566
|
const caveMessages = await buildCaveMessage(targetCave, config, fileManager, logger, session.platform, "已删除");
|
|
1562
1567
|
for (const message of caveMessages) if (message.length > 0) await session.send(import_koishi3.h.normalize(message));
|
|
1563
1568
|
cleanupPendingDeletions(ctx, config, fileManager, logger, reusableIds);
|
|
1564
|
-
} catch (
|
|
1565
|
-
logger.error(`标记回声洞(${id})失败:`,
|
|
1569
|
+
} catch (error2) {
|
|
1570
|
+
logger.error(`标记回声洞(${id})失败:`, error2);
|
|
1566
1571
|
return "删除失败,请稍后再试";
|
|
1567
1572
|
}
|
|
1568
1573
|
});
|
|
@@ -1591,8 +1596,8 @@ function apply(ctx, config) {
|
|
|
1591
1596
|
`;
|
|
1592
1597
|
});
|
|
1593
1598
|
return report.trim();
|
|
1594
|
-
} catch (
|
|
1595
|
-
logger.error("查询排行失败:",
|
|
1599
|
+
} catch (error2) {
|
|
1600
|
+
logger.error("查询排行失败:", error2);
|
|
1596
1601
|
return "查询失败,请稍后再试";
|
|
1597
1602
|
}
|
|
1598
1603
|
}
|