koishi-plugin-cocoyyy-console 1.1.3 → 1.1.5

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 +52 -4
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -1894,6 +1894,50 @@ 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");
1913
+ function findNestedValue(obj, key) {
1914
+ if (obj === null || obj === void 0) {
1915
+ return void 0;
1916
+ }
1917
+ if (typeof obj === "object" && !Array.isArray(obj)) {
1918
+ if (key in obj) {
1919
+ return obj[key];
1920
+ }
1921
+ for (const prop in obj) {
1922
+ if (obj.hasOwnProperty(prop)) {
1923
+ const result = findNestedValue(obj[prop], key);
1924
+ if (result !== void 0) {
1925
+ return result;
1926
+ }
1927
+ }
1928
+ }
1929
+ }
1930
+ if (Array.isArray(obj)) {
1931
+ for (const item of obj) {
1932
+ const result = findNestedValue(item, key);
1933
+ if (result !== void 0) {
1934
+ return result;
1935
+ }
1936
+ }
1937
+ }
1938
+ return void 0;
1939
+ }
1940
+ __name(findNestedValue, "findNestedValue");
1897
1941
  async function checkAudit(local_config4, wish_content, config, wish_config) {
1898
1942
  let prompt_msg = "";
1899
1943
  if (!local_config4) return { result: false, reason: "未加载配置文件,请查看日志" };
@@ -1920,7 +1964,7 @@ async function checkAudit(local_config4, wish_content, config, wish_config) {
1920
1964
  logger.error("[checkAudit Error]: API返回为空");
1921
1965
  return { result: false, reason: "API返回为空" };
1922
1966
  }
1923
- const json = JSON.parse(resp);
1967
+ const json = safeParseJSON(resp);
1924
1968
  if (json.category == "block") {
1925
1969
  logger.info("[checkAudit Info]: 愿望被拒绝,原因: " + json.reason);
1926
1970
  return { result: false, reason: json.reason };
@@ -1958,10 +2002,14 @@ async function gernerationSoluation(local_config4, wish_content, config, wish_co
1958
2002
  logger.error("[gernerationSoluation Error]: " + err);
1959
2003
  return null;
1960
2004
  }
1961
- const json = JSON.parse(resp);
1962
- const scenario = json.scenario;
2005
+ if (!resp) {
2006
+ logger.error("[gernerationSoluation Error]: API返回为空");
2007
+ return null;
2008
+ }
2009
+ const json = safeParseJSON(resp);
2010
+ const scenario = json.scenario || findNestedValue(json, "scenario");
1963
2011
  if (!scenario) {
1964
- logger.info("[gernerationSoluation Info]: API返回有误: " + resp);
2012
+ logger.info("[gernerationSoluation Info]: API返回有误,未找到scenario字段: " + resp);
1965
2013
  return null;
1966
2014
  }
1967
2015
  return scenario;
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.5",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "contributors": [