dsh-wdx-relief 0.5.1 → 0.5.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.5.2 — 2026-11-14
4
+
5
+ - 修掉「预测看不到提问框」:输入框灰字预测的上下文里现在会带上 `ask_user_question` 的问答,
6
+ 渲染成「助手(提问)…」「用户(回答)…」两行。
7
+ - 前台回答写在工具结果的 `{"answers":[…]}` 里,排队回答是 `source.kind === 'user-question-reply'`
8
+ 的用户消息;这两种记录以前都被当成工具噪音丢掉,预测只能看到助手最后的「已写入…」,
9
+ 看起来就像助手没问过就把事做完了。
10
+ - 同一题只保留一条回答;坏 JSON、失败的工具结果和 `isError` 结果直接跳过。
11
+ - 预测 prompt 里补了一句说明,让模型把这两行当成已经发生的确认,而不是待确认。
12
+
3
13
  ## 0.5.1 — 2026-11-12
4
14
 
5
15
  - 修掉「又问了一轮,输入框上面那一行还是上一轮的」:摘要默认改成每轮都更新(`recapCooldownTurns` 默认 1,原来是 3),沿用旧行的相似度阈值从 0.7 提到 0.9,只有几乎没变化时才不动。
package/docs/guide.md CHANGED
@@ -71,6 +71,9 @@ bundle 层只声明入口和输入层的开关,其余字段由 schema 默认
71
71
  空闲、空输入时以 350ms 防抖请求 `POST /api/wdx-relief/predict`。
72
72
  2. Host 用 `ctx.sessions.get(sessionId).deriveMessages()` 取最近几轮真人用户/助手文本,
73
73
  要求最后一条是助手消息;用 `agentDefaultModel` 的最低推理档位生成一条短句。
74
+ 提问框(`ask_user_question`)的问答会还原成「助手(提问)」「用户(回答)」两行:前台回答在
75
+ 工具结果的 `{"answers":[…]}` 里,排队回答是 `source.kind === 'user-question-reply'` 的用户消息;
76
+ 不还原的话,最近几轮看起来就像助手没问过就把事做完了(例如刚写完 11 条,预测却以为用户还没确认)。
74
77
  3. 结果按 `session.seq` 缓存;客户端把灰字绝对定位到 `[data-composer-input]` 的文字起点,
75
78
  Tab 通过标准 prop `inputActions.setDraft()` 写入 Lexical 编辑器。
76
79
  这条 Codex 式体验依赖 composer 的布局矩形,是本插件已知的 DOM 耦合点。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-wdx-relief",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "Take the friction out of daily DSH work: predicted next messages, a live one-line turn recap above the composer, and sidebar side questions with a composer-style model picker that stay out of the model context.",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/src/prediction.js CHANGED
@@ -22,6 +22,7 @@ export const SYSTEM_PROMPT = [
22
22
  '- 使用与用户最近消息相同的语言。',
23
23
  '- 优先给出具体动作、回答 agent 的提问、或确认下一步;不要泛泛地说「继续」。',
24
24
  '- 不要重复助手说过的话,也不要复述用户上一条消息。',
25
+ '- 「助手(提问)」/「用户(回答)」是助手用提问框向用户确认、用户点选的答案,按已经发生的对话处理。',
25
26
  '- 不要解释、不要引号、不要编号、不要加「预测:」之类的前缀。',
26
27
  '- 没有足够信息时只输出 <none>。',
27
28
  ].join('\n')
@@ -49,9 +50,71 @@ export function messageText(message) {
49
50
  return parts.join('\n').trim()
50
51
  }
51
52
 
53
+ /** 提问框工具:助手用它让用户点选答案,答案不会以用户消息的形式出现。 */
54
+ const QUESTION_TOOL = 'ask_user_question'
55
+ const MAX_QUESTION_CHARS = 400
56
+ const MAX_ANSWER_CHARS = 200
57
+
58
+ function parseObject(value) {
59
+ if (value !== null && typeof value === 'object' && !Array.isArray(value)) return value
60
+ if (typeof value !== 'string' || value.trim() === '') return null
61
+ try {
62
+ const parsed = JSON.parse(value)
63
+ return parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : null
64
+ } catch {
65
+ return null
66
+ }
67
+ }
68
+
69
+ /** 「要写入吗?(选项:可以、先别)」——把提问框的问题压成一行。 */
70
+ function questionLine(questions) {
71
+ const parts = []
72
+ for (const question of Array.isArray(questions) ? questions : []) {
73
+ if (question === null || typeof question !== 'object') continue
74
+ const text = typeof question.question === 'string' ? question.question.trim() : ''
75
+ if (text === '') continue
76
+ const labels = (Array.isArray(question.options) ? question.options : [])
77
+ .map((option) => (option !== null && typeof option === 'object' && typeof option.label === 'string' ? option.label.trim() : ''))
78
+ .filter((label) => label !== '')
79
+ .slice(0, 4)
80
+ parts.push(labels.length === 0 ? text : `${text}(选项:${labels.join('、')})`)
81
+ }
82
+ return parts.join(';')
83
+ }
84
+
85
+ /** 用户的点选结果;多问题时带上题目,单独一题时靠上一行提问就够。 */
86
+ function answerLine(answers, questions) {
87
+ const questionById = new Map()
88
+ let questionCount = 0
89
+ for (const question of Array.isArray(questions) ? questions : []) {
90
+ if (question === null || typeof question !== 'object') continue
91
+ questionCount += 1
92
+ const text = typeof question.question === 'string' ? question.question.trim() : ''
93
+ if (typeof question.id === 'string' && text !== '') questionById.set(question.id, text)
94
+ }
95
+
96
+ const parts = []
97
+ for (const answer of Array.isArray(answers) ? answers : []) {
98
+ if (answer === null || typeof answer !== 'object') continue
99
+ const selected = (Array.isArray(answer.selected) ? answer.selected : [])
100
+ .filter((value) => typeof value === 'string' && value.trim() !== '')
101
+ .map((value) => value.trim())
102
+ const custom = typeof answer.custom === 'string' ? answer.custom.trim() : ''
103
+ let picked
104
+ if (selected.length === 0) picked = custom === '' ? '跳过' : `自定义:${custom}`
105
+ else picked = custom === '' ? selected.join('、') : `${selected.join('、')}(补充:${custom})`
106
+ const prompt = questionCount > 1 && typeof answer.id === 'string' ? questionById.get(answer.id) : undefined
107
+ parts.push(prompt === undefined ? picked : `${prompt} → ${picked}`)
108
+ }
109
+ return parts.join(';')
110
+ }
111
+
52
112
  /**
53
113
  * 从 deriveMessages() 里截出最近几轮可读对话。
54
114
  * 只保留真人用户消息(source.kind === 'user',排除注入的上下文)和助手消息;
115
+ * 提问框的问答要单独还原:前台回答写在工具结果的 JSON 里,排队回答是
116
+ * `source.kind === 'user-question-reply'` 的用户消息,两者都不是普通用户文本。
117
+ * 少了这两行,最近几轮看起来就像助手没问过就直接把事做完了。
55
118
  * 最后一条必须是助手,否则说明当前轮次还没结束,不该预测。
56
119
  */
57
120
  export function extractConversation(messages, options = {}) {
@@ -60,12 +123,65 @@ export function extractConversation(messages, options = {}) {
60
123
  const maxPromptChars = options.maxPromptChars ?? MAX_PROMPT_CHARS
61
124
 
62
125
  const items = []
126
+ /** callId -> 这一问的问题列表,用来给回答补上题目。 */
127
+ const asked = new Map()
128
+ const answered = new Set()
129
+
130
+ const pushAnswer = (callId, answers, questions) => {
131
+ if (typeof callId === 'string') {
132
+ if (answered.has(callId)) return
133
+ answered.add(callId)
134
+ }
135
+ const text = answerLine(answers, questions)
136
+ if (text === '') return
137
+ items.push({ role: 'user', kind: 'answer', text: clipText(text, MAX_ANSWER_CHARS) })
138
+ }
139
+
63
140
  for (const message of messages ?? []) {
64
- if (!message || (message.role !== 'user' && message.role !== 'assistant')) continue
65
- if (message.role === 'user' && message.source && message.source.kind !== 'user') continue
141
+ if (!message || typeof message !== 'object') continue
142
+
143
+ // 前台回答:ask_user_question 的结果落在一条 role: 'tool' 的消息里。
144
+ if (message.role === 'tool') {
145
+ const source = message.source
146
+ const callId = typeof message.toolCallId === 'string'
147
+ ? message.toolCallId
148
+ : source && typeof source === 'object' && typeof source.callId === 'string' ? source.callId : undefined
149
+ if (callId === undefined || !asked.has(callId) || message.isError === true) continue
150
+ const result = parseObject(messageText(message))
151
+ if (result !== null && Array.isArray(result.answers)) pushAnswer(callId, result.answers, asked.get(callId))
152
+ continue
153
+ }
154
+
155
+ if (message.role === 'user') {
156
+ const source = message.source
157
+ // 排队回答:用户回答在等待窗口关闭后才到达时,DSH 会追加一条用户消息。
158
+ if (source && typeof source === 'object' && source.kind === 'user-question-reply') {
159
+ const reply = parseObject(messageText(message))
160
+ if (reply === null || !Array.isArray(reply.answers)) continue
161
+ const callId = typeof source.callId === 'string'
162
+ ? source.callId
163
+ : typeof reply.callId === 'string' ? reply.callId : undefined
164
+ pushAnswer(callId, reply.answers, Array.isArray(reply.questions) ? reply.questions : (callId === undefined ? undefined : asked.get(callId)))
165
+ continue
166
+ }
167
+ if (source && typeof source === 'object' && source.kind !== 'user') continue
168
+ } else if (message.role !== 'assistant') {
169
+ continue
170
+ }
171
+
66
172
  const raw = messageText(message)
67
- if (raw === '') continue
68
- items.push({ role: message.role, text: clipText(raw, maxMessageChars) })
173
+ if (raw !== '') items.push({ role: message.role, text: clipText(raw, maxMessageChars) })
174
+
175
+ const blocks = message.role === 'assistant' && Array.isArray(message.content) ? message.content : []
176
+ for (const block of blocks) {
177
+ if (!block || typeof block !== 'object' || block.type !== 'tool-call' || block.name !== QUESTION_TOOL) continue
178
+ const args = parseObject(block.arguments)
179
+ const questions = args === null ? [] : Array.isArray(args.questions) ? args.questions : []
180
+ const text = questionLine(questions)
181
+ if (text === '') continue
182
+ if (typeof block.id === 'string') asked.set(block.id, questions)
183
+ items.push({ role: 'assistant', kind: 'question', text: clipText(text, MAX_QUESTION_CHARS) })
184
+ }
69
185
  }
70
186
 
71
187
  if (items.length < 2) return null
@@ -83,10 +199,17 @@ export function extractConversation(messages, options = {}) {
83
199
  return tail
84
200
  }
85
201
 
202
+ /** 提问框的问答要标出来,模型才知道这是用户在弹窗里点的,不是随口说的。 */
203
+ function speakerOf(item) {
204
+ if (item.kind === 'question') return '助手(提问)'
205
+ if (item.kind === 'answer') return '用户(回答)'
206
+ return item.role === 'user' ? '用户' : '助手'
207
+ }
208
+
86
209
  /** 把对话尾段拼成给模型的 user prompt。 */
87
210
  export function buildPrompt(conversation) {
88
211
  const transcript = conversation
89
- .map((item) => `${item.role === 'user' ? '用户' : '助手'}:${item.text}`)
212
+ .map((item) => `${speakerOf(item)}:${item.text}`)
90
213
  .join('\n\n')
91
214
  return [
92
215
  '以下是用户与编码 agent 的最近对话:',