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.
Files changed (2) hide show
  1. package/lib/index.js +22 -2
  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 = JSON.parse(resp);
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
- const json = JSON.parse(resp);
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);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-cocoyyy-console",
3
3
  "description": "自用koishi插件,功能包含复读,记录黑历史,*人等",
4
- "version": "1.1.3",
4
+ "version": "1.1.4",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "contributors": [