@kangyishuai/dsh-next-step 0.4.2 → 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.
@@ -0,0 +1,53 @@
1
+ // @ts-check
2
+ // 会话日志 → 卡片 / 决策 / 揭示三张表(纯逻辑)。
3
+ //
4
+ // 背景(AUD-NS-06):`service.js` 的 `scanSession()` 是预算闸、去重闸、act/dismiss 守卫的
5
+ // 数据来源,但它依赖宿主的 `pluginRecordOf`,于是这段「哪些记录算数、同回合多条记录取哪条」
6
+ // 的逻辑一直没有单测。把「事件数组 + 取记录函数」作为入参之后,这里可以像 events/gates
7
+ // 一样被 `node --test` 覆盖;service 只负责把宿主的 session 适配成事件数组。
8
+ //
9
+ // 口径(三张表都是**每回合第一条赢**,与投影的折叠规则保持一致,避免「服务端算的和界面看到的」不一致):
10
+ // - card:`plugin:next-step/card`,结构不合法的记录直接忽略(旧版本/半条记录不能参与预算与去重);
11
+ // - decision:`plugin:next-step/decision`,只认带整数 turn 的记录(phase/index 的形状由写侧校验,
12
+ // 这里与旧实现的宽容度一致);
13
+ // - reveal:`plugin:next-step/reveal`,只认带整数 turn 的记录。
14
+
15
+ import { CARD_RECORD, DECISION_RECORD, isCardData, REVEAL_RECORD, readCard } from "./card-data.js";
16
+
17
+ /**
18
+ * @typedef {{ type?: string, data?: any } | undefined} PluginRecord
19
+ * @typedef {{
20
+ * cards: Map<number, any>,
21
+ * decisions: Map<number, any>,
22
+ * reveals: Map<number, true>,
23
+ * }} SessionRecords
24
+ */
25
+
26
+ /**
27
+ * 把一条会话事件的宿主记录入口注入进来,折叠成三张表。
28
+ * @param {any[]} events 会话事件数组(`session.snapshotEvents()` / `session.log`)
29
+ * @param {(event: any) => PluginRecord} recordOf 宿主的 `pluginRecordOf`(抽成参数才可单测)
30
+ * @returns {SessionRecords}
31
+ */
32
+ export function foldSessionRecords(events, recordOf) {
33
+ /** @type {Map<number, any>} */
34
+ const cards = new Map();
35
+ /** @type {Map<number, any>} */
36
+ const decisions = new Map();
37
+ /** @type {Map<number, true>} */
38
+ const reveals = new Map();
39
+ for (const event of Array.isArray(events) ? events : []) {
40
+ try {
41
+ const record = recordOf(event);
42
+ if (record === undefined || record === null) continue;
43
+ if (record.type === CARD_RECORD && isCardData(record.data)) {
44
+ if (!cards.has(record.data.turn)) cards.set(record.data.turn, readCard(record.data));
45
+ } else if (record.type === DECISION_RECORD && Number.isSafeInteger(record.data?.turn)) {
46
+ if (!decisions.has(record.data.turn)) decisions.set(record.data.turn, record.data);
47
+ } else if (record.type === REVEAL_RECORD && Number.isSafeInteger(record.data?.turn)) {
48
+ reveals.set(record.data.turn, true);
49
+ }
50
+ } catch (_error) {}
51
+ }
52
+ return { cards, decisions, reveals };
53
+ }
package/lib/settings.js CHANGED
@@ -9,20 +9,29 @@ import { isCardTier } from "./scheme.js";
9
9
  /** 设置文件名(位于数据根下,与 dsh-input-prediction 的落盘方式一致)。 */
10
10
  export const SETTINGS_FILE = "next-step.settings.json";
11
11
 
12
- /** 当前设置结构版本:v2 起有 schemaVersion / cardThreshold / telemetry。 */
13
- export const SETTINGS_SCHEMA_VERSION = 2;
12
+ /** 当前设置结构版本:v2 起有 schemaVersion / cardThreshold / telemetry;v3 调低预算默认值。 */
13
+ export const SETTINGS_SCHEMA_VERSION = 3;
14
14
 
15
15
  /** v1 时代的 `maxCardsPerSession` 默认值(迁移时只动这个值,见 migrateSettings)。 */
16
16
  export const LEGACY_MAX_CARDS = 30;
17
17
 
18
- /** 默认设置:卡片开、幽灵关、出卡档位 balanced、每会话最多 120 张卡、采集开。 */
18
+ /** v2 时代的 `maxCardsPerSession` 默认值(v3 起默认 60;等于 120 的旧文件跟着迁移)。 */
19
+ export const LEGACY_MAX_CARDS_V2 = 120;
20
+
21
+ /**
22
+ * 默认设置:卡片开、幽灵关、出卡档位 balanced、每会话最多 60 张卡、采集开。
23
+ *
24
+ * 预算默认值为什么是 60(AUD-NS-02):真实 14 天数据里单会话峰值 80 张、≥20 张的 23 个会话
25
+ * 占了全部卡片的 82%,而 120 这个上限从来没有被碰到(budget 闸 0 命中)——旋钮没人调,
26
+ * 默认值就决定了实际体验。60 只裁掉最长会话的尾部,配合「旧卡折叠成一行」一起收噪音。
27
+ */
19
28
  export const DEFAULT_SETTINGS = Object.freeze({
20
29
  schemaVersion: SETTINGS_SCHEMA_VERSION,
21
30
  enabled: true,
22
31
  card: true,
23
32
  ghost: false,
24
33
  cardThreshold: "could",
25
- maxCardsPerSession: 120,
34
+ maxCardsPerSession: 60,
26
35
  pauseUntil: 0,
27
36
  timeoutMs: 20000,
28
37
  maxTokens: 500,
@@ -94,12 +103,14 @@ export function normalizeSettings(raw) {
94
103
  }
95
104
 
96
105
  /**
97
- * v1 → v2 迁移(纯函数,便于单测)。
106
+ * 旧版设置 → 当前版本(纯函数,便于单测)。
98
107
  *
99
- * 只做两件事,且都不覆盖用户的显式选择:
100
- * 1. 文件没有 `schemaVersion` 时补上版本号(一次性写回,之后不再判断);
101
- * 2. `maxCardsPerSession` **恰好等于 v1 默认值 30** 时抬到新的默认 120。
102
- * 显式写过 `schemaVersion: 2` 的文件即使值是 30 也不动——那说明是用户自己设的。
108
+ * 原则:**只迁移「恰好等于旧默认值」的字段**,用户显式设过的值一律不动。
109
+ * 1. 文件没有 `schemaVersion`(v1):一次性补上版本号并写回;
110
+ * `maxCardsPerSession` 恰好是 v1 默认 30 时换成当前默认值。
111
+ * 2. `schemaVersion: 2`(v2 时代):`maxCardsPerSession` 恰好是 v2 默认 120 时换成当前默认值
112
+ * (60,见 DEFAULT_SETTINGS 的注释);其它值(含用户显式设的 120 之外的值)不动。
113
+ * 3. 已是当前版本:只做规整,不迁移。
103
114
  * @param {unknown} raw 文件内容
104
115
  * @returns {{ settings: typeof DEFAULT_SETTINGS, migrated: boolean }}
105
116
  */
@@ -111,7 +122,14 @@ export function migrateSettings(raw) {
111
122
  }
112
123
  /** @type {any} */
113
124
  const patched = { ...input };
114
- if (patched.maxCardsPerSession === LEGACY_MAX_CARDS) {
125
+ const from =
126
+ Number.isSafeInteger(input.schemaVersion) && /** @type {number} */ (input.schemaVersion) >= 1
127
+ ? /** @type {number} */ (input.schemaVersion)
128
+ : 1;
129
+ if (from < 2 && patched.maxCardsPerSession === LEGACY_MAX_CARDS) {
130
+ patched.maxCardsPerSession = DEFAULT_SETTINGS.maxCardsPerSession;
131
+ }
132
+ if (from === 2 && patched.maxCardsPerSession === LEGACY_MAX_CARDS_V2) {
115
133
  patched.maxCardsPerSession = DEFAULT_SETTINGS.maxCardsPerSession;
116
134
  }
117
135
  return { settings: normalizeSettings(patched), migrated: true };
@@ -0,0 +1,49 @@
1
+ // @ts-check
2
+ // 「这条 `session/event` 该不该进入出卡流程」的判定:纯函数,可直接单测。
3
+ //
4
+ // 背景(AUD-NS-05):`service.js` 的 `handleSessionEvent` 原来在进入闸之前就
5
+ // `if (reason.kind !== "completed") return;` 直接返回,于是 `cheapSkipReason` 里的
6
+ // `turn-error` 分支**永远为假**——8 道闸里只有 `ok` / `short-answer` 两道会真的落盘。
7
+ // 真实数据 640 条 turn 事件里 `turn-error` 一次都没出现,维护者看闸门分布会误以为
8
+ // 「其它闸从不触发」,其实它们在事件层根本不可观测。
9
+ //
10
+ // 修法:completed 的过滤下移到闸之后——非 completed 的回合也走一次便宜闸(命中 `turn-error`
11
+ // 并落一条 turn 事件,仍然**不调模型**)。这个纯函数只负责「要不要进流程」,
12
+ // 不再关心 `reason.kind` 是什么;把「哪些原因该被统计」的决定留给 `cheapSkipReason`。
13
+
14
+ /**
15
+ * 回合结束原因的归一化:`reason.kind` **缺失 / 非字符串 / 空串**一律当成 `"unknown"`。
16
+ *
17
+ * 为什么必须归一化(而不是靠 `cheapSkipReason` 里那句 `typeof endKind === "string"`):
18
+ * AUD-NS-05 把 completed 过滤下移之后,`turn/end` 缺 `reason` 的事件也会进流程。
19
+ * 如果不归一化,`endKind === undefined` 会让 `cheapSkipReason` 的 `turn-error` 分支不成立,
20
+ * 于是一个「不知道原因」的回合反而可能过闸并调模型——这违背「只有正常结束的回合才生成」。
21
+ * 归一化成 `"unknown"` 之后,它必然命中 `turn-error`(不调模型、只落遥测)。
22
+ * @param {unknown} reasonKind
23
+ * @returns {string}
24
+ */
25
+ export function endKindOf(reasonKind) {
26
+ const text = typeof reasonKind === "string" ? reasonKind.trim() : "";
27
+ return text === "" ? "unknown" : text;
28
+ }
29
+
30
+ /**
31
+ * @param {{ subagent?: boolean, turn?: unknown, reasonKind?: unknown }} input
32
+ * @returns {{ consider: boolean, turn: number, endKind: string, reason?: "subagent" | "no-turn" }}
33
+ * `consider === true` 时 `turn` 是安全的整数回合号,`endKind` 已归一化。
34
+ */
35
+ export function turnEndDisposition(input) {
36
+ // 子代理会话按设计不出卡,也不落事件(它们不是用户可见的对话)。
37
+ if (input.subagent === true) {
38
+ return { consider: false, turn: -1, endKind: endKindOf(input.reasonKind), reason: "subagent" };
39
+ }
40
+ const turn = input.turn;
41
+ if (!Number.isSafeInteger(turn)) {
42
+ return { consider: false, turn: -1, endKind: endKindOf(input.reasonKind), reason: "no-turn" };
43
+ }
44
+ return {
45
+ consider: true,
46
+ turn: /** @type {number} */ (turn),
47
+ endKind: endKindOf(input.reasonKind),
48
+ };
49
+ }
@@ -0,0 +1,25 @@
1
+ // @ts-check
2
+ // 一次**成功**的生成结算成什么样:要不要显示、事件里的 gate 写哪个值。
3
+ //
4
+ // 为什么单独一个纯函数(AUD-NS-01):`shown` 是写卡时冻结的展示标记,三种输入共同决定它——
5
+ // 用户档位(`shouldShow`)、手动补生成(`force`)、以及这次生成是不是已经被更新的回合取代
6
+ // (`superseded`)。这三种组合以前散在 service.js 的一个三元表达式里,没法单测;而「superseded
7
+ // 优先于 force」正是「下一轮开始后不把过期卡塞回对话流(候选不占预算 / 不进曝光 / 不可揭示)」的
8
+ // 关键口径。注意:这一改动会让在途调用跑完,输出 token 比原来中止时更多,不是省钱优化。
9
+ //
10
+ // gate 是事件里的闸门分类:`ok` 表示正常出卡/被档位抑制;`superseded` 表示生成跑完了但这一轮
11
+ // 已经翻过去了,候选以 `shown:false` 落库(报表的「闸门分布」里能直接看到这一类的量)。
12
+
13
+ import { shouldShow } from "./scheme.js";
14
+
15
+ /**
16
+ * @param {{ value: unknown, tier: unknown, force?: boolean, superseded?: boolean }} input
17
+ * @returns {{ shown: boolean, gate: "ok" | "superseded" }}
18
+ */
19
+ export function planCardWrite(input) {
20
+ // 已经翻过去的回合:卡片属于历史轮次,显示出来只会变成「旧卡」噪音。
21
+ // 优先级高于 force——手动补生成期间用户又开始下一轮,说明这张卡已经不重要了。
22
+ if (input.superseded === true) return { shown: false, gate: "superseded" };
23
+ const shown = input.force === true ? true : shouldShow(input.value, input.tier);
24
+ return { shown, gate: "ok" };
25
+ }
package/locale/en.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "nextStep": {
3
3
  "card.aria": "Next-step suggestion",
4
+ "card.expired": "Earlier turn",
4
5
  "summary.expand": "Show more",
5
6
  "summary.collapse": "Show less",
6
7
  "pill.aria": "Send: {label}",
@@ -12,6 +13,12 @@
12
13
  "error.missing": "That suggestion is no longer available",
13
14
  "error.disabled": "The plugin is disabled",
14
15
  "error.failed": "Something went wrong. Try again.",
16
+ "error.aborted": "Generation timed out or was cancelled. Try again.",
17
+ "error.timeout": "Generation timed out. Try again.",
18
+ "error.cancelled": "Generation was cancelled. Try again.",
19
+ "error.schema": "Generation failed: the model output did not match the card format",
20
+ "error.llm": "Generation failed: the model call errored",
21
+ "error.route": "Generation failed: no model route available",
15
22
  "error.send": "Could not fill the composer",
16
23
  "settings.title": "Next-step suggestions",
17
24
  "settings.desc": "Judges whether a turn is worth continuing; when it is, adds a status summary, a question and 1–2 concrete next steps (one extra model call).",
@@ -39,8 +46,14 @@
39
46
  "error.exists": "This turn already has a card",
40
47
  "error.inflight": "Already generating; try again in a moment",
41
48
  "error.noTurn": "No completed turn to generate a card for",
49
+ "error.ungrounded": "The generated card did not refer to this conversation; discarded",
50
+ "error.noContext": "No conversation content to generate a card from",
42
51
  "retry.generate": "Generate a next-step card",
43
52
  "retry.generating": "Generating…",
53
+ "retry.retry": "Try again",
54
+ "retry.waiting": "Still generating in the background…",
55
+ "retry.detail": "Details",
56
+ "retry.hideDetail": "Hide details",
44
57
  "settings.nav": "Next-step suggestions",
45
58
  "settings.tier": "Card frequency",
46
59
  "settings.tier.must": "Strict",
package/locale/zh.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "nextStep": {
3
3
  "card.aria": "下一步建议",
4
+ "card.expired": "更早的一轮",
4
5
  "summary.expand": "展开",
5
6
  "summary.collapse": "收起",
6
7
  "pill.aria": "发送:{label}",
@@ -12,6 +13,12 @@
12
13
  "error.missing": "找不到这条建议",
13
14
  "error.disabled": "插件已停用",
14
15
  "error.failed": "操作失败,请重试",
16
+ "error.aborted": "生成超时或被取消,请重试",
17
+ "error.timeout": "生成超时,可重试",
18
+ "error.cancelled": "生成已取消,可重试",
19
+ "error.schema": "生成失败:模型输出不符合卡片格式",
20
+ "error.llm": "生成失败:模型调用出错",
21
+ "error.route": "生成失败:没有可用的模型路由",
15
22
  "error.send": "没能把建议写进输入框",
16
23
  "settings.title": "下一步建议",
17
24
  "settings.desc": "答完后判断是否值得继续;需要时生成一张含摘要、提问与 1–2 条具体建议的决策卡(一次额外模型调用)。",
@@ -39,8 +46,14 @@
39
46
  "error.exists": "这一轮已经有卡片了",
40
47
  "error.inflight": "正在生成,请稍候",
41
48
  "error.noTurn": "没有可生成建议的回合",
49
+ "error.ungrounded": "生成的卡片与这段对话无关,已丢弃",
50
+ "error.noContext": "没有可用的对话内容,未生成卡片",
42
51
  "retry.generate": "生成下一步建议",
43
52
  "retry.generating": "正在生成…",
53
+ "retry.retry": "重试",
54
+ "retry.waiting": "后台正在生成,请稍候…",
55
+ "retry.detail": "查看详情",
56
+ "retry.hideDetail": "收起详情",
44
57
  "settings.nav": "下一步建议",
45
58
  "settings.tier": "出卡频率",
46
59
  "settings.tier.must": "严格",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kangyishuai/dsh-next-step",
3
- "version": "0.4.2",
3
+ "version": "0.5.0",
4
4
  "description": "Next-step decision card under each completed answer, plus an optional greyed-out next-prompt hint in the composer",
5
5
  "keywords": [
6
6
  "dsh",