@kangyishuai/dsh-next-step 0.3.1 → 0.4.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 +20 -13
- package/README.zh-CN.md +23 -15
- package/lib/card-data.js +157 -0
- package/lib/client.js +476 -81
- package/lib/events.js +287 -0
- package/lib/gates.js +27 -6
- package/lib/projection.js +32 -73
- package/lib/prompt.js +225 -55
- package/lib/report.js +557 -0
- package/lib/scheme.js +68 -0
- package/lib/service.js +612 -119
- package/lib/settings.js +68 -8
- package/locale/en.json +22 -2
- package/locale/zh.json +22 -2
- package/package.json +1 -1
package/lib/report.js
ADDED
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// 复盘报表:把 events.jsonl 折叠成指标 + 判读句 + 一份 Markdown。
|
|
3
|
+
//
|
|
4
|
+
// 设计要点(见 docs/adr/0013):
|
|
5
|
+
// - **纯函数在前**:解析、汇总、判读都只吃数组/数字,`node --test` 直接覆盖;
|
|
6
|
+
// 写文件只有一个薄封装(`writeReport`)。
|
|
7
|
+
// - **样本门禁**:`turns < 80` 或 `shown < 25` 时只输出计数,不给任何判读——
|
|
8
|
+
// 一两天几十条数据得出的"结论"只会把噪声当信号。
|
|
9
|
+
// - **比例给 Wilson 95% 区间**:两组比较只在区间不重叠时才说"有差别",否则明写"看不出差别"。
|
|
10
|
+
// - **每个指标都印分母**:避免"忽略率 86%"这种没有分母、无法复核的数字。
|
|
11
|
+
// - 判读句写成「现象 + 该动什么(只动一类)」:复盘要能落成一个具体改动。
|
|
12
|
+
|
|
13
|
+
import { mkdir, readdir, rm, writeFile } from "node:fs/promises";
|
|
14
|
+
import { join } from "node:path";
|
|
15
|
+
|
|
16
|
+
import { eventsDirOf } from "./events.js";
|
|
17
|
+
|
|
18
|
+
/** 判读的最小样本:少于这个数只列计数。 */
|
|
19
|
+
export const MIN_TURNS = 80;
|
|
20
|
+
/** 判读的最小出卡数(点击率/忽略率的分母)。 */
|
|
21
|
+
export const MIN_SHOWN = 25;
|
|
22
|
+
/** 报表文件保留份数(更早的自动删除,只在插件自己的目录内)。 */
|
|
23
|
+
export const REPORT_KEEP = 20;
|
|
24
|
+
/** Wilson 区间的 z 值(95%)。 */
|
|
25
|
+
export const WILSON_Z = 1.96;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 逐行解析事件文件(坏行跳过并计数,绝不抛错)。
|
|
29
|
+
* @param {string} text
|
|
30
|
+
* @returns {{ events: any[], badLines: number }}
|
|
31
|
+
*/
|
|
32
|
+
export function parseEvents(text) {
|
|
33
|
+
const events = [];
|
|
34
|
+
let badLines = 0;
|
|
35
|
+
for (const line of String(text ?? "").split("\n")) {
|
|
36
|
+
const trimmed = line.trim();
|
|
37
|
+
if (trimmed === "") continue;
|
|
38
|
+
try {
|
|
39
|
+
const parsed = JSON.parse(trimmed);
|
|
40
|
+
if (parsed !== null && typeof parsed === "object" && typeof parsed.t === "string") {
|
|
41
|
+
events.push(parsed);
|
|
42
|
+
} else {
|
|
43
|
+
badLines += 1;
|
|
44
|
+
}
|
|
45
|
+
} catch (_error) {
|
|
46
|
+
badLines += 1;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return { events, badLines };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Wilson 比例区间(比正态近似在小样本/极端比例下更可靠)。
|
|
54
|
+
* @param {number} k 命中数
|
|
55
|
+
* @param {number} n 样本数
|
|
56
|
+
* @param {number} [z]
|
|
57
|
+
* @returns {{ p: number, low: number, high: number, n: number } | null} n=0 时返回 null
|
|
58
|
+
*/
|
|
59
|
+
export function wilson(k, n, z = WILSON_Z) {
|
|
60
|
+
const hits = Number.isFinite(k) ? Math.max(0, k) : 0;
|
|
61
|
+
const total = Number.isFinite(n) ? Math.max(0, n) : 0;
|
|
62
|
+
if (total === 0) return null;
|
|
63
|
+
const p = hits / total;
|
|
64
|
+
const z2 = z * z;
|
|
65
|
+
const denominator = 1 + z2 / total;
|
|
66
|
+
const center = (p + z2 / (2 * total)) / denominator;
|
|
67
|
+
const spread = (z * Math.sqrt((p * (1 - p)) / total + z2 / (4 * total * total))) / denominator;
|
|
68
|
+
return {
|
|
69
|
+
p,
|
|
70
|
+
low: Math.max(0, center - spread),
|
|
71
|
+
high: Math.min(1, center + spread),
|
|
72
|
+
n: total,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 两个区间是否重叠(重叠 = 看不出版本/分组之间有差别)。
|
|
78
|
+
* @param {{ low: number, high: number } | null} left
|
|
79
|
+
* @param {{ low: number, high: number } | null} right
|
|
80
|
+
* @returns {boolean}
|
|
81
|
+
*/
|
|
82
|
+
export function intervalsOverlap(left, right) {
|
|
83
|
+
if (left === null || right === null) return true;
|
|
84
|
+
return Math.max(left.low, right.low) <= Math.min(left.high, right.high);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* label 长度分桶(复盘按钮文案用)。
|
|
89
|
+
* @param {number} length
|
|
90
|
+
* @returns {string}
|
|
91
|
+
*/
|
|
92
|
+
export function labelBucketOf(length) {
|
|
93
|
+
const n = Number.isFinite(length) ? length : 0;
|
|
94
|
+
if (n <= 8) return "≤8";
|
|
95
|
+
if (n <= 12) return "9–12";
|
|
96
|
+
if (n <= 16) return "13–16";
|
|
97
|
+
return "17–20";
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 会话 id + 回合号 → join 键。
|
|
102
|
+
* @param {any} event
|
|
103
|
+
* @returns {string}
|
|
104
|
+
*/
|
|
105
|
+
function keyOf(event) {
|
|
106
|
+
return `${String(event.sid)}:${String(event.turn)}`;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* @param {number} k @param {number} n
|
|
111
|
+
* @returns {{ n: number, clicked: number, ctr: number | null, ci: any }}
|
|
112
|
+
*/
|
|
113
|
+
function rateCell(k, n) {
|
|
114
|
+
return { n, clicked: k, ctr: n === 0 ? null : k / n, ci: wilson(k, n) };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* 汇总指标。所有比例都能从返回的计数复算(报表里也把计数一起印出来)。
|
|
119
|
+
* @param {any[]} events
|
|
120
|
+
* @returns {any}
|
|
121
|
+
*/
|
|
122
|
+
export function summarize(events) {
|
|
123
|
+
/** @type {Map<string, any>} */
|
|
124
|
+
const turns = new Map();
|
|
125
|
+
/** @type {Map<string, any>} */
|
|
126
|
+
const actions = new Map();
|
|
127
|
+
/** @type {Map<string, any>} */
|
|
128
|
+
const followups = new Map();
|
|
129
|
+
/** @type {Map<string, any>} */
|
|
130
|
+
const impressions = new Map();
|
|
131
|
+
/** @type {Set<string>} */
|
|
132
|
+
const promoted = new Set();
|
|
133
|
+
/** @type {any[]} */
|
|
134
|
+
const errors = [];
|
|
135
|
+
let meta = null;
|
|
136
|
+
let lastAt = 0;
|
|
137
|
+
/** @type {Set<string>} */
|
|
138
|
+
const sessions = new Set();
|
|
139
|
+
|
|
140
|
+
for (const event of Array.isArray(events) ? events : []) {
|
|
141
|
+
if (typeof event.at === "number" && event.at > lastAt) lastAt = event.at;
|
|
142
|
+
if (typeof event.sid === "string") sessions.add(event.sid);
|
|
143
|
+
if (event.t === "meta") {
|
|
144
|
+
if (meta === null) meta = event;
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (event.t === "turn") {
|
|
148
|
+
const key = keyOf(event);
|
|
149
|
+
if (!turns.has(key)) turns.set(key, event);
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (event.t === "action") {
|
|
153
|
+
const key = keyOf(event);
|
|
154
|
+
if (!actions.has(key)) actions.set(key, event);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (event.t === "followup") {
|
|
158
|
+
const key = keyOf(event);
|
|
159
|
+
if (!followups.has(key)) followups.set(key, event);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (event.t === "impression") {
|
|
163
|
+
const key = keyOf(event);
|
|
164
|
+
const previous = impressions.get(key);
|
|
165
|
+
// 同一张卡可能上报多次(重新挂载):保留可见时长最大的那条
|
|
166
|
+
if (previous === undefined || Number(event.visibleMs) > Number(previous.visibleMs)) {
|
|
167
|
+
impressions.set(key, event);
|
|
168
|
+
}
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (event.t === "promote") {
|
|
172
|
+
if (event.found === true) promoted.add(keyOf(event));
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
if (event.t === "error") errors.push(event);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const gateCounts = /** @type {Record<string, number>} */ ({});
|
|
179
|
+
let shown = 0;
|
|
180
|
+
let suppressed = 0;
|
|
181
|
+
let seen = 0;
|
|
182
|
+
let unseen = 0;
|
|
183
|
+
let unseenUnknown = 0;
|
|
184
|
+
let clicked = 0;
|
|
185
|
+
let dismissed = 0;
|
|
186
|
+
let missedByOverlap = 0;
|
|
187
|
+
/** @type {Record<string, { n: number, clicked: number, ctr: number | null, ci: any }>} */
|
|
188
|
+
const byValue = {};
|
|
189
|
+
/** @type {Record<string, { n: number, clicked: number, ctr: number | null, ci: any }>} */
|
|
190
|
+
const byTier = {};
|
|
191
|
+
/** @type {Record<string, { n: number, clicked: number, ctr: number | null, ci: any }>} */
|
|
192
|
+
const byIndex = {};
|
|
193
|
+
/** @type {Record<string, { n: number, clicked: number, ctr: number | null, ci: any }>} */
|
|
194
|
+
const byOptionCount = {};
|
|
195
|
+
/** @type {Record<string, { n: number, clicked: number, ctr: number | null, ci: any }>} */
|
|
196
|
+
const byLabelBucket = {};
|
|
197
|
+
/** @type {Record<string, { n: number, clicked: number, ctr: number | null, ci: any }>} */
|
|
198
|
+
const byPromptVersion = {};
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* @param {Record<string, any>} bucket
|
|
202
|
+
* @param {string} name
|
|
203
|
+
* @param {boolean} hit
|
|
204
|
+
* @returns {void}
|
|
205
|
+
*/
|
|
206
|
+
function bump(bucket, name, hit) {
|
|
207
|
+
const cell = bucket[name] ?? { n: 0, clicked: 0 };
|
|
208
|
+
cell.n += 1;
|
|
209
|
+
if (hit) cell.clicked += 1;
|
|
210
|
+
bucket[name] = cell;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
for (const [key, turn] of turns) {
|
|
214
|
+
gateCounts[turn.gate] = (gateCounts[turn.gate] ?? 0) + 1;
|
|
215
|
+
const cand = turn.cand;
|
|
216
|
+
if (turn.gate !== "ok" || cand === undefined || cand === null) continue;
|
|
217
|
+
const action = actions.get(key);
|
|
218
|
+
const isClicked = action !== undefined && action.kind === "clicked";
|
|
219
|
+
if (cand.shown === true) {
|
|
220
|
+
shown += 1;
|
|
221
|
+
const impression = impressions.get(key);
|
|
222
|
+
if (impression !== undefined) {
|
|
223
|
+
if (Number(impression.ratio) < 0) unseenUnknown += 1;
|
|
224
|
+
else if (Number(impression.visibleMs) > 0) seen += 1;
|
|
225
|
+
else unseen += 1;
|
|
226
|
+
}
|
|
227
|
+
if (action !== undefined && action.kind === "clicked") clicked += 1;
|
|
228
|
+
if (action !== undefined && action.kind === "dismissed") dismissed += 1;
|
|
229
|
+
bump(byValue, String(cand.value ?? "unknown"), isClicked);
|
|
230
|
+
bump(byTier, String(turn.tier ?? "unknown"), isClicked);
|
|
231
|
+
bump(byOptionCount, String(Array.isArray(cand.opts) ? cand.opts.length : 0), isClicked);
|
|
232
|
+
bump(byPromptVersion, String(turn.pv ?? 0), isClicked);
|
|
233
|
+
if (action !== undefined && action.kind === "clicked" && Number.isInteger(action.index)) {
|
|
234
|
+
bump(byIndex, String(action.index), isClicked);
|
|
235
|
+
}
|
|
236
|
+
const opts = Array.isArray(cand.opts) ? cand.opts : [];
|
|
237
|
+
const clickIndex = action !== undefined && Number.isInteger(action.index) ? action.index : -1;
|
|
238
|
+
for (let index = 0; index < opts.length; index += 1) {
|
|
239
|
+
const labelLen = Number(opts[index]?.len ?? 0);
|
|
240
|
+
bump(byLabelBucket, labelBucketOf(labelLen), isClicked && clickIndex === index);
|
|
241
|
+
}
|
|
242
|
+
} else {
|
|
243
|
+
suppressed += 1;
|
|
244
|
+
const followup = followups.get(key);
|
|
245
|
+
if (followup !== undefined && followup.typed === true && Number(followup.overlap) > 0) {
|
|
246
|
+
missedByOverlap += 1;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// 桶内补齐点击率与区间
|
|
252
|
+
for (const bucket of [byValue, byTier, byIndex, byOptionCount, byLabelBucket, byPromptVersion]) {
|
|
253
|
+
for (const name of Object.keys(bucket)) {
|
|
254
|
+
const cell = bucket[name];
|
|
255
|
+
Object.assign(cell, rateCell(cell.clicked, cell.n));
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const ignored = Math.max(0, shown - clicked - dismissed);
|
|
260
|
+
const misses = promoted.size + missedByOverlap;
|
|
261
|
+
const errorKinds = /** @type {Record<string, number>} */ ({});
|
|
262
|
+
for (const error of errors)
|
|
263
|
+
errorKinds[String(error.kind ?? "unknown")] =
|
|
264
|
+
(errorKinds[String(error.kind ?? "unknown")] ?? 0) + 1;
|
|
265
|
+
|
|
266
|
+
const gateOkTurns = (gateCounts.ok ?? 0) + errors.length;
|
|
267
|
+
return {
|
|
268
|
+
scheme: typeof meta?.scheme === "string" ? meta.scheme : "v0.4",
|
|
269
|
+
since: typeof meta?.at === "number" ? meta.at : 0,
|
|
270
|
+
lastAt,
|
|
271
|
+
days:
|
|
272
|
+
typeof meta?.at === "number" && lastAt > meta.at
|
|
273
|
+
? Math.max(1, Math.round((lastAt - meta.at) / 86400000))
|
|
274
|
+
: 0,
|
|
275
|
+
sessions: sessions.size,
|
|
276
|
+
turns: turns.size,
|
|
277
|
+
shown,
|
|
278
|
+
suppressed,
|
|
279
|
+
seen,
|
|
280
|
+
unseen,
|
|
281
|
+
unseenUnknown,
|
|
282
|
+
clicked,
|
|
283
|
+
dismissed,
|
|
284
|
+
ignored,
|
|
285
|
+
promoted: promoted.size,
|
|
286
|
+
missedByOverlap,
|
|
287
|
+
misses,
|
|
288
|
+
rates: {
|
|
289
|
+
cardRate: turns.size === 0 ? null : shown / turns.size,
|
|
290
|
+
seenRate: shown === 0 ? null : seen / shown,
|
|
291
|
+
ctr: shown === 0 ? null : clicked / shown,
|
|
292
|
+
ignoreRate: shown === 0 ? null : ignored / shown,
|
|
293
|
+
missRate: suppressed === 0 ? null : misses / suppressed,
|
|
294
|
+
},
|
|
295
|
+
ci: {
|
|
296
|
+
ctr: wilson(clicked, shown),
|
|
297
|
+
missRate: wilson(misses, suppressed),
|
|
298
|
+
},
|
|
299
|
+
byValue,
|
|
300
|
+
byTier,
|
|
301
|
+
byIndex,
|
|
302
|
+
byOptionCount,
|
|
303
|
+
byLabelBucket,
|
|
304
|
+
byPromptVersion,
|
|
305
|
+
gates: gateCounts,
|
|
306
|
+
errors: { total: errors.length, byKind: errorKinds },
|
|
307
|
+
schemaShare: gateOkTurns === 0 ? 0 : (errorKinds.schema ?? 0) / gateOkTurns,
|
|
308
|
+
insufficient: turns.size < MIN_TURNS || shown < MIN_SHOWN,
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* 判读:把指标变成「现象 + 该动什么」。样本不足时返回空数组(只列计数)。
|
|
314
|
+
* @param {any} metrics summarize() 的结果
|
|
315
|
+
* @returns {{ code: string, text: string, action: string }[]}
|
|
316
|
+
*/
|
|
317
|
+
export function verdicts(metrics) {
|
|
318
|
+
/** @type {{ code: string, text: string, action: string }[]} */
|
|
319
|
+
const out = [];
|
|
320
|
+
if (metrics.insufficient === true) return out;
|
|
321
|
+
const rates = metrics.rates;
|
|
322
|
+
|
|
323
|
+
if (rates.seenRate !== null && rates.seenRate < 0.6) {
|
|
324
|
+
out.push({
|
|
325
|
+
code: "low-exposure",
|
|
326
|
+
text: `出过的卡里只有 ${Math.round(rates.seenRate * 100)}% 真的进过视口(${metrics.seen}/${metrics.shown})。`,
|
|
327
|
+
action: "先修渲染位置或出现时机,不要动提示词——现在还不知道内容好不好。",
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
if (rates.ignoreRate !== null && rates.ignoreRate > 0.7 && (rates.seenRate ?? 0) >= 0.6) {
|
|
331
|
+
out.push({
|
|
332
|
+
code: "ignored-but-seen",
|
|
333
|
+
text: `卡片多数被看见却没被点(忽略 ${metrics.ignored}/${metrics.shown})。`,
|
|
334
|
+
action: "改内容:摘要是否写了可核查事实、label 是否带具体对象。一次只改一类。",
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
if (rates.missRate !== null && rates.missRate > 0.2) {
|
|
338
|
+
out.push({
|
|
339
|
+
code: "too-strict",
|
|
340
|
+
text: `被抑制的候选里有 ${Math.round(rates.missRate * 100)}% 事后看是想要的(${metrics.misses}/${metrics.suppressed})。`,
|
|
341
|
+
action: "把出卡频率从「平衡」往「积极」调一档;先别改提示词。",
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
if (rates.ctr !== null && metrics.ci.ctr !== null && metrics.ci.ctr.high < 0.1) {
|
|
345
|
+
out.push({
|
|
346
|
+
code: "low-ctr",
|
|
347
|
+
text: `点击率只有 ${Math.round(rates.ctr * 100)}%(${metrics.clicked}/${metrics.shown},95% 区间上限 ${Math.round(metrics.ci.ctr.high * 100)}%)。`,
|
|
348
|
+
action: "内容或位置问题,不是门控问题:先看曝光与摘要具体性。",
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const must = metrics.byValue.must;
|
|
353
|
+
const could = metrics.byValue.could;
|
|
354
|
+
if (must !== undefined && could !== undefined && intervalsOverlap(must.ci, could.ci)) {
|
|
355
|
+
out.push({
|
|
356
|
+
code: "tiers-indistinct",
|
|
357
|
+
text: `must(${must.clicked}/${must.n})与 could(${could.clicked}/${could.n})的点击率区间重叠。`,
|
|
358
|
+
action: "三档目前没有区分力:先改提示词里 could 的判据,或把档位固定成「严格」。",
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const second = metrics.byIndex["1"];
|
|
363
|
+
// 用 Wilson 区间上界判"几乎没人点":0/40 的点估计是 0,但区间上界约 9%,
|
|
364
|
+
// 直接比点估计会得出"绝对没人点"这种小样本不敢下的结论。
|
|
365
|
+
if (second !== undefined && second.n >= 20 && second.ci !== null && second.ci.high < 0.1) {
|
|
366
|
+
out.push({
|
|
367
|
+
code: "second-option-useless",
|
|
368
|
+
text: `第二条建议几乎没人点(${second.clicked}/${second.n})。`,
|
|
369
|
+
action: "让提示词默认只给 1 条;第二条只在两个方向都明确时才要。",
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
if (metrics.schemaShare > 0.15) {
|
|
374
|
+
out.push({
|
|
375
|
+
code: "schema-failures",
|
|
376
|
+
text: `有 ${Math.round(metrics.schemaShare * 100)}% 的生成是格式失败(${metrics.errors.byKind.schema ?? 0}/${metrics.turns + metrics.errors.total})。`,
|
|
377
|
+
action: "解析还需要放宽,或修复调用没有生效——先看 error.detail 的前缀分布。",
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
if ((metrics.gates.budget ?? 0) > 0) {
|
|
382
|
+
out.push({
|
|
383
|
+
code: "budget-hit",
|
|
384
|
+
text: `有 ${metrics.gates.budget} 个回合撞上会话上限而没有出卡。`,
|
|
385
|
+
action: "上调「每会话最多 N 张卡」,或把它当成一次性的观察上限。",
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
return out;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* 百分比显示(null → "—")。
|
|
393
|
+
* @param {number | null} value
|
|
394
|
+
* @returns {string}
|
|
395
|
+
*/
|
|
396
|
+
function pct(value) {
|
|
397
|
+
return value === null || !Number.isFinite(value) ? "—" : `${Math.round(value * 100)}%`;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* 区间显示。
|
|
402
|
+
* @param {{ low: number, high: number } | null} ci
|
|
403
|
+
* @returns {string}
|
|
404
|
+
*/
|
|
405
|
+
function ciText(ci) {
|
|
406
|
+
return ci === null ? "—" : `[${Math.round(ci.low * 100)}–${Math.round(ci.high * 100)}%]`;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* 渲染 Markdown 报告(五段固定结构)。
|
|
411
|
+
* @param {any} metrics
|
|
412
|
+
* @param {{ code: string, text: string, action: string }[]} list
|
|
413
|
+
* @returns {string}
|
|
414
|
+
*/
|
|
415
|
+
export function renderMarkdown(metrics, list) {
|
|
416
|
+
const lines = [];
|
|
417
|
+
const iso = (/** @type {number} */ at) =>
|
|
418
|
+
at > 0 ? new Date(at).toISOString().replace("T", " ").slice(0, 16) + "Z" : "—";
|
|
419
|
+
lines.push("# next-step 复盘报告");
|
|
420
|
+
lines.push("");
|
|
421
|
+
lines.push(`- 生成时间:${iso(metrics.lastAt)}`);
|
|
422
|
+
lines.push(
|
|
423
|
+
`- 数据起算:${iso(metrics.since)}(共 ${metrics.days} 天,${metrics.sessions} 个会话)`,
|
|
424
|
+
);
|
|
425
|
+
lines.push("");
|
|
426
|
+
lines.push("## 1. 样本量");
|
|
427
|
+
lines.push("");
|
|
428
|
+
lines.push(`- 合格回合:**${metrics.turns}**(判读门槛 ${MIN_TURNS})`);
|
|
429
|
+
lines.push(
|
|
430
|
+
`- 出过的卡:**${metrics.shown}**(判读门槛 ${MIN_SHOWN}),被抑制的候选:${metrics.suppressed}`,
|
|
431
|
+
);
|
|
432
|
+
lines.push(
|
|
433
|
+
metrics.insufficient
|
|
434
|
+
? `- **样本不足,本次只列计数,不给判读。** 继续用 2–3 天再来。`
|
|
435
|
+
: `- 样本量已够,下面的判读可以采信;改动请一次只改一类,改完 ` + `promptVersion +1 再比。`,
|
|
436
|
+
);
|
|
437
|
+
lines.push("");
|
|
438
|
+
lines.push("## 2. 频率");
|
|
439
|
+
lines.push("");
|
|
440
|
+
lines.push(`- 出卡率:${pct(metrics.rates.cardRate)}(${metrics.shown}/${metrics.turns})`);
|
|
441
|
+
lines.push(
|
|
442
|
+
`- 曝光率:${pct(metrics.rates.seenRate)}(${metrics.seen}/${metrics.shown};未进视口 ${metrics.unseen},未知 ${metrics.unseenUnknown})`,
|
|
443
|
+
);
|
|
444
|
+
lines.push(
|
|
445
|
+
`- 点击率:${pct(metrics.rates.ctr)}(${metrics.clicked}/${metrics.shown})${ciText(metrics.ci.ctr)}`,
|
|
446
|
+
);
|
|
447
|
+
lines.push(`- 忽略率:${pct(metrics.rates.ignoreRate)}(${metrics.ignored}/${metrics.shown})`);
|
|
448
|
+
lines.push(`- 主动关闭:${metrics.dismissed}`);
|
|
449
|
+
lines.push("");
|
|
450
|
+
lines.push("## 3. 归因:漏判与噪音");
|
|
451
|
+
lines.push("");
|
|
452
|
+
lines.push(
|
|
453
|
+
`- 漏判率:${pct(metrics.rates.missRate)}(${metrics.misses}/${metrics.suppressed})${ciText(metrics.ci.missRate)}`,
|
|
454
|
+
);
|
|
455
|
+
lines.push(` - 用户点「生成下一步建议」揭示候选:${metrics.promoted}`);
|
|
456
|
+
lines.push(` - 抑制后用户自己打了相关的一句:${metrics.missedByOverlap}`);
|
|
457
|
+
lines.push(
|
|
458
|
+
`- 闸门分布:${
|
|
459
|
+
Object.entries(metrics.gates)
|
|
460
|
+
.map(([k, v]) => `${k}=${v}`)
|
|
461
|
+
.join(" · ") || "—"
|
|
462
|
+
}`,
|
|
463
|
+
);
|
|
464
|
+
lines.push(
|
|
465
|
+
`- 生成失败:${metrics.errors.total}(${
|
|
466
|
+
Object.entries(metrics.errors.byKind)
|
|
467
|
+
.map(([k, v]) => `${k}=${v}`)
|
|
468
|
+
.join(" · ") || "无"
|
|
469
|
+
})`,
|
|
470
|
+
);
|
|
471
|
+
lines.push("");
|
|
472
|
+
lines.push("## 4. 内容与按钮");
|
|
473
|
+
lines.push("");
|
|
474
|
+
lines.push("| 分组 | 出卡 | 点击 | 点击率 | 95% 区间 |");
|
|
475
|
+
lines.push("|---|---|---|---|---|");
|
|
476
|
+
/**
|
|
477
|
+
* @param {string} name
|
|
478
|
+
* @param {Record<string, any>} bucket
|
|
479
|
+
* @returns {void}
|
|
480
|
+
*/
|
|
481
|
+
const table = (name, bucket) => {
|
|
482
|
+
for (const key of Object.keys(bucket).sort()) {
|
|
483
|
+
const cell = bucket[key];
|
|
484
|
+
if (cell.n === 0) continue;
|
|
485
|
+
lines.push(
|
|
486
|
+
`| ${name} ${key} | ${cell.n} | ${cell.clicked} | ${pct(cell.ctr)} | ${ciText(cell.ci)} |`,
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
table("判定", metrics.byValue);
|
|
491
|
+
table("档位", metrics.byTier);
|
|
492
|
+
table("第几条", metrics.byIndex);
|
|
493
|
+
table("选项数", metrics.byOptionCount);
|
|
494
|
+
table("label 长度", metrics.byLabelBucket);
|
|
495
|
+
table("提示词版本", metrics.byPromptVersion);
|
|
496
|
+
lines.push("");
|
|
497
|
+
lines.push("## 5. 建议动作");
|
|
498
|
+
lines.push("");
|
|
499
|
+
if (list.length === 0) {
|
|
500
|
+
lines.push(
|
|
501
|
+
metrics.insufficient
|
|
502
|
+
? "- 样本不足,暂无建议。"
|
|
503
|
+
: "- 没有命中任何判读规则:当前设置与内容都在合理区间,保持不动,继续积累。",
|
|
504
|
+
);
|
|
505
|
+
} else {
|
|
506
|
+
for (const item of list) {
|
|
507
|
+
lines.push(`- **${item.code}**:${item.text}`);
|
|
508
|
+
lines.push(` - 动作:${item.action}`);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
lines.push("");
|
|
512
|
+
lines.push("---");
|
|
513
|
+
lines.push("");
|
|
514
|
+
lines.push("口径说明:忽略 = 出过的卡 − 点击 − 主动关闭(左连接,不写状态机);");
|
|
515
|
+
lines.push("曝光 = 卡片真正进入视口(IntersectionObserver 相交 ≥0.5 且持续 ≥300ms);");
|
|
516
|
+
lines.push("比例区间为 Wilson 95%,两组比较只在区间不重叠时才算有差别。");
|
|
517
|
+
return `${lines.join("\n")}\n`;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* 报表文件名(UTC 时间戳,同日多次生成不覆盖)。
|
|
522
|
+
* @param {number} at
|
|
523
|
+
* @returns {string}
|
|
524
|
+
*/
|
|
525
|
+
export function reportFileName(at) {
|
|
526
|
+
const iso = new Date(at).toISOString(); // 2026-11-14T10:20:30.000Z
|
|
527
|
+
const stamp = `${iso.slice(0, 10).replace(/-/gu, "")}-${iso.slice(11, 19).replace(/:/gu, "")}`;
|
|
528
|
+
return `report-${stamp}.md`;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* 写入报表并清理旧文件(保留最新 REPORT_KEEP 份,只在插件目录内)。
|
|
533
|
+
* @param {{ home: string, markdown: string, now?: number, keep?: number }} input
|
|
534
|
+
* @returns {Promise<string>} 写入的绝对路径
|
|
535
|
+
*/
|
|
536
|
+
export async function writeReport({ home, markdown, now, keep }) {
|
|
537
|
+
const dir = eventsDirOf(home);
|
|
538
|
+
const at = typeof now === "number" ? now : Date.now();
|
|
539
|
+
const limit =
|
|
540
|
+
Number.isSafeInteger(keep) && /** @type {number} */ (keep) > 0
|
|
541
|
+
? /** @type {number} */ (keep)
|
|
542
|
+
: REPORT_KEEP;
|
|
543
|
+
await mkdir(dir, { recursive: true });
|
|
544
|
+
const path = join(dir, reportFileName(at));
|
|
545
|
+
await writeFile(path, markdown, "utf8");
|
|
546
|
+
try {
|
|
547
|
+
const entries = (await readdir(dir))
|
|
548
|
+
.filter((name) => /^report-\d{8}-\d{6}\.md$/u.test(name))
|
|
549
|
+
.sort();
|
|
550
|
+
for (const stale of entries.slice(0, Math.max(0, entries.length - limit))) {
|
|
551
|
+
await rm(join(dir, stale), { force: true });
|
|
552
|
+
}
|
|
553
|
+
} catch (_error) {
|
|
554
|
+
// 清理失败不影响本次报告
|
|
555
|
+
}
|
|
556
|
+
return path;
|
|
557
|
+
}
|
package/lib/scheme.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// 版本位与出卡档位:一次生成产出「该不该继续」的三档判断,档位决定展示阈值。
|
|
3
|
+
//
|
|
4
|
+
// 为什么要单开一个文件:
|
|
5
|
+
// - 三档判定(must / could / no)是 v0.4 的核心语义,卡片记录、事件、报表、设置页都要用它,
|
|
6
|
+
// 散落在各处的字符串常量最容易写错一个字母且无人发现(`shouldShow` 是纯函数,可穷举单测)。
|
|
7
|
+
// - `SCHEME` / `PROMPT_VERSION` 是复盘的分组维度:改一次提示词或按钮规则就 +1,
|
|
8
|
+
// 报表按它对比改前改后(见 docs/adr/0013)。
|
|
9
|
+
|
|
10
|
+
/** 采集口径的方案号:报表只统计同一 scheme 的事件,避免跨版本混算。 */
|
|
11
|
+
export const SCHEME = "v0.4";
|
|
12
|
+
|
|
13
|
+
/** 提示词版本位:每次改提示词或按钮文案规则就 +1。 */
|
|
14
|
+
export const PROMPT_VERSION = 3;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 用户可选的出卡档位(显示阈值),从紧到松。
|
|
18
|
+
* - `must`:只出「明确需要」的卡
|
|
19
|
+
* - `could`:`must` + 「拿不准」也出(默认)
|
|
20
|
+
* - `any`:三档都出(含模型判为 `no` 的,实验档)
|
|
21
|
+
*/
|
|
22
|
+
export const CARD_TIERS = /** @type {const} */ (["must", "could", "any"]);
|
|
23
|
+
|
|
24
|
+
/** 三档判定的强弱排序:数值越大越"该出卡"。 */
|
|
25
|
+
export const VALUE_RANK = Object.freeze(
|
|
26
|
+
/** @type {Record<string, number>} */ ({ no: 0, could: 1, must: 2 }),
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 是不是合法档位。
|
|
31
|
+
* @param {unknown} value
|
|
32
|
+
* @returns {value is "must" | "could" | "any"}
|
|
33
|
+
*/
|
|
34
|
+
export function isCardTier(value) {
|
|
35
|
+
return value === "must" || value === "could" || value === "any";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 是不是合法三档判定。
|
|
40
|
+
* @param {unknown} value
|
|
41
|
+
* @returns {value is "must" | "could" | "no"}
|
|
42
|
+
*/
|
|
43
|
+
export function isCardValue(value) {
|
|
44
|
+
return value === "must" || value === "could" || value === "no";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 档位 → 显示阈值(越低越松)。未知档位按默认档位 `could` 处理:
|
|
49
|
+
* 设置文件被手改成非法值时,宁可回到默认行为,也不要静默变成"永不出卡"。
|
|
50
|
+
* @param {unknown} tier
|
|
51
|
+
* @returns {number}
|
|
52
|
+
*/
|
|
53
|
+
export function tierRank(tier) {
|
|
54
|
+
if (!isCardTier(tier)) return VALUE_RANK.could;
|
|
55
|
+
return tier === "any" ? VALUE_RANK.no : VALUE_RANK[tier];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 这一档判定在当前档位下要不要显示。
|
|
60
|
+
* 未知判定值一律不显示(防御:卡片内容不可信时宁可不渲染)。
|
|
61
|
+
* @param {unknown} value 三档判定
|
|
62
|
+
* @param {unknown} tier 档位
|
|
63
|
+
* @returns {boolean}
|
|
64
|
+
*/
|
|
65
|
+
export function shouldShow(value, tier) {
|
|
66
|
+
if (!isCardValue(value)) return false;
|
|
67
|
+
return VALUE_RANK[value] >= tierRank(tier);
|
|
68
|
+
}
|