koishi-plugin-best-cave 2.7.33 → 2.7.34
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 +22 -9
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -1366,18 +1366,31 @@ ${candidatesText}`;
|
|
|
1366
1366
|
const response = await this.http.post(fullUrl, payload, { headers, timeout: 6e5 });
|
|
1367
1367
|
const content = response?.choices?.[0]?.message?.content;
|
|
1368
1368
|
if (!content?.trim()) throw new Error();
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1369
|
+
const potentialStrings = /* @__PURE__ */ new Set();
|
|
1370
|
+
const jsonBlockMatch = content.match(/```json\s*([\s\S]*?)\s*```/i);
|
|
1371
|
+
if (jsonBlockMatch?.[1]) potentialStrings.add(jsonBlockMatch[1]);
|
|
1372
|
+
const firstBrace = content.indexOf("{");
|
|
1373
|
+
const lastBrace = content.lastIndexOf("}");
|
|
1374
|
+
const firstBracket = content.indexOf("[");
|
|
1375
|
+
const lastBracket = content.lastIndexOf("]");
|
|
1376
|
+
if (firstBrace !== -1 && (firstBracket === -1 || firstBrace < firstBracket)) {
|
|
1377
|
+
if (lastBrace > firstBrace) potentialStrings.add(content.substring(firstBrace, lastBrace + 1));
|
|
1378
|
+
} else if (firstBracket !== -1) {
|
|
1379
|
+
if (lastBracket > firstBracket) potentialStrings.add(content.substring(firstBracket, lastBracket + 1));
|
|
1380
|
+
}
|
|
1381
|
+
potentialStrings.add(content);
|
|
1382
|
+
for (const jsonString of potentialStrings) {
|
|
1383
|
+
try {
|
|
1384
|
+
const result = JSON.parse(jsonString);
|
|
1385
|
+
this.failureCount = 0;
|
|
1386
|
+
return result;
|
|
1387
|
+
} catch (e) {
|
|
1388
|
+
}
|
|
1377
1389
|
}
|
|
1390
|
+
this.logger.error("原始响应:", JSON.stringify(response, null, 2));
|
|
1391
|
+
throw new Error();
|
|
1378
1392
|
} catch (error) {
|
|
1379
1393
|
this.failureCount++;
|
|
1380
|
-
this.logger.warn(`请求失败: ${error.message}`);
|
|
1381
1394
|
if (this.failureCount >= 3) this.retryTime = Date.now() + 6e4;
|
|
1382
1395
|
throw error;
|
|
1383
1396
|
}
|