koishi-plugin-cocoyyy-console 1.1.3 → 1.1.4
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 -2
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -1894,6 +1894,22 @@ var tools = [
|
|
|
1894
1894
|
}
|
|
1895
1895
|
}
|
|
1896
1896
|
];
|
|
1897
|
+
function safeParseJSON(jsonString) {
|
|
1898
|
+
if (!jsonString || typeof jsonString !== "string") {
|
|
1899
|
+
throw new Error("输入不是有效的字符串");
|
|
1900
|
+
}
|
|
1901
|
+
let cleaned = jsonString.trim();
|
|
1902
|
+
cleaned = cleaned.replace(/^```(?:json)?\s*\n?/i, "");
|
|
1903
|
+
cleaned = cleaned.replace(/\n?```\s*$/i, "");
|
|
1904
|
+
cleaned = cleaned.trim();
|
|
1905
|
+
try {
|
|
1906
|
+
return JSON.parse(cleaned);
|
|
1907
|
+
} catch (error) {
|
|
1908
|
+
logger.error(`[safeParseJSON Error]: 无法解析 JSON,原始内容: ${jsonString.substring(0, 200)}...`);
|
|
1909
|
+
throw error;
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
__name(safeParseJSON, "safeParseJSON");
|
|
1897
1913
|
async function checkAudit(local_config4, wish_content, config, wish_config) {
|
|
1898
1914
|
let prompt_msg = "";
|
|
1899
1915
|
if (!local_config4) return { result: false, reason: "未加载配置文件,请查看日志" };
|
|
@@ -1920,7 +1936,7 @@ async function checkAudit(local_config4, wish_content, config, wish_config) {
|
|
|
1920
1936
|
logger.error("[checkAudit Error]: API返回为空");
|
|
1921
1937
|
return { result: false, reason: "API返回为空" };
|
|
1922
1938
|
}
|
|
1923
|
-
const json =
|
|
1939
|
+
const json = safeParseJSON(resp);
|
|
1924
1940
|
if (json.category == "block") {
|
|
1925
1941
|
logger.info("[checkAudit Info]: 愿望被拒绝,原因: " + json.reason);
|
|
1926
1942
|
return { result: false, reason: json.reason };
|
|
@@ -1958,7 +1974,11 @@ async function gernerationSoluation(local_config4, wish_content, config, wish_co
|
|
|
1958
1974
|
logger.error("[gernerationSoluation Error]: " + err);
|
|
1959
1975
|
return null;
|
|
1960
1976
|
}
|
|
1961
|
-
|
|
1977
|
+
if (!resp) {
|
|
1978
|
+
logger.error("[gernerationSoluation Error]: API返回为空");
|
|
1979
|
+
return null;
|
|
1980
|
+
}
|
|
1981
|
+
const json = safeParseJSON(resp);
|
|
1962
1982
|
const scenario = json.scenario;
|
|
1963
1983
|
if (!scenario) {
|
|
1964
1984
|
logger.info("[gernerationSoluation Info]: API返回有误: " + resp);
|