dsh-bagu 0.3.1 → 0.5.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/README.md +153 -71
- package/client.js +96 -80
- package/cordis.patch.yml +1 -0
- package/dsh-plugin.json +3 -3
- package/lib/config.js +2 -0
- package/lib/guard.js +368 -0
- package/lib/index.js +43 -37
- package/lib/layers.js +89 -0
- package/lib/lexicon.js +118 -1
- package/package.json +6 -3
- package/test/client.test.mjs +3 -3
- package/test/edge.test.mjs +98 -0
- package/test/git.test.mjs +126 -0
- package/test/guard.test.mjs +93 -0
package/lib/guard.js
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 守门器:agent 把要发出去的那句话递进来,只回「能发」或「改」。
|
|
3
|
+
*
|
|
4
|
+
* 跟 analyze() 的区别:
|
|
5
|
+
* analyze 给人看,多行报告,判断有余地
|
|
6
|
+
* guard 给 agent 用,一行,只有通过和不通过
|
|
7
|
+
*
|
|
8
|
+
* 这个文件的存在理由就一个:QQ 机器人、养鱼这类场景里,
|
|
9
|
+
* 一条回复二三十字,人不会自己开脚本查,得让机器在发出前拦一道。
|
|
10
|
+
*
|
|
11
|
+
* 输出预算:通过两个字,打回一行(30 字以内)。每次调用都要烧 token,
|
|
12
|
+
* 所以这里每一行都要挣来自己的位置。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { analyze } from './analyze.js';
|
|
16
|
+
import { STOCK_PHRASES, GIT_STOCK, GIT_TEMPLATE_ROWS, countTemplateRows, REVIEW_STOCK } from './lexicon.js';
|
|
17
|
+
import { HOLLOW_ACTIONS, HOLLOW_ADJECTIVES, LEARNED_TONE, CODE_COMMENT_NARRATION, MOTIVATIONAL_SLOP, UNIVERSAL_FILLER } from './layers.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 打回原因按严重度排。谁先命中谁负责,只报一条 —— 报三条 agent 会挑最轻的改。
|
|
21
|
+
*
|
|
22
|
+
* 权重来自 lieflat 的 δ:
|
|
23
|
+
* 同义轮换 +0.91 > 不是A而是B +0.88 > 顿号并列 +0.56
|
|
24
|
+
* 聊天场景再叠一层:一条二十字的回复里出现模板三连,比长文里更假。
|
|
25
|
+
*/
|
|
26
|
+
const OPPOSITION_FRAMES = [
|
|
27
|
+
// 不是A而是B 已经在 STRUCTURE_FRAMES 里,这里补同族的
|
|
28
|
+
/不在于[^。!?\n]{1,20}(?:而在于|而在)/, // 问题不在于A,而在于B
|
|
29
|
+
/与其说[^。!?\n]{1,20}(?:不如说|不如)/, // 与其说A,不如说B
|
|
30
|
+
/与其([^。!?\n]{1,20})不如/, // 与其A不如B
|
|
31
|
+
/看似[^。!?\n]{1,20}(?:实则|实际|其实)/, // 看似A,实则B
|
|
32
|
+
/不仅[^。!?\n]{1,24}(?:更|还|而且)/, // 不仅A更B
|
|
33
|
+
/不是[^。!?\n]{1,20}[。!]\s*[^。!?\n]{0,4}(?:这|那|而|就)?(?:才)?是/, // 拆成两句:不是X。这是Y
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
const REASONS = [
|
|
37
|
+
{
|
|
38
|
+
id: 'universal-filler',
|
|
39
|
+
// 万能话:放到任何语境都通顺,等于什么都没说
|
|
40
|
+
test: (r, text, o = {}) => {
|
|
41
|
+
const bar = o.repeatBar || 1;
|
|
42
|
+
const n = UNIVERSAL_FILLER.reduce((a, re) => {
|
|
43
|
+
const m = text.match(new RegExp(re.source, 'g'));
|
|
44
|
+
return a + (m ? m.length : 0);
|
|
45
|
+
}, 0);
|
|
46
|
+
return n >= bar;
|
|
47
|
+
},
|
|
48
|
+
why: (r, text) => {
|
|
49
|
+
const re = UNIVERSAL_FILLER.map((x) => new RegExp(x.source)).find((x) => x.test(text));
|
|
50
|
+
return re ? `万能话「${(text.match(re) || [''])[0].slice(0, 12)}」` : '万能话';
|
|
51
|
+
},
|
|
52
|
+
hint: '删掉这句,直接说事',
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
id: 'hollow-action',
|
|
56
|
+
// 动词 + 宏观名词:说了动作,没说改哪一行。不分场景。
|
|
57
|
+
test: (r, text, o = {}) => {
|
|
58
|
+
const bar = o.repeatBar || 1;
|
|
59
|
+
const n = HOLLOW_ACTIONS.reduce((a, re) => {
|
|
60
|
+
const m = text.match(new RegExp(re.source, 'g'));
|
|
61
|
+
return a + (m ? m.length : 0);
|
|
62
|
+
}, 0);
|
|
63
|
+
return n >= bar;
|
|
64
|
+
},
|
|
65
|
+
why: (r, text) => {
|
|
66
|
+
const re = HOLLOW_ACTIONS.map((x) => new RegExp(x.source)).find((x) => x.test(text));
|
|
67
|
+
const hit = re ? (text.match(re) || [''])[0] : '';
|
|
68
|
+
return hit ? `空转「${hit.slice(0, 14)}」` : '空转';
|
|
69
|
+
},
|
|
70
|
+
hint: '说清改了什么,比如具体到哪一行、哪一次',
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
{
|
|
74
|
+
id: 'motivational',
|
|
75
|
+
// 鸡汤/口号:不求证的句子
|
|
76
|
+
test: (r, text) => MOTIVATIONAL_SLOP.some((re) => new RegExp(re.source).test(text)),
|
|
77
|
+
why: (r, text) => {
|
|
78
|
+
const re = MOTIVATIONAL_SLOP.map((x) => new RegExp(x.source)).find((x) => x.test(text));
|
|
79
|
+
return re ? `鸡汤「${(text.match(re) || [''])[0].slice(0, 12)}」` : '鸡汤';
|
|
80
|
+
},
|
|
81
|
+
hint: '说一件具体的事',
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
id: 'changelog-hollow',
|
|
85
|
+
// 变更日志里的空转条目:一行说了个动作,但没说改了什么
|
|
86
|
+
// 「性能大幅提升」「全新的用户界面」「修复了若干已知问题」
|
|
87
|
+
test: (r, text) => {
|
|
88
|
+
if (!/^#{1,4}\s*(?:新增|修复|变更|改进|移除|Added|Fixed|Changed|Removed)/im.test(text)) return false;
|
|
89
|
+
// 逐条看 - 开头的行
|
|
90
|
+
const lines = text.split('\n').filter((l) => /^-\s/.test(l));
|
|
91
|
+
const hollow = lines.filter((l) =>
|
|
92
|
+
/(?:全新|大幅|显著|全面|若干|多个|一些|相关|优化了|提升了|完善了)/.test(l)
|
|
93
|
+
);
|
|
94
|
+
return hollow.length >= 1 && hollow.length >= lines.length * 0.5;
|
|
95
|
+
},
|
|
96
|
+
why: '变更日志空转',
|
|
97
|
+
hint: '每条说清改了什么,比如「登录失败时不再清空手机号」',
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
id: 'comment-narration',
|
|
101
|
+
// 代码注释把代码念一遍:「// 初始化计数器」「// 遍历每个商品」
|
|
102
|
+
test: (r, text) => {
|
|
103
|
+
if (!text.includes('//') && !text.includes('#')) return false;
|
|
104
|
+
return CODE_COMMENT_NARRATION.some((re) => new RegExp(re.source, re.flags).test(text));
|
|
105
|
+
},
|
|
106
|
+
why: '注释念代码',
|
|
107
|
+
hint: '写为什么这么做,不写代码在做什么',
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
id: 'hollow-adjective',
|
|
111
|
+
// 空形容词:能贴到任何产品上的词堆在一起
|
|
112
|
+
test: (r, text, o = {}) => {
|
|
113
|
+
const bar = o.repeatBar || 1;
|
|
114
|
+
const n = HOLLOW_ADJECTIVES.reduce((a, re) => {
|
|
115
|
+
const m = text.match(new RegExp(re.source, 'g'));
|
|
116
|
+
return a + (m ? m.length : 0);
|
|
117
|
+
}, 0);
|
|
118
|
+
return n >= bar;
|
|
119
|
+
},
|
|
120
|
+
why: (r, text) => {
|
|
121
|
+
const re = HOLLOW_ADJECTIVES.map((x) => new RegExp(x.source)).find((x) => x.test(text));
|
|
122
|
+
return re ? `空形容词「${(text.match(re) || [''])[0].slice(0, 14)}」` : '空形容词';
|
|
123
|
+
},
|
|
124
|
+
hint: '说它具体做到了什么',
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
id: 'learned-tone',
|
|
128
|
+
// 学习腔:学到了 / 涨知识 / 希望多发。评论区特有,换到哪都是假学生
|
|
129
|
+
test: (r, text) => LEARNED_TONE.some((re) => new RegExp(re.source).test(text)),
|
|
130
|
+
why: (r, text) => {
|
|
131
|
+
const re = LEARNED_TONE.map((x) => new RegExp(x.source)).find((x) => x.test(text));
|
|
132
|
+
return re ? `学习腔「${(text.match(re) || [''])[0].slice(0, 12)}」` : '学习腔';
|
|
133
|
+
},
|
|
134
|
+
hint: '说你自己看到的具体东西,别说学到了',
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
id: 'triple-template',
|
|
138
|
+
// 逗号和分号都算。QQ 机器人一条回复通常不分号,写成「首先A,其次B,最后C」
|
|
139
|
+
test: (r, text) => /首先[^。!?\n]{1,30}[,,;;][^。!?\n]{0,12}其次/.test(text),
|
|
140
|
+
why: '模板三连',
|
|
141
|
+
hint: '直接说事,不用「首先其次最后」',
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
id: 'bare-first',
|
|
145
|
+
// 单列的「首先/其次/最后」开头:没有后续的列举,直接是空转
|
|
146
|
+
test: (r, text) => {
|
|
147
|
+
const m = text.match(/^(首先|其次|最后|首先|其一|其二)[,,]/);
|
|
148
|
+
return m !== null && text.length < 40;
|
|
149
|
+
},
|
|
150
|
+
why: '单列首先/其次',
|
|
151
|
+
hint: '直接说正题',
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
id: 'rhetorical',
|
|
155
|
+
// 反问句里的对举:难道这不是A吗 / 这难道不是B吗
|
|
156
|
+
test: (r, text) => /(?:难道|岂不|岂不是)[^。!?\n]{0,20}(?:不是|而是|才)/.test(text),
|
|
157
|
+
why: '反问式对举',
|
|
158
|
+
hint: '直接说结论,别用反问',
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
id: 'git-stock',
|
|
162
|
+
// commit / PR 里的「优化了结构、提升可维护性」—— 说了动作没说改哪一行
|
|
163
|
+
test: (r, text, o = {}) => {
|
|
164
|
+
const bar = o.repeatBar || 1;
|
|
165
|
+
const n = GIT_STOCK.reduce((a, re) => {
|
|
166
|
+
const m = text.match(new RegExp(re.source, 'g'));
|
|
167
|
+
return a + (m ? m.length : 0);
|
|
168
|
+
}, 0);
|
|
169
|
+
return n >= bar;
|
|
170
|
+
},
|
|
171
|
+
why: (r, text) => {
|
|
172
|
+
const re = GIT_STOCK.find((x) => x.test(text));
|
|
173
|
+
return re ? `套话「${(text.match(re) || [''])[0]}」` : 'git 套话';
|
|
174
|
+
},
|
|
175
|
+
hint: '说改了什么,比如「登录失败时不再清空已填的手机号」',
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
id: 'git-template',
|
|
179
|
+
// PR 模板骨架:## 概述 / ## 改动 / ## 影响,两行以上就假
|
|
180
|
+
test: (r, text) => countTemplateRows(text) >= 2,
|
|
181
|
+
why: 'PR 模板骨架',
|
|
182
|
+
hint: '一两句话说清为什么改,不要分节',
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
id: 'review-stock',
|
|
186
|
+
// review 里的「思路清晰、建议后续优化」—— 提了意见没指哪一行
|
|
187
|
+
test: (r, text) => REVIEW_STOCK.some((re) => re.test(text)),
|
|
188
|
+
why: (r, text) => {
|
|
189
|
+
const re = REVIEW_STOCK.find((x) => x.test(text));
|
|
190
|
+
return re ? `套话「${(text.match(re) || [''])[0]}」` : 'review 套话';
|
|
191
|
+
},
|
|
192
|
+
hint: '指出具体哪一行、什么问题',
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
id: 'stock',
|
|
196
|
+
test: (r, text) => {
|
|
197
|
+
const hit = STOCK_PHRASES.find((re) => re.test(text));
|
|
198
|
+
return hit !== undefined;
|
|
199
|
+
},
|
|
200
|
+
why: (r, text) => {
|
|
201
|
+
const hit = STOCK_PHRASES.find((re) => re.test(text));
|
|
202
|
+
return hit ? `套话「${(text.match(hit) || [''])[0]}」` : '套话';
|
|
203
|
+
},
|
|
204
|
+
hint: '这句是模板,换成自己会说的话',
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
id: 'negation',
|
|
208
|
+
test: (r, text) => {
|
|
209
|
+
const hit = r.hits.structures.some((s) => s.id === 'negation') ||
|
|
210
|
+
OPPOSITION_FRAMES.some((re) => re.test(text));
|
|
211
|
+
if (!hit) return false;
|
|
212
|
+
// 前半句已经种下一个可能被误解的说法 → 这个对立有活干,放过。
|
|
213
|
+
// 判据抄 issue #1:前面出现过「以为/很多人/不是词表」这类,或者
|
|
214
|
+
// 「不是X」里的 X 在前面真正被提过。
|
|
215
|
+
const m = text.match(/不是([^,。!?\n]{1,20})[,]?(?:而是|,是)/);
|
|
216
|
+
if (m) {
|
|
217
|
+
const x = m[1].trim();
|
|
218
|
+
const before = text.slice(0, text.indexOf(m[0]));
|
|
219
|
+
if (before.includes(x)) return false; // X 在前面被种下过
|
|
220
|
+
}
|
|
221
|
+
// 前面有「以为/很多人/都」这类误解标记
|
|
222
|
+
if (/(?:以为|很多人|不少人|都说|总有人|别人说|以为)/.test(text.slice(0, text.indexOf(m ? m[0] : '')))) {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
return true;
|
|
226
|
+
},
|
|
227
|
+
why: (r, text) => {
|
|
228
|
+
if (r.hits.structures.some((s) => s.id === 'negation')) return '不是A而是B';
|
|
229
|
+
const re = OPPOSITION_FRAMES.find((x) => x.test(text));
|
|
230
|
+
return re ? `对举「${(text.match(re) || [''])[0]}」` : '对举';
|
|
231
|
+
},
|
|
232
|
+
hint: '拆成两句话,或者直接说结论',
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
id: 'synonym',
|
|
236
|
+
// 两组以上,或者一组里挑了三个以上 —— 后者已经是明显在轮换
|
|
237
|
+
test: (r) => r.synonyms.length >= 2 || r.synonyms.some((g) => g.length >= 3),
|
|
238
|
+
why: (r) => `同义轮换${r.synonyms.map((g) => g.length).reduce((a, b) => a + b, 0)}个词`,
|
|
239
|
+
hint: (r) => `「${r.synonyms[0].join('/')}」选一个,重复用没关系`,
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
id: 'ai-words',
|
|
243
|
+
test: (r) => r.hits.aiWords.length >= 1,
|
|
244
|
+
why: (r) => r.hits.aiWords.map((h) => h.word).slice(0, 2).join('、'),
|
|
245
|
+
hint: '换成平时会说的词',
|
|
246
|
+
},
|
|
247
|
+
{
|
|
248
|
+
id: 'triple-list',
|
|
249
|
+
// 列举真实存在的东西不算凑数。判据:并列项里能不能指认出具体的名词或数字。
|
|
250
|
+
// 「开关、三个阈值、当场试一下」→ 能指认,放过
|
|
251
|
+
// 「成本低、维护简单、扩展性好」→ 全是形容词,拦
|
|
252
|
+
test: (r, text) => {
|
|
253
|
+
const row = r.hits.structures.find((s) => s.id === 'triple');
|
|
254
|
+
if (!row) return false;
|
|
255
|
+
const span = row.spans[0]?.text || '';
|
|
256
|
+
const items = span.split('、').filter(Boolean);
|
|
257
|
+
// 每一项里有没有数字、英文、或者带量词的实词
|
|
258
|
+
// 判据:这一项末尾是不是形容词/评价。
|
|
259
|
+
// 「成本低」「维护简单」「扩展性好」—— 都以形容词收尾,是凑数。
|
|
260
|
+
// 「三个阈值」「当场试一下」「开关」—— 是能指认的东西。
|
|
261
|
+
const ADJ_TAIL = /(?:低|高|好|快|慢|强|弱|简单|复杂|清晰|合理|稳定|流畅|方便|优秀|完善|丰富|独特|重要|关键|核心)$/;
|
|
262
|
+
const concrete = items.filter((it) => !ADJ_TAIL.test(it.trim()));
|
|
263
|
+
return concrete.length < 2;
|
|
264
|
+
},
|
|
265
|
+
why: '三段并列',
|
|
266
|
+
hint: '挑一个说透,别凑三个',
|
|
267
|
+
},
|
|
268
|
+
{
|
|
269
|
+
id: 'summary',
|
|
270
|
+
test: (r) => r.hits.summary.length > 0,
|
|
271
|
+
why: (r) => r.hits.summary[0].word,
|
|
272
|
+
hint: '删掉这句,前面已经说完了',
|
|
273
|
+
},
|
|
274
|
+
{
|
|
275
|
+
id: 'fake-significance',
|
|
276
|
+
test: (r) => r.hits.fakeSignificance.length > 0,
|
|
277
|
+
why: (r) => r.hits.fakeSignificance[0].word,
|
|
278
|
+
hint: '讲它是什么,不讲它多重要',
|
|
279
|
+
},
|
|
280
|
+
{
|
|
281
|
+
id: 'emdash',
|
|
282
|
+
test: (r) => r.hits.structures.some((s) => s.id === 'emdash'),
|
|
283
|
+
why: '破折号',
|
|
284
|
+
hint: '聊天里用逗号或句号',
|
|
285
|
+
},
|
|
286
|
+
{
|
|
287
|
+
id: 'choppy-parallel',
|
|
288
|
+
// 三个以上同构短句连着:很强。很方便。很实用。
|
|
289
|
+
test: (r, text) => {
|
|
290
|
+
const sents = text.split(/[。!?]/).map((s) => s.trim()).filter(Boolean);
|
|
291
|
+
if (sents.length < 3) return false;
|
|
292
|
+
// 前三个句子长度相近(±3 字)且都以同一个字开头
|
|
293
|
+
const first3 = sents.slice(0, 3);
|
|
294
|
+
const sameHead = first3.every((s) => s[0] === first3[0][0]);
|
|
295
|
+
const similarLen = Math.max(...first3.map((s) => s.length)) - Math.min(...first3.map((s) => s.length)) <= 3;
|
|
296
|
+
return sameHead && similarLen && r.scores.concretePer100 === 0;
|
|
297
|
+
},
|
|
298
|
+
why: '同构短句排比',
|
|
299
|
+
hint: '合成一句,或者补一个具体的东西',
|
|
300
|
+
},
|
|
301
|
+
{
|
|
302
|
+
id: 'hollow-praise',
|
|
303
|
+
// 夸人不是问题,问题是整条回复只有夸、没有一个具体东西。
|
|
304
|
+
// 「转场那里剪得挺流畅的」有具体位置,能过;
|
|
305
|
+
// 「真的很不错,非常值得一看」只有空气,拦住。
|
|
306
|
+
test: (r, text) => {
|
|
307
|
+
if (text.length < 12) return false;
|
|
308
|
+
if (r.scores.concretePer100 > 0) return false;
|
|
309
|
+
// 评价词 + 程度词 + 感叹号,三样凑齐就是空夸
|
|
310
|
+
// 只要两个以上评价词、且零具体物,就是空夸。
|
|
311
|
+
// 不管带不带「很/非常」这些程度词。
|
|
312
|
+
return r.scores.evaluation >= 2;
|
|
313
|
+
},
|
|
314
|
+
why: '只有夸,没有具体东西',
|
|
315
|
+
hint: '说一句你具体看到的东西',
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
id: 'no-concrete',
|
|
319
|
+
// 长一点的回复里一个具体物都没有,说明在说空话
|
|
320
|
+
// 聊天回复 20 字就是正常长度,门槛不该按长文的标准设
|
|
321
|
+
test: (r, text) => text.length >= 12 && r.scores.concretePer100 === 0 && r.scores.evaluation >= 2,
|
|
322
|
+
why: '全在评价,没有具体东西',
|
|
323
|
+
hint: '说一件具体发生的事',
|
|
324
|
+
},
|
|
325
|
+
];
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* 过一遍。
|
|
329
|
+
*
|
|
330
|
+
* @param {string} text 准备发出去的那句话
|
|
331
|
+
* @param {{strict?: boolean}} opts strict = 连轻微的信号也拦
|
|
332
|
+
* @returns {{ok: boolean, why?: string, hint?: string, line: string}}
|
|
333
|
+
*/
|
|
334
|
+
export function guard(text, opts = {}) {
|
|
335
|
+
const src = String(text ?? '').trim();
|
|
336
|
+
if (!src) return { ok: true, line: 'OK' };
|
|
337
|
+
|
|
338
|
+
// PR 描述、长 commit 也是对外文本,不该当文档放过。
|
|
339
|
+
// 先判是不是 git 内容,再决定要不要跳过长文。
|
|
340
|
+
// 判是不是 git 文本,用逐行的独立正则,不依赖 GIT_TEMPLATE_ROWS 的 m 标志
|
|
341
|
+
// 长文本不跳过 —— 长邮件、长周报、长 PR 一样会像八股。
|
|
342
|
+
// 但长文的判据要放宽:同一信号出现两三次才算,一次不算。
|
|
343
|
+
const isLong = src.length > 300;
|
|
344
|
+
const repeatBar = isLong ? 2 : 1;
|
|
345
|
+
|
|
346
|
+
const r = analyze(src);
|
|
347
|
+
const strict = opts.strict === true;
|
|
348
|
+
|
|
349
|
+
for (const reason of REASONS) {
|
|
350
|
+
let hit = false;
|
|
351
|
+
try { hit = reason.test(r, src, { repeatBar }); } catch { hit = false; }
|
|
352
|
+
if (!hit) continue;
|
|
353
|
+
|
|
354
|
+
// 非 strict 时,最后两条(破折号、无具体物)只警告不打回
|
|
355
|
+
if (!strict && (reason.id === 'emdash' || reason.id === 'no-concrete')) continue;
|
|
356
|
+
|
|
357
|
+
const why = typeof reason.why === 'function' ? reason.why(r) : reason.why;
|
|
358
|
+
const hint = typeof reason.hint === 'function' ? reason.hint(r) : reason.hint;
|
|
359
|
+
return { ok: false, why, hint, line: `改:${why} · ${hint}` };
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return { ok: true, line: 'OK' };
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/** 只回一行,给最省的模式。 */
|
|
366
|
+
export function guardLine(text, opts) {
|
|
367
|
+
return guard(text, opts).line;
|
|
368
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
|
|
14
14
|
import { analyze } from './analyze.js';
|
|
15
15
|
import { render, renderBrief } from './render.js';
|
|
16
|
+
import { guard } from './guard.js';
|
|
16
17
|
import { readConfig, writeConfig, CONFIG_FILE, DEFAULTS } from './config.js';
|
|
17
18
|
|
|
18
19
|
export const name = 'dsh-bagu';
|
|
@@ -22,18 +23,12 @@ export { analyze, render, renderBrief };
|
|
|
22
23
|
let live = readConfig();
|
|
23
24
|
const syncers = new Set();
|
|
24
25
|
|
|
25
|
-
/**
|
|
26
|
-
const PROMPT =
|
|
27
|
-
|
|
28
|
-
- 别急着认错。对方问一句,不等于你在说错。
|
|
29
|
-
- 留一处不确定。全篇一个「大概」都没有,是在下断言。
|
|
30
|
-
- 具体压过评价。「骨架在推所以有点飘」只有看过的人才说得出。
|
|
31
|
-
- 别每句一样长。整齐本身就是痕迹。`;
|
|
26
|
+
/** 每次会话都注入,多一个字都是白烧。 */
|
|
27
|
+
const PROMPT = `要发出去的话,先过 bagu_guard。被打回就改,别硬发。`;
|
|
32
28
|
|
|
33
29
|
/** 60 字。 */
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const TOOL_ASK = `只要问句,不要结论。`;
|
|
30
|
+
/** 一个工具,三种模式。描述只占一份。 */
|
|
31
|
+
const TOOL_GUARD = `发出去之前过一遍。默认只回 OK 或一行改法。mode=report 给详细报告,mode=ask 给问句。`;
|
|
37
32
|
|
|
38
33
|
function toolOutput() {
|
|
39
34
|
return {
|
|
@@ -112,44 +107,47 @@ export function apply(root, config = {}) {
|
|
|
112
107
|
});
|
|
113
108
|
|
|
114
109
|
root.inject(['tools'], (ctx) => {
|
|
110
|
+
// 一个工具,三种模式。mode 缺省是守门(日常路径),另外两个是调试用。
|
|
115
111
|
ctx.tools.register({
|
|
116
|
-
name: '
|
|
117
|
-
description:
|
|
112
|
+
name: 'bagu_guard',
|
|
113
|
+
description: TOOL_GUARD,
|
|
118
114
|
parameters: {
|
|
119
115
|
type: 'object',
|
|
120
116
|
properties: {
|
|
121
|
-
text: { type: 'string', description: '
|
|
122
|
-
|
|
117
|
+
text: { type: 'string', description: '要发出去的那段话。' },
|
|
118
|
+
mode: {
|
|
119
|
+
type: 'string',
|
|
120
|
+
enum: ['guard', 'report', 'ask'],
|
|
121
|
+
description: 'guard 只回 OK/一行改法(默认);report 给详细报告;ask 只给问句。',
|
|
122
|
+
},
|
|
123
|
+
strict: { type: 'boolean', description: '连轻微信号也拦。' },
|
|
124
|
+
full: { type: 'boolean', description: 'mode=report 时给完整输出。' },
|
|
125
|
+
n: { type: 'number', description: 'mode=ask 时几条问句,默认 3,最多 6。' },
|
|
123
126
|
},
|
|
124
127
|
required: ['text'],
|
|
125
128
|
},
|
|
126
129
|
output: toolOutput(),
|
|
127
130
|
execute: async (args) => {
|
|
128
131
|
const src = String(args?.text ?? '');
|
|
129
|
-
|
|
130
|
-
const r = analyze(src, thresholdsOf(live));
|
|
131
|
-
return { ok: true, text: render(r, { full: args?.full === true }) };
|
|
132
|
-
},
|
|
133
|
-
});
|
|
132
|
+
const mode = args?.mode || 'guard';
|
|
134
133
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
return { ok: true, text: r.asks.slice(0, n).map((a, i) => `${i + 1}. ${a}`).join('\n') };
|
|
134
|
+
if (!src.trim()) {
|
|
135
|
+
return { ok: true, text: mode === 'guard' ? 'OK' : '没有文字。' };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (mode === 'report') {
|
|
139
|
+
const r = analyze(src, thresholdsOf(live));
|
|
140
|
+
return { ok: true, text: render(r, { full: args?.full === true }) };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (mode === 'ask') {
|
|
144
|
+
const r = analyze(src, thresholdsOf(live));
|
|
145
|
+
const n = Math.min(6, Math.max(1, Number(args?.n) || 3));
|
|
146
|
+
return { ok: true, text: r.asks.slice(0, n).map((a, i) => `${i + 1}. ${a}`).join('\n') };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const g = guard(src, { strict: args?.strict ?? live.strict });
|
|
150
|
+
return { ok: true, text: g.line };
|
|
153
151
|
},
|
|
154
152
|
});
|
|
155
153
|
});
|
|
@@ -176,6 +174,14 @@ export function apply(root, config = {}) {
|
|
|
176
174
|
return sendJson(res, 200, { ok: true, config: next });
|
|
177
175
|
}
|
|
178
176
|
|
|
177
|
+
if (req.method === 'POST' && path === '/guard') {
|
|
178
|
+
const body = await readBody(req);
|
|
179
|
+
const src = String(body?.text ?? '');
|
|
180
|
+
if (!src.trim()) return sendJson(res, 200, { ok: true, line: 'OK' });
|
|
181
|
+
const g = guard(src, { strict: body?.strict ?? live.strict });
|
|
182
|
+
return sendJson(res, 200, { ok: g.ok, line: g.line, why: g.why, hint: g.hint });
|
|
183
|
+
}
|
|
184
|
+
|
|
179
185
|
if (req.method === 'POST' && path === '/preview') {
|
|
180
186
|
const body = await readBody(req);
|
|
181
187
|
const src = String(body?.text ?? '');
|
package/lib/layers.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 通用层:不分场景,凡是人写的话都成立。
|
|
3
|
+
*
|
|
4
|
+
* 下面前两层跟场景无关 —— README、微博、邮件、commit、review,
|
|
5
|
+
* 都是在「一句话换个人说还成不成立」这一个判据上过的。
|
|
6
|
+
*
|
|
7
|
+
* 场景层只是补充:每个场景有自己长出来的套话,但那不是判断依据,
|
|
8
|
+
* 只是提高命中率的加速器。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** 第二层:空转。说了一个动作,但没说做了什么。哪都适用。 */
|
|
12
|
+
export const HOLLOW_ACTIONS = [
|
|
13
|
+
// 动词 + 宏观名词。改哪一行?没说。
|
|
14
|
+
/(?:优化|提升|改善|增强|完善|强化|改进)了?[^,。!?\n]{0,8}(?:结构|性能|体验|流程|逻辑|质量|效率|可维护性|可读性|稳定性|兼容性|扩展性|可用性|合理性|规范性)/g,
|
|
15
|
+
/(?:调整|修改|更新|变更)了?[^,。!?\n]{0,10}(?:相关|部分|若干|一些|整体|全部|必要|相应)/g,
|
|
16
|
+
/(?:修复|解决|处理)了?(?:若干|多个|一些|部分|一系列|相关|已知)[^。!?\n]{0,8}(?:问题|bug|缺陷|错误|情况)?/gi,
|
|
17
|
+
// 「进行了」开头的一律是空转
|
|
18
|
+
/进行了?[^,。!?\n]{0,12}(?:处理|操作|调整|优化|改进|完善)/g,
|
|
19
|
+
// 「使得/使其」句式
|
|
20
|
+
/(?:使得?|令)[^,。!?\n]{0,12}(?:更加?|更)[^,。!?\n]{0,8}(?:清晰|简洁|友好|高效|稳定|合理)/g,
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 第二层补充:空形容词。能贴到任何产品/项目/方案上,就是在撑场面。
|
|
25
|
+
* 「先进的」「高性能」「高可用」「灵活」「易用」「完善的」「全面的」
|
|
26
|
+
*/
|
|
27
|
+
export const HOLLOW_ADJECTIVES = [
|
|
28
|
+
// 「具备/拥有 + 良好的 X」是空转的标准形状,X 填什么都行
|
|
29
|
+
/(?:具备|拥有|具有)[^,。!?\n]{0,6}(?:良好|优秀|出色|卓越|强大|完善)的[^,。!?\n]{0,8}/g,
|
|
30
|
+
// 「能够满足…需要/需求」——满足谁的需要?没说是谁
|
|
31
|
+
/(?:能够?|可以|可)(?:满足|适应|支持)[^,。!?\n]{0,12}(?:需要|需求|要求|发展)/g,
|
|
32
|
+
/(?:先进|领先|专业|成熟|完善|全面|丰富|强大|灵活|易用|高效|稳定|可靠|安全|优质|卓越|创新)(?:的)?(?:技术|架构|方案|体系|能力|功能|体验|服务|设计|平台|产品|支持|保障)/g,
|
|
33
|
+
/(?:高|极)(?:性能|可用|并发|效率|质量|标准|精度|稳定)/g,
|
|
34
|
+
/(?:多种|各类|全方位|一站式|集成化|模块化)[^,。!?\n]{0,12}(?:支持|方案|服务|能力|方式)/g,
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 第三层补充:学习/认可腔。评论区特有,但换到哪都是假学生。
|
|
39
|
+
* 「学到了」「涨知识」「受益匪浅」「说得太对了」
|
|
40
|
+
*/
|
|
41
|
+
export const LEARNED_TONE = [
|
|
42
|
+
/(?:学|涨)到了?[^,。!?\n]{0,4}(?:很多|不少|东西|知识)?/g,
|
|
43
|
+
/(?:涨|长)(?:知识|见识|姿势)了?/g,
|
|
44
|
+
/(?:受益|收获)(?:匪浅|颇多|良多|很大)/g,
|
|
45
|
+
/(?:说得|讲得)(?:太|很|非常)?(?:对|好|准)了?/g,
|
|
46
|
+
/(?:非常|很|挺)(?:实用|有用|干货|有价值)/g,
|
|
47
|
+
/希望(?:以后|下次|继续|多)[^,。!?\n]{0,8}(?:多发|更新|分享)/g,
|
|
48
|
+
];
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 代码注释:把代码本身念一遍。
|
|
52
|
+
*
|
|
53
|
+
* 来源:beagle style-criteria「Obvious Comments」、voku code-slop「comments-hygiene」。
|
|
54
|
+
* 判据是「注释删掉之后,读代码能不能得到同样的信息」。能,就是废话。
|
|
55
|
+
*
|
|
56
|
+
* 注意:这条只认最明显的形态 —— 注释里出现「初始化/循环/遍历/返回/设置」
|
|
57
|
+
* 这类把动作念一遍的动词。真正聪明的判断要读代码,正则做不到。
|
|
58
|
+
*/
|
|
59
|
+
export const CODE_COMMENT_NARRATION = [
|
|
60
|
+
/^\s*\/\/\s*(?:初始化|循环|遍历|迭代|返回|设置|赋值|定义|声明|创建|获取|判断|检查)[^\n]{0,20}$/gm,
|
|
61
|
+
/^\s*#\s*(?:initialize|loop|iterate|return|set|create|get|check|define)[^\n]{0,24}$/gim,
|
|
62
|
+
/\/\/\s*end\s+(?:if|for|while|switch|function|class)\s*$/gim,
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 鸡汤/口号。微博、朋友圈、公众号开头重灾区。
|
|
67
|
+
*
|
|
68
|
+
* 判据跟别的不一样:这类句子的问题不在句式,在「它不求证」。
|
|
69
|
+
* 「努力不一定会成功」——对,但跟什么都没说一样。
|
|
70
|
+
* 「愿你所行皆坦途」——祝福,但没有一个具体的人。
|
|
71
|
+
*/
|
|
72
|
+
export const MOTIVATIONAL_SLOP = [
|
|
73
|
+
/愿[你您][^。!?\n]{0,20}(?:皆|都|一切)[^。!?\n]{0,8}(?:坦途|美好|顺遂|如意|如愿)/g,
|
|
74
|
+
/(?:新|崭新)的(?:一天|开始|一年|征程)[,,]?/g,
|
|
75
|
+
/(?:早安|晚安|中午好)[!!,,]?\s*(?:新|今天|愿)/g,
|
|
76
|
+
/(?:努力|坚持|奋斗|拼搏)[^。!?\n]{0,10}(?:不一定|就会|才能|才会)/g,
|
|
77
|
+
/(?:加|一起)?油[!!]/g,
|
|
78
|
+
/(?:所行|所遇|所见|所得)[^。!?\n]{0,6}(?:皆|都)/g,
|
|
79
|
+
/(?:每一个|每一天)[^。!?\n]{0,10}(?:值得|都)(?:被|是)/g,
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
/** 第三层:万能话。放到任何语境都通顺,等于什么都没说。 */
|
|
83
|
+
export const UNIVERSAL_FILLER = [
|
|
84
|
+
/(?:随着|在)[^,。!?\n]{0,10}(?:的)?(?:发展|进步|普及|到来|推动)/g,
|
|
85
|
+
/(?:在)?(?:当今|现在|如今)[^,。!?\n]{0,6}(?:时代|社会|环境|背景)/g,
|
|
86
|
+
/众所周知[,,]?/g,
|
|
87
|
+
/(?:值得|需要)(?:一提|注意|强调|指出)的?是/g,
|
|
88
|
+
/(?:不仅|不只)(?:仅)?[^,。!?\n]{0,16}(?:更|还|而且)/g,
|
|
89
|
+
];
|