koishi-plugin-cocoyyy-console 1.2.2-alpha.0 → 1.2.2-alpha.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,6 +1781,84 @@ 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") {
1793
+ const t = seg.data?.text ?? "";
1794
+ if (t) msgTexts.push(t);
1795
+ } else if (seg?.type === "image") {
1796
+ const url = seg.data?.url ?? seg.data?.file ?? "";
1797
+ if (url) allImageUrls.push(url);
1798
+ }
1799
+ }
1800
+ if (msgTexts.length > 0) {
1801
+ allTexts.push(`[${sender}]: ${msgTexts.join("")}`);
1802
+ }
1803
+ }
1804
+ return { imageUrls: allImageUrls, text: allTexts.join("\n") };
1805
+ }
1806
+ __name(parseOneBotForwardNodes, "parseOneBotForwardNodes");
1807
+ function extractFromForwardChildren(children) {
1808
+ const allTexts = [];
1809
+ 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("")}`);
1827
+ }
1828
+ }
1829
+ return { imageUrls: allImageUrls, text: allTexts.join("\n") };
1830
+ }
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");
1855
+ function extractForwardId(elements, content) {
1856
+ const forwardEl = elements.find((el) => el.type === "forward");
1857
+ if (forwardEl?.attrs?.id) return String(forwardEl.attrs.id);
1858
+ const match = content.match(/<forward\b[^>]*\bid=["']?([^"'\s>/]+)["']?/i);
1859
+ return match?.[1] ?? null;
1860
+ }
1861
+ __name(extractForwardId, "extractForwardId");
1784
1862
  async function checkShitOrNot(session, parttern_msg, config) {
1785
1863
  chatHistory = [];
1786
1864
  chatHistory.push({ role: "system", content: [{ type: "text", text: parttern_msg }] });
@@ -1805,6 +1883,69 @@ async function checkShitOrNot(session, parttern_msg, config) {
1805
1883
  return resp2;
1806
1884
  }
1807
1885
  __name(checkShitOrNot, "checkShitOrNot");
1886
+ async function checkShitOrNot_v2(session, parttern_msg, config) {
1887
+ chatHistory = [];
1888
+ chatHistory.push({ role: "system", content: [{ type: "text", text: parttern_msg }] });
1889
+ const quoteContent = session.quote.content;
1890
+ const quoteElements = session.quote.elements ?? [];
1891
+ logger.info("[checkShitOrNot_v2 Info]: 引用消息内容: " + quoteContent);
1892
+ logger.info("[checkShitOrNot_v2 Info]: quoteElements 长度: " + quoteElements.length);
1893
+ let imageUrls = [];
1894
+ let text = "";
1895
+ let isForward = false;
1896
+ const forwardId = extractForwardId(quoteElements, quoteContent);
1897
+ if (forwardId) {
1898
+ isForward = true;
1899
+ logger.info("[checkShitOrNot_v2 Info]: 检测到转发聊天记录,ID: " + forwardId);
1900
+ const forwardEl = quoteElements.find((el) => el.type === "forward");
1901
+ const attrContent = Array.isArray(forwardEl?.attrs?.content) ? forwardEl.attrs.content : [];
1902
+ if (attrContent.length > 0) {
1903
+ logger.info("[checkShitOrNot_v2 Info]: 从 attrs.content 提取转发内容,共 " + attrContent.length + " 条");
1904
+ const extracted = parseOneBotForwardNodes(attrContent);
1905
+ imageUrls = extracted.imageUrls;
1906
+ text = extracted.text;
1907
+ }
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
+ } else {
1924
+ const extracted = extractImageAndText(quoteContent);
1925
+ imageUrls = extracted.imageUrls;
1926
+ text = extracted.text;
1927
+ }
1928
+ if (!text && imageUrls.length === 0) {
1929
+ logger.warn("[checkShitOrNot_v2 Warn]: 引用消息中未发现文本或图片");
1930
+ return isForward ? "转发聊天记录中未发现可分析的文本或图片" : "引用消息中未发现文本或图片";
1931
+ }
1932
+ logger.info("[checkShitOrNot_v2 Info]: 提取文本长度: " + text.length + ",图片数: " + imageUrls.length);
1933
+ const displayText = isForward ? `以下是转发聊天记录的内容:
1934
+
1935
+ ${text}` : text;
1936
+ const targetContent = [
1937
+ ...displayText ? [{ type: "text", text: displayText }] : [],
1938
+ ...imageUrls.map((url) => ({ type: "image_url", image_url: { url } }))
1939
+ ];
1940
+ chatHistory.push({ role: "user", content: targetContent });
1941
+ const { err: err2, resp: resp2 } = await callOpenRouter(config.api_url, config.api_key, config.api_model, chatHistory);
1942
+ if (err2 != null) {
1943
+ logger.error("[checkShitOrNot_v2 Error]: " + err2);
1944
+ return null;
1945
+ }
1946
+ return resp2;
1947
+ }
1948
+ __name(checkShitOrNot_v2, "checkShitOrNot_v2");
1808
1949
 
1809
1950
  // src/services/son_func/shit_or_not_command.ts
1810
1951
  var local_config = null;
@@ -1851,6 +1992,26 @@ son判断模式列表:
1851
1992
  think_flag = false;
1852
1993
  }
1853
1994
  });
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
+ });
1854
2015
  }
1855
2016
  __name(registerShitOrNotCommands, "registerShitOrNotCommands");
1856
2017
  function extractAtId(content) {
@@ -2,4 +2,5 @@ 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
- export { checkShitOrNot, ocrImage };
5
+ declare function checkShitOrNot_v2(session: Session, parttern_msg: string, config: AiAPIConfig): Promise<string>;
6
+ export { checkShitOrNot, checkShitOrNot_v2, 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.0",
4
+ "version": "1.2.2-alpha.2",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "contributors": [