@nestim/koishi-plugin-qqgm-chat-style 1.0.5 → 1.1.0
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/index.js +155 -25
- package/lib/compact.js +12 -3
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -57,6 +57,65 @@ const ExtraStyle = Schema.object({
|
|
|
57
57
|
.description('叠加时的行数上限;留默认即可。'),
|
|
58
58
|
}).description('追加风格');
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* 从 Koishi 配置文件里读取主插件的群号,供配置页下拉选择。
|
|
62
|
+
* 这样分群配置不用再手填一遍群号;新增群后重载本插件即可刷新列表。
|
|
63
|
+
*/
|
|
64
|
+
function collectMainGroupIds() {
|
|
65
|
+
try {
|
|
66
|
+
const fs = require('node:fs');
|
|
67
|
+
const yaml = require('js-yaml');
|
|
68
|
+
const candidates = ['koishi.yml', 'koishi.yaml', 'koishi.json'];
|
|
69
|
+
let doc = null;
|
|
70
|
+
for (const file of candidates) {
|
|
71
|
+
const full = path.join(process.cwd(), file);
|
|
72
|
+
if (!fs.existsSync(full)) continue;
|
|
73
|
+
const raw = fs.readFileSync(full, 'utf8');
|
|
74
|
+
doc = file.endsWith('.json') ? JSON.parse(raw) : yaml.load(raw);
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
const ids = new Set();
|
|
78
|
+
const visit = (node, depth) => {
|
|
79
|
+
if (!node || typeof node !== 'object' || depth > 8) return;
|
|
80
|
+
for (const [key, value] of Object.entries(node)) {
|
|
81
|
+
if (typeof key === 'string' && key.includes('qq-group-manager') && value && typeof value === 'object') {
|
|
82
|
+
for (const rule of value.groupRules || []) {
|
|
83
|
+
const id = String(rule?.guildId || '').trim();
|
|
84
|
+
if (id) ids.add(id);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (value && typeof value === 'object') visit(value, depth + 1);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
visit(doc, 0);
|
|
91
|
+
return [...ids].sort();
|
|
92
|
+
} catch {
|
|
93
|
+
return [];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const MAIN_GROUP_IDS = collectMainGroupIds();
|
|
98
|
+
|
|
99
|
+
/** 单群风格配置:不填/不启用的项自动回落到全局 */
|
|
100
|
+
const GroupStyle = Schema.object({
|
|
101
|
+
groupId: (MAIN_GROUP_IDS.length
|
|
102
|
+
? Schema.union(MAIN_GROUP_IDS.map((id) => Schema.const(id).description(id)))
|
|
103
|
+
: Schema.string()).required()
|
|
104
|
+
.description('群号。列表自动读取主插件 groupRules;新增群后重载本插件即可出现。'),
|
|
105
|
+
enabled: Schema.boolean().default(true)
|
|
106
|
+
.description('启用该群的独立配置;关闭则完全使用全局配置。'),
|
|
107
|
+
systemPrompt: Schema.string().role('textarea').default('')
|
|
108
|
+
.description('覆盖主插件的默认系统提示词(aiSystemPrompt);留空则沿用主插件。'),
|
|
109
|
+
primaryStyle: Schema.string().default('')
|
|
110
|
+
.description('该群主风格;留空沿用全局主风格。'),
|
|
111
|
+
extraStyles: Schema.array(ExtraStyle).default([])
|
|
112
|
+
.description('该群追加风格;留空沿用全局追加风格。'),
|
|
113
|
+
maxChars: Schema.natural().min(10).max(500)
|
|
114
|
+
.description('该群单条回复最大字符数;留空沿用全局。'),
|
|
115
|
+
maxLines: Schema.natural().min(1).max(10)
|
|
116
|
+
.description('该群单条回复最大行数;留空沿用全局。'),
|
|
117
|
+
}).description('单群风格');
|
|
118
|
+
|
|
60
119
|
const Config = Schema.intersect([
|
|
61
120
|
Schema.object({
|
|
62
121
|
primaryStyle: Schema.string().default('')
|
|
@@ -92,11 +151,15 @@ const Config = Schema.intersect([
|
|
|
92
151
|
debug: Schema.boolean().default(false)
|
|
93
152
|
.description('日志里打印风格组合、随机结果与过滤细节。'),
|
|
94
153
|
}).description('输出规范'),
|
|
154
|
+
Schema.object({
|
|
155
|
+
groups: Schema.array(GroupStyle).role('table').default([])
|
|
156
|
+
.description('按群覆盖:不启用或不填的项自动使用全局配置。群号从主插件 groupRules 自动读取,不用重复录入。'),
|
|
157
|
+
}).description('分群配置'),
|
|
95
158
|
Schema.object({
|
|
96
159
|
compact: Schema.boolean().default(true)
|
|
97
160
|
.description('把主风格 skill 压缩成一张更短的风格卡(保留人设/语气/口癖,丢掉示例与说明),显著降低每次调用的输入 token。'),
|
|
98
|
-
compactMaxChars: Schema.natural().min(200).max(3000).default(
|
|
99
|
-
.description('
|
|
161
|
+
compactMaxChars: Schema.natural().min(200).max(3000).default(900)
|
|
162
|
+
.description('风格卡的字数上限(默认 900)。越小越省 token,但可能把锐评/暴论的骨架压丢;改小前先看卡片内容。'),
|
|
100
163
|
compactModel: Schema.string().default('')
|
|
101
164
|
.description('生成风格卡用的模型;留空沿用主插件的 aiModel。只在首次生成或 skill 正文变更时调用一次。'),
|
|
102
165
|
compactExtras: Schema.boolean().default(false)
|
|
@@ -141,6 +204,48 @@ function apply(ctx, config) {
|
|
|
141
204
|
for (const extra of set.extras) cards.ensure(extra.skill, extra.id);
|
|
142
205
|
}
|
|
143
206
|
|
|
207
|
+
const globalProfile = {
|
|
208
|
+
set,
|
|
209
|
+
maxChars: Number(config.maxChars) || 60,
|
|
210
|
+
maxLines: Number(config.maxLines) || 3,
|
|
211
|
+
systemPrompt: '',
|
|
212
|
+
};
|
|
213
|
+
const groupProfiles = new Map();
|
|
214
|
+
for (const entry of Array.isArray(config.groups) ? config.groups : []) {
|
|
215
|
+
const gid = String(entry?.groupId || '').trim();
|
|
216
|
+
if (!gid || entry.enabled === false) continue;
|
|
217
|
+
const merged = {
|
|
218
|
+
...config,
|
|
219
|
+
primaryStyle: String(entry.primaryStyle || '').trim() || config.primaryStyle,
|
|
220
|
+
extraStyles: Array.isArray(entry.extraStyles) && entry.extraStyles.length
|
|
221
|
+
? entry.extraStyles
|
|
222
|
+
: config.extraStyles,
|
|
223
|
+
};
|
|
224
|
+
let groupSet;
|
|
225
|
+
try {
|
|
226
|
+
groupSet = buildStyleSet(root, merged);
|
|
227
|
+
} catch (error) {
|
|
228
|
+
logger.warn(`[群 ${gid}] 读取风格失败:${String(error)}`);
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
for (const warning of groupSet.warnings) logger.warn(`[群 ${gid}] ${warning}`);
|
|
232
|
+
if (!groupSet.primary) {
|
|
233
|
+
logger.warn(`[群 ${gid}] 没有可用的主风格,继续使用全局配置`);
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
groupProfiles.set(gid, {
|
|
237
|
+
set: groupSet,
|
|
238
|
+
maxChars: Number(entry.maxChars) > 0 ? Number(entry.maxChars) : globalProfile.maxChars,
|
|
239
|
+
maxLines: Number(entry.maxLines) > 0 ? Number(entry.maxLines) : globalProfile.maxLines,
|
|
240
|
+
systemPrompt: String(entry.systemPrompt || '').trim(),
|
|
241
|
+
});
|
|
242
|
+
cards.ensure(groupSet.primary.skill, groupSet.primary.id);
|
|
243
|
+
if (config.compactExtras) {
|
|
244
|
+
for (const extra of groupSet.extras) cards.ensure(extra.skill, extra.id);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
const profileFor = (groupId) => groupProfiles.get(String(groupId || '')) || globalProfile;
|
|
248
|
+
|
|
144
249
|
// 只用 get() 读取:直接访问未注册属性会触发 Koishi 的 "not registered" 告警
|
|
145
250
|
const findHooks = () => {
|
|
146
251
|
const read = (target, key) => {
|
|
@@ -180,6 +285,20 @@ function apply(ctx, config) {
|
|
|
180
285
|
lines.push('', `风格卡:${config.compact === false
|
|
181
286
|
? '已关闭(每次发送完整 skill)'
|
|
182
287
|
: (card ? `已启用(${card.length} 字)` : '生成中/生成失败,暂用完整 skill')}`);
|
|
288
|
+
if (groupProfiles.size) {
|
|
289
|
+
lines.push('', '分群配置:');
|
|
290
|
+
for (const [gid, profile] of groupProfiles) {
|
|
291
|
+
const extras = profile.set.extras.length
|
|
292
|
+
? profile.set.extras.map((item) => `${item.id}(${Math.round(item.probability * 100)}%)`).join('、')
|
|
293
|
+
: '无';
|
|
294
|
+
lines.push(
|
|
295
|
+
`- ${gid}:主 ${profile.set.primary.id}|追加 ${extras}`
|
|
296
|
+
+ `|提示词 ${profile.systemPrompt ? '覆盖' : '跟随全局'}`,
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
} else {
|
|
300
|
+
lines.push('', '分群配置:(无,全部使用全局)');
|
|
301
|
+
}
|
|
183
302
|
if (set.disabled && set.disabled.length) {
|
|
184
303
|
lines.push('', '已关闭的追加风格:');
|
|
185
304
|
for (const item of set.disabled) {
|
|
@@ -197,15 +316,17 @@ function apply(ctx, config) {
|
|
|
197
316
|
function mount(hooks) {
|
|
198
317
|
mounted = true;
|
|
199
318
|
|
|
200
|
-
//
|
|
319
|
+
// 主风格:按群取 profile;没有独立配置就用全局
|
|
201
320
|
offs.push(hooks.registerPromptSection(async (hookCtx) => {
|
|
202
|
-
const
|
|
203
|
-
|
|
204
|
-
const
|
|
321
|
+
const profile = profileFor(hookCtx?.groupId);
|
|
322
|
+
const active = profile.set;
|
|
323
|
+
const primaryCard = cards.get(active.primary.id);
|
|
324
|
+
if (!primaryCard) cards.ensure(active.primary.skill, active.primary.id);
|
|
325
|
+
const parts = [primarySection(active.primary, config, {
|
|
205
326
|
direct: !!hookCtx?.session?.isDirect,
|
|
206
327
|
compactText: primaryCard,
|
|
207
328
|
})];
|
|
208
|
-
for (const extra of
|
|
329
|
+
for (const extra of active.extras) {
|
|
209
330
|
if (extra.probability <= 0) continue;
|
|
210
331
|
if (!rolls.roll(hookCtx, extra.id, extra.probability)) continue;
|
|
211
332
|
if (config.debug) {
|
|
@@ -218,27 +339,29 @@ function apply(ctx, config) {
|
|
|
218
339
|
return parts.join('\n\n');
|
|
219
340
|
}));
|
|
220
341
|
|
|
342
|
+
// 分群系统提示词:覆盖主插件的 aiSystemPrompt(需要主插件 >= 1.4.11)
|
|
343
|
+
if (typeof hooks.registerPersonaOverride === 'function') {
|
|
344
|
+
offs.push(hooks.registerPersonaOverride((hookCtx) => {
|
|
345
|
+
const profile = profileFor(hookCtx?.groupId);
|
|
346
|
+
return profile.systemPrompt || null;
|
|
347
|
+
}));
|
|
348
|
+
} else {
|
|
349
|
+
logger.warn('主插件 aiHooks 不支持 registerPersonaOverride(需要 >= 1.4.11),分群系统提示词不会生效');
|
|
350
|
+
}
|
|
351
|
+
|
|
221
352
|
// 输出规范:字数上限取“本轮真正叠上的最宽值”,避免追加风格被主风格的长度截断
|
|
222
|
-
const baseFilter = createOutputFilter(config, logger);
|
|
223
353
|
offs.push(hooks.registerOutputFilter((text, hookCtx) => {
|
|
224
|
-
|
|
225
|
-
|
|
354
|
+
const profile = profileFor(hookCtx?.groupId);
|
|
355
|
+
const active = profile.set;
|
|
356
|
+
let maxChars = Math.max(10, Number(profile.maxChars) || Number(config.maxChars) || 60);
|
|
357
|
+
let maxLines = Math.max(1, Number(profile.maxLines) || Number(config.maxLines) || 3);
|
|
358
|
+
for (const extra of active.extras) {
|
|
226
359
|
if (extra.probability <= 0) continue;
|
|
227
360
|
if (!rolls.roll(hookCtx, extra.id, extra.probability)) continue;
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
maxChars: Math.max(limit.maxChars, extra.minChars),
|
|
231
|
-
maxLines: Math.max(limit.maxLines, extra.maxLines),
|
|
232
|
-
}
|
|
233
|
-
: { maxChars: extra.minChars, maxLines: extra.maxLines };
|
|
361
|
+
maxChars = Math.max(maxChars, extra.minChars);
|
|
362
|
+
maxLines = Math.max(maxLines, extra.maxLines);
|
|
234
363
|
}
|
|
235
|
-
|
|
236
|
-
const merged = {
|
|
237
|
-
...config,
|
|
238
|
-
maxChars: Math.max(Number(config.maxChars) || 60, limit.maxChars),
|
|
239
|
-
maxLines: Math.max(Number(config.maxLines) || 3, limit.maxLines),
|
|
240
|
-
};
|
|
241
|
-
return createOutputFilter(merged, logger)(text);
|
|
364
|
+
return createOutputFilter({ ...config, maxChars, maxLines }, logger)(text);
|
|
242
365
|
}));
|
|
243
366
|
|
|
244
367
|
const extraDesc = set.extras.length
|
|
@@ -250,8 +373,15 @@ function apply(ctx, config) {
|
|
|
250
373
|
: (card ? `${card.length} 字` : '生成中(暂用完整 skill)');
|
|
251
374
|
logger.info(
|
|
252
375
|
`主风格「${set.primary.name}」(${set.primary.id}):正文 ${set.primary.skill.body.length} 字`
|
|
253
|
-
+ ` | 风格卡:${cardDesc} | 追加风格:${extraDesc}
|
|
376
|
+
+ ` | 风格卡:${cardDesc} | 追加风格:${extraDesc}`
|
|
377
|
+
+ ` | 分群配置:${groupProfiles.size} 个`,
|
|
254
378
|
);
|
|
379
|
+
for (const [gid, profile] of groupProfiles) {
|
|
380
|
+
logger.info(
|
|
381
|
+
`[群 ${gid}] 主风格「${profile.set.primary.name}」(${profile.set.primary.id})`
|
|
382
|
+
+ ` | 提示词:${profile.systemPrompt ? '覆盖' : '跟随全局'}`,
|
|
383
|
+
);
|
|
384
|
+
}
|
|
255
385
|
if (config.debug) {
|
|
256
386
|
logger.info(
|
|
257
387
|
`[debug] skillDir=${root} 可用=${set.available.filter((i) => i.ok).map((i) => i.id).join(',')}`,
|
|
@@ -297,4 +427,4 @@ function apply(ctx, config) {
|
|
|
297
427
|
});
|
|
298
428
|
}
|
|
299
429
|
|
|
300
|
-
module.exports = { name, inject, Config, apply };
|
|
430
|
+
module.exports = { name, inject, Config, apply, MAIN_GROUP_IDS };
|
package/lib/compact.js
CHANGED
|
@@ -19,10 +19,12 @@ const path = require('node:path');
|
|
|
19
19
|
|
|
20
20
|
const CACHE_FILE = '.style-cards.json';
|
|
21
21
|
const RETRY_MS = 10 * 60 * 1000;
|
|
22
|
+
/** 压缩提示词版本:改了提示词就 +1,缓存哈希跟着变,旧卡自动重生成 */
|
|
23
|
+
const PROMPT_VERSION = 2;
|
|
22
24
|
|
|
23
25
|
function hashSkill(skill) {
|
|
24
26
|
const body = String((skill && skill.body) || '');
|
|
25
|
-
return crypto.createHash('sha256').update(body).digest('hex').slice(0, 16);
|
|
27
|
+
return crypto.createHash('sha256').update(`${PROMPT_VERSION}|${body}`).digest('hex').slice(0, 16);
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
function cleanCard(raw, maxChars) {
|
|
@@ -35,7 +37,7 @@ function cleanCard(raw, maxChars) {
|
|
|
35
37
|
.trim();
|
|
36
38
|
if (!text) return '';
|
|
37
39
|
|
|
38
|
-
const limit = Math.max(50, Number(maxChars) ||
|
|
40
|
+
const limit = Math.max(50, Number(maxChars) || 900);
|
|
39
41
|
if (text.length > limit) {
|
|
40
42
|
const slice = text.slice(0, limit);
|
|
41
43
|
const cut = Math.max(
|
|
@@ -66,12 +68,19 @@ function buildCompactPrompt(skill, maxChars) {
|
|
|
66
68
|
'- 高频口癖与语气词(挑最有辨识度的,不要抄整段);',
|
|
67
69
|
'- 明确的禁忌与边界。',
|
|
68
70
|
'',
|
|
71
|
+
'如果原文里写了下面这些「行为机制」,必须原样保留其骨架,不能只留态度:',
|
|
72
|
+
'- 模式与切换:接话 / 锐评 / 对线 / 收着说 分别在什么信号下触发、大概多长;',
|
|
73
|
+
'- 锐评与暴论的结构:先给结论、再给机制、最后补一刀;或者「接住 → 换层 → 给证据 → 补刀/留台阶」;',
|
|
74
|
+
'- 骂人分档与使用范围(哪些档位只能在对喷模式用)、对喷的退出条件;',
|
|
75
|
+
'- 什么时候必须收嘴、脱角色。',
|
|
76
|
+
'',
|
|
69
77
|
'必须丢掉:',
|
|
70
78
|
'- 背景故事、来源说明、示例对话、作者/版本/标签等元信息;',
|
|
71
79
|
'- 重复解释、教学式说明、“这段用来……”之类的描述。',
|
|
72
80
|
'',
|
|
73
81
|
'输出要求:',
|
|
74
82
|
`- 一张纯文本风格卡,不超过 ${maxChars} 字;`,
|
|
83
|
+
'- 锐评/暴论的部分宁可占篇幅,也不要压成一句“爱抬杠”;',
|
|
75
84
|
'- 接近字数上限时用一句完整的话收尾,不要截断在半句;',
|
|
76
85
|
'- 用第二人称写(“你说话……”),像给演员的角色提示;',
|
|
77
86
|
'- 不要标题、编号、列表、Markdown、代码块;',
|
|
@@ -96,7 +105,7 @@ class StyleCardStore {
|
|
|
96
105
|
this.config = options.config || {};
|
|
97
106
|
this.enabled = this.config.compact !== false;
|
|
98
107
|
const configured = Number(this.config.compactMaxChars);
|
|
99
|
-
this.maxChars = Math.max(200, Math.min(3000, Number.isFinite(configured) && configured > 0 ? configured :
|
|
108
|
+
this.maxChars = Math.max(200, Math.min(3000, Number.isFinite(configured) && configured > 0 ? configured : 900));
|
|
100
109
|
this.model = String(this.config.compactModel || '').trim();
|
|
101
110
|
|
|
102
111
|
this.texts = new Map();
|
package/package.json
CHANGED