dsh-bagu 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +117 -2
- package/client.js +253 -0
- package/cordis.patch.yml +18 -0
- package/dsh-plugin.json +36 -0
- package/lib/analyze.js +306 -0
- package/lib/config.js +64 -0
- package/lib/index.js +223 -0
- package/lib/lexicon.js +174 -0
- package/package.json +68 -4
- package/test/analyze.test.mjs +92 -0
- package/test/client.test.mjs +111 -0
- package/test/config.test.mjs +90 -0
package/lib/analyze.js
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 核心判断:这段话有多「可替换」。
|
|
3
|
+
*
|
|
4
|
+
* 「可替换」是这门判断的起点:
|
|
5
|
+
* 八股的问题不是礼貌,是可替换。谁都能说的话,慢慢就没人想看了。
|
|
6
|
+
*
|
|
7
|
+
* 但「可替换」本身没法直接判。这里用四个可测的代理指标,各算各的,不合成一个漂亮总分:
|
|
8
|
+
*
|
|
9
|
+
* 1. 具体性 —— 数字、时间、经历、感官、原话、标识符。只有真的在说事的人才给得出。
|
|
10
|
+
* 2. 抽象度 —— 意义、价值、本质、趋势…… 说得越多,越是在说事之外撑场面。
|
|
11
|
+
* 3. 评价度 —— 重要、关键、深刻、全面。说得越满,越像评职称。
|
|
12
|
+
* 4. 结构腔 —— 真人密度极低,超标 3 倍就报。
|
|
13
|
+
*
|
|
14
|
+
* 外加两条不占分但单独报的:
|
|
15
|
+
* 5. 留白词 —— 一个都没有 = 句句在下断言。
|
|
16
|
+
* 6. 扎堆 —— 同一百字里换了三种句式,是 AI 指纹;同一种来两次不算。
|
|
17
|
+
*
|
|
18
|
+
* 刻意不做的事:
|
|
19
|
+
* - 不给「人味分」这种单一数字。分数会让人只盯数字,不看句子。
|
|
20
|
+
* - 不自动改写。改要人改,工具只把材料摆出来。
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import {
|
|
24
|
+
AI_WORDS,
|
|
25
|
+
SUMMARY_OPENERS,
|
|
26
|
+
FAKE_SIGNIFICANCE,
|
|
27
|
+
ABSTRACT_NOUNS,
|
|
28
|
+
EVAL_ADJECTIVES,
|
|
29
|
+
HEDGE_WORDS,
|
|
30
|
+
CONCRETE_MARKERS,
|
|
31
|
+
STRUCTURE_FRAMES,
|
|
32
|
+
REGISTERS,
|
|
33
|
+
HEDGE_BASELINE,
|
|
34
|
+
} from './lexicon.js';
|
|
35
|
+
import { DEFAULTS } from './config.js';
|
|
36
|
+
|
|
37
|
+
/** 去代码块和行内代码,只留正文。 */
|
|
38
|
+
export function stripCode(text) {
|
|
39
|
+
return String(text ?? '')
|
|
40
|
+
.replace(/```[\s\S]*?```/g, '')
|
|
41
|
+
.replace(/`[^`\n]*`/g, '')
|
|
42
|
+
.replace(/^#{1,6}\s.*$/gm, '');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function countWords(text, list) {
|
|
46
|
+
const hits = [];
|
|
47
|
+
for (const w of list) {
|
|
48
|
+
let from = 0;
|
|
49
|
+
for (;;) {
|
|
50
|
+
const at = text.indexOf(w, from);
|
|
51
|
+
if (at < 0) break;
|
|
52
|
+
hits.push({ word: w, at });
|
|
53
|
+
from = at + w.length;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return hits;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function countPattern(text, re) {
|
|
60
|
+
const g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g');
|
|
61
|
+
const hits = [];
|
|
62
|
+
for (const m of text.matchAll(g)) hits.push({ word: m[0], at: m.index });
|
|
63
|
+
return hits;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** 猜文体。规则很土,但每条都能说清为什么。 */
|
|
67
|
+
export function registerOf(text) {
|
|
68
|
+
const s = String(text ?? '');
|
|
69
|
+
const n = s.length;
|
|
70
|
+
const c = (re) => (s.match(re) || []).length;
|
|
71
|
+
|
|
72
|
+
const casual = c(/哈哈|笑死|草|emmm|嗯嗯|诶|啊这|233|好耶|卧槽|靠/gu)
|
|
73
|
+
+ c(/[((][^))]{0,6}[))]/g);
|
|
74
|
+
const steps = c(/然后|接着|第一步|第二步|之后|再是|最后|先是/g);
|
|
75
|
+
const argue = c(/但是|可是|其实|问题是|然而|不过|反而|恰恰|我觉得|我认为|说句|不是这样/g);
|
|
76
|
+
const explain = c(/因为|所以|也就是说|相当于|意思是|本质上|原因|意味着|换句话说/g);
|
|
77
|
+
|
|
78
|
+
if (casual >= 2) return 'chat';
|
|
79
|
+
if (steps >= 3) return 'steps';
|
|
80
|
+
if (n < 40) return 'chat';
|
|
81
|
+
if (argue >= 2) return 'argue';
|
|
82
|
+
if (explain >= 2) return 'explain';
|
|
83
|
+
if (n < 150) return 'note';
|
|
84
|
+
return 'explain';
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 具体性:每百字命中几个「只有当事人给得出的东西」。
|
|
89
|
+
* @returns {{hits: Array<{kind:string,text:string,at:number}>, per100:number}}
|
|
90
|
+
*/
|
|
91
|
+
export function concreteness(text) {
|
|
92
|
+
const src = String(text ?? '');
|
|
93
|
+
const hits = [];
|
|
94
|
+
for (const [kind, re] of Object.entries(CONCRETE_MARKERS)) {
|
|
95
|
+
const g = new RegExp(re.source, 'g');
|
|
96
|
+
for (const m of src.matchAll(g)) {
|
|
97
|
+
hits.push({ kind, text: m[0], at: m.index });
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const per100 = src.length ? (hits.length / src.length) * 100 : 0;
|
|
101
|
+
return { hits, per100: Math.round(per100 * 100) / 100 };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** 结构腔扫描。 */
|
|
105
|
+
export function scanStructures(text) {
|
|
106
|
+
const src = String(text ?? '');
|
|
107
|
+
const rows = [];
|
|
108
|
+
for (const f of STRUCTURE_FRAMES) {
|
|
109
|
+
const g = new RegExp(f.re.source, 'g');
|
|
110
|
+
const spans = [...src.matchAll(g)].map((m) => ({ text: m[0], at: m.index }));
|
|
111
|
+
if (spans.length) {
|
|
112
|
+
rows.push({ id: f.id, name: f.name, per1k: f.per1k, count: spans.length, spans });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return rows;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** 同一百字窗口里,最多几种不同句式、最多个命中。 */
|
|
119
|
+
export function cluster(hits, width = 100) {
|
|
120
|
+
if (!hits.length) return { width, maxHits: 0, maxDistinct: 0, at: -1 };
|
|
121
|
+
let maxHits = 0;
|
|
122
|
+
let maxDistinct = 0;
|
|
123
|
+
let at = -1;
|
|
124
|
+
for (const h of hits) {
|
|
125
|
+
const win = hits.filter((x) => x.at >= h.at && x.at < h.at + width);
|
|
126
|
+
if (win.length > maxHits) { maxHits = win.length; at = h.at; }
|
|
127
|
+
const d = new Set(win.map((x) => x.id)).size;
|
|
128
|
+
if (d > maxDistinct) { maxDistinct = d; at = h.at; }
|
|
129
|
+
}
|
|
130
|
+
return { width, maxHits, maxDistinct, at };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 主入口。
|
|
135
|
+
* @returns 结构化判断结果,不含任何改写建议的原句
|
|
136
|
+
*/
|
|
137
|
+
export function analyze(input, opts = {}) {
|
|
138
|
+
const hedgeFloor = Number.isFinite(opts.hedgeFloor) ? opts.hedgeFloor : DEFAULTS.hedgeFloor;
|
|
139
|
+
const structureTimes = Number.isFinite(opts.structureTimes) ? opts.structureTimes : DEFAULTS.structureTimes;
|
|
140
|
+
const concreteFloor = Number.isFinite(opts.concreteFloor) ? opts.concreteFloor : DEFAULTS.concreteFloor;
|
|
141
|
+
const raw = String(input ?? '');
|
|
142
|
+
const text = stripCode(raw);
|
|
143
|
+
const chars = text.length;
|
|
144
|
+
const per1k = (n) => (chars ? Math.round((n / chars) * 1000 * 10) / 10 : 0);
|
|
145
|
+
|
|
146
|
+
const reg = registerOf(text);
|
|
147
|
+
const baseline = REGISTERS[reg].per1k;
|
|
148
|
+
|
|
149
|
+
// ── 四个代理指标 ──────────────────────────────
|
|
150
|
+
const conc = concreteness(text);
|
|
151
|
+
const abstractHits = countWords(text, ABSTRACT_NOUNS);
|
|
152
|
+
const evalHits = countWords(text, EVAL_ADJECTIVES);
|
|
153
|
+
const aiWordHits = countWords(text, AI_WORDS);
|
|
154
|
+
const summaryHits = countWords(text, SUMMARY_OPENERS);
|
|
155
|
+
const fakeHits = countWords(text, FAKE_SIGNIFICANCE);
|
|
156
|
+
|
|
157
|
+
// ── 留白 ─────────────────────────────────────
|
|
158
|
+
const hedgeHits = countWords(text, HEDGE_WORDS);
|
|
159
|
+
const hedgePer1k = per1k(hedgeHits.length);
|
|
160
|
+
|
|
161
|
+
// ── 结构腔 ────────────────────────────────────
|
|
162
|
+
const structures = scanStructures(text);
|
|
163
|
+
const totalStructures = structures.reduce((a, r) => a + r.count, 0);
|
|
164
|
+
const structurePer1k = per1k(totalStructures);
|
|
165
|
+
const allowed = Math.max(chars >= 200 ? 1 : 0, Math.round((baseline * chars) / 1000));
|
|
166
|
+
const overTimes = baseline ? Math.round((structurePer1k / baseline) * 10) / 10 : 0;
|
|
167
|
+
|
|
168
|
+
const flatHits = [];
|
|
169
|
+
for (const row of structures) {
|
|
170
|
+
for (const s of row.spans) flatHits.push({ id: row.id, name: row.name, at: s.at, span: s.text });
|
|
171
|
+
}
|
|
172
|
+
const cl = cluster(flatHits);
|
|
173
|
+
|
|
174
|
+
// ── 判档 ─────────────────────────────────────
|
|
175
|
+
// 可替换度:六个信号,各算一分。用密度不用绝对数 —— 五十个字里出一个「意义」,
|
|
176
|
+
// 比五百字里出四个「意义」更能说明问题。
|
|
177
|
+
const per100 = (n) => (chars ? Math.round((n / chars) * 100 * 10) / 10 : 0);
|
|
178
|
+
const abstractPer100 = per100(abstractHits.length);
|
|
179
|
+
const evalPer100 = per100(evalHits.length);
|
|
180
|
+
const aiWordPer100 = per100(aiWordHits.length);
|
|
181
|
+
|
|
182
|
+
const replaceable =
|
|
183
|
+
(conc.per100 < concreteFloor ? 1 : 0) +
|
|
184
|
+
(abstractPer100 + evalPer100 >= 2 ? 1 : 0) +
|
|
185
|
+
(aiWordPer100 >= 1 || aiWordHits.length >= 2 ? 1 : 0) +
|
|
186
|
+
(structurePer1k > baseline * structureTimes ? 1 : 0) +
|
|
187
|
+
(hedgePer1k < hedgeFloor && chars > 40 ? 1 : 0) +
|
|
188
|
+
(summaryHits.length + fakeHits.length > 0 ? 1 : 0);
|
|
189
|
+
|
|
190
|
+
let band;
|
|
191
|
+
let bandLabel;
|
|
192
|
+
let bandNote;
|
|
193
|
+
if (replaceable >= 4) {
|
|
194
|
+
band = 'heavy';
|
|
195
|
+
bandLabel = '换谁都行';
|
|
196
|
+
bandNote = '把这些句子搬到另一个人嘴里,读起来不会有差别。';
|
|
197
|
+
} else if (replaceable >= 2) {
|
|
198
|
+
band = 'mid';
|
|
199
|
+
bandLabel = '有一半可以换人';
|
|
200
|
+
bandNote = '有具体东西,但撑场面的那部分谁都能说。';
|
|
201
|
+
} else {
|
|
202
|
+
band = 'low';
|
|
203
|
+
bandLabel = '换个人说会不一样';
|
|
204
|
+
bandNote = '看得出是谁在说话。';
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// ── 逐条问题:只给问句,不给答案 ────────────────
|
|
208
|
+
const asks = [];
|
|
209
|
+
if (aiWordHits.length) {
|
|
210
|
+
asks.push(`「${aiWordHits.map((h) => h.word).join('」「')}」—— 这几个词,换成平时说话你会怎么讲?`);
|
|
211
|
+
}
|
|
212
|
+
if (summaryHits.length) {
|
|
213
|
+
asks.push(`开头就在总结(${summaryHits.map((h) => h.word).join('、')})。删掉这一句,下面的东西还站得住吗?`);
|
|
214
|
+
}
|
|
215
|
+
if (fakeHits.length) {
|
|
216
|
+
asks.push(`「${fakeHits.map((h) => h.word).join('」「')}」—— 这是它是什么,还是它多重要?`);
|
|
217
|
+
}
|
|
218
|
+
if (abstractHits.length >= 4) {
|
|
219
|
+
asks.push('抽象名词成片了。挑其中一个,说一件具体发生过的事代替它。');
|
|
220
|
+
}
|
|
221
|
+
if (evalHits.length >= 3) {
|
|
222
|
+
asks.push(`评价词用了 ${evalHits.length} 个(${evalHits.map((h) => h.word).slice(0, 4).join('、')}…)。说清夸的是哪一句,比夸得满有用。`);
|
|
223
|
+
}
|
|
224
|
+
if (hedgePer1k < hedgeFloor && chars > 40) {
|
|
225
|
+
asks.push('一个「好像 / 应该 / 大概」都没有。全篇没有一处不确定,是真的都想清楚了?');
|
|
226
|
+
}
|
|
227
|
+
if (cl.maxDistinct >= 3) {
|
|
228
|
+
asks.push(`同一百字里换了 ${cl.maxDistinct} 种句式。挑一种留下,其余换成别的说法。`);
|
|
229
|
+
}
|
|
230
|
+
if (structurePer1k > baseline * structureTimes) {
|
|
231
|
+
asks.push(`结构腔 ${structurePer1k}/千字,真人同文体约 ${baseline}。那一处在说事,还是在给事分层?`);
|
|
232
|
+
}
|
|
233
|
+
if (conc.per100 < concreteFloor && chars > 40) {
|
|
234
|
+
asks.push('没有数字、时间、经历过的事、原话、具体名字。这段话是你看见的,还是你听说的?');
|
|
235
|
+
}
|
|
236
|
+
if (!asks.length) {
|
|
237
|
+
asks.push('找不明显的毛病。剩下的那点别扭,得你自己读一遍才知道在哪。');
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return {
|
|
241
|
+
chars,
|
|
242
|
+
register: reg,
|
|
243
|
+
registerLabel: REGISTERS[reg].label,
|
|
244
|
+
baseline,
|
|
245
|
+
band,
|
|
246
|
+
bandLabel,
|
|
247
|
+
bandNote,
|
|
248
|
+
scores: {
|
|
249
|
+
replaceable,
|
|
250
|
+
concretePer100: conc.per100,
|
|
251
|
+
abstract: abstractHits.length,
|
|
252
|
+
evaluation: evalHits.length,
|
|
253
|
+
aiWords: aiWordHits.length,
|
|
254
|
+
hedges: hedgeHits.length,
|
|
255
|
+
hedgePer1k,
|
|
256
|
+
structurePer1k,
|
|
257
|
+
overTimes,
|
|
258
|
+
clusterDistinct: cl.maxDistinct,
|
|
259
|
+
clusterMax: cl.maxHits,
|
|
260
|
+
},
|
|
261
|
+
hits: {
|
|
262
|
+
aiWords: aiWordHits,
|
|
263
|
+
summary: summaryHits,
|
|
264
|
+
fakeSignificance: fakeHits,
|
|
265
|
+
abstract: abstractHits,
|
|
266
|
+
evaluation: evalHits,
|
|
267
|
+
hedges: hedgeHits,
|
|
268
|
+
concrete: conc.hits,
|
|
269
|
+
structures,
|
|
270
|
+
},
|
|
271
|
+
asks,
|
|
272
|
+
baselineHedge: HEDGE_BASELINE,
|
|
273
|
+
thresholds: { hedgeFloor, structureTimes, concreteFloor },
|
|
274
|
+
blind: '这一步只认字面和句式。同一件事换个说法又说一遍、每段都收个小结论、通篇一个调子 —— 这些它看不见,得你自己读。',
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** 渲染成人看的文本。 */
|
|
279
|
+
export function render(result) {
|
|
280
|
+
const r = result;
|
|
281
|
+
const L = [];
|
|
282
|
+
L.push(`[判断] ${r.bandLabel} · ${r.bandNote}`);
|
|
283
|
+
L.push(`[文体] ${r.registerLabel} · 真人同文体结构腔基线 ${r.baseline}/千字`);
|
|
284
|
+
L.push(
|
|
285
|
+
`[具体] 每百字 ${r.scores.concretePer100} 个具体物(数字/时间/经历/原话/名字)· ` +
|
|
286
|
+
`抽象词 ${r.scores.abstract} · 评价词 ${r.scores.evaluation}`
|
|
287
|
+
);
|
|
288
|
+
L.push(
|
|
289
|
+
`[留白] ${r.scores.hedges} 个(${r.scores.hedgePer1k}/千字,真人约 ${r.baselineHedge})` +
|
|
290
|
+
(r.scores.hedgePer1k < (r.thresholds?.hedgeFloor ?? DEFAULTS.hedgeFloor) ? ' —— 一个都没有,等于句句在下断言' : '')
|
|
291
|
+
);
|
|
292
|
+
L.push(
|
|
293
|
+
`[结构腔] ${r.scores.structurePer1k}/千字 · 真人基线的 ${r.scores.overTimes} 倍 · ` +
|
|
294
|
+
`同百字最多 ${r.scores.clusterDistinct} 种句式`
|
|
295
|
+
);
|
|
296
|
+
if (r.hits.structures.length) {
|
|
297
|
+
L.push('[逐处]');
|
|
298
|
+
for (const row of r.hits.structures) {
|
|
299
|
+
L.push(` · ${row.name} ×${row.count}:${row.spans.map((s) => `「${s.text}」`).join(' ')}`);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
L.push('[要你自己答]');
|
|
303
|
+
for (const a of r.asks) L.push(` - ${a}`);
|
|
304
|
+
L.push(`[看不见] ${r.blind}`);
|
|
305
|
+
return L.join('\n');
|
|
306
|
+
}
|
package/lib/config.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 配置:读写插件目录的 config.json,热生效。
|
|
3
|
+
*
|
|
4
|
+
* 存在插件自己的目录里(不放全局 settings.yaml),这样插件删了配置也跟着走。
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { readFileSync, writeFileSync, mkdirSync } from 'node:fs';
|
|
8
|
+
import { dirname, join } from 'node:path';
|
|
9
|
+
import { fileURLToPath } from 'node:url';
|
|
10
|
+
|
|
11
|
+
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
12
|
+
export const CONFIG_FILE = join(HERE, '..', 'config.json');
|
|
13
|
+
|
|
14
|
+
export const DEFAULTS = {
|
|
15
|
+
/** 整节系统提示挂不挂 */
|
|
16
|
+
enabled: true,
|
|
17
|
+
/** 只要「查」,不挂 prompt 那一节的话设 false */
|
|
18
|
+
prompt: true,
|
|
19
|
+
/** prompt 节的 order,越大越靠后 */
|
|
20
|
+
order: 921,
|
|
21
|
+
/** 留白词低于这个密度(每千字)就报「句句在下断言」 */
|
|
22
|
+
hedgeFloor: 0.5,
|
|
23
|
+
/** 结构腔超过基线几倍才报 */
|
|
24
|
+
structureTimes: 3,
|
|
25
|
+
/** 具体物低于每百字这个数就报 */
|
|
26
|
+
concreteFloor: 0.6,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export function readConfig() {
|
|
30
|
+
try {
|
|
31
|
+
const raw = readFileSync(CONFIG_FILE, 'utf8');
|
|
32
|
+
const saved = JSON.parse(raw);
|
|
33
|
+
return { ...DEFAULTS, ...saved };
|
|
34
|
+
} catch {
|
|
35
|
+
return { ...DEFAULTS };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function writeConfig(patch) {
|
|
40
|
+
const cur = readConfig();
|
|
41
|
+
const next = { ...cur };
|
|
42
|
+
for (const [k, v] of Object.entries(patch || {})) {
|
|
43
|
+
if (!(k in DEFAULTS)) continue;
|
|
44
|
+
if (typeof DEFAULTS[k] === 'boolean') next[k] = Boolean(v);
|
|
45
|
+
else if (typeof DEFAULTS[k] === 'number') {
|
|
46
|
+
const n = Number(v);
|
|
47
|
+
if (Number.isFinite(n)) next[k] = n;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
// order 夹一下,别把别人的节顶飞
|
|
51
|
+
next.order = Math.min(999, Math.max(1, Math.round(next.order)));
|
|
52
|
+
// 阈值不许为负
|
|
53
|
+
next.hedgeFloor = Math.max(0, next.hedgeFloor);
|
|
54
|
+
next.structureTimes = Math.max(1, next.structureTimes);
|
|
55
|
+
next.concreteFloor = Math.max(0, next.concreteFloor);
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
mkdirSync(dirname(CONFIG_FILE), { recursive: true });
|
|
59
|
+
writeFileSync(CONFIG_FILE, JSON.stringify(next, null, 2) + '\n', 'utf8');
|
|
60
|
+
} catch {
|
|
61
|
+
/* 只读环境就只在内存里生效 */
|
|
62
|
+
}
|
|
63
|
+
return next;
|
|
64
|
+
}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-bagu · 宿主半侧
|
|
3
|
+
*
|
|
4
|
+
* 三件事:
|
|
5
|
+
* 1. 往系统提示里挂一节「说什么」,讲的是念头不是词表(可在设置页关掉)
|
|
6
|
+
* 2. 挂两个只读工具:bagu_check、bagu_ask
|
|
7
|
+
* 3. 开 /api/bagu,给设置页用
|
|
8
|
+
*
|
|
9
|
+
* 刻意不做的事:不自动改写。改要人改。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { analyze, render } from './analyze.js';
|
|
13
|
+
import { readConfig, writeConfig, CONFIG_FILE, DEFAULTS } from './config.js';
|
|
14
|
+
|
|
15
|
+
export const name = 'dsh-bagu';
|
|
16
|
+
|
|
17
|
+
export { analyze, render };
|
|
18
|
+
|
|
19
|
+
/** 当前生效的配置。设置页改了立刻换掉。 */
|
|
20
|
+
let live = readConfig();
|
|
21
|
+
|
|
22
|
+
/** 已挂上去的 prompt 节,改了要先撤再挂。 */
|
|
23
|
+
const syncers = new Set();
|
|
24
|
+
|
|
25
|
+
const PROMPT = `## 说话的念头
|
|
26
|
+
|
|
27
|
+
开口前先问的不是「这么答对不对」,是「我要说什么」。
|
|
28
|
+
|
|
29
|
+
两者差得远。前一个问题让你去猜对方想听什么,后一个问题让你去想自己要给什么。
|
|
30
|
+
|
|
31
|
+
一句话换个人说,还成不成立?成立,就是八股。谁都能说的话,慢慢就没人想看了。
|
|
32
|
+
问题不在礼貌,在可替换。
|
|
33
|
+
|
|
34
|
+
几个具体的:
|
|
35
|
+
|
|
36
|
+
- **别急着认错。**对方只是问一句,不一定是在说你错。对就说是,别为了顺着改口,把对的也改错了。
|
|
37
|
+
- **留一处不确定。**一个「好像 / 应该 / 大概」都没有的段落,是在下断言,不是在说话。
|
|
38
|
+
- **具体压过评价。**「挺好的」谁都能说;「骨架在推所以有点飘」只有看过的人才说得出。
|
|
39
|
+
- **篇幅别总一样齐。**每段都三句、每句都差不多长,整齐本身就是痕迹。
|
|
40
|
+
- **说七分。**话别一次说完。留点让人问。
|
|
41
|
+
|
|
42
|
+
不硬塞语气词,不为了显得像人而加口癖。加进去的是另一种假。`;
|
|
43
|
+
|
|
44
|
+
const TOOL_CHECK = `查一段话有多「可替换」。给几个分开的指标和一个判档,不给单一总分,也不自动改写。
|
|
45
|
+
|
|
46
|
+
指标:
|
|
47
|
+
- 具体性(每百字几个数字/时间/经历/原话/名字)
|
|
48
|
+
- 抽象度、评价度(意义、价值、本质 / 重要、关键、深刻)
|
|
49
|
+
- 留白词密度(好像、应该、大概)
|
|
50
|
+
- 结构腔(不是X而是Y 等),以及同一百字里几种句式扎堆
|
|
51
|
+
|
|
52
|
+
返回里带一组「要你自己答」的问句。答不上来,那处就是摆设。`;
|
|
53
|
+
|
|
54
|
+
const TOOL_ASK = `只给问句,不给结论。想让人自己看着改、不想被工具改的时候用这个。`;
|
|
55
|
+
|
|
56
|
+
function toolOutput() {
|
|
57
|
+
return {
|
|
58
|
+
schema: {
|
|
59
|
+
type: 'object',
|
|
60
|
+
additionalProperties: true,
|
|
61
|
+
required: ['ok'],
|
|
62
|
+
properties: {
|
|
63
|
+
ok: { type: 'boolean' },
|
|
64
|
+
text: { type: 'string' },
|
|
65
|
+
detail: { type: 'object' },
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
render: (_args, value) => [{ type: 'text', text: String(value?.text ?? '') }],
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function thresholdsOf(cfg) {
|
|
73
|
+
return {
|
|
74
|
+
hedgeFloor: cfg.hedgeFloor,
|
|
75
|
+
structureTimes: cfg.structureTimes,
|
|
76
|
+
concreteFloor: cfg.concreteFloor,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** 配置变了以后,让已经挂上的 prompt 节跟着换。 */
|
|
81
|
+
function applyLive(next) {
|
|
82
|
+
live = next;
|
|
83
|
+
for (const sync of syncers) {
|
|
84
|
+
try { sync(); } catch { /* 单个失败不影响别的 */ }
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function sendJson(res, status, body) {
|
|
89
|
+
const text = JSON.stringify(body);
|
|
90
|
+
res.statusCode = status;
|
|
91
|
+
res.setHeader('content-type', 'application/json; charset=utf-8');
|
|
92
|
+
res.setHeader('cache-control', 'no-store');
|
|
93
|
+
res.end(text);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function readBody(req) {
|
|
97
|
+
return new Promise((resolve) => {
|
|
98
|
+
let raw = '';
|
|
99
|
+
req.on('data', (c) => { raw += c; if (raw.length > 1e6) raw = raw.slice(0, 1e6); });
|
|
100
|
+
req.on('end', () => {
|
|
101
|
+
try { resolve(raw ? JSON.parse(raw) : {}); } catch { resolve({}); }
|
|
102
|
+
});
|
|
103
|
+
req.on('error', () => resolve({}));
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function apply(root, config = {}) {
|
|
108
|
+
// cordis.patch.yml 里的初始值,只在 config.json 还没写过时作底
|
|
109
|
+
live = readConfig();
|
|
110
|
+
if (config && typeof config === 'object') {
|
|
111
|
+
for (const k of Object.keys(DEFAULTS)) {
|
|
112
|
+
if (config[k] !== undefined && live[k] === DEFAULTS[k]) live[k] = config[k];
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
root.inject(['systemPrompt'], (ctx) => {
|
|
117
|
+
let dispose = null;
|
|
118
|
+
const sync = () => {
|
|
119
|
+
if (dispose) { dispose(); dispose = null; }
|
|
120
|
+
if (!live.enabled || !live.prompt) return;
|
|
121
|
+
dispose = ctx.systemPrompt.section({
|
|
122
|
+
name: 'bagu:voice',
|
|
123
|
+
order: live.order,
|
|
124
|
+
text: PROMPT,
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
sync();
|
|
128
|
+
syncers.add(sync);
|
|
129
|
+
ctx.effect(() => () => {
|
|
130
|
+
syncers.delete(sync);
|
|
131
|
+
if (dispose) dispose();
|
|
132
|
+
}, 'dsh-bagu: 说话的念头');
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
root.inject(['tools'], (ctx) => {
|
|
136
|
+
ctx.tools.register({
|
|
137
|
+
name: 'bagu_check',
|
|
138
|
+
description: TOOL_CHECK,
|
|
139
|
+
parameters: {
|
|
140
|
+
type: 'object',
|
|
141
|
+
properties: {
|
|
142
|
+
text: { type: 'string', description: '要查的整段话。整段传,别自己先切句。' },
|
|
143
|
+
},
|
|
144
|
+
required: ['text'],
|
|
145
|
+
},
|
|
146
|
+
output: toolOutput(),
|
|
147
|
+
execute: async (args) => {
|
|
148
|
+
const src = String(args?.text ?? '');
|
|
149
|
+
if (!src.trim()) return { ok: false, text: '没有收到文字。' };
|
|
150
|
+
const r = analyze(src, thresholdsOf(live));
|
|
151
|
+
return { ok: true, text: render(r), detail: { band: r.band, scores: r.scores } };
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
ctx.tools.register({
|
|
156
|
+
name: 'bagu_ask',
|
|
157
|
+
description: TOOL_ASK,
|
|
158
|
+
parameters: {
|
|
159
|
+
type: 'object',
|
|
160
|
+
properties: {
|
|
161
|
+
text: { type: 'string', description: '要问的那段话。' },
|
|
162
|
+
},
|
|
163
|
+
required: ['text'],
|
|
164
|
+
},
|
|
165
|
+
output: toolOutput(),
|
|
166
|
+
execute: async (args) => {
|
|
167
|
+
const src = String(args?.text ?? '');
|
|
168
|
+
if (!src.trim()) return { ok: false, text: '没有收到文字。' };
|
|
169
|
+
const r = analyze(src, thresholdsOf(live));
|
|
170
|
+
const text = r.asks.map((a, i) => `${i + 1}. ${a}`).join('\n');
|
|
171
|
+
return { ok: true, text, detail: { band: r.band } };
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
root.inject(['webServer'], (ctx) => {
|
|
177
|
+
const handler = async (req, res) => {
|
|
178
|
+
const url = new URL(req.url, 'http://127.0.0.1');
|
|
179
|
+
const path = url.pathname.replace(/^\/api\/bagu/, '') || '/';
|
|
180
|
+
|
|
181
|
+
if (req.method === 'GET' && (path === '/' || path === '/status')) {
|
|
182
|
+
return sendJson(res, 200, {
|
|
183
|
+
ok: true,
|
|
184
|
+
config: live,
|
|
185
|
+
defaults: DEFAULTS,
|
|
186
|
+
promptActive: live.enabled && live.prompt,
|
|
187
|
+
configFile: CONFIG_FILE,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (req.method === 'POST' && path === '/config') {
|
|
192
|
+
const body = await readBody(req);
|
|
193
|
+
const next = writeConfig(body);
|
|
194
|
+
applyLive(next);
|
|
195
|
+
return sendJson(res, 200, { ok: true, config: next });
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (req.method === 'POST' && path === '/preview') {
|
|
199
|
+
const body = await readBody(req);
|
|
200
|
+
const src = String(body?.text ?? '');
|
|
201
|
+
if (!src.trim()) return sendJson(res, 200, { ok: false, error: '没有文字' });
|
|
202
|
+
const r = analyze(src, thresholdsOf(live));
|
|
203
|
+
return sendJson(res, 200, { ok: true, text: render(r), band: r.band, scores: r.scores });
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return sendJson(res, 404, { ok: false, error: `unknown ${req.method} ${path}` });
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const server = ctx.webServer || (typeof ctx.get === 'function' ? ctx.get('webServer') : undefined);
|
|
210
|
+
if (!server || typeof server.register !== 'function') return;
|
|
211
|
+
try {
|
|
212
|
+
ctx.effect(() => server.register({
|
|
213
|
+
kind: 'prefix',
|
|
214
|
+
path: '/api/bagu',
|
|
215
|
+
handler: (req, res) => {
|
|
216
|
+
Promise.resolve(handler(req, res)).catch((e) => {
|
|
217
|
+
try { sendJson(res, 500, { ok: false, error: e?.message ?? String(e) }) } catch { /* 已经走了 */ }
|
|
218
|
+
});
|
|
219
|
+
},
|
|
220
|
+
}), 'dsh-bagu: /api/bagu');
|
|
221
|
+
} catch { /* headless 里没有 webServer 是正常的 */ }
|
|
222
|
+
});
|
|
223
|
+
}
|