@nestim/koishi-plugin-qq-group-manager 1.2.9 → 1.3.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/CHANGELOG.md +20 -0
- package/lib/ai/engine.d.ts +2 -0
- package/lib/ai/engine.js +40 -0
- package/lib/ai/prompt.js +4 -1
- package/lib/ai/state.d.ts +2 -0
- package/lib/core/text.d.ts +7 -0
- package/lib/core/text.js +22 -0
- package/lib/index.d.ts +8 -0
- package/lib/index.js +8 -0
- package/lib/service.d.ts +1 -1
- package/lib/service.js +5 -2
- package/package.json +1 -1
- package/readme.md +35 -1
- package/src/ai/engine.ts +34 -0
- package/src/ai/prompt.ts +4 -1
- package/src/ai/state.ts +2 -0
- package/src/core/text.ts +30 -0
- package/src/index.ts +8 -0
- package/src/service.js +5 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
本文件记录所有值得注意的变更。版本号遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
|
|
4
4
|
|
|
5
|
+
## 1.3.1
|
|
6
|
+
|
|
7
|
+
### 文档
|
|
8
|
+
|
|
9
|
+
- README 增加配套插件跳转:`@nestim/koishi-plugin-qqgm-chat-style`(聊天风格)与
|
|
10
|
+
`@nestim/koishi-plugin-qqgm-meme`(表情包),以及两个可直接使用的聊天风格 skill
|
|
11
|
+
(`northern-netizen-chat`、`naruto-mobile-chat`)。
|
|
12
|
+
- 明确标注**定时提醒内建在本插件**,无需额外安装。
|
|
13
|
+
|
|
14
|
+
## 1.3.0
|
|
15
|
+
|
|
16
|
+
### 新增
|
|
17
|
+
|
|
18
|
+
- **回复引用可随机化**:新增 `aiQuoteReplyMode`(`random` / `always` / `never`)与
|
|
19
|
+
`aiQuoteReplyProbability`(默认 0.5)。不再强制每条回复都引用触发消息,
|
|
20
|
+
更像正常群友插话。
|
|
21
|
+
- **读取合并转发**:`aiReadForwardMessage`(默认开)+ `aiForwardMaxNodes`(默认 20)。
|
|
22
|
+
通过 OneBot `get_forward_msg` 拉取子消息,按“发言人: 内容”逐行注入上下文;
|
|
23
|
+
适配器不支持或失败时降级为 `[合并转发]` 占位。
|
|
24
|
+
|
|
5
25
|
## 1.2.9
|
|
6
26
|
|
|
7
27
|
### 变更
|
package/lib/ai/engine.d.ts
CHANGED
|
@@ -81,6 +81,8 @@ export declare class AiEngine {
|
|
|
81
81
|
private webSearch;
|
|
82
82
|
/** 模型自己都说不知道/搜不了时,补一次搜索再重写;只在不认识的问题上多一次调用。 */
|
|
83
83
|
private shouldReviseWithSearch;
|
|
84
|
+
/** 读取合并转发的子消息,压成一行行文本;读不到就返回空串。 */
|
|
85
|
+
private readForwards;
|
|
84
86
|
private recentImageRecords;
|
|
85
87
|
private collectImages;
|
|
86
88
|
private judge;
|
package/lib/ai/engine.js
CHANGED
|
@@ -173,6 +173,7 @@ class AiEngine {
|
|
|
173
173
|
text: rendered.text,
|
|
174
174
|
images: rendered.images,
|
|
175
175
|
quote: rendered.quote,
|
|
176
|
+
forwardText: await this.readForwards(session, rendered.forwards || []),
|
|
176
177
|
mentions: rendered.mentions,
|
|
177
178
|
atBot: rendered.atBot,
|
|
178
179
|
self: false,
|
|
@@ -642,6 +643,45 @@ class AiEngine {
|
|
|
642
643
|
return false;
|
|
643
644
|
return /(不知道|记不清|记不准|记不牢|不敢乱说|说岔|没法上网|查不了|搜不了|没有联网|无法联网|不确定|不太清楚|分不清|瞎猜)/.test(raw);
|
|
644
645
|
}
|
|
646
|
+
/** 读取合并转发的子消息,压成一行行文本;读不到就返回空串。 */
|
|
647
|
+
async readForwards(session, forwards) {
|
|
648
|
+
if (this.config.aiReadForwardMessage === false)
|
|
649
|
+
return '';
|
|
650
|
+
if (!Array.isArray(forwards) || !forwards.length)
|
|
651
|
+
return '';
|
|
652
|
+
const limit = Math.max(1, Math.min(50, Number(this.config.aiForwardMaxNodes ?? 20)));
|
|
653
|
+
const onebot = this.host.getOneBotApi(session);
|
|
654
|
+
if (!onebot?.getForwardMsg)
|
|
655
|
+
return '';
|
|
656
|
+
const blocks = [];
|
|
657
|
+
for (const item of forwards.slice(0, 3)) {
|
|
658
|
+
const id = String(item?.id || '').trim();
|
|
659
|
+
if (!id)
|
|
660
|
+
continue;
|
|
661
|
+
try {
|
|
662
|
+
const data = await onebot.getForwardMsg(id);
|
|
663
|
+
const messages = Array.isArray(data?.messages) ? data.messages
|
|
664
|
+
: (Array.isArray(data?.message) ? data.message : (Array.isArray(data) ? data : []));
|
|
665
|
+
for (const node of messages.slice(0, limit)) {
|
|
666
|
+
const sender = String(node?.sender?.card || node?.sender?.nickname || node?.nickname || node?.name || '').trim();
|
|
667
|
+
const content = Array.isArray(node?.message)
|
|
668
|
+
? node.message.filter((seg) => seg?.type === 'text').map((seg) => String(seg?.data?.text ?? '')).join('')
|
|
669
|
+
: String(node?.content ?? node?.raw_message ?? node?.message ?? '');
|
|
670
|
+
const text = content.replace(/\[CQ:[^\]]+\]/g, '[图片]').replace(/\s+/g, ' ').trim();
|
|
671
|
+
if (!text)
|
|
672
|
+
continue;
|
|
673
|
+
blocks.push(`${sender ? `${sender}: ` : ''}${(0, text_1.truncate)(text, 160)}`);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
catch (error) {
|
|
677
|
+
this.log('forward', { ok: false, error: String(error) }, session);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
if (!blocks.length)
|
|
681
|
+
return '';
|
|
682
|
+
this.log('forward', { ok: true, nodes: blocks.length }, session);
|
|
683
|
+
return blocks.join('\n');
|
|
684
|
+
}
|
|
645
685
|
recentImageRecords(state, record, maxAgeMs = 120000) {
|
|
646
686
|
const cutoff = Number(record.at) - maxAgeMs;
|
|
647
687
|
const candidates = state.records
|
package/lib/ai/prompt.js
CHANGED
|
@@ -53,7 +53,10 @@ function renderRecordLine(record, selfName) {
|
|
|
53
53
|
? `「回复 ${record.quote.name}:${(0, text_1.truncate)(record.quote.text, 40)}」`
|
|
54
54
|
: '';
|
|
55
55
|
const body = quote ? `${quote}${record.text}` : record.text;
|
|
56
|
-
|
|
56
|
+
const forward = record.forwardText
|
|
57
|
+
? `\n └ 转发内容:\n${record.forwardText.split('\n').map((line) => ` ${(0, text_1.truncate)(line, 120)}`).join('\n')}`
|
|
58
|
+
: '';
|
|
59
|
+
return `[${time}] ${speaker(record, selfName)}: ${body || '[空消息]'}${forward}`;
|
|
57
60
|
}
|
|
58
61
|
function buildSystemPrompt(input) {
|
|
59
62
|
const lines = [];
|
package/lib/ai/state.d.ts
CHANGED
package/lib/core/text.d.ts
CHANGED
|
@@ -20,6 +20,11 @@ export interface RenderedQuote {
|
|
|
20
20
|
/** 被引用消息的 messageId,拿不到图片时可用于 get_msg */
|
|
21
21
|
id: string;
|
|
22
22
|
}
|
|
23
|
+
/** 合并转发:只保留 id 和摘要,正文由引擎按需向 OneBot 拉取 */
|
|
24
|
+
export interface RenderedForward {
|
|
25
|
+
id: string;
|
|
26
|
+
summary: string;
|
|
27
|
+
}
|
|
23
28
|
export interface RenderedMessage {
|
|
24
29
|
/** 已渲染的正文(含 @名字 / [图片] 等占位) */
|
|
25
30
|
text: string;
|
|
@@ -27,6 +32,8 @@ export interface RenderedMessage {
|
|
|
27
32
|
images: RenderedImage[];
|
|
28
33
|
/** 引用(被回复)的那条消息 */
|
|
29
34
|
quote?: RenderedQuote;
|
|
35
|
+
/** 本条消息里的合并转发(可再拉取子消息内容) */
|
|
36
|
+
forwards: RenderedForward[];
|
|
30
37
|
/** 正文里点名到的 QQ 号(不含 bot 自己) */
|
|
31
38
|
mentions: string[];
|
|
32
39
|
/** 本条消息是否 @ 了 bot(与 session.content 的格式无关) */
|
package/lib/core/text.js
CHANGED
|
@@ -143,6 +143,15 @@ function renderFromContent(content, selfId, resolveName) {
|
|
|
143
143
|
text = text.replace(/\[CQ:file,([^\]]*)\]/gi, ' [文件] ');
|
|
144
144
|
return { text: stripTags(text), images, mentions };
|
|
145
145
|
}
|
|
146
|
+
/** 从合并转发元素里抽 id 与摘要(napcat 常见字段:id / resid / content / summary) */
|
|
147
|
+
function forwardFromElement(el) {
|
|
148
|
+
const attrs = el?.attrs ?? el ?? {};
|
|
149
|
+
const id = String(attrs.id ?? attrs.resid ?? attrs.resId ?? attrs.forwardId ?? attrs.content ?? '').trim();
|
|
150
|
+
if (!id)
|
|
151
|
+
return null;
|
|
152
|
+
const summary = String(attrs.summary ?? attrs.title ?? attrs.name ?? attrs.desc ?? '').trim();
|
|
153
|
+
return { id, summary };
|
|
154
|
+
}
|
|
146
155
|
/**
|
|
147
156
|
* 渲染一条消息。
|
|
148
157
|
* - @ 会解析成群名片(`@你` 表示 @bot)
|
|
@@ -154,6 +163,7 @@ async function renderMessage(session, resolveName, options = {}) {
|
|
|
154
163
|
const quoteMax = options.quoteMaxChars ?? 60;
|
|
155
164
|
const elements = Array.isArray(session?.elements) ? session.elements : [];
|
|
156
165
|
const images = [];
|
|
166
|
+
const forwards = [];
|
|
157
167
|
const mentions = [];
|
|
158
168
|
const parts = [];
|
|
159
169
|
let atBot = false;
|
|
@@ -161,9 +171,17 @@ async function renderMessage(session, resolveName, options = {}) {
|
|
|
161
171
|
const fallback = renderFromContent(String(session?.content ?? ''), selfId, resolveName);
|
|
162
172
|
// 兜底路径无法异步解析 @ 名字,尽力把 html 里的 name 用上
|
|
163
173
|
const withNames = await replaceMentionsWithNames(fallback.text, resolveName, selfId);
|
|
174
|
+
const forwardsFromCq = [];
|
|
175
|
+
for (const match of String(session?.content ?? '').matchAll(/\[CQ:(?:forward|node),([^\]]*)\]/gi)) {
|
|
176
|
+
const attrs = cqAttrs(match[0]);
|
|
177
|
+
const id = String(attrs.id ?? attrs.resid ?? attrs.content ?? '').trim();
|
|
178
|
+
if (id)
|
|
179
|
+
forwardsFromCq.push({ id, summary: String(attrs.summary ?? '') });
|
|
180
|
+
}
|
|
164
181
|
return {
|
|
165
182
|
text: withNames.text,
|
|
166
183
|
images: fallback.images,
|
|
184
|
+
forwards: forwardsFromCq,
|
|
167
185
|
mentions: [...new Set([...fallback.mentions, ...withNames.mentions])],
|
|
168
186
|
atBot: !!selfId && (fallback.text.includes('@你')
|
|
169
187
|
|| fallback.text.includes(`<at id="${selfId}"`)
|
|
@@ -219,6 +237,9 @@ async function renderMessage(session, resolveName, options = {}) {
|
|
|
219
237
|
continue;
|
|
220
238
|
}
|
|
221
239
|
if (FORWARD_TYPES.has(type)) {
|
|
240
|
+
const forward = forwardFromElement(el);
|
|
241
|
+
if (forward)
|
|
242
|
+
forwards.push(forward);
|
|
222
243
|
parts.push(' [合并转发] ');
|
|
223
244
|
continue;
|
|
224
245
|
}
|
|
@@ -246,6 +267,7 @@ async function renderMessage(session, resolveName, options = {}) {
|
|
|
246
267
|
text,
|
|
247
268
|
images,
|
|
248
269
|
quote,
|
|
270
|
+
forwards,
|
|
249
271
|
mentions: [...new Set(mentions)],
|
|
250
272
|
atBot,
|
|
251
273
|
plain: text,
|
package/lib/index.d.ts
CHANGED
|
@@ -145,6 +145,10 @@ export declare const Config: Schema<Schemastery.ObjectS<{
|
|
|
145
145
|
aiTemperature: Schema<number, number>;
|
|
146
146
|
aiMaxOutputTokens: Schema<number, number>;
|
|
147
147
|
aiMaxReplyChars: Schema<number, number>;
|
|
148
|
+
aiQuoteReplyMode: Schema<"random" | "always" | "never", "random" | "always" | "never">;
|
|
149
|
+
aiQuoteReplyProbability: Schema<number, number>;
|
|
150
|
+
aiReadForwardMessage: Schema<boolean, boolean>;
|
|
151
|
+
aiForwardMaxNodes: Schema<number, number>;
|
|
148
152
|
aiReplyDecision: Schema<"two-stage" | "single", "two-stage" | "single">;
|
|
149
153
|
aiEnableImageRecognition: Schema<boolean, boolean>;
|
|
150
154
|
aiImageMaxCount: Schema<number, number>;
|
|
@@ -292,6 +296,10 @@ export declare const Config: Schema<Schemastery.ObjectS<{
|
|
|
292
296
|
aiTemperature: number;
|
|
293
297
|
aiMaxOutputTokens: number;
|
|
294
298
|
aiMaxReplyChars: number;
|
|
299
|
+
aiQuoteReplyMode: "random" | "always" | "never";
|
|
300
|
+
aiQuoteReplyProbability: number;
|
|
301
|
+
aiReadForwardMessage: boolean;
|
|
302
|
+
aiForwardMaxNodes: number;
|
|
295
303
|
aiReplyDecision: "two-stage" | "single";
|
|
296
304
|
aiEnableImageRecognition: boolean;
|
|
297
305
|
aiImageMaxCount: number;
|
package/lib/index.js
CHANGED
|
@@ -137,6 +137,14 @@ exports.Config = koishi_1.Schema.intersect([
|
|
|
137
137
|
aiTemperature: koishi_1.Schema.number().min(0).max(2).step(0.1).default(0.8).description('生成温度(0~2)。群聊接话建议 0.7~0.9,过高会跑题、复述、发散。'),
|
|
138
138
|
aiMaxOutputTokens: koishi_1.Schema.natural().default(400).description('AI 最大输出 token 数。群聊一句话不需要太大,过大会增加延迟。'),
|
|
139
139
|
aiMaxReplyChars: koishi_1.Schema.natural().min(20).max(500).default(120).description('单条回复的最大字符数,超出会在句末截断。'),
|
|
140
|
+
aiQuoteReplyMode: koishi_1.Schema.union([
|
|
141
|
+
koishi_1.Schema.const('random').description('随机决定'),
|
|
142
|
+
koishi_1.Schema.const('always').description('总是引用'),
|
|
143
|
+
koishi_1.Schema.const('never').description('从不引用'),
|
|
144
|
+
]).role('radio').default('random').description('AI 回复是否引用触发它的那条消息。'),
|
|
145
|
+
aiQuoteReplyProbability: koishi_1.Schema.number().min(0).max(1).step(0.05).default(0.5).hidden().description('引用概率(仅 random 模式生效)。'),
|
|
146
|
+
aiReadForwardMessage: koishi_1.Schema.boolean().default(true).description('读取合并转发聊天记录的内容,作为上下文的一部分。'),
|
|
147
|
+
aiForwardMaxNodes: koishi_1.Schema.natural().min(1).max(50).default(20).description('单条合并转发最多读取多少条子消息。'),
|
|
140
148
|
aiReplyDecision: koishi_1.Schema.union([
|
|
141
149
|
koishi_1.Schema.const('single').description('一次调用:模型直接输出回复,或用 <pass/> 放弃(推荐)'),
|
|
142
150
|
koishi_1.Schema.const('two-stage').description('两次调用:先判定是否值得回复,再生成正文'),
|
package/lib/service.d.ts
CHANGED
|
@@ -319,7 +319,7 @@ export class QQGroupManagerService extends koishi_1.Service<any, koishi_1.Contex
|
|
|
319
319
|
fetchQuotedImages(session: any, quote: any): Promise<any[]>;
|
|
320
320
|
/** 供 AI 引擎把图片引用转成 data URL */
|
|
321
321
|
resolveImage(ref: any, session: any): Promise<any>;
|
|
322
|
-
/** AI
|
|
322
|
+
/** AI 回复发送:按 aiQuoteReplyMode 决定是否引用触发消息,失败则退化为纯文本 */
|
|
323
323
|
sendReply(session: any, text: any): Promise<void>;
|
|
324
324
|
extractTextForFilter(session: any): any;
|
|
325
325
|
hasTextContent(session: any): boolean;
|
package/lib/service.js
CHANGED
|
@@ -2057,11 +2057,14 @@ class QQGroupManagerService extends koishi_1.Service {
|
|
|
2057
2057
|
resolveImage(ref, session) {
|
|
2058
2058
|
return this.resolveImageReferenceToDataUrl(ref, session);
|
|
2059
2059
|
}
|
|
2060
|
-
/** AI
|
|
2060
|
+
/** AI 回复发送:按 aiQuoteReplyMode 决定是否引用触发消息,失败则退化为纯文本 */
|
|
2061
2061
|
async sendReply(session, text) {
|
|
2062
2062
|
if (!text)
|
|
2063
2063
|
return;
|
|
2064
|
-
|
|
2064
|
+
const mode = String(this.config?.aiQuoteReplyMode || 'random');
|
|
2065
|
+
const probability = Math.min(1, Math.max(0, Number(this.config?.aiQuoteReplyProbability ?? 0.5)));
|
|
2066
|
+
const shouldQuote = !!session.messageId && (mode === 'always' || (mode === 'random' && Math.random() < probability));
|
|
2067
|
+
if (shouldQuote) {
|
|
2065
2068
|
try {
|
|
2066
2069
|
await session.send([koishi_1.h.quote(session.messageId), koishi_1.h.text(text)]);
|
|
2067
2070
|
return;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nestim/koishi-plugin-qq-group-manager",
|
|
3
3
|
"description": "Koishi QQ群管理插件(OneBot/LLOneBot):群管命令、权限校验、图片菜单、状态卡片、复读、AI群聊回复、记忆库、识图与自动管控(禁言/踢人)。",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.3.1",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
7
7
|
"files": [
|
package/readme.md
CHANGED
|
@@ -101,6 +101,10 @@ npm i @nestim/koishi-plugin-qq-group-manager
|
|
|
101
101
|
| `aiContextWindow` | `30` | 进入提示词的消息条数(建议 20~40) |
|
|
102
102
|
| `aiReplyDecision` | `single` | `single` 一次调用(推荐)/ `two-stage` 两次调用 |
|
|
103
103
|
| `aiMaxReplyChars` | `120` | 单条回复最大字符数 |
|
|
104
|
+
| `aiQuoteReplyMode` | `random` | 回复是否引用触发消息:`random` / `always` / `never` |
|
|
105
|
+
| `aiQuoteReplyProbability` | `0.5` | `random` 模式下的引用概率 |
|
|
106
|
+
| `aiReadForwardMessage` | `true` | 读取合并转发聊天记录作为上下文 |
|
|
107
|
+
| `aiForwardMaxNodes` | `20` | 单条合并转发最多读取多少条子消息 |
|
|
104
108
|
| `aiImageBudgetMs` | `6000` | 图片解析总预算(毫秒),超时跳过 |
|
|
105
109
|
| `aiWebSearchEnabled` | `true` | 启用联网搜索(DeepSeek Search API,复用 `aiApiKey`) |
|
|
106
110
|
| `aiWebSearchMode` | `auto` | `auto` 自动判断 / `always` 每次都搜 / `off` 关闭 |
|
|
@@ -141,11 +145,23 @@ npm i @nestim/koishi-plugin-qq-group-manager
|
|
|
141
145
|
如果模型第一次回答表示“不知道/记不清/搜不了”,会补一次搜索并重写最终回复(仅此时多一次模型调用)。
|
|
142
146
|
默认每群每小时最多 10 次,相同 query 缓存 10 分钟。
|
|
143
147
|
- **输出**:单条消息,`aiMaxReplyChars`(默认 120)限制长度;推荐 `aiTemperature` 0.7~0.9。
|
|
148
|
+
`aiQuoteReplyMode` 控制是否引用触发消息:`random`(默认,按 `aiQuoteReplyProbability` 掷骰)、
|
|
149
|
+
`always`、`never`。不引用时直接发到群里,不会出现"每条都挂一条引用"的机械感。
|
|
150
|
+
- **合并转发**:开启 `aiReadForwardMessage` 后,消息里的合并转发会通过 OneBot `get_forward_msg`
|
|
151
|
+
拉取子消息,按“发言人: 内容”逐行注入上下文(最多 `aiForwardMaxNodes` 条)。
|
|
152
|
+
适配器不支持或拉取失败时降级为 `[合并转发]` 占位,不影响正常回复。
|
|
144
153
|
- **调试**:`aiDebugPrompt` 打开后会把最终提示词写进日志,便于排查"到底看到了什么"。
|
|
145
154
|
- **扩展点**:`ctx.nestimQqGroupManager.aiHooks`(接口版本 1)。外部插件可
|
|
146
155
|
`registerPromptSection()` 追加风格段、`registerOutputFilter()` 过滤或丢弃输出;
|
|
147
156
|
钩子异常会被隔离,不影响正常回复。跨插件使用时请注入 root 上的 `qqgmAiHooks`。
|
|
148
|
-
|
|
157
|
+
配套插件已单独发布,按需安装:
|
|
158
|
+
|
|
159
|
+
| 插件 | 作用 | 地址 |
|
|
160
|
+
| --- | --- | --- |
|
|
161
|
+
| `@nestim/koishi-plugin-qqgm-chat-style` | 聊天风格:一个主风格 + 多个按概率随机叠加的追加风格 | [npm](https://www.npmjs.com/package/@nestim/koishi-plugin-qqgm-chat-style) · [GitHub](https://github.com/Miseat/koishi-plugin-qqgm-chat-style) |
|
|
162
|
+
| `@nestim/koishi-plugin-qqgm-meme` | 表情包:AI 用 `[表情: 情绪]` 抽图,按概率补在文字后面 | [npm](https://www.npmjs.com/package/@nestim/koishi-plugin-qqgm-meme) · [GitHub](https://github.com/Miseat/koishi-plugin-qqgm-meme) |
|
|
163
|
+
|
|
164
|
+
定时提醒(`[提醒] 明天 8 点 开会`)**内建在本插件里**,不需要额外安装。
|
|
149
165
|
|
|
150
166
|
## 外部插件接口(1.2.2+)
|
|
151
167
|
|
|
@@ -314,6 +330,24 @@ Moderation commands (`[mute]` `[unmute]` `[kick]` `[ban]` `[unban]` `[banlist]`)
|
|
|
314
330
|
Both steps must pass. Authorization details and command results are written to logs only and
|
|
315
331
|
are never echoed into the group.
|
|
316
332
|
|
|
333
|
+
## 相关插件 / Related plugins
|
|
334
|
+
|
|
335
|
+
主插件是这套东西的核心,其余插件都通过它暴露的 `qqgmAiHooks` 挂上来,可以自由组合:
|
|
336
|
+
|
|
337
|
+
| 插件 | 作用 | 链接 |
|
|
338
|
+
| --- | --- | --- |
|
|
339
|
+
| **`@nestim/koishi-plugin-qq-group-manager`** | 本包:群管、消息管控、自动处罚、黑名单、入群审核、记忆库、AI 回复、识图、联网搜索、定时提醒 | [npm](https://www.npmjs.com/package/@nestim/koishi-plugin-qq-group-manager) · [GitHub](https://github.com/Miseat/koishi-plugin-qq-group-manager) |
|
|
340
|
+
| `@nestim/koishi-plugin-qqgm-chat-style` | 聊天风格:一个主风格决定人设,多个追加风格按各自概率随机加味,主风格永远优先 | [npm](https://www.npmjs.com/package/@nestim/koishi-plugin-qqgm-chat-style) · [GitHub](https://github.com/Miseat/koishi-plugin-qqgm-chat-style) |
|
|
341
|
+
| `@nestim/koishi-plugin-qqgm-meme` | 表情包:AI 用 `[表情: 情绪]` 表达想发哪类图,按概率从本地图库抽一张补在文字后面 | [npm](https://www.npmjs.com/package/@nestim/koishi-plugin-qqgm-meme) · [GitHub](https://github.com/Miseat/koishi-plugin-qqgm-meme) |
|
|
342
|
+
|
|
343
|
+
配套的聊天风格 skill(可直接放进 `data/chat-styles/`):
|
|
344
|
+
|
|
345
|
+
- [northern-netizen-chat](https://github.com/Miseat/northern-netizen-chat) —— 中文北方网友,短句、带刺、归因降维
|
|
346
|
+
- [naruto-mobile-chat](https://github.com/Miseat/naruto-mobile-chat) —— 火影忍者手游圈口癖,适合当追加风格
|
|
347
|
+
|
|
348
|
+
All companion plugins attach through the `qqgmAiHooks` extension point this package exposes,
|
|
349
|
+
so you can mix and match. Timing reminders are built in — no extra package needed.
|
|
350
|
+
|
|
317
351
|
## License
|
|
318
352
|
|
|
319
353
|
MIT
|
package/src/ai/engine.ts
CHANGED
|
@@ -214,6 +214,7 @@ export class AiEngine {
|
|
|
214
214
|
text: rendered.text,
|
|
215
215
|
images: rendered.images,
|
|
216
216
|
quote: rendered.quote,
|
|
217
|
+
forwardText: await this.readForwards(session, rendered.forwards || []),
|
|
217
218
|
mentions: rendered.mentions,
|
|
218
219
|
atBot: rendered.atBot,
|
|
219
220
|
self: false,
|
|
@@ -684,6 +685,39 @@ export class AiEngine {
|
|
|
684
685
|
return /(不知道|记不清|记不准|记不牢|不敢乱说|说岔|没法上网|查不了|搜不了|没有联网|无法联网|不确定|不太清楚|分不清|瞎猜)/.test(raw);
|
|
685
686
|
}
|
|
686
687
|
|
|
688
|
+
/** 读取合并转发的子消息,压成一行行文本;读不到就返回空串。 */
|
|
689
|
+
private async readForwards(session: any, forwards: any[]): Promise<string> {
|
|
690
|
+
if (this.config.aiReadForwardMessage === false) return '';
|
|
691
|
+
if (!Array.isArray(forwards) || !forwards.length) return '';
|
|
692
|
+
const limit = Math.max(1, Math.min(50, Number(this.config.aiForwardMaxNodes ?? 20)));
|
|
693
|
+
const onebot = this.host.getOneBotApi(session);
|
|
694
|
+
if (!onebot?.getForwardMsg) return '';
|
|
695
|
+
const blocks: string[] = [];
|
|
696
|
+
for (const item of forwards.slice(0, 3)) {
|
|
697
|
+
const id = String(item?.id || '').trim();
|
|
698
|
+
if (!id) continue;
|
|
699
|
+
try {
|
|
700
|
+
const data = await onebot.getForwardMsg(id);
|
|
701
|
+
const messages = Array.isArray(data?.messages) ? data.messages
|
|
702
|
+
: (Array.isArray(data?.message) ? data.message : (Array.isArray(data) ? data : []));
|
|
703
|
+
for (const node of messages.slice(0, limit)) {
|
|
704
|
+
const sender = String(node?.sender?.card || node?.sender?.nickname || node?.nickname || node?.name || '').trim();
|
|
705
|
+
const content = Array.isArray(node?.message)
|
|
706
|
+
? node.message.filter((seg: any) => seg?.type === 'text').map((seg: any) => String(seg?.data?.text ?? '')).join('')
|
|
707
|
+
: String(node?.content ?? node?.raw_message ?? node?.message ?? '');
|
|
708
|
+
const text = content.replace(/\[CQ:[^\]]+\]/g, '[图片]').replace(/\s+/g, ' ').trim();
|
|
709
|
+
if (!text) continue;
|
|
710
|
+
blocks.push(`${sender ? `${sender}: ` : ''}${truncate(text, 160)}`);
|
|
711
|
+
}
|
|
712
|
+
} catch (error) {
|
|
713
|
+
this.log('forward', { ok: false, error: String(error) }, session);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
if (!blocks.length) return '';
|
|
717
|
+
this.log('forward', { ok: true, nodes: blocks.length }, session);
|
|
718
|
+
return blocks.join('\n');
|
|
719
|
+
}
|
|
720
|
+
|
|
687
721
|
private recentImageRecords(state: GroupState, record: ChatRecord, maxAgeMs = 120000): RenderedImage[] {
|
|
688
722
|
const cutoff = Number(record.at) - maxAgeMs;
|
|
689
723
|
const candidates = state.records
|
package/src/ai/prompt.ts
CHANGED
|
@@ -69,7 +69,10 @@ export function renderRecordLine(record: ChatRecord, selfName: string): string {
|
|
|
69
69
|
? `「回复 ${record.quote.name}:${truncate(record.quote.text, 40)}」`
|
|
70
70
|
: '';
|
|
71
71
|
const body = quote ? `${quote}${record.text}` : record.text;
|
|
72
|
-
|
|
72
|
+
const forward = record.forwardText
|
|
73
|
+
? `\n └ 转发内容:\n${record.forwardText.split('\n').map((line) => ` ${truncate(line, 120)}`).join('\n')}`
|
|
74
|
+
: '';
|
|
75
|
+
return `[${time}] ${speaker(record, selfName)}: ${body || '[空消息]'}${forward}`;
|
|
73
76
|
}
|
|
74
77
|
|
|
75
78
|
export function buildSystemPrompt(input: PromptInput): string {
|
package/src/ai/state.ts
CHANGED
package/src/core/text.ts
CHANGED
|
@@ -24,6 +24,12 @@ export interface RenderedQuote {
|
|
|
24
24
|
id: string;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
/** 合并转发:只保留 id 和摘要,正文由引擎按需向 OneBot 拉取 */
|
|
28
|
+
export interface RenderedForward {
|
|
29
|
+
id: string;
|
|
30
|
+
summary: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
27
33
|
export interface RenderedMessage {
|
|
28
34
|
/** 已渲染的正文(含 @名字 / [图片] 等占位) */
|
|
29
35
|
text: string;
|
|
@@ -31,6 +37,8 @@ export interface RenderedMessage {
|
|
|
31
37
|
images: RenderedImage[];
|
|
32
38
|
/** 引用(被回复)的那条消息 */
|
|
33
39
|
quote?: RenderedQuote;
|
|
40
|
+
/** 本条消息里的合并转发(可再拉取子消息内容) */
|
|
41
|
+
forwards: RenderedForward[];
|
|
34
42
|
/** 正文里点名到的 QQ 号(不含 bot 自己) */
|
|
35
43
|
mentions: string[];
|
|
36
44
|
/** 本条消息是否 @ 了 bot(与 session.content 的格式无关) */
|
|
@@ -182,6 +190,17 @@ function renderFromContent(
|
|
|
182
190
|
return { text: stripTags(text), images, mentions };
|
|
183
191
|
}
|
|
184
192
|
|
|
193
|
+
/** 从合并转发元素里抽 id 与摘要(napcat 常见字段:id / resid / content / summary) */
|
|
194
|
+
function forwardFromElement(el: any): RenderedForward | null {
|
|
195
|
+
const attrs = el?.attrs ?? el ?? {};
|
|
196
|
+
const id = String(
|
|
197
|
+
attrs.id ?? attrs.resid ?? attrs.resId ?? attrs.forwardId ?? attrs.content ?? '',
|
|
198
|
+
).trim();
|
|
199
|
+
if (!id) return null;
|
|
200
|
+
const summary = String(attrs.summary ?? attrs.title ?? attrs.name ?? attrs.desc ?? '').trim();
|
|
201
|
+
return { id, summary };
|
|
202
|
+
}
|
|
203
|
+
|
|
185
204
|
/**
|
|
186
205
|
* 渲染一条消息。
|
|
187
206
|
* - @ 会解析成群名片(`@你` 表示 @bot)
|
|
@@ -197,6 +216,7 @@ export async function renderMessage(
|
|
|
197
216
|
const quoteMax = options.quoteMaxChars ?? 60;
|
|
198
217
|
const elements: any[] = Array.isArray(session?.elements) ? session.elements : [];
|
|
199
218
|
const images: RenderedImage[] = [];
|
|
219
|
+
const forwards: RenderedForward[] = [];
|
|
200
220
|
const mentions: string[] = [];
|
|
201
221
|
const parts: string[] = [];
|
|
202
222
|
let atBot = false;
|
|
@@ -205,9 +225,16 @@ export async function renderMessage(
|
|
|
205
225
|
const fallback = renderFromContent(String(session?.content ?? ''), selfId, resolveName);
|
|
206
226
|
// 兜底路径无法异步解析 @ 名字,尽力把 html 里的 name 用上
|
|
207
227
|
const withNames = await replaceMentionsWithNames(fallback.text, resolveName, selfId);
|
|
228
|
+
const forwardsFromCq: RenderedForward[] = [];
|
|
229
|
+
for (const match of String(session?.content ?? '').matchAll(/\[CQ:(?:forward|node),([^\]]*)\]/gi)) {
|
|
230
|
+
const attrs = cqAttrs(match[0]);
|
|
231
|
+
const id = String(attrs.id ?? attrs.resid ?? attrs.content ?? '').trim();
|
|
232
|
+
if (id) forwardsFromCq.push({ id, summary: String(attrs.summary ?? '') });
|
|
233
|
+
}
|
|
208
234
|
return {
|
|
209
235
|
text: withNames.text,
|
|
210
236
|
images: fallback.images,
|
|
237
|
+
forwards: forwardsFromCq,
|
|
211
238
|
mentions: [...new Set([...fallback.mentions, ...withNames.mentions])],
|
|
212
239
|
atBot: !!selfId && (fallback.text.includes('@你')
|
|
213
240
|
|| fallback.text.includes(`<at id="${selfId}"`)
|
|
@@ -262,6 +289,8 @@ export async function renderMessage(
|
|
|
262
289
|
continue;
|
|
263
290
|
}
|
|
264
291
|
if (FORWARD_TYPES.has(type)) {
|
|
292
|
+
const forward = forwardFromElement(el);
|
|
293
|
+
if (forward) forwards.push(forward);
|
|
265
294
|
parts.push(' [合并转发] ');
|
|
266
295
|
continue;
|
|
267
296
|
}
|
|
@@ -289,6 +318,7 @@ export async function renderMessage(
|
|
|
289
318
|
text,
|
|
290
319
|
images,
|
|
291
320
|
quote,
|
|
321
|
+
forwards,
|
|
292
322
|
mentions: [...new Set(mentions)],
|
|
293
323
|
atBot,
|
|
294
324
|
plain: text,
|
package/src/index.ts
CHANGED
|
@@ -134,6 +134,14 @@ export const Config = Schema.intersect([
|
|
|
134
134
|
aiTemperature: Schema.number().min(0).max(2).step(0.1).default(0.8).description('生成温度(0~2)。群聊接话建议 0.7~0.9,过高会跑题、复述、发散。'),
|
|
135
135
|
aiMaxOutputTokens: Schema.natural().default(400).description('AI 最大输出 token 数。群聊一句话不需要太大,过大会增加延迟。'),
|
|
136
136
|
aiMaxReplyChars: Schema.natural().min(20).max(500).default(120).description('单条回复的最大字符数,超出会在句末截断。'),
|
|
137
|
+
aiQuoteReplyMode: Schema.union([
|
|
138
|
+
Schema.const('random').description('随机决定'),
|
|
139
|
+
Schema.const('always').description('总是引用'),
|
|
140
|
+
Schema.const('never').description('从不引用'),
|
|
141
|
+
]).role('radio').default('random').description('AI 回复是否引用触发它的那条消息。'),
|
|
142
|
+
aiQuoteReplyProbability: Schema.number().min(0).max(1).step(0.05).default(0.5).hidden().description('引用概率(仅 random 模式生效)。'),
|
|
143
|
+
aiReadForwardMessage: Schema.boolean().default(true).description('读取合并转发聊天记录的内容,作为上下文的一部分。'),
|
|
144
|
+
aiForwardMaxNodes: Schema.natural().min(1).max(50).default(20).description('单条合并转发最多读取多少条子消息。'),
|
|
137
145
|
aiReplyDecision: Schema.union([
|
|
138
146
|
Schema.const('single').description('一次调用:模型直接输出回复,或用 <pass/> 放弃(推荐)'),
|
|
139
147
|
Schema.const('two-stage').description('两次调用:先判定是否值得回复,再生成正文'),
|
package/src/service.js
CHANGED
|
@@ -2057,11 +2057,14 @@ class QQGroupManagerService extends koishi_1.Service {
|
|
|
2057
2057
|
resolveImage(ref, session) {
|
|
2058
2058
|
return this.resolveImageReferenceToDataUrl(ref, session);
|
|
2059
2059
|
}
|
|
2060
|
-
/** AI
|
|
2060
|
+
/** AI 回复发送:按 aiQuoteReplyMode 决定是否引用触发消息,失败则退化为纯文本 */
|
|
2061
2061
|
async sendReply(session, text) {
|
|
2062
2062
|
if (!text)
|
|
2063
2063
|
return;
|
|
2064
|
-
|
|
2064
|
+
const mode = String(this.config?.aiQuoteReplyMode || 'random');
|
|
2065
|
+
const probability = Math.min(1, Math.max(0, Number(this.config?.aiQuoteReplyProbability ?? 0.5)));
|
|
2066
|
+
const shouldQuote = !!session.messageId && (mode === 'always' || (mode === 'random' && Math.random() < probability));
|
|
2067
|
+
if (shouldQuote) {
|
|
2065
2068
|
try {
|
|
2066
2069
|
await session.send([koishi_1.h.quote(session.messageId), koishi_1.h.text(text)]);
|
|
2067
2070
|
return;
|