@nestim/koishi-plugin-qq-group-manager 1.4.5 → 1.4.7
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 +127 -48
- package/lib/ai/engine.d.ts +45 -1
- package/lib/ai/engine.js +206 -21
- package/lib/ai/hooks.d.ts +10 -0
- package/lib/ai/hooks.js +42 -0
- package/lib/ai/learn.d.ts +19 -0
- package/lib/ai/learn.js +50 -1
- package/lib/ai/provider.d.ts +4 -0
- package/lib/ai/provider.js +7 -5
- package/lib/ai/runtime.d.ts +29 -0
- package/lib/ai/runtime.js +52 -0
- package/lib/ai/state.d.ts +2 -0
- package/lib/ai/state.js +2 -0
- package/lib/ai/trigger.d.ts +11 -9
- package/lib/ai/trigger.js +23 -13
- package/lib/index.d.ts +32 -0
- package/lib/index.js +15 -1
- package/lib/service.d.ts +7 -0
- package/lib/service.js +65 -11
- package/package.json +1 -1
- package/src/ai/engine.ts +440 -222
- package/src/ai/hooks.ts +45 -0
- package/src/ai/learn.ts +56 -0
- package/src/ai/provider.ts +11 -5
- package/src/ai/runtime.ts +63 -0
- package/src/ai/state.ts +2 -0
- package/src/ai/trigger.ts +28 -18
- package/src/index.ts +16 -2
- package/src/service.js +65 -11
package/lib/ai/hooks.js
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
*/
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.AiHookRegistry = void 0;
|
|
13
|
+
exports.getSharedAiHooks = getSharedAiHooks;
|
|
13
14
|
class AiHookRegistry {
|
|
14
15
|
logger;
|
|
15
16
|
/** 钩子接口版本;外部插件应检查 >= 2 才能用 contextEvicted */
|
|
@@ -21,6 +22,20 @@ class AiHookRegistry {
|
|
|
21
22
|
constructor(logger) {
|
|
22
23
|
this.logger = logger;
|
|
23
24
|
}
|
|
25
|
+
/** 主插件重载后把新实例的 logger 接到同一个注册表上。 */
|
|
26
|
+
setLogger(logger) {
|
|
27
|
+
if (logger)
|
|
28
|
+
this.logger = logger;
|
|
29
|
+
}
|
|
30
|
+
/** 诊断用:当前已注册的钩子数量。 */
|
|
31
|
+
stats() {
|
|
32
|
+
return {
|
|
33
|
+
sections: this.sections.size,
|
|
34
|
+
filters: this.filters.size,
|
|
35
|
+
afterReply: this.afterReplyHandlers.size,
|
|
36
|
+
evicted: this.evictedHandlers.size,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
24
39
|
registerPromptSection(provider) {
|
|
25
40
|
this.sections.add(provider);
|
|
26
41
|
return () => this.sections.delete(provider);
|
|
@@ -99,3 +114,30 @@ class AiHookRegistry {
|
|
|
99
114
|
}
|
|
100
115
|
}
|
|
101
116
|
exports.AiHookRegistry = AiHookRegistry;
|
|
117
|
+
/**
|
|
118
|
+
* 进程级共享的钩子注册表。
|
|
119
|
+
*
|
|
120
|
+
* 背景:meme / chat-style / private 等外部插件只在启动时解析一次
|
|
121
|
+
* `qqgmAiHooks`,之后一直持有这个对象。如果主插件每次重载都 `new` 一个
|
|
122
|
+
* 新注册表,新引擎看到的是空注册表,而外部插件还挂在旧对象上——表现就是
|
|
123
|
+
* 主插件单独重启后表情包不发、聊天风格段消失、私聊用旧配置。
|
|
124
|
+
*
|
|
125
|
+
* 用 globalThis + Symbol.for 做单例:即使插件模块被重新 require、甚至从
|
|
126
|
+
* 不同路径加载了两份,拿到的也是同一个注册表,重载前后对象身份不变。
|
|
127
|
+
*/
|
|
128
|
+
const SHARED_HOOKS = Symbol.for('qqgm.ai-hooks-registry');
|
|
129
|
+
function getSharedAiHooks(logger) {
|
|
130
|
+
const store = globalThis;
|
|
131
|
+
let registry = store[SHARED_HOOKS];
|
|
132
|
+
if (!registry) {
|
|
133
|
+
registry = new AiHookRegistry(logger);
|
|
134
|
+
store[SHARED_HOOKS] = registry;
|
|
135
|
+
logger?.info?.(`[ai-hooks] v${registry.version} 注册表已创建(进程级单例)`);
|
|
136
|
+
}
|
|
137
|
+
else if (logger) {
|
|
138
|
+
registry.setLogger(logger);
|
|
139
|
+
const stats = registry.stats();
|
|
140
|
+
logger.info?.(`[ai-hooks] 复用进程级注册表:风格段 ${stats.sections} / 过滤器 ${stats.filters} / 后处理 ${stats.afterReply}`);
|
|
141
|
+
}
|
|
142
|
+
return registry;
|
|
143
|
+
}
|
package/lib/ai/learn.d.ts
CHANGED
|
@@ -56,3 +56,22 @@ export declare function cleanSummary(raw: string, maxChars?: number): string;
|
|
|
56
56
|
export declare function similarityOf(a: string, b: string): number;
|
|
57
57
|
/** 判断是否与已有记忆重复 */
|
|
58
58
|
export declare function isDuplicate(candidate: string, existing: string[], threshold?: number): boolean;
|
|
59
|
+
/** 自动归纳条目的标题标记(写进 Markdown 块首行) */
|
|
60
|
+
export declare const AUTO_MEMORY_LABEL = "\u81EA\u52A8\u5F52\u7EB3";
|
|
61
|
+
/** 自动归纳块首行形如 `## 2026/10/28 12:00:00 · 自动归纳` */
|
|
62
|
+
export declare function isAutoMemoryBlock(block: string): boolean;
|
|
63
|
+
/** 取记忆块正文(去掉 `## 时间 · 来源` 标题行) */
|
|
64
|
+
export declare function memoryBlockBody(block: string): string;
|
|
65
|
+
/**
|
|
66
|
+
* 记忆压缩提示词:把 N 条自动归纳合并成一条更短的。
|
|
67
|
+
*
|
|
68
|
+
* 目的不是“再总结一遍”,而是去掉重复与过时信息——群里连续几天聊同一件事时,
|
|
69
|
+
* 自动归纳会反复产出高度相似的条目,压缩后只保留事实差异。
|
|
70
|
+
*/
|
|
71
|
+
export declare function buildConsolidatePrompt(blocks: string[], options?: {
|
|
72
|
+
maxChars?: number;
|
|
73
|
+
kind?: string;
|
|
74
|
+
}): {
|
|
75
|
+
system: string;
|
|
76
|
+
user: string;
|
|
77
|
+
};
|
package/lib/ai/learn.js
CHANGED
|
@@ -9,12 +9,15 @@
|
|
|
9
9
|
* 归纳出来的内容是一次性的一长串,150 字以内,写入对应作用域的记忆文件。
|
|
10
10
|
*/
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.LearnStore = exports.LearnBucket = exports.MAX_SUMMARY_CHARS = void 0;
|
|
12
|
+
exports.AUTO_MEMORY_LABEL = exports.LearnStore = exports.LearnBucket = exports.MAX_SUMMARY_CHARS = void 0;
|
|
13
13
|
exports.renderForSummary = renderForSummary;
|
|
14
14
|
exports.buildSummaryPrompt = buildSummaryPrompt;
|
|
15
15
|
exports.cleanSummary = cleanSummary;
|
|
16
16
|
exports.similarityOf = similarityOf;
|
|
17
17
|
exports.isDuplicate = isDuplicate;
|
|
18
|
+
exports.isAutoMemoryBlock = isAutoMemoryBlock;
|
|
19
|
+
exports.memoryBlockBody = memoryBlockBody;
|
|
20
|
+
exports.buildConsolidatePrompt = buildConsolidatePrompt;
|
|
18
21
|
const text_1 = require("../core/text");
|
|
19
22
|
exports.MAX_SUMMARY_CHARS = 150;
|
|
20
23
|
/** 正在累计的一批消息 */
|
|
@@ -198,3 +201,49 @@ function isDuplicate(candidate, existing, threshold = 0.55) {
|
|
|
198
201
|
}
|
|
199
202
|
return false;
|
|
200
203
|
}
|
|
204
|
+
// ---------------------------------------------------------------- 记忆压缩
|
|
205
|
+
/** 自动归纳条目的标题标记(写进 Markdown 块首行) */
|
|
206
|
+
exports.AUTO_MEMORY_LABEL = '自动归纳';
|
|
207
|
+
/** 自动归纳块首行形如 `## 2026/10/28 12:00:00 · 自动归纳` */
|
|
208
|
+
function isAutoMemoryBlock(block) {
|
|
209
|
+
const first = String(block || '').split('\n', 1)[0] || '';
|
|
210
|
+
return /·\s*自动归纳/.test(first);
|
|
211
|
+
}
|
|
212
|
+
/** 取记忆块正文(去掉 `## 时间 · 来源` 标题行) */
|
|
213
|
+
function memoryBlockBody(block) {
|
|
214
|
+
const lines = String(block || '').split('\n');
|
|
215
|
+
if (lines.length <= 1)
|
|
216
|
+
return String(block || '').trim();
|
|
217
|
+
return lines.slice(1).join('\n').trim();
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* 记忆压缩提示词:把 N 条自动归纳合并成一条更短的。
|
|
221
|
+
*
|
|
222
|
+
* 目的不是“再总结一遍”,而是去掉重复与过时信息——群里连续几天聊同一件事时,
|
|
223
|
+
* 自动归纳会反复产出高度相似的条目,压缩后只保留事实差异。
|
|
224
|
+
*/
|
|
225
|
+
function buildConsolidatePrompt(blocks, options = {}) {
|
|
226
|
+
const maxChars = Math.max(40, Number(options.maxChars) || 90);
|
|
227
|
+
const label = options.kind === 'private' ? '私聊' : '群聊';
|
|
228
|
+
const bodies = (blocks || [])
|
|
229
|
+
.map((block) => memoryBlockBody(block).replace(/\s+/g, ' ').trim())
|
|
230
|
+
.filter(Boolean);
|
|
231
|
+
const system = [
|
|
232
|
+
`你在压缩一段${label}的长期记忆。`,
|
|
233
|
+
`下面有 ${bodies.length} 条已经归纳过的记忆,请合并成一条。`,
|
|
234
|
+
'',
|
|
235
|
+
'要求:',
|
|
236
|
+
'- 只保留互不重复的事实:人物与称呼、关系、稳定偏好、反复出现的话题;',
|
|
237
|
+
'- 删掉重复表述、一次性情绪、已经过时的细节;',
|
|
238
|
+
'- 不要编造,不要写“他们聊到”“讨论了”这类过程描述;',
|
|
239
|
+
`- 只输出一条纯文本,不超过 ${maxChars} 字,不要标题、编号、列表、Markdown;`,
|
|
240
|
+
'- 如果这些记忆里没有任何值得保留的内容,只输出一个词:NONE',
|
|
241
|
+
].join('\n');
|
|
242
|
+
const user = [
|
|
243
|
+
'【待压缩的记忆】',
|
|
244
|
+
...bodies.map((text, index) => `${index + 1}. ${text}`),
|
|
245
|
+
'',
|
|
246
|
+
'现在输出合并后的那一条记忆,或输出 NONE。',
|
|
247
|
+
].join('\n');
|
|
248
|
+
return { system, user };
|
|
249
|
+
}
|
package/lib/ai/provider.d.ts
CHANGED
|
@@ -13,6 +13,8 @@ export interface ChatRequest {
|
|
|
13
13
|
temperature?: number;
|
|
14
14
|
maxTokens?: number;
|
|
15
15
|
timeoutMs?: number;
|
|
16
|
+
/** 覆盖默认 aiModel(例如自动归纳用更便宜的模型) */
|
|
17
|
+
model?: string;
|
|
16
18
|
}
|
|
17
19
|
export interface ChatResult {
|
|
18
20
|
ok: boolean;
|
|
@@ -21,6 +23,8 @@ export interface ChatResult {
|
|
|
21
23
|
status?: number;
|
|
22
24
|
/** 是否因为图片被拒而发生了降级重试 */
|
|
23
25
|
degradedToText?: boolean;
|
|
26
|
+
/** 服务端 token 用量(OpenAI: usage;Gemini: usageMetadata) */
|
|
27
|
+
usage?: any;
|
|
24
28
|
}
|
|
25
29
|
export interface ProviderConfig {
|
|
26
30
|
aiProvider?: string;
|
package/lib/ai/provider.js
CHANGED
|
@@ -39,7 +39,7 @@ class ChatProvider {
|
|
|
39
39
|
async chat(request) {
|
|
40
40
|
if (!this.apiKey)
|
|
41
41
|
return { ok: false, error: 'AI 未配置 apiKey' };
|
|
42
|
-
if (!this.model)
|
|
42
|
+
if (!this.model && !String(request.model || '').trim())
|
|
43
43
|
return { ok: false, error: 'AI 未配置 model' };
|
|
44
44
|
const provider = String(this.config.aiProvider ?? 'openai-compatible');
|
|
45
45
|
const requested = (request.images ?? []).filter((item) => /^data:image\//i.test(item) || /^https?:\/\//i.test(item));
|
|
@@ -77,6 +77,7 @@ class ChatProvider {
|
|
|
77
77
|
}
|
|
78
78
|
async callOpenAi(request, images, options = {}) {
|
|
79
79
|
const base = String(this.config.aiBaseUrl || 'https://api.openai.com/v1').replace(/\/+$/, '');
|
|
80
|
+
const model = String(request.model || this.model).trim();
|
|
80
81
|
const content = [{ type: 'text', text: request.user }];
|
|
81
82
|
for (const url of images)
|
|
82
83
|
content.push({ type: 'image_url', image_url: { url } });
|
|
@@ -92,7 +93,7 @@ class ChatProvider {
|
|
|
92
93
|
'Content-Type': 'application/json',
|
|
93
94
|
},
|
|
94
95
|
body: JSON.stringify({
|
|
95
|
-
model
|
|
96
|
+
model,
|
|
96
97
|
temperature: request.temperature ?? this.config.aiTemperature ?? 0.8,
|
|
97
98
|
max_tokens: maxTokens,
|
|
98
99
|
messages: [
|
|
@@ -143,7 +144,7 @@ class ChatProvider {
|
|
|
143
144
|
error: `模型返回空内容(finish=${payload?.choices?.[0]?.finish_reason || 'unknown'})`,
|
|
144
145
|
};
|
|
145
146
|
}
|
|
146
|
-
return { ok: true, text, status: response.status };
|
|
147
|
+
return { ok: true, text, status: response.status, usage: payload?.usage };
|
|
147
148
|
}
|
|
148
149
|
catch (error) {
|
|
149
150
|
const aborted = error?.name === 'AbortError';
|
|
@@ -156,7 +157,8 @@ class ChatProvider {
|
|
|
156
157
|
async callGemini(request, images) {
|
|
157
158
|
const rawBase = String(this.config.aiBaseUrl || 'https://generativelanguage.googleapis.com/v1beta').replace(/\/+$/, '');
|
|
158
159
|
const base = /\/v\d/i.test(rawBase) ? rawBase : `${rawBase}/v1beta`;
|
|
159
|
-
const
|
|
160
|
+
const model = String(request.model || this.model).trim();
|
|
161
|
+
const endpoint = `${base}/models/${encodeURIComponent(model)}:generateContent?key=${encodeURIComponent(this.apiKey)}`;
|
|
160
162
|
const parts = [{ text: request.user }];
|
|
161
163
|
for (const url of images) {
|
|
162
164
|
const match = /^data:(image\/[^;]+);base64,(.*)$/i.exec(url);
|
|
@@ -193,7 +195,7 @@ class ChatProvider {
|
|
|
193
195
|
.trim();
|
|
194
196
|
if (!text)
|
|
195
197
|
return { ok: false, status: response.status, error: 'Gemini 返回空内容' };
|
|
196
|
-
return { ok: true, text, status: response.status };
|
|
198
|
+
return { ok: true, text, status: response.status, usage: payload?.usageMetadata };
|
|
197
199
|
}
|
|
198
200
|
catch (error) {
|
|
199
201
|
const aborted = error?.name === 'AbortError';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 主插件对外暴露的运行时门面(进程级单例)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要它:
|
|
5
|
+
* - 外部插件(qqgm-private 等)会 `ctx.root.get('qqgmAiRuntime')` 之后长期持有该对象;
|
|
6
|
+
* - 如果每次主插件重载都 `set` 一个全新的 `{ config, hooks, chat }` 快照,
|
|
7
|
+
* 外部插件手里的旧快照就永远指向已经销毁的引擎:配置不更新、钩子对不上、
|
|
8
|
+
* chat() 走旧实例;
|
|
9
|
+
* - 这里改成稳定的门面对象,重载只是把 `current` 指针换到新服务,
|
|
10
|
+
* 外部插件持有的引用不变,读到的 config / hooks / chat 永远是最新的。
|
|
11
|
+
*/
|
|
12
|
+
import { AiHookRegistry } from './hooks';
|
|
13
|
+
export interface AiRuntimeChatRequest {
|
|
14
|
+
system: string;
|
|
15
|
+
user: string;
|
|
16
|
+
temperature?: number;
|
|
17
|
+
maxTokens?: number;
|
|
18
|
+
[key: string]: any;
|
|
19
|
+
}
|
|
20
|
+
export declare class AiRuntimeFacade {
|
|
21
|
+
/** 当前存活的主插件服务;主插件卸载后为 null。 */
|
|
22
|
+
current: any;
|
|
23
|
+
bind(service: any): void;
|
|
24
|
+
unbind(service: any): void;
|
|
25
|
+
get config(): any;
|
|
26
|
+
get hooks(): AiHookRegistry;
|
|
27
|
+
chat(request: AiRuntimeChatRequest): Promise<any>;
|
|
28
|
+
}
|
|
29
|
+
export declare function getAiRuntimeFacade(): AiRuntimeFacade;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* 主插件对外暴露的运行时门面(进程级单例)。
|
|
4
|
+
*
|
|
5
|
+
* 为什么需要它:
|
|
6
|
+
* - 外部插件(qqgm-private 等)会 `ctx.root.get('qqgmAiRuntime')` 之后长期持有该对象;
|
|
7
|
+
* - 如果每次主插件重载都 `set` 一个全新的 `{ config, hooks, chat }` 快照,
|
|
8
|
+
* 外部插件手里的旧快照就永远指向已经销毁的引擎:配置不更新、钩子对不上、
|
|
9
|
+
* chat() 走旧实例;
|
|
10
|
+
* - 这里改成稳定的门面对象,重载只是把 `current` 指针换到新服务,
|
|
11
|
+
* 外部插件持有的引用不变,读到的 config / hooks / chat 永远是最新的。
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.AiRuntimeFacade = void 0;
|
|
15
|
+
exports.getAiRuntimeFacade = getAiRuntimeFacade;
|
|
16
|
+
const hooks_1 = require("./hooks");
|
|
17
|
+
const SHARED_RUNTIME = Symbol.for('qqgm.ai-runtime-facade');
|
|
18
|
+
class AiRuntimeFacade {
|
|
19
|
+
/** 当前存活的主插件服务;主插件卸载后为 null。 */
|
|
20
|
+
current = null;
|
|
21
|
+
bind(service) {
|
|
22
|
+
this.current = service;
|
|
23
|
+
}
|
|
24
|
+
unbind(service) {
|
|
25
|
+
if (this.current === service)
|
|
26
|
+
this.current = null;
|
|
27
|
+
}
|
|
28
|
+
get config() {
|
|
29
|
+
return this.current ? this.current.config : {};
|
|
30
|
+
}
|
|
31
|
+
get hooks() {
|
|
32
|
+
const live = this.current && this.current.ai && this.current.ai.hooks;
|
|
33
|
+
// 服务不在时也返回共享注册表,避免外部插件拿到 null 后直接崩掉。
|
|
34
|
+
return live || (0, hooks_1.getSharedAiHooks)();
|
|
35
|
+
}
|
|
36
|
+
chat(request) {
|
|
37
|
+
if (!this.current || !this.current.ai) {
|
|
38
|
+
return Promise.resolve({ ok: false, text: '', error: 'qqgm service not loaded' });
|
|
39
|
+
}
|
|
40
|
+
return this.current.ai.chat(request);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.AiRuntimeFacade = AiRuntimeFacade;
|
|
44
|
+
function getAiRuntimeFacade() {
|
|
45
|
+
const store = globalThis;
|
|
46
|
+
let facade = store[SHARED_RUNTIME];
|
|
47
|
+
if (!facade) {
|
|
48
|
+
facade = new AiRuntimeFacade();
|
|
49
|
+
store[SHARED_RUNTIME] = facade;
|
|
50
|
+
}
|
|
51
|
+
return facade;
|
|
52
|
+
}
|
package/lib/ai/state.d.ts
CHANGED
|
@@ -39,6 +39,8 @@ export declare class GroupState {
|
|
|
39
39
|
counter: number;
|
|
40
40
|
lastReplyAt: number;
|
|
41
41
|
lastAttemptAt: number;
|
|
42
|
+
/** 上次发「冷却中」提示的时间,用于避免 @ 刷屏时反复提示 */
|
|
43
|
+
lastCooldownNoticeAt: number;
|
|
42
44
|
inFlight: boolean;
|
|
43
45
|
followup: FollowupState | null;
|
|
44
46
|
seen: Map<string, number>;
|
package/lib/ai/state.js
CHANGED
package/lib/ai/trigger.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 触发放行判定(纯函数,便于单测)。
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
4
|
+
* 冷却的语义已经统一:**所有真正调用模型的行为**(含 @、点名后跟随)
|
|
5
|
+
* 都受同一个冷却约束,按「上一次调用模型的时刻」计算,而不是按
|
|
6
|
+
* 「上一次成功发出回复」——模型返回 <pass/>、被过滤器拦下、请求失败
|
|
7
|
+
* 都算用过一次,避免一直失败却一直重试。
|
|
8
|
+
*
|
|
9
|
+
* 被 @ 但仍在冷却里时返回 remainingMs,由引擎决定是否在群里提示还剩多久。
|
|
9
10
|
*/
|
|
10
11
|
export interface TriggerInput {
|
|
11
12
|
mode: 'threshold' | 'random' | 'hybrid';
|
|
@@ -15,10 +16,9 @@ export interface TriggerInput {
|
|
|
15
16
|
now: number;
|
|
16
17
|
lastAttemptAt: number;
|
|
17
18
|
minIntervalMs: number;
|
|
18
|
-
/**
|
|
19
|
-
lastReplyAt
|
|
20
|
-
|
|
21
|
-
mentionCooldownMs: number;
|
|
19
|
+
/** 兼容字段:旧版按“上次回复”计算的 @ 冷却,已合并进 minIntervalMs */
|
|
20
|
+
lastReplyAt?: number;
|
|
21
|
+
mentionCooldownMs?: number;
|
|
22
22
|
inFlight: boolean;
|
|
23
23
|
mention: boolean;
|
|
24
24
|
followup: boolean;
|
|
@@ -28,5 +28,7 @@ export interface TriggerDecision {
|
|
|
28
28
|
fire: boolean;
|
|
29
29
|
forced: boolean;
|
|
30
30
|
reason: string;
|
|
31
|
+
/** 冷却中时距离可以再次调用模型的剩余毫秒数 */
|
|
32
|
+
remainingMs?: number;
|
|
31
33
|
}
|
|
32
34
|
export declare function decideTrigger(input: TriggerInput): TriggerDecision;
|
package/lib/ai/trigger.js
CHANGED
|
@@ -2,29 +2,38 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* 触发放行判定(纯函数,便于单测)。
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
5
|
+
* 冷却的语义已经统一:**所有真正调用模型的行为**(含 @、点名后跟随)
|
|
6
|
+
* 都受同一个冷却约束,按「上一次调用模型的时刻」计算,而不是按
|
|
7
|
+
* 「上一次成功发出回复」——模型返回 <pass/>、被过滤器拦下、请求失败
|
|
8
|
+
* 都算用过一次,避免一直失败却一直重试。
|
|
9
|
+
*
|
|
10
|
+
* 被 @ 但仍在冷却里时返回 remainingMs,由引擎决定是否在群里提示还剩多久。
|
|
10
11
|
*/
|
|
11
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
13
|
exports.decideTrigger = decideTrigger;
|
|
13
14
|
function decideTrigger(input) {
|
|
14
15
|
if (input.inFlight)
|
|
15
16
|
return { fire: false, forced: false, reason: 'in-flight' };
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
// ---------- 1) 模型调用冷却:@ 也不能绕过 ----------
|
|
18
|
+
const cooldown = Math.max(0, Number(input.minIntervalMs) || 0);
|
|
19
|
+
const lastAttempt = Number(input.lastAttemptAt) || 0;
|
|
20
|
+
if (cooldown > 0 && lastAttempt > 0) {
|
|
21
|
+
const elapsed = input.now - lastAttempt;
|
|
22
|
+
if (elapsed < cooldown) {
|
|
23
|
+
return {
|
|
24
|
+
fire: false,
|
|
25
|
+
forced: false,
|
|
26
|
+
reason: input.mention ? 'cooldown-mention' : 'cooldown',
|
|
27
|
+
remainingMs: Math.max(0, cooldown - elapsed),
|
|
28
|
+
};
|
|
20
29
|
}
|
|
21
|
-
return { fire: true, forced: true, reason: 'direct-mention' };
|
|
22
30
|
}
|
|
31
|
+
// ---------- 2) 点名与点名后跟随:越过阈值/概率,但不越过冷却 ----------
|
|
32
|
+
if (input.mention)
|
|
33
|
+
return { fire: true, forced: true, reason: 'direct-mention' };
|
|
23
34
|
if (input.followup)
|
|
24
35
|
return { fire: true, forced: true, reason: 'followup' };
|
|
25
|
-
|
|
26
|
-
return { fire: false, forced: false, reason: 'cooldown' };
|
|
27
|
-
}
|
|
36
|
+
// ---------- 3) 阈值 ----------
|
|
28
37
|
const needThreshold = input.mode === 'threshold' || input.mode === 'hybrid';
|
|
29
38
|
if (needThreshold && input.counter < Math.max(1, input.threshold)) {
|
|
30
39
|
return {
|
|
@@ -33,6 +42,7 @@ function decideTrigger(input) {
|
|
|
33
42
|
reason: `below-threshold(${input.counter}/${Math.max(1, input.threshold)})`,
|
|
34
43
|
};
|
|
35
44
|
}
|
|
45
|
+
// ---------- 4) 概率 ----------
|
|
36
46
|
const needRandom = input.mode === 'random' || input.mode === 'hybrid';
|
|
37
47
|
if (needRandom) {
|
|
38
48
|
const probability = Math.max(0, Math.min(1, Number(input.probability) || 0));
|
package/lib/index.d.ts
CHANGED
|
@@ -25,6 +25,10 @@ export declare const Config: Schema<Schemastery.ObjectS<{
|
|
|
25
25
|
groupRules: Schema<Schemastery.ObjectS<{
|
|
26
26
|
guildId: Schema<string, string>;
|
|
27
27
|
enableAiReply: Schema<boolean, boolean>;
|
|
28
|
+
aiMessageThreshold: Schema<number, number>;
|
|
29
|
+
aiRandomReplyProbability: Schema<number, number>;
|
|
30
|
+
aiMinReplyIntervalSeconds: Schema<number, number>;
|
|
31
|
+
aiCooldownNotice: Schema<boolean, boolean>;
|
|
28
32
|
bannedWords: Schema<string[], string[]>;
|
|
29
33
|
whitelistKeywords: Schema<string[], string[]>;
|
|
30
34
|
strongKeywords: Schema<string[], string[]>;
|
|
@@ -44,6 +48,10 @@ export declare const Config: Schema<Schemastery.ObjectS<{
|
|
|
44
48
|
}>[], Schemastery.ObjectT<{
|
|
45
49
|
guildId: Schema<string, string>;
|
|
46
50
|
enableAiReply: Schema<boolean, boolean>;
|
|
51
|
+
aiMessageThreshold: Schema<number, number>;
|
|
52
|
+
aiRandomReplyProbability: Schema<number, number>;
|
|
53
|
+
aiMinReplyIntervalSeconds: Schema<number, number>;
|
|
54
|
+
aiCooldownNotice: Schema<boolean, boolean>;
|
|
47
55
|
bannedWords: Schema<string[], string[]>;
|
|
48
56
|
whitelistKeywords: Schema<string[], string[]>;
|
|
49
57
|
strongKeywords: Schema<string[], string[]>;
|
|
@@ -81,6 +89,11 @@ export declare const Config: Schema<Schemastery.ObjectS<{
|
|
|
81
89
|
memoryLearnPushBack: Schema<boolean, boolean>;
|
|
82
90
|
memoryLearnDuplicateThreshold: Schema<number, number>;
|
|
83
91
|
memoryLearnDailyLimit: Schema<number, number>;
|
|
92
|
+
memoryLearnConsolidate: Schema<boolean, boolean>;
|
|
93
|
+
memoryLearnConsolidateEvery: Schema<number, number>;
|
|
94
|
+
memoryLearnMaxEntries: Schema<number, number>;
|
|
95
|
+
memoryLearnConsolidateMaxChars: Schema<number, number>;
|
|
96
|
+
memoryLearnModel: Schema<string, string>;
|
|
84
97
|
}> | Schemastery.ObjectS<{
|
|
85
98
|
enableBlacklist: Schema<boolean, boolean>;
|
|
86
99
|
blacklistFileName: Schema<string, string>;
|
|
@@ -131,6 +144,9 @@ export declare const Config: Schema<Schemastery.ObjectS<{
|
|
|
131
144
|
aiModel: Schema<string, string>;
|
|
132
145
|
aiAgentName: Schema<string, string>;
|
|
133
146
|
aiMentionCooldownSeconds: Schema<number, number>;
|
|
147
|
+
aiCooldownNotice: Schema<boolean, boolean>;
|
|
148
|
+
aiCooldownNoticeText: Schema<string, string>;
|
|
149
|
+
aiCooldownNoticeIntervalSeconds: Schema<number, number>;
|
|
134
150
|
aiIdleTopicMinutes: Schema<number, number>;
|
|
135
151
|
aiIdleTopicMinHour: Schema<number, number>;
|
|
136
152
|
aiIdleTopicMaxHour: Schema<number, number>;
|
|
@@ -151,6 +167,8 @@ export declare const Config: Schema<Schemastery.ObjectS<{
|
|
|
151
167
|
aiRandomReplyProbability: Schema<number, number>;
|
|
152
168
|
aiMinReplyIntervalSeconds: Schema<number, number>;
|
|
153
169
|
aiContextWindow: Schema<number, number>;
|
|
170
|
+
aiContextMaxChars: Schema<number, number>;
|
|
171
|
+
aiStyleMaxChars: Schema<number, number>;
|
|
154
172
|
aiTemperature: Schema<number, number>;
|
|
155
173
|
aiMaxOutputTokens: Schema<number, number>;
|
|
156
174
|
aiMaxReplyChars: Schema<number, number>;
|
|
@@ -217,6 +235,10 @@ export declare const Config: Schema<Schemastery.ObjectS<{
|
|
|
217
235
|
groupRules: Schemastery.ObjectT<{
|
|
218
236
|
guildId: Schema<string, string>;
|
|
219
237
|
enableAiReply: Schema<boolean, boolean>;
|
|
238
|
+
aiMessageThreshold: Schema<number, number>;
|
|
239
|
+
aiRandomReplyProbability: Schema<number, number>;
|
|
240
|
+
aiMinReplyIntervalSeconds: Schema<number, number>;
|
|
241
|
+
aiCooldownNotice: Schema<boolean, boolean>;
|
|
220
242
|
bannedWords: Schema<string[], string[]>;
|
|
221
243
|
whitelistKeywords: Schema<string[], string[]>;
|
|
222
244
|
strongKeywords: Schema<string[], string[]>;
|
|
@@ -254,6 +276,11 @@ export declare const Config: Schema<Schemastery.ObjectS<{
|
|
|
254
276
|
memoryLearnPushBack: boolean;
|
|
255
277
|
memoryLearnDuplicateThreshold: number;
|
|
256
278
|
memoryLearnDailyLimit: number;
|
|
279
|
+
memoryLearnConsolidate: boolean;
|
|
280
|
+
memoryLearnConsolidateEvery: number;
|
|
281
|
+
memoryLearnMaxEntries: number;
|
|
282
|
+
memoryLearnConsolidateMaxChars: number;
|
|
283
|
+
memoryLearnModel: string;
|
|
257
284
|
} & {
|
|
258
285
|
enableBlacklist: boolean;
|
|
259
286
|
blacklistFileName: string;
|
|
@@ -295,6 +322,9 @@ export declare const Config: Schema<Schemastery.ObjectS<{
|
|
|
295
322
|
aiModel: string;
|
|
296
323
|
aiAgentName: string;
|
|
297
324
|
aiMentionCooldownSeconds: number;
|
|
325
|
+
aiCooldownNotice: boolean;
|
|
326
|
+
aiCooldownNoticeText: string;
|
|
327
|
+
aiCooldownNoticeIntervalSeconds: number;
|
|
298
328
|
aiIdleTopicMinutes: number;
|
|
299
329
|
aiIdleTopicMinHour: number;
|
|
300
330
|
aiIdleTopicMaxHour: number;
|
|
@@ -311,6 +341,8 @@ export declare const Config: Schema<Schemastery.ObjectS<{
|
|
|
311
341
|
aiRandomReplyProbability: number;
|
|
312
342
|
aiMinReplyIntervalSeconds: number;
|
|
313
343
|
aiContextWindow: number;
|
|
344
|
+
aiContextMaxChars: number;
|
|
345
|
+
aiStyleMaxChars: number;
|
|
314
346
|
aiTemperature: number;
|
|
315
347
|
aiMaxOutputTokens: number;
|
|
316
348
|
aiMaxReplyChars: number;
|
package/lib/index.js
CHANGED
|
@@ -35,6 +35,10 @@ exports.Config = koishi_1.Schema.intersect([
|
|
|
35
35
|
groupRules: koishi_1.Schema.array(koishi_1.Schema.object({
|
|
36
36
|
guildId: koishi_1.Schema.string().required().description('群号。'),
|
|
37
37
|
enableAiReply: koishi_1.Schema.boolean().description('该群是否启用 AI 回复(留空则跟随全局设置)。'),
|
|
38
|
+
aiMessageThreshold: koishi_1.Schema.natural().description('该群触发的消息数阈值(留空跟随全局 aiMessageThreshold)。'),
|
|
39
|
+
aiRandomReplyProbability: koishi_1.Schema.percent().description('该群随机触发概率(0~1,控制台按百分比显示;留空跟随全局)。'),
|
|
40
|
+
aiMinReplyIntervalSeconds: koishi_1.Schema.natural().description('该群模型调用冷却(秒;留空跟随全局 aiMinReplyIntervalSeconds)。'),
|
|
41
|
+
aiCooldownNotice: koishi_1.Schema.boolean().description('该群被 @ 但处于冷却时是否提示还剩多久(留空跟随全局)。'),
|
|
38
42
|
bannedWords: koishi_1.Schema.array(String).role('table').default([]).description('该群专属违禁词列表(同样支持“关键词|分值”与顺序模糊匹配)。'),
|
|
39
43
|
whitelistKeywords: koishi_1.Schema.array(String).role('table').default([]).description('该群违禁词白名单:消息只要命中其中任意一个关键词,就整条跳过违禁词评分(用于校学生会招新等允许的宣传场景)。'),
|
|
40
44
|
strongKeywords: koishi_1.Schema.array(String).role('table').description('该群强关键词(留空则跟随全局设置)。命中后无视白名单豁免,用于堵住「附一个白名单词即可绕过管控」的漏洞。'),
|
|
@@ -74,6 +78,11 @@ exports.Config = koishi_1.Schema.intersect([
|
|
|
74
78
|
memoryLearnPushBack: koishi_1.Schema.boolean().default(true).description('归纳后把结果回推到原会话:私聊发私聊、群聊发对应群。'),
|
|
75
79
|
memoryLearnDuplicateThreshold: koishi_1.Schema.number().min(0.3).max(0.95).step(0.05).default(0.55).description('与已有记忆相似度超过该值就跳过,避免同一件事反复记。'),
|
|
76
80
|
memoryLearnDailyLimit: koishi_1.Schema.natural().min(1).max(200).default(30).description('每个作用域每天最多归纳几次,防止刷模型额度。'),
|
|
81
|
+
memoryLearnConsolidate: koishi_1.Schema.boolean().default(true).description('自动归纳压缩:每累计 N 条就合并成一条更短的记忆,避免记忆越堆越长、反复占用提示词。'),
|
|
82
|
+
memoryLearnConsolidateEvery: koishi_1.Schema.natural().min(2).max(20).default(5).description('每多少条自动记忆压缩一次(默认 5)。'),
|
|
83
|
+
memoryLearnMaxEntries: koishi_1.Schema.natural().min(1).max(100).default(15).description('自动归纳记忆的条数上限(不含手动 [memory] 保存的内容);超过后会强制压缩。'),
|
|
84
|
+
memoryLearnConsolidateMaxChars: koishi_1.Schema.natural().min(40).max(300).default(90).description('压缩后单条记忆的字数上限。'),
|
|
85
|
+
memoryLearnModel: koishi_1.Schema.string().default('').description('自动归纳/压缩使用的模型(留空沿用 aiModel)。可用更便宜的模型降低开销。'),
|
|
77
86
|
}).description('记忆库'),
|
|
78
87
|
koishi_1.Schema.object({
|
|
79
88
|
enableBlacklist: koishi_1.Schema.boolean().default(true).description('全局黑名单系统开关。单个群未单独设置时跟随此值;单个群设置后以该群为准(全局关着也能为某群单独开启)。'),
|
|
@@ -122,7 +131,10 @@ exports.Config = koishi_1.Schema.intersect([
|
|
|
122
131
|
aiBaseUrl: koishi_1.Schema.string().default('https://api.openai.com/v1').description('接口基础地址(OpenAI 兼容模式需以 /v1 结尾;Gemini 模式可留默认)。'),
|
|
123
132
|
aiModel: koishi_1.Schema.string().default('gpt-4o-mini').description('模型名称,如 gpt-4o-mini / doubao-1.5-lite / gemini-2.0-flash。'),
|
|
124
133
|
aiAgentName: koishi_1.Schema.string().default('bot').description('默认智能体自称(可在配置页修改)。'),
|
|
125
|
-
aiMentionCooldownSeconds: koishi_1.Schema.natural().min(0).max(3600).default(5).description('
|
|
134
|
+
aiMentionCooldownSeconds: koishi_1.Schema.natural().min(0).max(3600).default(5).description('(兼容字段)旧的 @ 冷却;现在所有模型调用统一走 aiMinReplyIntervalSeconds。'),
|
|
135
|
+
aiCooldownNotice: koishi_1.Schema.boolean().default(true).description('被 @ 但处于模型调用冷却时,是否在群里提示还剩多久(纯本地消息,不调用模型)。'),
|
|
136
|
+
aiCooldownNoticeText: koishi_1.Schema.string().default('还在冷却,{seconds} 秒后再叫我。').description('冷却提示文案,可用 {seconds} 与 {minutes} 占位。'),
|
|
137
|
+
aiCooldownNoticeIntervalSeconds: koishi_1.Schema.natural().min(0).max(600).default(20).description('同一群两次冷却提示的最小间隔(秒),防止连续 @ 刷屏;0 = 不限制。'),
|
|
126
138
|
aiIdleTopicMinutes: koishi_1.Schema.natural().min(0).max(1440).default(120).description('群内无消息多少分钟后主动抛话题;0=关闭。'),
|
|
127
139
|
aiIdleTopicMinHour: koishi_1.Schema.natural().min(0).max(23).default(9).description('主动话题允许的最早小时(0-23)。'),
|
|
128
140
|
aiIdleTopicMaxHour: koishi_1.Schema.natural().min(0).max(24).default(23).description('主动话题允许的最晚小时(不含)。'),
|
|
@@ -143,6 +155,8 @@ exports.Config = koishi_1.Schema.intersect([
|
|
|
143
155
|
aiRandomReplyProbability: koishi_1.Schema.percent().default(0.08).description('随机触发概率(每条消息掷一次骰)。random / hybrid 模式下未掷中直接跳过,不会调用 AI 接口。填 0 等同于关闭随机触发。'),
|
|
144
156
|
aiMinReplyIntervalSeconds: koishi_1.Schema.natural().default(45).description('同一群两次真正调用模型的最短间隔(秒)。'),
|
|
145
157
|
aiContextWindow: koishi_1.Schema.natural().default(30).description('参与推理的最近消息条数(建议 20~40;太小会导致模型看不到前文)。'),
|
|
158
|
+
aiContextMaxChars: koishi_1.Schema.natural().min(0).max(20000).default(0).description('历史上下文的字数上限:超过后从最旧的消息开始丢(至少保留最近 4 条)。0 = 不限。想省 token 可设 2500~3500。'),
|
|
159
|
+
aiStyleMaxChars: koishi_1.Schema.natural().min(0).max(20000).default(0).description('外部插件注入的「说话风格」段每条最多保留多少字。0 = 不限。风格 skill 很长时可设 1200~1800 显著省 token。'),
|
|
146
160
|
aiTemperature: koishi_1.Schema.number().min(0).max(2).step(0.1).default(0.8).description('生成温度(0~2)。群聊接话建议 0.7~0.9,过高会跑题、复述、发散。'),
|
|
147
161
|
aiMaxOutputTokens: koishi_1.Schema.natural().default(400).description('AI 最大输出 token 数。群聊一句话不需要太大,过大会增加延迟。'),
|
|
148
162
|
aiMaxReplyChars: koishi_1.Schema.natural().min(20).max(500).default(120).description('单条回复的最大字符数,超出会在句末截断。'),
|
package/lib/service.d.ts
CHANGED
|
@@ -226,6 +226,11 @@ export class QQGroupManagerService extends koishi_1.Service<any, koishi_1.Contex
|
|
|
226
226
|
kind: "group" | "private";
|
|
227
227
|
id: string;
|
|
228
228
|
} | null, label?: string): Promise<any>;
|
|
229
|
+
/**
|
|
230
|
+
* 自动归纳专用写入:追加一条新记忆,并可同时删掉若干旧条目。
|
|
231
|
+
* 用于「每 N 条压缩成 1 条」——remove 里的旧条目会被新条目替换。
|
|
232
|
+
*/
|
|
233
|
+
commitAutoMemory(scope: any, payload: any): Promise<any>;
|
|
229
234
|
searchMemory(keyword: any, limit: number, scope: any): Promise<string[]>;
|
|
230
235
|
/** 按关键词删除记忆条目(含该关键词的整条记忆块)。返回 { ok, removed, message } */
|
|
231
236
|
deleteMemoryByKeyword(keyword: any, scope: any): Promise<any>;
|
|
@@ -352,6 +357,8 @@ export class QQGroupManagerService extends koishi_1.Service<any, koishi_1.Contex
|
|
|
352
357
|
resolveImage(ref: any, session: any): Promise<any>;
|
|
353
358
|
/** AI 回复发送:按 aiQuoteReplyMode 决定是否引用触发消息,失败则退化为纯文本 */
|
|
354
359
|
sendReply(session: any, text: any): Promise<void>;
|
|
360
|
+
/** 系统提示(例如「冷却中」),不引用原消息、不写入 AI 上下文 */
|
|
361
|
+
sendNotice(session: any, text: any): Promise<void>;
|
|
355
362
|
extractTextForFilter(session: any): any;
|
|
356
363
|
hasTextContent(session: any): boolean;
|
|
357
364
|
hasImageOrFace(session: any): any;
|