@kangyishuai/dsh-next-step 0.1.1 → 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/README.md +22 -12
- package/README.zh-CN.md +22 -12
- package/lib/client.js +401 -89
- package/lib/projection.js +3 -4
- package/lib/prompt.js +41 -25
- package/lib/queue-gate.js +21 -0
- package/lib/service.js +176 -13
- package/locale/en.json +12 -2
- package/locale/zh.json +12 -2
- package/package.json +1 -1
package/lib/projection.js
CHANGED
|
@@ -11,7 +11,8 @@ export const DECISION_RECORD = "plugin:next-step/decision";
|
|
|
11
11
|
export const PROJECTION_KEY = "nextStep";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
|
-
* 一条 option 是否合法(label
|
|
14
|
+
* 一条 option 是否合法(label 既是按钮文案,也是点击后原样发出的用户消息;
|
|
15
|
+
* v0.2 起不再有隐藏的 text 字段——旧记录里残留的 text 会被忽略)。
|
|
15
16
|
* @param {any} option
|
|
16
17
|
* @returns {boolean}
|
|
17
18
|
*/
|
|
@@ -22,9 +23,7 @@ function isOption(option) {
|
|
|
22
23
|
typeof option.stance === "string" &&
|
|
23
24
|
option.stance.trim() !== "" &&
|
|
24
25
|
typeof option.label === "string" &&
|
|
25
|
-
option.label.trim() !== ""
|
|
26
|
-
typeof option.text === "string" &&
|
|
27
|
-
option.text.trim() !== ""
|
|
26
|
+
option.label.trim() !== ""
|
|
28
27
|
);
|
|
29
28
|
}
|
|
30
29
|
|
package/lib/prompt.js
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
// 提示词与输出解析:纯函数,不 import 宿主包(单测可以直接跑)。
|
|
3
|
-
//
|
|
3
|
+
// 一次调用同时产出四样东西:是否需要继续、判断依据、摘要 + 提问 + 两条动作、一条给输入框的幽灵提示。
|
|
4
|
+
//
|
|
5
|
+
// 设计要点(v0.2,见 ADR 0009 的增补):
|
|
6
|
+
// - `needed` 由模型自评:只有这一轮结束后**真的值得继续**时才出卡;否则只返回判断与理由。
|
|
7
|
+
// 没有这一步时,每轮都硬出一张卡,答得越完整越吵。
|
|
8
|
+
// - `label` 就是点击后原样发送的用户消息(WYSIWYG),不再有隐藏的 `text` 字段:
|
|
9
|
+
// 用户看到什么、发出去的就是什么,日志、回显、消息三者一致。
|
|
4
10
|
|
|
5
11
|
/** 摘要最大字符数(1–3 句,超出视为模型跑偏)。 */
|
|
6
12
|
export const SUMMARY_MAX = 400;
|
|
7
13
|
/** 提问最大字符数。 */
|
|
8
14
|
export const QUESTION_MAX = 200;
|
|
9
|
-
/**
|
|
10
|
-
export const LABEL_MAX =
|
|
11
|
-
/**
|
|
12
|
-
export const
|
|
15
|
+
/** 按钮文案最大字符数(它同时是要发送的消息,允许两行)。 */
|
|
16
|
+
export const LABEL_MAX = 60;
|
|
17
|
+
/** 按钮文案最少字符数(太短多半是「继续」这种悬空指令)。 */
|
|
18
|
+
export const LABEL_MIN = 4;
|
|
19
|
+
/** 判断依据最大字符数(会显示在诊断里)。 */
|
|
20
|
+
export const REASON_MAX = 160;
|
|
13
21
|
/** 幽灵提示最大字符数。 */
|
|
14
22
|
export const NEXT_PROMPT_MAX = 120;
|
|
15
23
|
/** stance 最大字符数。 */
|
|
@@ -52,7 +60,7 @@ export function renderTranscript(messages, options = {}) {
|
|
|
52
60
|
}
|
|
53
61
|
|
|
54
62
|
/**
|
|
55
|
-
*
|
|
63
|
+
* 系统提示词:只做一件事——判断这一轮是否值得继续,并把对话压成一张决策卡,严格 JSON。
|
|
56
64
|
* @param {{ locale?: string }} [options]
|
|
57
65
|
* @returns {string}
|
|
58
66
|
*/
|
|
@@ -60,22 +68,27 @@ export function buildSystemPrompt(options = {}) {
|
|
|
60
68
|
const locale =
|
|
61
69
|
typeof options.locale === "string" && options.locale !== "" ? options.locale : "auto";
|
|
62
70
|
return [
|
|
63
|
-
"
|
|
71
|
+
"你是一个「下一步决策卡」生成器。读下面这段对话,先判断用户读完最后一条回复后是否真的需要继续,再给出判断依据与卡片内容。",
|
|
64
72
|
"只输出一个 JSON 对象,不要输出任何解释、不要用代码围栏。",
|
|
65
73
|
"",
|
|
66
74
|
"JSON 结构:",
|
|
67
|
-
'{ "summary": "…", "question": "…", "options": [ { "stance": "…", "label": "…"
|
|
75
|
+
'{ "needed": true|false, "reason": "…", "summary": "…", "question": "…", "options": [ { "stance": "…", "label": "…" }, { "stance": "…", "label": "…" } ], "nextPrompt": "…" }',
|
|
68
76
|
"",
|
|
69
77
|
"要求:",
|
|
70
|
-
"1.
|
|
71
|
-
"
|
|
72
|
-
|
|
73
|
-
`
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
`
|
|
77
|
-
|
|
78
|
-
|
|
78
|
+
"1. needed:布尔值,表示「这一轮结束后,用户是否真的值得继续」。",
|
|
79
|
+
" - false:任务已完整收尾、没有自然的下一步;最后的回答已经向用户提出了明确的问题或请求;用户已表示结束(如「不用了」「谢谢」);继续只会重复劳动。",
|
|
80
|
+
" - true:存在需要用户拍板的方向,或存在明显值得继续做下去的下一步。",
|
|
81
|
+
`2. reason:一句话说明 needed 的判断依据,${REASON_MAX} 字以内,直接说事实,不要客套。`,
|
|
82
|
+
"3. summary:1–3 句客观陈述当前状态(做到哪、卡在哪、还差什么),不评价、不客套。",
|
|
83
|
+
"4. question:一句向用户确认方向的问题,必须以问号结尾。",
|
|
84
|
+
`5. options:恰好 2 条,方向互斥(例如「按方案 A 改」vs「先补测试」),不能是同一件事的两种说法。`,
|
|
85
|
+
" - stance:英文小写短语标识立场,两条必须不同(如 verify / proceed / harder / alternative)。",
|
|
86
|
+
` - label:按钮文案,也就是用户点一下后**原样发送**给助手的用户消息;${LABEL_MIN}–${LABEL_MAX} 字,动词开头,单独发出去就能干活。`,
|
|
87
|
+
" 不要用「继续」「好的」这类悬空短语,不要写成问句,不要把解释塞进 label。",
|
|
88
|
+
`6. nextPrompt:一句用户口吻的下一句输入建议,${NEXT_PROMPT_MAX} 字以内;关键信息放前 40 字内。`,
|
|
89
|
+
`7. 语言:跟随对话中用户使用的语言(当前界面语言 ${locale},仅供参考)。`,
|
|
90
|
+
"8. needed 为 false 时也要按上面的结构给出 summary / question / options —— 用户可能手动请求这张卡。",
|
|
91
|
+
"9. 信息不足时,两个方向应当是「先补信息」与「按现有信息推进」,不要编造事实。",
|
|
79
92
|
].join("\n");
|
|
80
93
|
}
|
|
81
94
|
|
|
@@ -98,7 +111,7 @@ export function buildMessages(input = {}) {
|
|
|
98
111
|
/**
|
|
99
112
|
* 从模型输出里抠出 JSON 并校验;任何一处不合法就返回 null(整条丢弃,绝不截断)。
|
|
100
113
|
* @param {unknown} raw
|
|
101
|
-
* @returns {{ summary: string, question: string, options: { stance: string, label: string
|
|
114
|
+
* @returns {{ needed: boolean, reason: string, summary: string, question: string, options: { stance: string, label: string }[], nextPrompt?: string } | null}
|
|
102
115
|
*/
|
|
103
116
|
export function parseSuggestion(raw) {
|
|
104
117
|
if (typeof raw !== "string") return null;
|
|
@@ -122,6 +135,11 @@ export function parseSuggestion(raw) {
|
|
|
122
135
|
}
|
|
123
136
|
if (parsed === null || typeof parsed !== "object") return null;
|
|
124
137
|
|
|
138
|
+
if (typeof parsed.needed !== "boolean") return null;
|
|
139
|
+
const needed = parsed.needed;
|
|
140
|
+
const reason = typeof parsed.reason === "string" ? parsed.reason.trim() : "";
|
|
141
|
+
if (reason === "" || reason.length > REASON_MAX) return null;
|
|
142
|
+
|
|
125
143
|
const summary = typeof parsed.summary === "string" ? parsed.summary.trim() : "";
|
|
126
144
|
const question = typeof parsed.question === "string" ? parsed.question.trim() : "";
|
|
127
145
|
if (summary === "" || summary.length > SUMMARY_MAX) return null;
|
|
@@ -129,22 +147,20 @@ export function parseSuggestion(raw) {
|
|
|
129
147
|
|
|
130
148
|
const rawOptions = Array.isArray(parsed.options) ? parsed.options : [];
|
|
131
149
|
if (rawOptions.length !== 2) return null;
|
|
132
|
-
/** @type {{ stance: string, label: string
|
|
150
|
+
/** @type {{ stance: string, label: string }[]} */
|
|
133
151
|
const options = [];
|
|
134
152
|
for (const option of rawOptions) {
|
|
135
153
|
if (option === null || typeof option !== "object") return null;
|
|
136
154
|
const stance = typeof option.stance === "string" ? option.stance.trim().toLowerCase() : "";
|
|
137
155
|
const label = typeof option.label === "string" ? option.label.trim().replace(/\s+/gu, " ") : "";
|
|
138
|
-
const actionText = typeof option.text === "string" ? option.text.trim() : "";
|
|
139
156
|
if (stance === "" || stance.length > STANCE_MAX) return null;
|
|
140
|
-
if (label
|
|
141
|
-
|
|
142
|
-
options.push({ stance, label, text: actionText });
|
|
157
|
+
if (label.length < LABEL_MIN || label.length > LABEL_MAX) return null;
|
|
158
|
+
options.push({ stance, label });
|
|
143
159
|
}
|
|
144
160
|
if (options[0].stance === options[1].stance) return null;
|
|
145
161
|
|
|
146
|
-
/** @type {{ summary: string, question: string, options: { stance: string, label: string
|
|
147
|
-
const result = { summary, question, options };
|
|
162
|
+
/** @type {{ needed: boolean, reason: string, summary: string, question: string, options: { stance: string, label: string }[], nextPrompt?: string }} */
|
|
163
|
+
const result = { needed, reason, summary, question, options };
|
|
148
164
|
if (parsed.nextPrompt !== undefined) {
|
|
149
165
|
const nextPrompt = typeof parsed.nextPrompt === "string" ? parsed.nextPrompt.trim() : "";
|
|
150
166
|
if (nextPrompt.length > NEXT_PROMPT_MAX) return null;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// 「用户已经在排队,就别再塞建议」的闸——纯函数,方便单测。
|
|
3
|
+
//
|
|
4
|
+
// 依据:官方 `dsh-action-cards` 的 `queuedMessageLimit = 2`(用户已排 ≥2 条消息时不再出卡),
|
|
5
|
+
// 避免「你已经排了两条,它还弹一张卡让你再点一条」这种噪音。宿主的排队消息可读面是
|
|
6
|
+
// `agent.inbox.nextStep` / `agent.inbox.nextTurn`(见 docs/runtime/0.2.2-rc.1.md §9.6)。
|
|
7
|
+
|
|
8
|
+
/** 用户已排队多少条消息时就不再出卡(0 = 关闭该闸)。 */
|
|
9
|
+
export const QUEUED_MESSAGE_LIMIT = 2;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 该不该因为「用户已在排队」而跳过生成。
|
|
13
|
+
* @param {number} queued 当前排队(next-turn + next-step)的消息条数
|
|
14
|
+
* @param {number} [limit] 闸值;0 表示不启用
|
|
15
|
+
* @returns {boolean} true = 跳过
|
|
16
|
+
*/
|
|
17
|
+
export function shouldSkipForQueue(queued, limit = QUEUED_MESSAGE_LIMIT) {
|
|
18
|
+
if (!Number.isFinite(limit) || limit <= 0) return false;
|
|
19
|
+
if (!Number.isFinite(queued) || queued <= 0) return false;
|
|
20
|
+
return queued >= limit;
|
|
21
|
+
}
|
package/lib/service.js
CHANGED
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
nextStepProjection,
|
|
21
21
|
} from "./projection.js";
|
|
22
22
|
import { buildMessages, buildSystemPrompt, parseSuggestion, renderTranscript } from "./prompt.js";
|
|
23
|
+
import { shouldSkipForQueue } from "./queue-gate.js";
|
|
23
24
|
import { DEFAULT_SETTINGS, loadSettings, saveSettings } from "./settings.js";
|
|
24
25
|
|
|
25
26
|
/**
|
|
@@ -99,6 +100,28 @@ function isSubagentSession(session) {
|
|
|
99
100
|
);
|
|
100
101
|
}
|
|
101
102
|
|
|
103
|
+
/**
|
|
104
|
+
* 读「用户已排队」的消息条数:next-turn + next-step。
|
|
105
|
+
* 读不到就返回 0(宁可不拦,也不因为宿主形状变化误杀)。
|
|
106
|
+
* @param {any} ctx
|
|
107
|
+
* @param {string} sessionId
|
|
108
|
+
* @returns {number}
|
|
109
|
+
*/
|
|
110
|
+
function queuedCountOf(ctx, sessionId) {
|
|
111
|
+
const inbox = serviceOf(ctx, "agents")?.get?.(sessionId)?.inbox;
|
|
112
|
+
if (inbox === undefined || inbox === null) return 0;
|
|
113
|
+
let count = 0;
|
|
114
|
+
try {
|
|
115
|
+
const nextStep = inbox.nextStep;
|
|
116
|
+
const nextTurn = inbox.nextTurn;
|
|
117
|
+
if (Array.isArray(nextStep)) count += nextStep.length;
|
|
118
|
+
if (Array.isArray(nextTurn)) count += nextTurn.length;
|
|
119
|
+
} catch (_error) {
|
|
120
|
+
return 0;
|
|
121
|
+
}
|
|
122
|
+
return count;
|
|
123
|
+
}
|
|
124
|
+
|
|
102
125
|
/**
|
|
103
126
|
* 读会话日志里的卡片与决策(全量,用于预算与去重;渲染走投影,不读这里)。
|
|
104
127
|
* @param {any} session
|
|
@@ -124,6 +147,29 @@ function scanSession(session) {
|
|
|
124
147
|
return { cards, decisions };
|
|
125
148
|
}
|
|
126
149
|
|
|
150
|
+
/**
|
|
151
|
+
* 最近一个正常结束的回合(手动补生成用)。
|
|
152
|
+
* @param {any} session
|
|
153
|
+
* @returns {{ turn: number, seq: number } | undefined}
|
|
154
|
+
*/
|
|
155
|
+
function latestCompletedTurn(session) {
|
|
156
|
+
let events = [];
|
|
157
|
+
if (typeof session?.snapshotEvents === "function") events = session.snapshotEvents();
|
|
158
|
+
else if (Array.isArray(session?.log)) events = session.log;
|
|
159
|
+
const list = Array.isArray(events) ? events : [];
|
|
160
|
+
for (let index = list.length - 1; index >= 0; index -= 1) {
|
|
161
|
+
const event = list[index];
|
|
162
|
+
if (event?.type !== "turn/end") continue;
|
|
163
|
+
if (event?.data?.reason?.kind !== "completed") continue;
|
|
164
|
+
if (!Number.isSafeInteger(event?.data?.turn)) continue;
|
|
165
|
+
return {
|
|
166
|
+
turn: event.data.turn,
|
|
167
|
+
seq: Number.isSafeInteger(event?.seq) ? event.seq : 0,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
172
|
+
|
|
127
173
|
/** 目标:一次短补全。 */
|
|
128
174
|
export class NextStepService extends TypertRemoteService {
|
|
129
175
|
/**
|
|
@@ -135,10 +181,23 @@ export class NextStepService extends TypertRemoteService {
|
|
|
135
181
|
this.config = config ?? {};
|
|
136
182
|
this.logger = ctx?.logger ?? console;
|
|
137
183
|
this.dataRoot = resolveDataRoot(this.config);
|
|
138
|
-
/** @type {{ generated: number, failed: number, skipped: number, acted: number, dismissed: number }} */
|
|
139
|
-
this.counters = {
|
|
184
|
+
/** @type {{ generated: number, failed: number, skipped: number, skippedByModel: number, acted: number, dismissed: number }} */
|
|
185
|
+
this.counters = {
|
|
186
|
+
generated: 0,
|
|
187
|
+
failed: 0,
|
|
188
|
+
skipped: 0,
|
|
189
|
+
skippedByModel: 0,
|
|
190
|
+
acted: 0,
|
|
191
|
+
dismissed: 0,
|
|
192
|
+
};
|
|
140
193
|
/** 最近一次失败原因(诊断面板用)。 */
|
|
141
194
|
this.lastError = "";
|
|
195
|
+
/**
|
|
196
|
+
* 最近一次「这一轮没出卡」的原因——「我觉得该出卡却没出」时,先看这里。
|
|
197
|
+
* kind: paused | queued | budget | model | failed
|
|
198
|
+
* @type {{ kind: string, reason: string, at: number } | null}
|
|
199
|
+
*/
|
|
200
|
+
this.lastSkip = null;
|
|
142
201
|
/** 每会话在途生成(abort 用)。 @type {Map<string, AbortController>} */
|
|
143
202
|
this.inflight = new Map();
|
|
144
203
|
/** 设置缓存(1s TTL),使「改文件即生效」成立又不必每次读盘。 */
|
|
@@ -199,7 +258,8 @@ export class NextStepService extends TypertRemoteService {
|
|
|
199
258
|
}
|
|
200
259
|
|
|
201
260
|
/**
|
|
202
|
-
*
|
|
261
|
+
* 自动出卡:先过便宜的闸,再让模型自评 needed。
|
|
262
|
+
* 关闭开关 = 到这里直接返回,不发请求。
|
|
203
263
|
* @param {any} session
|
|
204
264
|
* @param {number} turn
|
|
205
265
|
* @param {any} event
|
|
@@ -208,14 +268,39 @@ export class NextStepService extends TypertRemoteService {
|
|
|
208
268
|
async maybeGenerate(session, turn, event) {
|
|
209
269
|
const settings = await this.currentSettings();
|
|
210
270
|
if (!settings.enabled || !settings.card) return;
|
|
211
|
-
if (settings.pauseUntil > Date.now()) return;
|
|
212
271
|
const sessionId = session.id;
|
|
272
|
+
if (settings.pauseUntil > Date.now()) {
|
|
273
|
+
this.markSkip("paused", "generation is paused");
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
// 用户已经排了 ≥2 条消息就不再出卡(官方 action-cards 的同一条规则)。
|
|
277
|
+
if (shouldSkipForQueue(queuedCountOf(this.ctx, sessionId))) {
|
|
278
|
+
this.markSkip("queued", "the user already has queued messages");
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
213
281
|
const { cards } = scanSession(session);
|
|
214
282
|
if (cards.has(turn)) return;
|
|
215
283
|
if (cards.size >= settings.maxCardsPerSession) {
|
|
216
|
-
this.
|
|
284
|
+
this.markSkip("budget", "per-session card budget reached");
|
|
217
285
|
return;
|
|
218
286
|
}
|
|
287
|
+
await this.writeCardFor(session, settings, {
|
|
288
|
+
turn,
|
|
289
|
+
seq: Number.isSafeInteger(event?.seq) ? event.seq : 0,
|
|
290
|
+
force: false,
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* 跑一次模型调用并为该轮写卡(自动门控与手动补生成共用)。
|
|
296
|
+
* @param {any} session
|
|
297
|
+
* @param {any} settings
|
|
298
|
+
* @param {{ turn: number, seq: number, force: boolean }} options force=true 时忽略模型的 needed=false
|
|
299
|
+
* @returns {Promise<{ ok: true, card: any } | { ok: false, reason: string }>}
|
|
300
|
+
*/
|
|
301
|
+
async writeCardFor(session, settings, options) {
|
|
302
|
+
const sessionId = session.id;
|
|
303
|
+
const turn = options.turn;
|
|
219
304
|
if (this.inflight.has(sessionId)) this.abortInflight(sessionId);
|
|
220
305
|
const controller = new AbortController();
|
|
221
306
|
this.inflight.set(sessionId, controller);
|
|
@@ -225,16 +310,23 @@ export class NextStepService extends TypertRemoteService {
|
|
|
225
310
|
);
|
|
226
311
|
try {
|
|
227
312
|
const result = await this.generate(session, settings, controller.signal);
|
|
228
|
-
if (controller.signal.aborted) return;
|
|
229
|
-
if (this.inflight.get(sessionId) !== controller) return;
|
|
313
|
+
if (controller.signal.aborted) return { ok: false, reason: "aborted" };
|
|
314
|
+
if (this.inflight.get(sessionId) !== controller) return { ok: false, reason: "aborted" };
|
|
230
315
|
if (result === null) {
|
|
231
316
|
this.counters.failed += 1;
|
|
232
317
|
this.lastError = "model output did not match the card schema";
|
|
233
|
-
|
|
318
|
+
this.markFailure(this.lastError);
|
|
319
|
+
return { ok: false, reason: "schema" };
|
|
320
|
+
}
|
|
321
|
+
// 模型自评「这一轮不需要继续」:自动门控下不出卡,但把理由留给诊断。
|
|
322
|
+
if (result.suggestion.needed === false && options.force !== true) {
|
|
323
|
+
this.counters.skippedByModel += 1;
|
|
324
|
+
this.markSkip("model", result.suggestion.reason);
|
|
325
|
+
return { ok: false, reason: "not-needed" };
|
|
234
326
|
}
|
|
235
327
|
const card = {
|
|
236
328
|
turn,
|
|
237
|
-
seq:
|
|
329
|
+
seq: options.seq,
|
|
238
330
|
summary: result.suggestion.summary,
|
|
239
331
|
question: result.suggestion.question,
|
|
240
332
|
options: result.suggestion.options,
|
|
@@ -247,23 +339,53 @@ export class NextStepService extends TypertRemoteService {
|
|
|
247
339
|
if (!isCardData(card)) {
|
|
248
340
|
this.counters.failed += 1;
|
|
249
341
|
this.lastError = "generated card failed validation";
|
|
250
|
-
|
|
342
|
+
this.markFailure(this.lastError);
|
|
343
|
+
return { ok: false, reason: "schema" };
|
|
251
344
|
}
|
|
252
345
|
appendPluginRecord(session, CARD_RECORD, card);
|
|
253
346
|
this.counters.generated += 1;
|
|
254
347
|
this.lastError = "";
|
|
348
|
+
return { ok: true, card };
|
|
255
349
|
} catch (error) {
|
|
256
350
|
if (!controller.signal.aborted) {
|
|
257
351
|
this.counters.failed += 1;
|
|
258
352
|
this.lastError = error instanceof Error ? error.message : String(error);
|
|
353
|
+
this.markFailure(this.lastError);
|
|
259
354
|
this.warn("card generation failed", error);
|
|
260
355
|
}
|
|
356
|
+
return { ok: false, reason: "failed" };
|
|
261
357
|
} finally {
|
|
262
358
|
clearTimeout(timer);
|
|
263
359
|
if (this.inflight.get(sessionId) === controller) this.inflight.delete(sessionId);
|
|
264
360
|
}
|
|
265
361
|
}
|
|
266
362
|
|
|
363
|
+
/**
|
|
364
|
+
* 记录一次「没出卡」及其原因(诊断面板显示最近一次)。
|
|
365
|
+
* @param {string} kind paused | queued | budget | model | failed
|
|
366
|
+
* @param {string} reason 用户可读的原因
|
|
367
|
+
*/
|
|
368
|
+
markSkip(kind, reason) {
|
|
369
|
+
this.counters.skipped += 1;
|
|
370
|
+
this.lastSkip = {
|
|
371
|
+
kind,
|
|
372
|
+
reason: String(reason).slice(0, 160),
|
|
373
|
+
at: Date.now(),
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* 记录一次失败(不算 skip;failed 计数已经加过)。
|
|
379
|
+
* @param {string} reason
|
|
380
|
+
*/
|
|
381
|
+
markFailure(reason) {
|
|
382
|
+
this.lastSkip = {
|
|
383
|
+
kind: "failed",
|
|
384
|
+
reason: String(reason).slice(0, 160),
|
|
385
|
+
at: Date.now(),
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
|
|
267
389
|
/**
|
|
268
390
|
* 一次额外补全:prepareCall + stream,产出已校验的建议。
|
|
269
391
|
* @param {any} session
|
|
@@ -341,7 +463,11 @@ export class NextStepService extends TypertRemoteService {
|
|
|
341
463
|
}
|
|
342
464
|
}
|
|
343
465
|
const suggestion = parseSuggestion(raw);
|
|
344
|
-
if (suggestion === null)
|
|
466
|
+
if (suggestion === null) {
|
|
467
|
+
// 解析失败是静默的;把模型输出的开头留在日志里,排障时能分辨「模型跑偏」还是「宿主链路坏」。
|
|
468
|
+
this.warn(`model output did not match the card schema: ${String(raw ?? "").slice(0, 200)}`);
|
|
469
|
+
return null;
|
|
470
|
+
}
|
|
345
471
|
return { suggestion, route: `${route.provider}/${route.model}` };
|
|
346
472
|
}
|
|
347
473
|
|
|
@@ -432,7 +558,8 @@ export class NextStepService extends TypertRemoteService {
|
|
|
432
558
|
// ---- Remote ------------------------------------------------------------
|
|
433
559
|
|
|
434
560
|
/**
|
|
435
|
-
*
|
|
561
|
+
* 用户选中一条建议:只写决策记录并返回要发送的文本,发送由客户端走输入框。
|
|
562
|
+
* 返回的文本就是用户点的那粒胶囊上的 `label`(v0.2 起不再有隐藏的完整指令)。
|
|
436
563
|
* @param {any} request
|
|
437
564
|
* @returns {Promise<any>}
|
|
438
565
|
*/
|
|
@@ -452,6 +579,8 @@ export class NextStepService extends TypertRemoteService {
|
|
|
452
579
|
if (!Number.isSafeInteger(index) || index < 0 || option === undefined) {
|
|
453
580
|
return { ok: false, reason: "missing" };
|
|
454
581
|
}
|
|
582
|
+
const label = typeof option.label === "string" ? option.label.trim() : "";
|
|
583
|
+
if (label === "") return { ok: false, reason: "missing" };
|
|
455
584
|
appendPluginRecord(session, DECISION_RECORD, {
|
|
456
585
|
turn,
|
|
457
586
|
phase: "sent",
|
|
@@ -460,7 +589,7 @@ export class NextStepService extends TypertRemoteService {
|
|
|
460
589
|
});
|
|
461
590
|
this.counters.acted += 1;
|
|
462
591
|
this.abortInflight(sessionId);
|
|
463
|
-
return { ok: true, text:
|
|
592
|
+
return { ok: true, text: label };
|
|
464
593
|
} catch (error) {
|
|
465
594
|
rethrowRemote(error);
|
|
466
595
|
}
|
|
@@ -488,6 +617,38 @@ export class NextStepService extends TypertRemoteService {
|
|
|
488
617
|
}
|
|
489
618
|
}
|
|
490
619
|
|
|
620
|
+
/**
|
|
621
|
+
* 手动补生成:为最近一个正常结束的回合强制出一张卡(用户点「生成」或 /next-step)。
|
|
622
|
+
* 自动门控被跳过时用它兜底;模型判断 needed=false 时也照出(用户明确要了)。
|
|
623
|
+
* @param {any} request
|
|
624
|
+
* @returns {Promise<any>}
|
|
625
|
+
*/
|
|
626
|
+
async generateNow(request) {
|
|
627
|
+
try {
|
|
628
|
+
const sessionId = typeof request?.sessionId === "string" ? request.sessionId : "";
|
|
629
|
+
const settings = await this.currentSettings();
|
|
630
|
+
if (!settings.enabled || !settings.card) return { ok: false, reason: "disabled" };
|
|
631
|
+
const session = this.sessionOf(sessionId);
|
|
632
|
+
const latest = latestCompletedTurn(session);
|
|
633
|
+
if (latest === undefined) return { ok: false, reason: "no-turn" };
|
|
634
|
+
const requested = Number(request?.turn);
|
|
635
|
+
const turn = Number.isSafeInteger(requested) ? requested : latest.turn;
|
|
636
|
+
if (turn !== latest.turn) return { ok: false, reason: "stale" };
|
|
637
|
+
const { cards } = scanSession(session);
|
|
638
|
+
if (cards.has(turn)) return { ok: false, reason: "exists" };
|
|
639
|
+
if (cards.size >= settings.maxCardsPerSession) return { ok: false, reason: "budget" };
|
|
640
|
+
if (this.inflight.has(sessionId)) return { ok: false, reason: "inflight" };
|
|
641
|
+
const result = await this.writeCardFor(session, settings, {
|
|
642
|
+
turn,
|
|
643
|
+
seq: latest.seq,
|
|
644
|
+
force: true,
|
|
645
|
+
});
|
|
646
|
+
return result.ok === true ? { ok: true } : { ok: false, reason: result.reason };
|
|
647
|
+
} catch (error) {
|
|
648
|
+
rethrowRemote(error);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
|
|
491
652
|
/**
|
|
492
653
|
* 读设置(RPC)。
|
|
493
654
|
* @returns {Promise<any>}
|
|
@@ -532,6 +693,7 @@ export class NextStepService extends TypertRemoteService {
|
|
|
532
693
|
...this.counters,
|
|
533
694
|
inflight: this.inflight.size,
|
|
534
695
|
lastError: this.lastError,
|
|
696
|
+
lastSkip: this.lastSkip,
|
|
535
697
|
dataRoot: this.dataRoot,
|
|
536
698
|
settings: await this.currentSettings(),
|
|
537
699
|
// 运行时自证:与 `dsh plugin list` / npm tarball 对照用。
|
|
@@ -549,6 +711,7 @@ export class NextStepService extends TypertRemoteService {
|
|
|
549
711
|
|
|
550
712
|
declareRemote(NextStepService, "act");
|
|
551
713
|
declareRemote(NextStepService, "dismiss");
|
|
714
|
+
declareRemote(NextStepService, "generateNow");
|
|
552
715
|
declareRemote(NextStepService, "settings");
|
|
553
716
|
declareRemote(NextStepService, "updateSettings");
|
|
554
717
|
declareRemote(NextStepService, "diagnostics");
|
package/locale/en.json
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"error.failed": "Something went wrong. Try again.",
|
|
15
15
|
"error.send": "Could not fill the composer",
|
|
16
16
|
"settings.title": "Next-step suggestions",
|
|
17
|
-
"settings.desc": "
|
|
17
|
+
"settings.desc": "Judges whether a turn is worth continuing; when it is, adds a status summary and two directions (one extra model call).",
|
|
18
18
|
"settings.enabled": "Enable the plugin",
|
|
19
19
|
"settings.card": "Decision card under the answer",
|
|
20
20
|
"settings.ghost": "Ghost hint in the composer",
|
|
@@ -30,6 +30,16 @@
|
|
|
30
30
|
"settings.noError": "No errors",
|
|
31
31
|
"settings.paused": "Paused; resumes in about {time}",
|
|
32
32
|
"settings.updateFailed": "Could not save",
|
|
33
|
-
"settings.version": "Version {version} · build {hash}"
|
|
33
|
+
"settings.version": "Version {version} · build {hash}",
|
|
34
|
+
"sentLabel": "Sent: {label}",
|
|
35
|
+
"settings.offHint": "While off, no request is made; cards already in the conversation stay.",
|
|
36
|
+
"settings.lastSkip": "Last skipped: {kind} · {reason}",
|
|
37
|
+
"settings.generateHint": "When no card appears, a “Generate a next-step card” button shows under the latest answer.",
|
|
38
|
+
"error.budget": "This session has used up its card budget",
|
|
39
|
+
"error.exists": "This turn already has a card",
|
|
40
|
+
"error.inflight": "Already generating; try again in a moment",
|
|
41
|
+
"error.noTurn": "No completed turn to generate a card for",
|
|
42
|
+
"retry.generate": "Generate a next-step card",
|
|
43
|
+
"retry.generating": "Generating…"
|
|
34
44
|
}
|
|
35
45
|
}
|
package/locale/zh.json
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"error.failed": "操作失败,请重试",
|
|
15
15
|
"error.send": "没能把建议写进输入框",
|
|
16
16
|
"settings.title": "下一步建议",
|
|
17
|
-
"settings.desc": "
|
|
17
|
+
"settings.desc": "答完后判断是否值得继续;需要时生成一张含摘要与两个方向建议的决策卡(一次额外模型调用)。",
|
|
18
18
|
"settings.enabled": "启用插件",
|
|
19
19
|
"settings.card": "回答下方的决策卡",
|
|
20
20
|
"settings.ghost": "输入框里的幽灵提示",
|
|
@@ -30,6 +30,16 @@
|
|
|
30
30
|
"settings.noError": "无错误",
|
|
31
31
|
"settings.paused": "已暂停,约 {time} 后恢复",
|
|
32
32
|
"settings.updateFailed": "保存失败",
|
|
33
|
-
"settings.version": "版本 {version} · 构建 {hash}"
|
|
33
|
+
"settings.version": "版本 {version} · 构建 {hash}",
|
|
34
|
+
"sentLabel": "已发出:{label}",
|
|
35
|
+
"settings.offHint": "关闭后不会发起任何请求;已生成的卡片仍留在对话里。",
|
|
36
|
+
"settings.lastSkip": "最近跳过:{kind} · {reason}",
|
|
37
|
+
"settings.generateHint": "没有自动出卡时,最新回答下方会出现「生成下一步建议」,点一下即可补一张。",
|
|
38
|
+
"error.budget": "本会话的卡片预算已用完",
|
|
39
|
+
"error.exists": "这一轮已经有卡片了",
|
|
40
|
+
"error.inflight": "正在生成,请稍候",
|
|
41
|
+
"error.noTurn": "没有可生成建议的回合",
|
|
42
|
+
"retry.generate": "生成下一步建议",
|
|
43
|
+
"retry.generating": "正在生成…"
|
|
34
44
|
}
|
|
35
45
|
}
|
package/package.json
CHANGED