@nestim/koishi-plugin-qqgm-chat-style 1.1.0 → 1.1.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/index.js +2 -0
- package/lib/compact.js +226 -226
- package/lib/prompt.js +40 -40
- package/lib/registry.js +172 -172
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -255,7 +255,9 @@ function apply(ctx, config) {
|
|
|
255
255
|
return null;
|
|
256
256
|
}
|
|
257
257
|
};
|
|
258
|
+
const sdk = read(ctx.root, 'qqgmSdk');
|
|
258
259
|
return read(ctx.root, 'qqgmAiHooks')
|
|
260
|
+
|| (sdk && sdk.hooks)
|
|
259
261
|
|| read(ctx, 'qqgmAiHooks')
|
|
260
262
|
|| (read(ctx.root, 'nestimQqGroupManager') || {}).aiHooks
|
|
261
263
|
|| (read(ctx, 'nestimQqGroupManager') || {}).aiHooks
|
package/lib/compact.js
CHANGED
|
@@ -1,226 +1,226 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* lib/compact.js —— 风格卡压缩。
|
|
3
|
-
*
|
|
4
|
-
* 背景:一份完整的风格 skill 动辄 4000+ 字,而它每次模型调用都要整段塞进
|
|
5
|
-
* system prompt。真实用量里这是最大的一块固定开销。
|
|
6
|
-
*
|
|
7
|
-
* 做法:
|
|
8
|
-
* - 第一次运行时让模型把 skill 正文蒸馏成一张 700 字左右的「风格卡」
|
|
9
|
-
* (保留人设、语气、句长、口癖、称呼、禁忌;丢掉示例、解释、元信息);
|
|
10
|
-
* - 风格卡按 skill 正文的哈希缓存到 `<skillDir>/.style-cards.json`,
|
|
11
|
-
* 正文没变就一直复用,不会每次启动都调模型;
|
|
12
|
-
* - 生成失败时不影响聊天:继续用完整 skill,隔一段时间再重试。
|
|
13
|
-
*
|
|
14
|
-
* 依赖主插件暴露的 qqgmAiRuntime.chat();拿不到就静默跳过。
|
|
15
|
-
*/
|
|
16
|
-
const crypto = require('node:crypto');
|
|
17
|
-
const fs = require('node:fs');
|
|
18
|
-
const path = require('node:path');
|
|
19
|
-
|
|
20
|
-
const CACHE_FILE = '.style-cards.json';
|
|
21
|
-
const RETRY_MS = 10 * 60 * 1000;
|
|
22
|
-
/** 压缩提示词版本:改了提示词就 +1,缓存哈希跟着变,旧卡自动重生成 */
|
|
23
|
-
const PROMPT_VERSION = 2;
|
|
24
|
-
|
|
25
|
-
function hashSkill(skill) {
|
|
26
|
-
const body = String((skill && skill.body) || '');
|
|
27
|
-
return crypto.createHash('sha256').update(`${PROMPT_VERSION}|${body}`).digest('hex').slice(0, 16);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function cleanCard(raw, maxChars) {
|
|
31
|
-
let text = String(raw || '').trim();
|
|
32
|
-
if (!text) return '';
|
|
33
|
-
text = text
|
|
34
|
-
.replace(/^\s*```[a-zA-Z0-9_-]*\s*\n?/, '')
|
|
35
|
-
.replace(/\n?\s*```\s*$/, '')
|
|
36
|
-
.replace(/^["'“”‘’「」『』]+|["'“”‘’「」『』]+$/g, '')
|
|
37
|
-
.trim();
|
|
38
|
-
if (!text) return '';
|
|
39
|
-
|
|
40
|
-
const limit = Math.max(50, Number(maxChars) || 900);
|
|
41
|
-
if (text.length > limit) {
|
|
42
|
-
const slice = text.slice(0, limit);
|
|
43
|
-
const cut = Math.max(
|
|
44
|
-
slice.lastIndexOf('。'),
|
|
45
|
-
slice.lastIndexOf(';'),
|
|
46
|
-
slice.lastIndexOf('\n'),
|
|
47
|
-
);
|
|
48
|
-
text = cut >= Math.floor(limit * 0.5) ? slice.slice(0, cut + 1) : `${slice}…`;
|
|
49
|
-
}
|
|
50
|
-
// 模型在字数上限处截断时,可能停在一个逗号/顿号上:退回到最近的完整句
|
|
51
|
-
if (/[,、:;,;]$/.test(text)) {
|
|
52
|
-
const cut = Math.max(text.lastIndexOf('。'), text.lastIndexOf('\n'));
|
|
53
|
-
if (cut > text.length * 0.6) text = text.slice(0, cut + 1).trim();
|
|
54
|
-
}
|
|
55
|
-
return text.trim();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** 让模型把 skill 压成风格卡 */
|
|
59
|
-
function buildCompactPrompt(skill, maxChars) {
|
|
60
|
-
const name = (skill && (skill.name || skill.id)) || '当前风格';
|
|
61
|
-
const system = [
|
|
62
|
-
'你在把一份「聊天风格说明」压缩成一张更短的风格卡,供另一个模型直接照着说话。',
|
|
63
|
-
'',
|
|
64
|
-
'必须保留:',
|
|
65
|
-
'- 人设身份:这个角色是谁、怎么称呼别人、自称什么;',
|
|
66
|
-
'- 语气与态度:说话冲不冲、爱不爱抬杠、是否骂人;',
|
|
67
|
-
'- 句长与节奏:一句话大概多长、一次说几句;',
|
|
68
|
-
'- 高频口癖与语气词(挑最有辨识度的,不要抄整段);',
|
|
69
|
-
'- 明确的禁忌与边界。',
|
|
70
|
-
'',
|
|
71
|
-
'如果原文里写了下面这些「行为机制」,必须原样保留其骨架,不能只留态度:',
|
|
72
|
-
'- 模式与切换:接话 / 锐评 / 对线 / 收着说 分别在什么信号下触发、大概多长;',
|
|
73
|
-
'- 锐评与暴论的结构:先给结论、再给机制、最后补一刀;或者「接住 → 换层 → 给证据 → 补刀/留台阶」;',
|
|
74
|
-
'- 骂人分档与使用范围(哪些档位只能在对喷模式用)、对喷的退出条件;',
|
|
75
|
-
'- 什么时候必须收嘴、脱角色。',
|
|
76
|
-
'',
|
|
77
|
-
'必须丢掉:',
|
|
78
|
-
'- 背景故事、来源说明、示例对话、作者/版本/标签等元信息;',
|
|
79
|
-
'- 重复解释、教学式说明、“这段用来……”之类的描述。',
|
|
80
|
-
'',
|
|
81
|
-
'输出要求:',
|
|
82
|
-
`- 一张纯文本风格卡,不超过 ${maxChars} 字;`,
|
|
83
|
-
'- 锐评/暴论的部分宁可占篇幅,也不要压成一句“爱抬杠”;',
|
|
84
|
-
'- 接近字数上限时用一句完整的话收尾,不要截断在半句;',
|
|
85
|
-
'- 用第二人称写(“你说话……”),像给演员的角色提示;',
|
|
86
|
-
'- 不要标题、编号、列表、Markdown、代码块;',
|
|
87
|
-
'- 不要新增原说明里没有的设定。',
|
|
88
|
-
].join('\n');
|
|
89
|
-
|
|
90
|
-
const user = [
|
|
91
|
-
`【原始风格说明:${name}】`,
|
|
92
|
-
String((skill && skill.body) || '').trim(),
|
|
93
|
-
'',
|
|
94
|
-
'现在输出压缩后的风格卡。',
|
|
95
|
-
].join('\n');
|
|
96
|
-
|
|
97
|
-
return { system, user };
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
class StyleCardStore {
|
|
101
|
-
constructor(options = {}) {
|
|
102
|
-
this.ctx = options.ctx;
|
|
103
|
-
this.root = options.root;
|
|
104
|
-
this.logger = options.logger;
|
|
105
|
-
this.config = options.config || {};
|
|
106
|
-
this.enabled = this.config.compact !== false;
|
|
107
|
-
const configured = Number(this.config.compactMaxChars);
|
|
108
|
-
this.maxChars = Math.max(200, Math.min(3000, Number.isFinite(configured) && configured > 0 ? configured : 900));
|
|
109
|
-
this.model = String(this.config.compactModel || '').trim();
|
|
110
|
-
|
|
111
|
-
this.texts = new Map();
|
|
112
|
-
this.pending = new Map();
|
|
113
|
-
this.retryAt = new Map();
|
|
114
|
-
this.file = path.join(this.root, CACHE_FILE);
|
|
115
|
-
this.cache = this.readCache();
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
readCache() {
|
|
119
|
-
try {
|
|
120
|
-
const data = JSON.parse(fs.readFileSync(this.file, 'utf8'));
|
|
121
|
-
return data && typeof data === 'object' ? data : {};
|
|
122
|
-
} catch {
|
|
123
|
-
return {};
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
writeCache() {
|
|
128
|
-
try {
|
|
129
|
-
fs.mkdirSync(path.dirname(this.file), { recursive: true });
|
|
130
|
-
const tmp = `${this.file}.tmp`;
|
|
131
|
-
fs.writeFileSync(tmp, JSON.stringify(this.cache, null, 2), 'utf8');
|
|
132
|
-
fs.renameSync(tmp, this.file);
|
|
133
|
-
} catch (error) {
|
|
134
|
-
this.logger?.warn?.(`[style-card] 写入缓存失败: ${String(error)}`);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/** 当前可用的风格卡文本;没有就返回 null(调用方继续用完整 skill) */
|
|
139
|
-
get(id) {
|
|
140
|
-
if (!this.enabled) return null;
|
|
141
|
-
return this.texts.get(id) || null;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/** 确保该 skill 的风格卡已就绪(命中缓存 / 后台生成 / 冷却中跳过) */
|
|
145
|
-
ensure(skill, id) {
|
|
146
|
-
if (!this.enabled || !skill || !id) return;
|
|
147
|
-
if (this.texts.has(id) || this.pending.has(id)) return;
|
|
148
|
-
if ((this.retryAt.get(id) || 0) > Date.now()) return;
|
|
149
|
-
|
|
150
|
-
const hash = hashSkill(skill);
|
|
151
|
-
const cached = this.cache[id];
|
|
152
|
-
if (cached && cached.hash === hash && typeof cached.text === 'string' && cached.text.trim()) {
|
|
153
|
-
this.texts.set(id, cached.text);
|
|
154
|
-
this.logger?.info?.(
|
|
155
|
-
`[style-card] ${id} 命中缓存:${cached.text.length} 字(原 ${String(skill.body || '').length} 字)`,
|
|
156
|
-
);
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
this.pending.set(id, this.generate(skill, id, hash).finally(() => this.pending.delete(id)));
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
resolveRuntime() {
|
|
164
|
-
try {
|
|
165
|
-
const value = this.ctx.root.get('qqgmAiRuntime');
|
|
166
|
-
if (value && typeof value.chat === 'function') return value;
|
|
167
|
-
} catch { /* ignore */ }
|
|
168
|
-
return null;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
async generate(skill, id, hash) {
|
|
172
|
-
const runtime = this.resolveRuntime();
|
|
173
|
-
if (!runtime) {
|
|
174
|
-
// 主插件还没挂上门面:等下一次 AI 回复时再试
|
|
175
|
-
this.retryAt.set(id, Date.now() + 60 * 1000);
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
const prompt = buildCompactPrompt(skill, this.maxChars);
|
|
180
|
-
try {
|
|
181
|
-
const result = await runtime.chat({
|
|
182
|
-
system: prompt.system,
|
|
183
|
-
user: prompt.user,
|
|
184
|
-
temperature: 0.2,
|
|
185
|
-
maxTokens: Math.max(800, Math.min(3000, this.maxChars * 4)),
|
|
186
|
-
model: this.model || undefined,
|
|
187
|
-
});
|
|
188
|
-
if (!result || !result.ok) {
|
|
189
|
-
this.retryAt.set(id, Date.now() + RETRY_MS);
|
|
190
|
-
this.logger?.warn?.(`[style-card] ${id} 生成失败: ${result && result.error}`);
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
const text = cleanCard(result.text, this.maxChars);
|
|
194
|
-
if (!text) {
|
|
195
|
-
this.retryAt.set(id, Date.now() + RETRY_MS);
|
|
196
|
-
this.logger?.warn?.(`[style-card] ${id} 生成结果为空,继续用完整 skill`);
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
this.cache[id] = {
|
|
201
|
-
hash,
|
|
202
|
-
text,
|
|
203
|
-
chars: text.length,
|
|
204
|
-
sourceChars: String(skill.body || '').length,
|
|
205
|
-
model: this.model || '',
|
|
206
|
-
at: new Date().toISOString(),
|
|
207
|
-
};
|
|
208
|
-
this.writeCache();
|
|
209
|
-
this.texts.set(id, text);
|
|
210
|
-
this.logger?.info?.(
|
|
211
|
-
`[style-card] ${id} 已生成:${text.length} 字(原 ${String(skill.body || '').length} 字,`
|
|
212
|
-
+ `每次省约 ${Math.max(0, String(skill.body || '').length - text.length)} 字)`,
|
|
213
|
-
);
|
|
214
|
-
} catch (error) {
|
|
215
|
-
this.retryAt.set(id, Date.now() + RETRY_MS);
|
|
216
|
-
this.logger?.warn?.(`[style-card] ${id} 生成异常: ${String(error)}`);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
module.exports = {
|
|
222
|
-
StyleCardStore,
|
|
223
|
-
buildCompactPrompt,
|
|
224
|
-
cleanCard,
|
|
225
|
-
hashSkill,
|
|
226
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* lib/compact.js —— 风格卡压缩。
|
|
3
|
+
*
|
|
4
|
+
* 背景:一份完整的风格 skill 动辄 4000+ 字,而它每次模型调用都要整段塞进
|
|
5
|
+
* system prompt。真实用量里这是最大的一块固定开销。
|
|
6
|
+
*
|
|
7
|
+
* 做法:
|
|
8
|
+
* - 第一次运行时让模型把 skill 正文蒸馏成一张 700 字左右的「风格卡」
|
|
9
|
+
* (保留人设、语气、句长、口癖、称呼、禁忌;丢掉示例、解释、元信息);
|
|
10
|
+
* - 风格卡按 skill 正文的哈希缓存到 `<skillDir>/.style-cards.json`,
|
|
11
|
+
* 正文没变就一直复用,不会每次启动都调模型;
|
|
12
|
+
* - 生成失败时不影响聊天:继续用完整 skill,隔一段时间再重试。
|
|
13
|
+
*
|
|
14
|
+
* 依赖主插件暴露的 qqgmAiRuntime.chat();拿不到就静默跳过。
|
|
15
|
+
*/
|
|
16
|
+
const crypto = require('node:crypto');
|
|
17
|
+
const fs = require('node:fs');
|
|
18
|
+
const path = require('node:path');
|
|
19
|
+
|
|
20
|
+
const CACHE_FILE = '.style-cards.json';
|
|
21
|
+
const RETRY_MS = 10 * 60 * 1000;
|
|
22
|
+
/** 压缩提示词版本:改了提示词就 +1,缓存哈希跟着变,旧卡自动重生成 */
|
|
23
|
+
const PROMPT_VERSION = 2;
|
|
24
|
+
|
|
25
|
+
function hashSkill(skill) {
|
|
26
|
+
const body = String((skill && skill.body) || '');
|
|
27
|
+
return crypto.createHash('sha256').update(`${PROMPT_VERSION}|${body}`).digest('hex').slice(0, 16);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function cleanCard(raw, maxChars) {
|
|
31
|
+
let text = String(raw || '').trim();
|
|
32
|
+
if (!text) return '';
|
|
33
|
+
text = text
|
|
34
|
+
.replace(/^\s*```[a-zA-Z0-9_-]*\s*\n?/, '')
|
|
35
|
+
.replace(/\n?\s*```\s*$/, '')
|
|
36
|
+
.replace(/^["'“”‘’「」『』]+|["'“”‘’「」『』]+$/g, '')
|
|
37
|
+
.trim();
|
|
38
|
+
if (!text) return '';
|
|
39
|
+
|
|
40
|
+
const limit = Math.max(50, Number(maxChars) || 900);
|
|
41
|
+
if (text.length > limit) {
|
|
42
|
+
const slice = text.slice(0, limit);
|
|
43
|
+
const cut = Math.max(
|
|
44
|
+
slice.lastIndexOf('。'),
|
|
45
|
+
slice.lastIndexOf(';'),
|
|
46
|
+
slice.lastIndexOf('\n'),
|
|
47
|
+
);
|
|
48
|
+
text = cut >= Math.floor(limit * 0.5) ? slice.slice(0, cut + 1) : `${slice}…`;
|
|
49
|
+
}
|
|
50
|
+
// 模型在字数上限处截断时,可能停在一个逗号/顿号上:退回到最近的完整句
|
|
51
|
+
if (/[,、:;,;]$/.test(text)) {
|
|
52
|
+
const cut = Math.max(text.lastIndexOf('。'), text.lastIndexOf('\n'));
|
|
53
|
+
if (cut > text.length * 0.6) text = text.slice(0, cut + 1).trim();
|
|
54
|
+
}
|
|
55
|
+
return text.trim();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 让模型把 skill 压成风格卡 */
|
|
59
|
+
function buildCompactPrompt(skill, maxChars) {
|
|
60
|
+
const name = (skill && (skill.name || skill.id)) || '当前风格';
|
|
61
|
+
const system = [
|
|
62
|
+
'你在把一份「聊天风格说明」压缩成一张更短的风格卡,供另一个模型直接照着说话。',
|
|
63
|
+
'',
|
|
64
|
+
'必须保留:',
|
|
65
|
+
'- 人设身份:这个角色是谁、怎么称呼别人、自称什么;',
|
|
66
|
+
'- 语气与态度:说话冲不冲、爱不爱抬杠、是否骂人;',
|
|
67
|
+
'- 句长与节奏:一句话大概多长、一次说几句;',
|
|
68
|
+
'- 高频口癖与语气词(挑最有辨识度的,不要抄整段);',
|
|
69
|
+
'- 明确的禁忌与边界。',
|
|
70
|
+
'',
|
|
71
|
+
'如果原文里写了下面这些「行为机制」,必须原样保留其骨架,不能只留态度:',
|
|
72
|
+
'- 模式与切换:接话 / 锐评 / 对线 / 收着说 分别在什么信号下触发、大概多长;',
|
|
73
|
+
'- 锐评与暴论的结构:先给结论、再给机制、最后补一刀;或者「接住 → 换层 → 给证据 → 补刀/留台阶」;',
|
|
74
|
+
'- 骂人分档与使用范围(哪些档位只能在对喷模式用)、对喷的退出条件;',
|
|
75
|
+
'- 什么时候必须收嘴、脱角色。',
|
|
76
|
+
'',
|
|
77
|
+
'必须丢掉:',
|
|
78
|
+
'- 背景故事、来源说明、示例对话、作者/版本/标签等元信息;',
|
|
79
|
+
'- 重复解释、教学式说明、“这段用来……”之类的描述。',
|
|
80
|
+
'',
|
|
81
|
+
'输出要求:',
|
|
82
|
+
`- 一张纯文本风格卡,不超过 ${maxChars} 字;`,
|
|
83
|
+
'- 锐评/暴论的部分宁可占篇幅,也不要压成一句“爱抬杠”;',
|
|
84
|
+
'- 接近字数上限时用一句完整的话收尾,不要截断在半句;',
|
|
85
|
+
'- 用第二人称写(“你说话……”),像给演员的角色提示;',
|
|
86
|
+
'- 不要标题、编号、列表、Markdown、代码块;',
|
|
87
|
+
'- 不要新增原说明里没有的设定。',
|
|
88
|
+
].join('\n');
|
|
89
|
+
|
|
90
|
+
const user = [
|
|
91
|
+
`【原始风格说明:${name}】`,
|
|
92
|
+
String((skill && skill.body) || '').trim(),
|
|
93
|
+
'',
|
|
94
|
+
'现在输出压缩后的风格卡。',
|
|
95
|
+
].join('\n');
|
|
96
|
+
|
|
97
|
+
return { system, user };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
class StyleCardStore {
|
|
101
|
+
constructor(options = {}) {
|
|
102
|
+
this.ctx = options.ctx;
|
|
103
|
+
this.root = options.root;
|
|
104
|
+
this.logger = options.logger;
|
|
105
|
+
this.config = options.config || {};
|
|
106
|
+
this.enabled = this.config.compact !== false;
|
|
107
|
+
const configured = Number(this.config.compactMaxChars);
|
|
108
|
+
this.maxChars = Math.max(200, Math.min(3000, Number.isFinite(configured) && configured > 0 ? configured : 900));
|
|
109
|
+
this.model = String(this.config.compactModel || '').trim();
|
|
110
|
+
|
|
111
|
+
this.texts = new Map();
|
|
112
|
+
this.pending = new Map();
|
|
113
|
+
this.retryAt = new Map();
|
|
114
|
+
this.file = path.join(this.root, CACHE_FILE);
|
|
115
|
+
this.cache = this.readCache();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
readCache() {
|
|
119
|
+
try {
|
|
120
|
+
const data = JSON.parse(fs.readFileSync(this.file, 'utf8'));
|
|
121
|
+
return data && typeof data === 'object' ? data : {};
|
|
122
|
+
} catch {
|
|
123
|
+
return {};
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
writeCache() {
|
|
128
|
+
try {
|
|
129
|
+
fs.mkdirSync(path.dirname(this.file), { recursive: true });
|
|
130
|
+
const tmp = `${this.file}.tmp`;
|
|
131
|
+
fs.writeFileSync(tmp, JSON.stringify(this.cache, null, 2), 'utf8');
|
|
132
|
+
fs.renameSync(tmp, this.file);
|
|
133
|
+
} catch (error) {
|
|
134
|
+
this.logger?.warn?.(`[style-card] 写入缓存失败: ${String(error)}`);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** 当前可用的风格卡文本;没有就返回 null(调用方继续用完整 skill) */
|
|
139
|
+
get(id) {
|
|
140
|
+
if (!this.enabled) return null;
|
|
141
|
+
return this.texts.get(id) || null;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** 确保该 skill 的风格卡已就绪(命中缓存 / 后台生成 / 冷却中跳过) */
|
|
145
|
+
ensure(skill, id) {
|
|
146
|
+
if (!this.enabled || !skill || !id) return;
|
|
147
|
+
if (this.texts.has(id) || this.pending.has(id)) return;
|
|
148
|
+
if ((this.retryAt.get(id) || 0) > Date.now()) return;
|
|
149
|
+
|
|
150
|
+
const hash = hashSkill(skill);
|
|
151
|
+
const cached = this.cache[id];
|
|
152
|
+
if (cached && cached.hash === hash && typeof cached.text === 'string' && cached.text.trim()) {
|
|
153
|
+
this.texts.set(id, cached.text);
|
|
154
|
+
this.logger?.info?.(
|
|
155
|
+
`[style-card] ${id} 命中缓存:${cached.text.length} 字(原 ${String(skill.body || '').length} 字)`,
|
|
156
|
+
);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
this.pending.set(id, this.generate(skill, id, hash).finally(() => this.pending.delete(id)));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
resolveRuntime() {
|
|
164
|
+
try {
|
|
165
|
+
const value = this.ctx.root.get('qqgmAiRuntime');
|
|
166
|
+
if (value && typeof value.chat === 'function') return value;
|
|
167
|
+
} catch { /* ignore */ }
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async generate(skill, id, hash) {
|
|
172
|
+
const runtime = this.resolveRuntime();
|
|
173
|
+
if (!runtime) {
|
|
174
|
+
// 主插件还没挂上门面:等下一次 AI 回复时再试
|
|
175
|
+
this.retryAt.set(id, Date.now() + 60 * 1000);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const prompt = buildCompactPrompt(skill, this.maxChars);
|
|
180
|
+
try {
|
|
181
|
+
const result = await runtime.chat({
|
|
182
|
+
system: prompt.system,
|
|
183
|
+
user: prompt.user,
|
|
184
|
+
temperature: 0.2,
|
|
185
|
+
maxTokens: Math.max(800, Math.min(3000, this.maxChars * 4)),
|
|
186
|
+
model: this.model || undefined,
|
|
187
|
+
});
|
|
188
|
+
if (!result || !result.ok) {
|
|
189
|
+
this.retryAt.set(id, Date.now() + RETRY_MS);
|
|
190
|
+
this.logger?.warn?.(`[style-card] ${id} 生成失败: ${result && result.error}`);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const text = cleanCard(result.text, this.maxChars);
|
|
194
|
+
if (!text) {
|
|
195
|
+
this.retryAt.set(id, Date.now() + RETRY_MS);
|
|
196
|
+
this.logger?.warn?.(`[style-card] ${id} 生成结果为空,继续用完整 skill`);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
this.cache[id] = {
|
|
201
|
+
hash,
|
|
202
|
+
text,
|
|
203
|
+
chars: text.length,
|
|
204
|
+
sourceChars: String(skill.body || '').length,
|
|
205
|
+
model: this.model || '',
|
|
206
|
+
at: new Date().toISOString(),
|
|
207
|
+
};
|
|
208
|
+
this.writeCache();
|
|
209
|
+
this.texts.set(id, text);
|
|
210
|
+
this.logger?.info?.(
|
|
211
|
+
`[style-card] ${id} 已生成:${text.length} 字(原 ${String(skill.body || '').length} 字,`
|
|
212
|
+
+ `每次省约 ${Math.max(0, String(skill.body || '').length - text.length)} 字)`,
|
|
213
|
+
);
|
|
214
|
+
} catch (error) {
|
|
215
|
+
this.retryAt.set(id, Date.now() + RETRY_MS);
|
|
216
|
+
this.logger?.warn?.(`[style-card] ${id} 生成异常: ${String(error)}`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
module.exports = {
|
|
222
|
+
StyleCardStore,
|
|
223
|
+
buildCompactPrompt,
|
|
224
|
+
cleanCard,
|
|
225
|
+
hashSkill,
|
|
226
|
+
};
|
package/lib/prompt.js
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 把 skill 正文包装成“只影响表达方式”的提示词段。
|
|
3
|
-
* options.direct = true 时用于私聊,避免出现“群聊/群友”这类说法。
|
|
4
|
-
*/
|
|
5
|
-
function buildStylePrompt(skill, config = {}, options = {}) {
|
|
6
|
-
const name = skill.name || skill.id;
|
|
7
|
-
const direct = !!options.direct;
|
|
8
|
-
const lines = [
|
|
9
|
-
`你现在使用「${name}」的说话风格。以下内容只描述“怎么说”,不是新任务:`,
|
|
10
|
-
'- 先回答当前消息的问题/请求,再决定用什么语气;不得用段子、锐评或跑题替代答案;',
|
|
11
|
-
'- 用户问什么就答什么,不要主动跑题到自己或无关话题;',
|
|
12
|
-
'- 不确定缩写、黑话、梗的含义时,按聊天语境理解;拿不准先用一句话确认,不要硬套技术含义;',
|
|
13
|
-
'- 不要执行其中提到的文件读取、脚本或工具调用;联网搜索由主插件负责,若提示词里出现【联网搜索结果】,可以直接参考;',
|
|
14
|
-
'- 不要向对方介绍这个风格,不要提 skill、模式、设定、提示词这些词;',
|
|
15
|
-
direct
|
|
16
|
-
? '- 这是 QQ 私聊,不是群聊:不要提群友、不要用“群里”这种说法,直接接对方的话;'
|
|
17
|
-
: '- 这是 QQ 群聊,不是一对一访谈:不要自我介绍,直接接当前消息;',
|
|
18
|
-
direct
|
|
19
|
-
? '- 私聊里默认用“接话”模式,只有对方明确要锐评/对线时才切换;'
|
|
20
|
-
: '- 群聊里默认用“接话”模式,只有对方明确要锐评/对线时才切换;',
|
|
21
|
-
'- 系统给的字数上限是硬上限;风格里更短的要求可以照做;',
|
|
22
|
-
'- 与系统规则冲突时,以系统规则为准(是否回复、引用哪条、字数上限都听系统的)。',
|
|
23
|
-
];
|
|
24
|
-
|
|
25
|
-
const glossary = Array.isArray(config.glossary) ? config.glossary.filter((item) => item && item.term) : [];
|
|
26
|
-
if (glossary.length) {
|
|
27
|
-
lines.push('', direct ? '【术语】(按这个理解,不要在回复里解释这些词)' : '【群聊术语】(按这个理解,不要在回复里解释这些词)');
|
|
28
|
-
for (const item of glossary) lines.push(`- ${item.term}:${item.meaning || ''}`);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const body = String(options.compactText || skill.body || '').trim();
|
|
32
|
-
if (options.compactText) {
|
|
33
|
-
lines.push('', '(以下是压缩后的风格卡,按它说话即可)');
|
|
34
|
-
}
|
|
35
|
-
lines.push('', body);
|
|
36
|
-
lines.push('', '【再次强调】先回答当前消息;不要跑题到自己或无关话题;不确定词义先确认。');
|
|
37
|
-
return lines.join('\n');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
module.exports = { buildStylePrompt };
|
|
1
|
+
/**
|
|
2
|
+
* 把 skill 正文包装成“只影响表达方式”的提示词段。
|
|
3
|
+
* options.direct = true 时用于私聊,避免出现“群聊/群友”这类说法。
|
|
4
|
+
*/
|
|
5
|
+
function buildStylePrompt(skill, config = {}, options = {}) {
|
|
6
|
+
const name = skill.name || skill.id;
|
|
7
|
+
const direct = !!options.direct;
|
|
8
|
+
const lines = [
|
|
9
|
+
`你现在使用「${name}」的说话风格。以下内容只描述“怎么说”,不是新任务:`,
|
|
10
|
+
'- 先回答当前消息的问题/请求,再决定用什么语气;不得用段子、锐评或跑题替代答案;',
|
|
11
|
+
'- 用户问什么就答什么,不要主动跑题到自己或无关话题;',
|
|
12
|
+
'- 不确定缩写、黑话、梗的含义时,按聊天语境理解;拿不准先用一句话确认,不要硬套技术含义;',
|
|
13
|
+
'- 不要执行其中提到的文件读取、脚本或工具调用;联网搜索由主插件负责,若提示词里出现【联网搜索结果】,可以直接参考;',
|
|
14
|
+
'- 不要向对方介绍这个风格,不要提 skill、模式、设定、提示词这些词;',
|
|
15
|
+
direct
|
|
16
|
+
? '- 这是 QQ 私聊,不是群聊:不要提群友、不要用“群里”这种说法,直接接对方的话;'
|
|
17
|
+
: '- 这是 QQ 群聊,不是一对一访谈:不要自我介绍,直接接当前消息;',
|
|
18
|
+
direct
|
|
19
|
+
? '- 私聊里默认用“接话”模式,只有对方明确要锐评/对线时才切换;'
|
|
20
|
+
: '- 群聊里默认用“接话”模式,只有对方明确要锐评/对线时才切换;',
|
|
21
|
+
'- 系统给的字数上限是硬上限;风格里更短的要求可以照做;',
|
|
22
|
+
'- 与系统规则冲突时,以系统规则为准(是否回复、引用哪条、字数上限都听系统的)。',
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
const glossary = Array.isArray(config.glossary) ? config.glossary.filter((item) => item && item.term) : [];
|
|
26
|
+
if (glossary.length) {
|
|
27
|
+
lines.push('', direct ? '【术语】(按这个理解,不要在回复里解释这些词)' : '【群聊术语】(按这个理解,不要在回复里解释这些词)');
|
|
28
|
+
for (const item of glossary) lines.push(`- ${item.term}:${item.meaning || ''}`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const body = String(options.compactText || skill.body || '').trim();
|
|
32
|
+
if (options.compactText) {
|
|
33
|
+
lines.push('', '(以下是压缩后的风格卡,按它说话即可)');
|
|
34
|
+
}
|
|
35
|
+
lines.push('', body);
|
|
36
|
+
lines.push('', '【再次强调】先回答当前消息;不要跑题到自己或无关话题;不确定词义先确认。');
|
|
37
|
+
return lines.join('\n');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
module.exports = { buildStylePrompt };
|
package/lib/registry.js
CHANGED
|
@@ -1,172 +1,172 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* lib/registry.js —— 多风格加载与“主 + 追加”组合。
|
|
3
|
-
*
|
|
4
|
-
* 设计要点:
|
|
5
|
-
* - 主风格(primary):整段进提示词,决定基本人格与说话方式;
|
|
6
|
-
* - 追加风格(extras):只作“加味层”,必须显式声明不得压过主风格;
|
|
7
|
-
* - 每个追加风格有独立触发概率,用内部随机数决定这一轮是否叠上;
|
|
8
|
-
* - 随机判定按 (会话, 轮次) 记忆,保证同一轮里提示词与输出过滤器看到同一个结果。
|
|
9
|
-
*/
|
|
10
|
-
const fs = require('node:fs');
|
|
11
|
-
const path = require('node:path');
|
|
12
|
-
const { loadSkillFromDir, listSkillDirs } = require('./loader');
|
|
13
|
-
|
|
14
|
-
/** 追加风格一旦被抽中,本轮的固定种子;同一轮内多次取板块结果一致。 */
|
|
15
|
-
class RollCache {
|
|
16
|
-
constructor() {
|
|
17
|
-
this.map = new Map();
|
|
18
|
-
}
|
|
19
|
-
key(ctx, id) {
|
|
20
|
-
const group = ctx && ctx.groupId != null ? String(ctx.groupId) : 'direct';
|
|
21
|
-
return `${group}::${id}`;
|
|
22
|
-
}
|
|
23
|
-
roll(ctx, id, probability) {
|
|
24
|
-
const key = this.key(ctx, id);
|
|
25
|
-
const now = Date.now();
|
|
26
|
-
const hit = this.map.get(key);
|
|
27
|
-
// 8 秒内视为同一轮:足够覆盖一次模型调用 + 一次发送
|
|
28
|
-
if (hit && now - hit.at < 8000) return hit.value;
|
|
29
|
-
const value = Math.random() < probability;
|
|
30
|
-
this.map.set(key, { at: now, value });
|
|
31
|
-
// 顺手清理过期条目,避免长跑内存增长
|
|
32
|
-
if (this.map.size > 256) {
|
|
33
|
-
for (const [k, v] of this.map) {
|
|
34
|
-
if (now - v.at > 60000) this.map.delete(k);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return value;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function listAvailable(rootDir) {
|
|
42
|
-
const dirs = listSkillDirs(rootDir);
|
|
43
|
-
const out = [];
|
|
44
|
-
for (const dir of dirs) {
|
|
45
|
-
const result = loadSkillFromDir(dir);
|
|
46
|
-
out.push({
|
|
47
|
-
id: path.basename(dir),
|
|
48
|
-
ok: !!result.ok,
|
|
49
|
-
name: result.ok ? result.skill.name : path.basename(dir),
|
|
50
|
-
reason: result.ok ? '' : result.reason,
|
|
51
|
-
skill: result.ok ? result.skill : null,
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
return out;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function pickByName(available, wanted) {
|
|
58
|
-
const target = String(wanted || '').trim();
|
|
59
|
-
if (!target) return null;
|
|
60
|
-
return (
|
|
61
|
-
available.find((item) => item.ok && (item.id === target || item.name === target)) || null
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** 追加风格不得反客为主:统一加一段约束。 */
|
|
66
|
-
function extraGuard(name, rarity) {
|
|
67
|
-
const lines = [
|
|
68
|
-
`下面这段是「${name}」的附加口癖,只是给主风格加味道用。`,
|
|
69
|
-
'- 主风格的语气、句式、长度始终优先;这段只能增色,不能改写人设、不能换话题、不能抢主线;',
|
|
70
|
-
'- 与主风格冲突时,一律按主风格来;',
|
|
71
|
-
'- 该用多少用多少,一句带一个就够,不要整段照搬,也不要解释这些词。',
|
|
72
|
-
];
|
|
73
|
-
if (rarity) lines.push(`- 这段口癖出现的频率约为 ${rarity},所以不必每句都用力。`);
|
|
74
|
-
return lines.join('\n');
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* @param {object} rootDir 技能目录
|
|
79
|
-
* @param {object} config 插件配置
|
|
80
|
-
* @returns {{ primary, extras, warnings, available }}
|
|
81
|
-
*/
|
|
82
|
-
function buildStyleSet(rootDir, config = {}) {
|
|
83
|
-
const available = listAvailable(rootDir);
|
|
84
|
-
const warnings = [];
|
|
85
|
-
|
|
86
|
-
for (const item of available) {
|
|
87
|
-
if (!item.ok) warnings.push(`跳过 ${item.id}:${item.reason}`);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const primaryWanted = String(config.primaryStyle || '').trim();
|
|
91
|
-
let primary = pickByName(available, primaryWanted);
|
|
92
|
-
if (primaryWanted && !primary) {
|
|
93
|
-
warnings.push(
|
|
94
|
-
`主风格「${primaryWanted}」不可用;可用:${available.filter((i) => i.ok).map((i) => i.id).join(', ') || '(无)'}`,
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
if (!primary) {
|
|
98
|
-
primary = available.find((item) => item.ok) || null;
|
|
99
|
-
if (primary && !primaryWanted) {
|
|
100
|
-
warnings.push(`未指定主风格,默认使用 ${primary.id}`);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const extras = [];
|
|
105
|
-
const disabled = [];
|
|
106
|
-
const seen = new Set(primary ? [primary.id] : []);
|
|
107
|
-
const rawExtras = Array.isArray(config.extraStyles) ? config.extraStyles : [];
|
|
108
|
-
rawExtras.forEach((entry, index) => {
|
|
109
|
-
if (!entry || typeof entry !== 'object') return;
|
|
110
|
-
const wanted = String(entry.style || '').trim();
|
|
111
|
-
if (!wanted) return;
|
|
112
|
-
|
|
113
|
-
// enabled 未填(undefined / null)一律视为启用,只有显式 false 才算关闭。
|
|
114
|
-
// 以前这里直接 return 且不记日志,用户完全看不出为什么没生效。
|
|
115
|
-
const isOff = entry.enabled === false;
|
|
116
|
-
if (isOff) {
|
|
117
|
-
disabled.push({ index, style: wanted });
|
|
118
|
-
warnings.push(
|
|
119
|
-
`追加风格 #${index + 1}「${wanted}」已关闭(enabled=false),本次不会叠加;想启用请把该项勾上`,
|
|
120
|
-
);
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const found = pickByName(available, wanted);
|
|
125
|
-
if (!found) {
|
|
126
|
-
warnings.push(`追加风格 #${index + 1}「${wanted}」不可用,已跳过`);
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
if (seen.has(found.id)) {
|
|
130
|
-
warnings.push(`追加风格「${found.id}」与已有风格重复,已跳过`);
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
seen.add(found.id);
|
|
134
|
-
let probability = Number(entry.probability);
|
|
135
|
-
if (!Number.isFinite(probability)) probability = 0.3;
|
|
136
|
-
probability = Math.min(1, Math.max(0, probability));
|
|
137
|
-
extras.push({
|
|
138
|
-
index,
|
|
139
|
-
id: found.id,
|
|
140
|
-
name: found.skill.name,
|
|
141
|
-
skill: found.skill,
|
|
142
|
-
probability,
|
|
143
|
-
minChars: Math.max(10, Number(entry.maxChars) || Number(config.maxChars) || 60),
|
|
144
|
-
maxLines: Math.max(1, Number(entry.maxLines) || Number(config.maxLines) || 3),
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
return { primary, extras, disabled, warnings, available };
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/** 主风格段:与旧版行为一致,整段进入提示词。 */
|
|
152
|
-
function primarySection(primary, config, options = {}) {
|
|
153
|
-
const { buildStylePrompt } = require('./prompt');
|
|
154
|
-
return buildStylePrompt(primary.skill, config, options);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/** 追加风格段:加约束后拼接。compactText 为压缩后的风格卡(可选)。 */
|
|
158
|
-
function extraSection(extra, probability, compactText) {
|
|
159
|
-
const percent = `${Math.round(probability * 100)}%`;
|
|
160
|
-
const body = String(compactText || extra.skill.body || '').trim();
|
|
161
|
-
return [extraGuard(extra.name, percent), '', body].join('\n');
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
module.exports = {
|
|
165
|
-
RollCache,
|
|
166
|
-
listAvailable,
|
|
167
|
-
pickByName,
|
|
168
|
-
buildStyleSet,
|
|
169
|
-
primarySection,
|
|
170
|
-
extraSection,
|
|
171
|
-
extraGuard,
|
|
172
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* lib/registry.js —— 多风格加载与“主 + 追加”组合。
|
|
3
|
+
*
|
|
4
|
+
* 设计要点:
|
|
5
|
+
* - 主风格(primary):整段进提示词,决定基本人格与说话方式;
|
|
6
|
+
* - 追加风格(extras):只作“加味层”,必须显式声明不得压过主风格;
|
|
7
|
+
* - 每个追加风格有独立触发概率,用内部随机数决定这一轮是否叠上;
|
|
8
|
+
* - 随机判定按 (会话, 轮次) 记忆,保证同一轮里提示词与输出过滤器看到同一个结果。
|
|
9
|
+
*/
|
|
10
|
+
const fs = require('node:fs');
|
|
11
|
+
const path = require('node:path');
|
|
12
|
+
const { loadSkillFromDir, listSkillDirs } = require('./loader');
|
|
13
|
+
|
|
14
|
+
/** 追加风格一旦被抽中,本轮的固定种子;同一轮内多次取板块结果一致。 */
|
|
15
|
+
class RollCache {
|
|
16
|
+
constructor() {
|
|
17
|
+
this.map = new Map();
|
|
18
|
+
}
|
|
19
|
+
key(ctx, id) {
|
|
20
|
+
const group = ctx && ctx.groupId != null ? String(ctx.groupId) : 'direct';
|
|
21
|
+
return `${group}::${id}`;
|
|
22
|
+
}
|
|
23
|
+
roll(ctx, id, probability) {
|
|
24
|
+
const key = this.key(ctx, id);
|
|
25
|
+
const now = Date.now();
|
|
26
|
+
const hit = this.map.get(key);
|
|
27
|
+
// 8 秒内视为同一轮:足够覆盖一次模型调用 + 一次发送
|
|
28
|
+
if (hit && now - hit.at < 8000) return hit.value;
|
|
29
|
+
const value = Math.random() < probability;
|
|
30
|
+
this.map.set(key, { at: now, value });
|
|
31
|
+
// 顺手清理过期条目,避免长跑内存增长
|
|
32
|
+
if (this.map.size > 256) {
|
|
33
|
+
for (const [k, v] of this.map) {
|
|
34
|
+
if (now - v.at > 60000) this.map.delete(k);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function listAvailable(rootDir) {
|
|
42
|
+
const dirs = listSkillDirs(rootDir);
|
|
43
|
+
const out = [];
|
|
44
|
+
for (const dir of dirs) {
|
|
45
|
+
const result = loadSkillFromDir(dir);
|
|
46
|
+
out.push({
|
|
47
|
+
id: path.basename(dir),
|
|
48
|
+
ok: !!result.ok,
|
|
49
|
+
name: result.ok ? result.skill.name : path.basename(dir),
|
|
50
|
+
reason: result.ok ? '' : result.reason,
|
|
51
|
+
skill: result.ok ? result.skill : null,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function pickByName(available, wanted) {
|
|
58
|
+
const target = String(wanted || '').trim();
|
|
59
|
+
if (!target) return null;
|
|
60
|
+
return (
|
|
61
|
+
available.find((item) => item.ok && (item.id === target || item.name === target)) || null
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** 追加风格不得反客为主:统一加一段约束。 */
|
|
66
|
+
function extraGuard(name, rarity) {
|
|
67
|
+
const lines = [
|
|
68
|
+
`下面这段是「${name}」的附加口癖,只是给主风格加味道用。`,
|
|
69
|
+
'- 主风格的语气、句式、长度始终优先;这段只能增色,不能改写人设、不能换话题、不能抢主线;',
|
|
70
|
+
'- 与主风格冲突时,一律按主风格来;',
|
|
71
|
+
'- 该用多少用多少,一句带一个就够,不要整段照搬,也不要解释这些词。',
|
|
72
|
+
];
|
|
73
|
+
if (rarity) lines.push(`- 这段口癖出现的频率约为 ${rarity},所以不必每句都用力。`);
|
|
74
|
+
return lines.join('\n');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @param {object} rootDir 技能目录
|
|
79
|
+
* @param {object} config 插件配置
|
|
80
|
+
* @returns {{ primary, extras, warnings, available }}
|
|
81
|
+
*/
|
|
82
|
+
function buildStyleSet(rootDir, config = {}) {
|
|
83
|
+
const available = listAvailable(rootDir);
|
|
84
|
+
const warnings = [];
|
|
85
|
+
|
|
86
|
+
for (const item of available) {
|
|
87
|
+
if (!item.ok) warnings.push(`跳过 ${item.id}:${item.reason}`);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const primaryWanted = String(config.primaryStyle || '').trim();
|
|
91
|
+
let primary = pickByName(available, primaryWanted);
|
|
92
|
+
if (primaryWanted && !primary) {
|
|
93
|
+
warnings.push(
|
|
94
|
+
`主风格「${primaryWanted}」不可用;可用:${available.filter((i) => i.ok).map((i) => i.id).join(', ') || '(无)'}`,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
if (!primary) {
|
|
98
|
+
primary = available.find((item) => item.ok) || null;
|
|
99
|
+
if (primary && !primaryWanted) {
|
|
100
|
+
warnings.push(`未指定主风格,默认使用 ${primary.id}`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const extras = [];
|
|
105
|
+
const disabled = [];
|
|
106
|
+
const seen = new Set(primary ? [primary.id] : []);
|
|
107
|
+
const rawExtras = Array.isArray(config.extraStyles) ? config.extraStyles : [];
|
|
108
|
+
rawExtras.forEach((entry, index) => {
|
|
109
|
+
if (!entry || typeof entry !== 'object') return;
|
|
110
|
+
const wanted = String(entry.style || '').trim();
|
|
111
|
+
if (!wanted) return;
|
|
112
|
+
|
|
113
|
+
// enabled 未填(undefined / null)一律视为启用,只有显式 false 才算关闭。
|
|
114
|
+
// 以前这里直接 return 且不记日志,用户完全看不出为什么没生效。
|
|
115
|
+
const isOff = entry.enabled === false;
|
|
116
|
+
if (isOff) {
|
|
117
|
+
disabled.push({ index, style: wanted });
|
|
118
|
+
warnings.push(
|
|
119
|
+
`追加风格 #${index + 1}「${wanted}」已关闭(enabled=false),本次不会叠加;想启用请把该项勾上`,
|
|
120
|
+
);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const found = pickByName(available, wanted);
|
|
125
|
+
if (!found) {
|
|
126
|
+
warnings.push(`追加风格 #${index + 1}「${wanted}」不可用,已跳过`);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (seen.has(found.id)) {
|
|
130
|
+
warnings.push(`追加风格「${found.id}」与已有风格重复,已跳过`);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
seen.add(found.id);
|
|
134
|
+
let probability = Number(entry.probability);
|
|
135
|
+
if (!Number.isFinite(probability)) probability = 0.3;
|
|
136
|
+
probability = Math.min(1, Math.max(0, probability));
|
|
137
|
+
extras.push({
|
|
138
|
+
index,
|
|
139
|
+
id: found.id,
|
|
140
|
+
name: found.skill.name,
|
|
141
|
+
skill: found.skill,
|
|
142
|
+
probability,
|
|
143
|
+
minChars: Math.max(10, Number(entry.maxChars) || Number(config.maxChars) || 60),
|
|
144
|
+
maxLines: Math.max(1, Number(entry.maxLines) || Number(config.maxLines) || 3),
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
return { primary, extras, disabled, warnings, available };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** 主风格段:与旧版行为一致,整段进入提示词。 */
|
|
152
|
+
function primarySection(primary, config, options = {}) {
|
|
153
|
+
const { buildStylePrompt } = require('./prompt');
|
|
154
|
+
return buildStylePrompt(primary.skill, config, options);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** 追加风格段:加约束后拼接。compactText 为压缩后的风格卡(可选)。 */
|
|
158
|
+
function extraSection(extra, probability, compactText) {
|
|
159
|
+
const percent = `${Math.round(probability * 100)}%`;
|
|
160
|
+
const body = String(compactText || extra.skill.body || '').trim();
|
|
161
|
+
return [extraGuard(extra.name, percent), '', body].join('\n');
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
module.exports = {
|
|
165
|
+
RollCache,
|
|
166
|
+
listAvailable,
|
|
167
|
+
pickByName,
|
|
168
|
+
buildStyleSet,
|
|
169
|
+
primarySection,
|
|
170
|
+
extraSection,
|
|
171
|
+
extraGuard,
|
|
172
|
+
};
|
package/package.json
CHANGED