koishi-plugin-cocoyyy-console 1.2.2-alpha.2 → 1.2.2

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 CHANGED
@@ -1781,77 +1781,65 @@ function extractImageAndText(content) {
1781
1781
  return { imageUrls, text };
1782
1782
  }
1783
1783
  __name(extractImageAndText, "extractImageAndText");
1784
- function parseOneBotForwardNodes(nodes) {
1785
- const allTexts = [];
1786
- const allImageUrls = [];
1787
- for (const node of nodes) {
1788
- const sender = node?.sender?.nickname ?? String(node?.sender?.user_id ?? "未知");
1789
- const segments = node?.message ?? node?.content ?? [];
1790
- const msgTexts = [];
1791
- for (const seg of segments) {
1792
- if (seg?.type === "text") {
1784
+ function parseSegments(segments, depth) {
1785
+ const texts = [];
1786
+ const imageUrls = [];
1787
+ for (const seg of segments) {
1788
+ switch (seg?.type) {
1789
+ case "text": {
1793
1790
  const t = seg.data?.text ?? "";
1794
- if (t) msgTexts.push(t);
1795
- } else if (seg?.type === "image") {
1791
+ if (t) texts.push(t);
1792
+ break;
1793
+ }
1794
+ case "image": {
1796
1795
  const url = seg.data?.url ?? seg.data?.file ?? "";
1797
- if (url) allImageUrls.push(url);
1796
+ if (url) imageUrls.push(url);
1797
+ break;
1798
1798
  }
1799
- }
1800
- if (msgTexts.length > 0) {
1801
- allTexts.push(`[${sender}]: ${msgTexts.join("")}`);
1799
+ case "forward": {
1800
+ if (depth >= 1) {
1801
+ texts.push("[嵌套转发消息]");
1802
+ } else {
1803
+ const nestedNodes = seg.data?.content ?? [];
1804
+ if (nestedNodes.length > 0) {
1805
+ const nested = parseOneBotForwardNodes(nestedNodes, depth + 1);
1806
+ if (nested.text) texts.push(`[嵌套转发记录]
1807
+ ${nested.text}
1808
+ [嵌套转发记录结束]`);
1809
+ imageUrls.push(...nested.imageUrls);
1810
+ } else {
1811
+ texts.push("[嵌套转发消息]");
1812
+ }
1813
+ }
1814
+ break;
1815
+ }
1816
+ case "json":
1817
+ case "xml":
1818
+ texts.push("[卡片消息]");
1819
+ break;
1820
+ default:
1821
+ break;
1802
1822
  }
1803
1823
  }
1804
- return { imageUrls: allImageUrls, text: allTexts.join("\n") };
1824
+ return { texts, imageUrls };
1805
1825
  }
1806
- __name(parseOneBotForwardNodes, "parseOneBotForwardNodes");
1807
- function extractFromForwardChildren(children) {
1826
+ __name(parseSegments, "parseSegments");
1827
+ function parseOneBotForwardNodes(nodes, depth = 0) {
1808
1828
  const allTexts = [];
1809
1829
  const allImageUrls = [];
1810
- for (const msgEl of children) {
1811
- if (msgEl.type !== "message") continue;
1812
- const msgChildren = msgEl.children ?? [];
1813
- const authorEl = msgChildren.find((c) => c.type === "author");
1814
- const senderName = authorEl?.attrs?.name ?? String(authorEl?.attrs?.id ?? "未知");
1815
- const msgTexts = [];
1816
- for (const child of msgChildren) {
1817
- if (child.type === "text") {
1818
- const t = child.attrs?.content ?? "";
1819
- if (t) msgTexts.push(t);
1820
- } else if (child.type === "img" || child.type === "image") {
1821
- const url = child.attrs?.src ?? child.attrs?.url ?? "";
1822
- if (url) allImageUrls.push(url);
1823
- }
1824
- }
1825
- if (msgTexts.length > 0) {
1826
- allTexts.push(`[${senderName}]: ${msgTexts.join("")}`);
1830
+ logger.info("[parseOneBotForwardNodes Info]:Depth [" + depth + "], parse node count : " + nodes.length);
1831
+ for (const node of nodes) {
1832
+ const sender = node?.sender?.nickname ?? String(node?.sender?.user_id ?? "未知");
1833
+ const segments = node?.message ?? node?.content ?? [];
1834
+ const { texts, imageUrls } = parseSegments(segments, depth);
1835
+ allImageUrls.push(...imageUrls);
1836
+ if (texts.length > 0) {
1837
+ allTexts.push(`[${sender}]: ${texts.join("")}`);
1827
1838
  }
1828
1839
  }
1829
1840
  return { imageUrls: allImageUrls, text: allTexts.join("\n") };
1830
1841
  }
1831
- __name(extractFromForwardChildren, "extractFromForwardChildren");
1832
- async function fetchForwardMessages(session, forwardId) {
1833
- const internal = session.bot.internal;
1834
- let result;
1835
- try {
1836
- result = await internal.getForwardMsg({ id: forwardId });
1837
- } catch (e1) {
1838
- logger.warn("[fetchForwardMessages Warn]: getForwardMsg 调用失败,尝试 get_forward_msg: " + e1);
1839
- try {
1840
- result = await internal.get_forward_msg({ id: forwardId });
1841
- } catch (e2) {
1842
- logger.error("[fetchForwardMessages Error]: 两种 API 调用均失败: " + e2);
1843
- return { imageUrls: [], text: "" };
1844
- }
1845
- }
1846
- logger.info("[fetchForwardMessages Info]: API 返回结构预览: " + JSON.stringify(result)?.slice(0, 300));
1847
- const messages = result?.messages ?? [];
1848
- if (messages.length === 0) {
1849
- logger.warn("[fetchForwardMessages Warn]: API 返回 messages 为空,result keys: " + Object.keys(result ?? {}).join(", "));
1850
- return { imageUrls: [], text: "" };
1851
- }
1852
- return parseOneBotForwardNodes(messages);
1853
- }
1854
- __name(fetchForwardMessages, "fetchForwardMessages");
1842
+ __name(parseOneBotForwardNodes, "parseOneBotForwardNodes");
1855
1843
  function extractForwardId(elements, content) {
1856
1844
  const forwardEl = elements.find((el) => el.type === "forward");
1857
1845
  if (forwardEl?.attrs?.id) return String(forwardEl.attrs.id);
@@ -1860,76 +1848,37 @@ function extractForwardId(elements, content) {
1860
1848
  }
1861
1849
  __name(extractForwardId, "extractForwardId");
1862
1850
  async function checkShitOrNot(session, parttern_msg, config) {
1863
- chatHistory = [];
1864
- chatHistory.push({ role: "system", content: [{ type: "text", text: parttern_msg }] });
1865
- const quoteContent = session.quote.content;
1866
- logger.info("[checkShitOrNot Info]: 引用消息内容: " + quoteContent);
1867
- const { imageUrls, text } = extractImageAndText(quoteContent);
1868
- if (!text && imageUrls.length === 0) {
1869
- logger.warn("[checkShitOrNot Warn]: 引用消息中未发现文本或图片");
1870
- return "引用消息中未发现文本或图片";
1871
- }
1872
- logger.info("[checkShitOrNot Info]: 提取图片 URL: " + imageUrls.join(", "));
1873
- const targetContent = [
1874
- ...text ? [{ type: "text", text }] : [],
1875
- ...imageUrls.map((url) => ({ type: "image_url", image_url: { url } }))
1876
- ];
1877
- chatHistory.push({ role: "user", content: targetContent });
1878
- const { err: err2, resp: resp2 } = await callOpenRouter(config.api_url, config.api_key, config.api_model, chatHistory);
1879
- if (err2 != null) {
1880
- logger.error("[checkShitOrNot Error]: " + err2);
1881
- return null;
1882
- }
1883
- return resp2;
1884
- }
1885
- __name(checkShitOrNot, "checkShitOrNot");
1886
- async function checkShitOrNot_v2(session, parttern_msg, config) {
1887
1851
  chatHistory = [];
1888
1852
  chatHistory.push({ role: "system", content: [{ type: "text", text: parttern_msg }] });
1889
1853
  const quoteContent = session.quote.content;
1890
1854
  const quoteElements = session.quote.elements ?? [];
1891
- logger.info("[checkShitOrNot_v2 Info]: 引用消息内容: " + quoteContent);
1892
- logger.info("[checkShitOrNot_v2 Info]: quoteElements 长度: " + quoteElements.length);
1855
+ logger.info("[checkShitOrNot Info]: 引用消息内容: " + quoteContent);
1856
+ logger.info("[checkShitOrNot Info]: quoteElements 长度: " + quoteElements.length);
1893
1857
  let imageUrls = [];
1894
1858
  let text = "";
1895
1859
  let isForward = false;
1896
1860
  const forwardId = extractForwardId(quoteElements, quoteContent);
1897
1861
  if (forwardId) {
1898
1862
  isForward = true;
1899
- logger.info("[checkShitOrNot_v2 Info]: 检测到转发聊天记录,ID: " + forwardId);
1863
+ logger.info("[checkShitOrNot Info]: 检测到转发聊天记录,ID: " + forwardId);
1900
1864
  const forwardEl = quoteElements.find((el) => el.type === "forward");
1901
1865
  const attrContent = Array.isArray(forwardEl?.attrs?.content) ? forwardEl.attrs.content : [];
1902
1866
  if (attrContent.length > 0) {
1903
- logger.info("[checkShitOrNot_v2 Info]: 从 attrs.content 提取转发内容,共 " + attrContent.length + " 条");
1867
+ logger.info("[checkShitOrNot Info]: 从 attrs.content 提取转发内容,共 " + attrContent.length + " 条");
1904
1868
  const extracted = parseOneBotForwardNodes(attrContent);
1905
1869
  imageUrls = extracted.imageUrls;
1906
1870
  text = extracted.text;
1907
1871
  }
1908
- if (!text && imageUrls.length === 0) {
1909
- const forwardChildren = forwardEl?.children ?? [];
1910
- if (forwardChildren.length > 0) {
1911
- logger.info("[checkShitOrNot_v2 Info]: 从 children 提取转发内容,共 " + forwardChildren.length + " 条");
1912
- const extracted = extractFromForwardChildren(forwardChildren);
1913
- imageUrls = extracted.imageUrls;
1914
- text = extracted.text;
1915
- }
1916
- }
1917
- if (!text && imageUrls.length === 0) {
1918
- logger.info("[checkShitOrNot_v2 Info]: 本地提取失败,尝试通过 NapCat API 拉取转发消息");
1919
- const extracted = await fetchForwardMessages(session, forwardId);
1920
- imageUrls = extracted.imageUrls;
1921
- text = extracted.text;
1922
- }
1923
1872
  } else {
1924
1873
  const extracted = extractImageAndText(quoteContent);
1925
1874
  imageUrls = extracted.imageUrls;
1926
1875
  text = extracted.text;
1927
1876
  }
1928
1877
  if (!text && imageUrls.length === 0) {
1929
- logger.warn("[checkShitOrNot_v2 Warn]: 引用消息中未发现文本或图片");
1878
+ logger.warn("[checkShitOrNot Warn]: 引用消息中未发现文本或图片");
1930
1879
  return isForward ? "转发聊天记录中未发现可分析的文本或图片" : "引用消息中未发现文本或图片";
1931
1880
  }
1932
- logger.info("[checkShitOrNot_v2 Info]: 提取文本长度: " + text.length + ",图片数: " + imageUrls.length);
1881
+ logger.info("[checkShitOrNot Info]: 提取文本长度: " + text.length + ",图片数: " + imageUrls.length);
1933
1882
  const displayText = isForward ? `以下是转发聊天记录的内容:
1934
1883
 
1935
1884
  ${text}` : text;
@@ -1940,12 +1889,12 @@ ${text}` : text;
1940
1889
  chatHistory.push({ role: "user", content: targetContent });
1941
1890
  const { err: err2, resp: resp2 } = await callOpenRouter(config.api_url, config.api_key, config.api_model, chatHistory);
1942
1891
  if (err2 != null) {
1943
- logger.error("[checkShitOrNot_v2 Error]: " + err2);
1892
+ logger.error("[checkShitOrNot Error]: " + err2);
1944
1893
  return null;
1945
1894
  }
1946
1895
  return resp2;
1947
1896
  }
1948
- __name(checkShitOrNot_v2, "checkShitOrNot_v2");
1897
+ __name(checkShitOrNot, "checkShitOrNot");
1949
1898
 
1950
1899
  // src/services/son_func/shit_or_not_command.ts
1951
1900
  var local_config = null;
@@ -1992,26 +1941,6 @@ son判断模式列表:
1992
1941
  think_flag = false;
1993
1942
  }
1994
1943
  });
1995
- ctx.command("sonv2 <参数>", "判定是否为史").action(async ({ session }, ...args) => {
1996
- if (!dev_mode) {
1997
- if (!is_at_bot_quote(session)) return "请提供正确格式,如: [引用消息] @bot son [判断模式]";
1998
- }
1999
- if (think_flag) return "请等待思考结果";
2000
- const parttern = args?.[0];
2001
- if (!parttern) return "请提供正确判断模式,如: [引用消息] @bot son [判断模式]";
2002
- if (!local_config) return "未加载配置文件,请查看日志";
2003
- const parttern_msg = local_config.shit_or_not?.prompts[parttern];
2004
- if (!parttern_msg) return "未找到判断模式,请查看日志";
2005
- await session.send("已收到,正在品鉴中...");
2006
- think_flag = true;
2007
- try {
2008
- let exec = await checkShitOrNot_v2(session, parttern_msg, ai_config);
2009
- if (exec == null) return "品鉴失败,请查看日志";
2010
- return exec;
2011
- } finally {
2012
- think_flag = false;
2013
- }
2014
- });
2015
1944
  }
2016
1945
  __name(registerShitOrNotCommands, "registerShitOrNotCommands");
2017
1946
  function extractAtId(content) {
@@ -2,5 +2,4 @@ import { Session } from 'koishi';
2
2
  import { AiAPIConfig } from '../../config/config';
3
3
  declare function ocrImage(buffer: Buffer): Promise<string>;
4
4
  declare function checkShitOrNot(session: Session, parttern_msg: string, config: AiAPIConfig): Promise<string>;
5
- declare function checkShitOrNot_v2(session: Session, parttern_msg: string, config: AiAPIConfig): Promise<string>;
6
- export { checkShitOrNot, checkShitOrNot_v2, ocrImage };
5
+ export { checkShitOrNot, ocrImage };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-cocoyyy-console",
3
3
  "description": "自用koishi插件,功能包含复读,记录黑历史,*人等",
4
- "version": "1.2.2-alpha.2",
4
+ "version": "1.2.2",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "contributors": [