koishi-plugin-best-cave 2.7.23 → 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.
Files changed (2) hide show
  1. package/lib/index.js +77 -72
  2. 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((error) => {
107
- this.logger.error(`创建资源目录失败 ${this.resourceDir}:`, error);
108
- throw error;
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 (error) {
143
- if (error.code !== "ENOENT" && error.name !== "NoSuchKey") {
144
- this.logger.warn(`删除文件 ${fileIdentifier} 失败:`, error);
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 (error) {
242
- logger2.warn(`解析回声洞(${cave.id})合并转发内容失败:`, error);
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 (error) {
256
- logger2.warn(`转换文件 ${fileName} 为 Base64 失败:`, error);
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 (error) {
336
- logger2.error("清理回声洞时发生错误:", 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 (error) {
485
- logger2.warn("相似度比较失败:", error);
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 (error) {
582
- this.logger.error("数据操作时发生错误:", error);
583
- return `操作失败: ${error.message}`;
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 (error) {
611
- throw new Error(`读取导入文件失败: ${error.message}`);
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 (error) {
707
- this.logger.error(`审核操作失败:`, error);
708
- return `操作失败: ${error.message}`;
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 (error) {
729
- this.logger.error(`发送回声洞(${cave.id})审核消息失败:`, error);
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 (error) {
812
+ } catch (error2) {
813
813
  errorCount++;
814
- this.logger.warn(`补全回声洞(${cave2.id})哈希时出错: ${error.message}`);
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 (error) {
821
- this.logger.error("补全哈希失败:", error);
822
- return `操作失败: ${error.message}`;
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 (error) {
907
- this.logger.error("检查相似度失败:", error);
908
- return `检查失败: ${error.message}`;
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 (error) {
935
- if (error.code !== "ENOENT" && error.name !== "NoSuchKey") {
936
- this.logger.warn(`无法修复回声洞(${cave2.id})的图片(${element.file}):`, error);
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 (error) {
944
- this.logger.error("修复图像文件时发生严重错误:", error);
945
- return `操作失败: ${error.message}`;
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 (error) {
1169
- this.logger.error("分析回声洞失败:", error);
1170
- return `操作失败: ${error.message}`;
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 (error) {
1216
- this.logger.error("检查重复性失败:", error);
1217
- return `检查失败: ${error.message}`;
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 (error) {
1244
- this.logger.error("查重回声洞出错:", 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 (error) {
1268
- this.logger.warn(`读取文件(${el.file})失败:`, error);
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 (error) {
1309
- this.logger.error(`比较回声洞(${caveA.id})与(${caveB.id})失败:`, error);
1309
+ } catch (error2) {
1310
+ this.logger.error(`比较回声洞(${caveA.id})与(${caveB.id})失败:`, error2);
1310
1311
  return false;
1311
1312
  }
1312
1313
  }
@@ -1347,16 +1348,20 @@ ${combinedText}` }, ...images];
1347
1348
  };
1348
1349
  const response = await this.http.post(fullUrl, payload, { headers, timeout: 6e5 });
1349
1350
  const content = response?.choices?.[0]?.message?.content;
1350
- if (typeof content !== "string" || !content.trim()) throw new Error();
1351
- try {
1352
- const jsonRegex = /```json\s*([\s\S]*?)\s*```/i;
1353
- const match = content.match(jsonRegex);
1354
- const jsonString = match && match[1] ? match[1] : content;
1355
- return JSON.parse(jsonString);
1356
- } catch (error) {
1357
- this.logger.error("解析失败:", error, "原始响应:", JSON.stringify(response, null, 2));
1358
- throw error;
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 (error) {
1439
- logger.error("清理残留回声洞时发生错误:", 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 (error) {
1456
- logger.error("随机获取回声洞失败:", 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 (error) {
1533
- logger.error(`回声洞(${newId})处理失败:`, error);
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})处理失败: ${error.message}`);
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 (error) {
1548
- logger.error(`查看回声洞(${id})失败:`, error);
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 (error) {
1565
- logger.error(`标记回声洞(${id})失败:`, error);
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 (error) {
1595
- logger.error("查询排行失败:", error);
1599
+ } catch (error2) {
1600
+ logger.error("查询排行失败:", error2);
1596
1601
  return "查询失败,请稍后再试";
1597
1602
  }
1598
1603
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-best-cave",
3
3
  "description": "功能强大、高度可定制的回声洞插件。支持丰富的媒体类型、内容查重、AI分析、人工审核、用户昵称、数据迁移以及本地/S3 双重文件存储后端。",
4
- "version": "2.7.23",
4
+ "version": "2.7.24",
5
5
  "contributors": [
6
6
  "Yis_Rime <yis_rime@outlook.com>"
7
7
  ],