koishi-plugin-best-cave 2.7.16 → 2.7.18

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 +39 -27
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -1187,33 +1187,45 @@ var AIManager = class {
1187
1187
  * @returns {Promise<CaveMetaObject[]>} 一个 Promise,解析为 AI 返回的分析结果数组。
1188
1188
  */
1189
1189
  async getAnalyses(caves, mediaBufferMap) {
1190
- const batchPayload = await Promise.all(caves.map(async (cave) => {
1191
- const combinedText = cave.elements.filter((el) => el.type === "text" && el.content).map((el) => el.content).join("\n");
1192
- const imagesBase64 = (await Promise.all(
1193
- cave.elements.filter((el) => el.type === "image" && el.file).map(async (el) => {
1194
- try {
1195
- const buffer = mediaBufferMap?.get(el.file) ?? await this.fileManager.readFile(el.file);
1196
- const mimeType = path3.extname(el.file).toLowerCase() === ".png" ? "image/png" : "image/jpeg";
1197
- return `data:${mimeType};base64,${buffer.toString("base64")}`;
1198
- } catch (error) {
1199
- this.logger.warn(`读取文件(${el.file})失败:`, error);
1200
- return null;
1201
- }
1202
- })
1203
- )).filter(Boolean);
1204
- return { id: cave.id, text: combinedText, images: imagesBase64 };
1205
- }));
1206
- const nonEmptyPayload = batchPayload.filter((p) => p.text.trim() || p.images.length > 0);
1207
- if (nonEmptyPayload.length === 0) return [];
1208
- const userMessage = { role: "user", content: JSON.stringify(nonEmptyPayload) };
1209
- const analysePrompt = `你是一位内容分析专家。请使用中文,分析我以JSON格式提供的一组内容,为每一项内容总结关键词、概括内容并评分。你的回复必须且只能是一个包裹在 \`\`\`json ... \`\`\` 代码块中的有效 JSON 对象。该JSON对象应有一个 "analyses" 键,其值为一个数组。数组中的每个对象都必须包含 "id" (整数), "keywords" (字符串数组), "description" (字符串), 和 "rating" (0-100的整数)。`;
1210
- const response = await this.requestAI([userMessage], analysePrompt);
1211
- return (response.analyses || []).map((res) => ({
1212
- cave: res.id,
1213
- keywords: res.keywords,
1214
- description: res.description,
1215
- rating: res.rating
1216
- }));
1190
+ const results = [];
1191
+ for (const cave of caves) {
1192
+ try {
1193
+ const combinedText = cave.elements.filter((el) => el.type === "text" && el.content).map((el) => el.content).join("\n");
1194
+ const imageElements = await Promise.all(
1195
+ cave.elements.filter((el) => el.type === "image" && el.file).map(async (el) => {
1196
+ try {
1197
+ const buffer = mediaBufferMap?.get(el.file) ?? await this.fileManager.readFile(el.file);
1198
+ const mimeType = path3.extname(el.file).toLowerCase() === ".png" ? "image/png" : "image/jpeg";
1199
+ return {
1200
+ type: "image_url",
1201
+ image_url: { url: `data:${mimeType};base64,${buffer.toString("base64")}` }
1202
+ };
1203
+ } catch (error) {
1204
+ this.logger.warn(`读取文件(${el.file})失败:`, error);
1205
+ return null;
1206
+ }
1207
+ })
1208
+ );
1209
+ const validImages = imageElements.filter(Boolean);
1210
+ if (!combinedText.trim() && validImages.length === 0) continue;
1211
+ const contentForAI = [{ type: "text", text: combinedText }];
1212
+ contentForAI.push(...validImages);
1213
+ const userMessage = { role: "user", content: contentForAI };
1214
+ const analysePrompt = `你是一位内容分析专家。请使用中文,分析我提供的内容(包含文本和可能的图片),并为其总结关键词、概括内容并评分。你的回复必须且只能是一个包裹在 \`\`\`json ... \`\`\` 代码块中的有效 JSON 对象。该对象必须包含 "keywords" (字符串数组), "description" (字符串), 和 "rating" (0-100的整数)。`;
1215
+ const response = await this.requestAI([userMessage], analysePrompt);
1216
+ if (response) {
1217
+ results.push({
1218
+ cave: cave.id,
1219
+ keywords: response.keywords || [],
1220
+ description: response.description || "",
1221
+ rating: response.rating || 0
1222
+ });
1223
+ }
1224
+ } catch (error) {
1225
+ this.logger.error(`分析回声洞(${cave.id})失败:`, error);
1226
+ }
1227
+ }
1228
+ return results;
1217
1229
  }
1218
1230
  /**
1219
1231
  * @description 封装了向 OpenAI 兼容的 API 发送请求的底层逻辑,并稳健地解析 JSON 响应。
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-best-cave",
3
3
  "description": "功能强大、高度可定制的回声洞。支持丰富的媒体类型、内容查重、人工审核、用户昵称、数据迁移以及本地/S3 双重文件存储后端。",
4
- "version": "2.7.16",
4
+ "version": "2.7.18",
5
5
  "contributors": [
6
6
  "Yis_Rime <yis_rime@outlook.com>"
7
7
  ],