koishi-plugin-cocoyyy-console 1.2.2-alpha.0 → 1.2.2-alpha.1
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,68 @@ function extractImageAndText(content) {
|
|
|
1781
1781
|
return { imageUrls, text };
|
|
1782
1782
|
}
|
|
1783
1783
|
__name(extractImageAndText, "extractImageAndText");
|
|
1784
|
+
function extractFromForwardChildren(children) {
|
|
1785
|
+
const allTexts = [];
|
|
1786
|
+
const allImageUrls = [];
|
|
1787
|
+
for (const msgEl of children) {
|
|
1788
|
+
if (msgEl.type !== "message") continue;
|
|
1789
|
+
const msgChildren = msgEl.children ?? [];
|
|
1790
|
+
const authorEl = msgChildren.find((c) => c.type === "author");
|
|
1791
|
+
const senderName = authorEl?.attrs?.name ?? String(authorEl?.attrs?.id ?? "未知");
|
|
1792
|
+
const msgTexts = [];
|
|
1793
|
+
for (const child of msgChildren) {
|
|
1794
|
+
if (child.type === "text") {
|
|
1795
|
+
const t = child.attrs?.content ?? "";
|
|
1796
|
+
if (t) msgTexts.push(t);
|
|
1797
|
+
} else if (child.type === "img" || child.type === "image") {
|
|
1798
|
+
const url = child.attrs?.src ?? child.attrs?.url ?? "";
|
|
1799
|
+
if (url) allImageUrls.push(url);
|
|
1800
|
+
}
|
|
1801
|
+
}
|
|
1802
|
+
if (msgTexts.length > 0) {
|
|
1803
|
+
allTexts.push(`[${senderName}]: ${msgTexts.join("")}`);
|
|
1804
|
+
}
|
|
1805
|
+
}
|
|
1806
|
+
return { imageUrls: allImageUrls, text: allTexts.join("\n") };
|
|
1807
|
+
}
|
|
1808
|
+
__name(extractFromForwardChildren, "extractFromForwardChildren");
|
|
1809
|
+
async function fetchForwardMessages(session, forwardId) {
|
|
1810
|
+
try {
|
|
1811
|
+
const result = await session.bot.internal.getForwardMsg({ id: forwardId });
|
|
1812
|
+
const messages = result?.messages ?? [];
|
|
1813
|
+
const allTexts = [];
|
|
1814
|
+
const allImageUrls = [];
|
|
1815
|
+
for (const msg of messages) {
|
|
1816
|
+
const sender = msg?.sender?.nickname ?? String(msg?.sender?.user_id ?? "未知");
|
|
1817
|
+
const segments = msg?.message ?? msg?.content ?? [];
|
|
1818
|
+
const msgTexts = [];
|
|
1819
|
+
for (const seg of segments) {
|
|
1820
|
+
if (seg.type === "text") {
|
|
1821
|
+
const t = seg.data?.text ?? "";
|
|
1822
|
+
if (t) msgTexts.push(t);
|
|
1823
|
+
} else if (seg.type === "image") {
|
|
1824
|
+
const url = seg.data?.url ?? seg.data?.file ?? "";
|
|
1825
|
+
if (url) allImageUrls.push(url);
|
|
1826
|
+
}
|
|
1827
|
+
}
|
|
1828
|
+
if (msgTexts.length > 0) {
|
|
1829
|
+
allTexts.push(`[${sender}]: ${msgTexts.join("")}`);
|
|
1830
|
+
}
|
|
1831
|
+
}
|
|
1832
|
+
return { imageUrls: allImageUrls, text: allTexts.join("\n") };
|
|
1833
|
+
} catch (e) {
|
|
1834
|
+
logger.error("[fetchForwardMessages Error]: 获取转发消息失败: " + e);
|
|
1835
|
+
return { imageUrls: [], text: "" };
|
|
1836
|
+
}
|
|
1837
|
+
}
|
|
1838
|
+
__name(fetchForwardMessages, "fetchForwardMessages");
|
|
1839
|
+
function extractForwardId(elements, content) {
|
|
1840
|
+
const forwardEl = elements.find((el) => el.type === "forward");
|
|
1841
|
+
if (forwardEl?.attrs?.id) return String(forwardEl.attrs.id);
|
|
1842
|
+
const match = content.match(/<forward\b[^>]*\bid=["']?([^"'\s>/]+)["']?/i);
|
|
1843
|
+
return match?.[1] ?? null;
|
|
1844
|
+
}
|
|
1845
|
+
__name(extractForwardId, "extractForwardId");
|
|
1784
1846
|
async function checkShitOrNot(session, parttern_msg, config) {
|
|
1785
1847
|
chatHistory = [];
|
|
1786
1848
|
chatHistory.push({ role: "system", content: [{ type: "text", text: parttern_msg }] });
|
|
@@ -1805,6 +1867,59 @@ async function checkShitOrNot(session, parttern_msg, config) {
|
|
|
1805
1867
|
return resp2;
|
|
1806
1868
|
}
|
|
1807
1869
|
__name(checkShitOrNot, "checkShitOrNot");
|
|
1870
|
+
async function checkShitOrNot_v2(session, parttern_msg, config) {
|
|
1871
|
+
chatHistory = [];
|
|
1872
|
+
chatHistory.push({ role: "system", content: [{ type: "text", text: parttern_msg }] });
|
|
1873
|
+
const quoteContent = session.quote.content;
|
|
1874
|
+
const quoteElements = session.quote.elements ?? [];
|
|
1875
|
+
logger.info("[checkShitOrNot_v2 Info]: 引用消息内容: " + quoteContent);
|
|
1876
|
+
let imageUrls = [];
|
|
1877
|
+
let text = "";
|
|
1878
|
+
let isForward = false;
|
|
1879
|
+
const forwardId = extractForwardId(quoteElements, quoteContent);
|
|
1880
|
+
if (forwardId) {
|
|
1881
|
+
isForward = true;
|
|
1882
|
+
logger.info("[checkShitOrNot_v2 Info]: 检测到转发聊天记录,ID: " + forwardId);
|
|
1883
|
+
const forwardEl = quoteElements.find((el) => el.type === "forward");
|
|
1884
|
+
const forwardChildren = forwardEl?.children ?? [];
|
|
1885
|
+
if (forwardChildren.length > 0) {
|
|
1886
|
+
const extracted = extractFromForwardChildren(forwardChildren);
|
|
1887
|
+
imageUrls = extracted.imageUrls;
|
|
1888
|
+
text = extracted.text;
|
|
1889
|
+
logger.info("[checkShitOrNot_v2 Info]: 从 children 提取转发内容,共 " + forwardChildren.length + " 条");
|
|
1890
|
+
}
|
|
1891
|
+
if (!text && imageUrls.length === 0) {
|
|
1892
|
+
logger.info("[checkShitOrNot_v2 Info]: children 为空,尝试通过 NapCat API 拉取转发消息");
|
|
1893
|
+
const extracted = await fetchForwardMessages(session, forwardId);
|
|
1894
|
+
imageUrls = extracted.imageUrls;
|
|
1895
|
+
text = extracted.text;
|
|
1896
|
+
}
|
|
1897
|
+
} else {
|
|
1898
|
+
const extracted = extractImageAndText(quoteContent);
|
|
1899
|
+
imageUrls = extracted.imageUrls;
|
|
1900
|
+
text = extracted.text;
|
|
1901
|
+
}
|
|
1902
|
+
if (!text && imageUrls.length === 0) {
|
|
1903
|
+
logger.warn("[checkShitOrNot_v2 Warn]: 引用消息中未发现文本或图片");
|
|
1904
|
+
return isForward ? "转发聊天记录中未发现可分析的文本或图片" : "引用消息中未发现文本或图片";
|
|
1905
|
+
}
|
|
1906
|
+
logger.info("[checkShitOrNot_v2 Info]: 提取图片 URL: " + imageUrls.join(", "));
|
|
1907
|
+
const displayText = isForward ? `以下是转发聊天记录的内容:
|
|
1908
|
+
|
|
1909
|
+
${text}` : text;
|
|
1910
|
+
const targetContent = [
|
|
1911
|
+
...displayText ? [{ type: "text", text: displayText }] : [],
|
|
1912
|
+
...imageUrls.map((url) => ({ type: "image_url", image_url: { url } }))
|
|
1913
|
+
];
|
|
1914
|
+
chatHistory.push({ role: "user", content: targetContent });
|
|
1915
|
+
const { err: err2, resp: resp2 } = await callOpenRouter(config.api_url, config.api_key, config.api_model, chatHistory);
|
|
1916
|
+
if (err2 != null) {
|
|
1917
|
+
logger.error("[checkShitOrNot_v2 Error]: " + err2);
|
|
1918
|
+
return null;
|
|
1919
|
+
}
|
|
1920
|
+
return resp2;
|
|
1921
|
+
}
|
|
1922
|
+
__name(checkShitOrNot_v2, "checkShitOrNot_v2");
|
|
1808
1923
|
|
|
1809
1924
|
// src/services/son_func/shit_or_not_command.ts
|
|
1810
1925
|
var local_config = null;
|
|
@@ -1851,6 +1966,26 @@ son判断模式列表:
|
|
|
1851
1966
|
think_flag = false;
|
|
1852
1967
|
}
|
|
1853
1968
|
});
|
|
1969
|
+
ctx.command("sonv2 <参数>", "判定是否为史").action(async ({ session }, ...args) => {
|
|
1970
|
+
if (!dev_mode) {
|
|
1971
|
+
if (!is_at_bot_quote(session)) return "请提供正确格式,如: [引用消息] @bot son [判断模式]";
|
|
1972
|
+
}
|
|
1973
|
+
if (think_flag) return "请等待思考结果";
|
|
1974
|
+
const parttern = args?.[0];
|
|
1975
|
+
if (!parttern) return "请提供正确判断模式,如: [引用消息] @bot son [判断模式]";
|
|
1976
|
+
if (!local_config) return "未加载配置文件,请查看日志";
|
|
1977
|
+
const parttern_msg = local_config.shit_or_not?.prompts[parttern];
|
|
1978
|
+
if (!parttern_msg) return "未找到判断模式,请查看日志";
|
|
1979
|
+
await session.send("已收到,正在品鉴中...");
|
|
1980
|
+
think_flag = true;
|
|
1981
|
+
try {
|
|
1982
|
+
let exec = await checkShitOrNot_v2(session, parttern_msg, ai_config);
|
|
1983
|
+
if (exec == null) return "品鉴失败,请查看日志";
|
|
1984
|
+
return exec;
|
|
1985
|
+
} finally {
|
|
1986
|
+
think_flag = false;
|
|
1987
|
+
}
|
|
1988
|
+
});
|
|
1854
1989
|
}
|
|
1855
1990
|
__name(registerShitOrNotCommands, "registerShitOrNotCommands");
|
|
1856
1991
|
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
|
-
|
|
5
|
+
declare function checkShitOrNot_v2(session: Session, parttern_msg: string, config: AiAPIConfig): Promise<string>;
|
|
6
|
+
export { checkShitOrNot, checkShitOrNot_v2, ocrImage };
|