dsh-channel-feishu 0.8.1 → 0.8.3
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 +4 -3
- package/index.js +303 -65
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -72,16 +72,17 @@ sidecar(`feishu-channel-state.json` 的 `chats`,以及进程内的 chat memo
|
|
|
72
72
|
|
|
73
73
|
## 安装
|
|
74
74
|
|
|
75
|
-
|
|
75
|
+
npm(<https://www.npmjs.com/package/dsh-channel-feishu>):
|
|
76
76
|
|
|
77
77
|
```bash
|
|
78
78
|
dsh plugin add dsh-channel-feishu
|
|
79
79
|
```
|
|
80
80
|
|
|
81
|
-
|
|
81
|
+
或从 GitHub 仓库/Release(内容一致):
|
|
82
82
|
|
|
83
83
|
```bash
|
|
84
|
-
dsh plugin add https://github.com/Poy-Coder/dsh-channel-feishu
|
|
84
|
+
dsh plugin add https://github.com/Poy-Coder/dsh-channel-feishu
|
|
85
|
+
dsh plugin add https://github.com/Poy-Coder/dsh-channel-feishu/releases/download/v0.8.3/dsh-channel-feishu-0.8.3.tgz
|
|
85
86
|
```
|
|
86
87
|
|
|
87
88
|
依赖必须用扁平结构装(非 ASCII 目录下 pnpm 的软链会把路径写坏):
|
package/index.js
CHANGED
|
@@ -2022,6 +2022,23 @@ export async function handleInboundEvent(ctx, config, data, seen) {
|
|
|
2022
2022
|
/** Card element that carries the waiting/answered note under the buttons. */
|
|
2023
2023
|
export const QA_STATUS_ELEMENT_ID = 'qa_note'
|
|
2024
2024
|
|
|
2025
|
+
/** Per-question element id prefix that mirrors a multi-select question's ticks. */
|
|
2026
|
+
export const QA_SELECTION_ELEMENT_PREFIX = 'qa_sel_'
|
|
2027
|
+
|
|
2028
|
+
/**
|
|
2029
|
+
* Selection line for one multi-select question.
|
|
2030
|
+
* @param chosen - a Set of selected option indexes, or undefined before any click
|
|
2031
|
+
*/
|
|
2032
|
+
export function questionSelectionText(question, chosen) {
|
|
2033
|
+
const picked = chosen && typeof chosen[Symbol.iterator] === 'function' ? [...chosen] : []
|
|
2034
|
+
if (picked.length === 0) return '<font color="grey">已选:无</font>'
|
|
2035
|
+
const labels = picked
|
|
2036
|
+
.slice()
|
|
2037
|
+
.sort((left, right) => left - right)
|
|
2038
|
+
.map((index) => String(question?.options?.[index]?.label ?? `选项${index + 1}`))
|
|
2039
|
+
return `<font color="grey">已选:${labels.join('、')}</font>`
|
|
2040
|
+
}
|
|
2041
|
+
|
|
2025
2042
|
/** One answerable request per session; the value is the pending record below. */
|
|
2026
2043
|
const pendingQuestions = new Map()
|
|
2027
2044
|
const questionStats = { asked: 0, answered: 0, skipped: 0, failed: 0 }
|
|
@@ -2071,75 +2088,143 @@ function normalizeQuestions(questions) {
|
|
|
2071
2088
|
}))
|
|
2072
2089
|
}
|
|
2073
2090
|
|
|
2091
|
+
/**
|
|
2092
|
+
* One column_set row whose columns share the width, so every element fills the
|
|
2093
|
+
* card instead of collapsing to its natural (tiny) width on a narrow phone.
|
|
2094
|
+
*/
|
|
2095
|
+
function fillRow(children) {
|
|
2096
|
+
return {
|
|
2097
|
+
tag: 'column_set',
|
|
2098
|
+
flex_mode: 'none',
|
|
2099
|
+
columns: children.map((element) => ({
|
|
2100
|
+
tag: 'column',
|
|
2101
|
+
width: 'weighted',
|
|
2102
|
+
weight: 1,
|
|
2103
|
+
elements: [element],
|
|
2104
|
+
})),
|
|
2105
|
+
}
|
|
2106
|
+
}
|
|
2107
|
+
|
|
2108
|
+
/** Option rows shown before the rest fold into a collapsible panel. */
|
|
2109
|
+
const OPTION_ROWS_VISIBLE = 3
|
|
2110
|
+
|
|
2111
|
+
/** A folded group the client expands in place, with no server round trip. */
|
|
2112
|
+
function collapsibleGroup(title, elements) {
|
|
2113
|
+
return {
|
|
2114
|
+
tag: 'collapsible_panel',
|
|
2115
|
+
expanded: false,
|
|
2116
|
+
header: {
|
|
2117
|
+
title: { tag: 'markdown', content: title },
|
|
2118
|
+
vertical_align: 'center',
|
|
2119
|
+
icon: { tag: 'standard_icon', token: 'down-small-ccm_outlined', size: '16px 16px' },
|
|
2120
|
+
icon_position: 'right',
|
|
2121
|
+
icon_expanded_angle: -180,
|
|
2122
|
+
},
|
|
2123
|
+
elements,
|
|
2124
|
+
}
|
|
2125
|
+
}
|
|
2126
|
+
|
|
2074
2127
|
/**
|
|
2075
2128
|
* Build the interactive question card (schema 2.0).
|
|
2076
2129
|
*
|
|
2077
2130
|
* Every option becomes a callback button whose value carries the call id and
|
|
2078
2131
|
* the (question, option) indexes, so a click needs no server-side lookup by
|
|
2079
|
-
* button text.
|
|
2080
|
-
*
|
|
2132
|
+
* button text. Options are queued one per row as bare fill-width buttons:
|
|
2133
|
+
* a row of auto-width buttons squeezes every label to an ellipsis on mobile,
|
|
2134
|
+
* and side-by-side options are hard to hit. Only the 提交 / 跳过 pair shares a
|
|
2135
|
+
* row. Option descriptions and the reply hint drop to notation size, and an
|
|
2136
|
+
* option list longer than OPTION_ROWS_VISIBLE folds its tail into a collapsible
|
|
2137
|
+
* panel. A multi-select question also gets an explicit 提交 button; a free-text
|
|
2138
|
+
* question relies on the reply fallback.
|
|
2081
2139
|
*/
|
|
2082
|
-
export function questionCardJson({ callId, questions, selections, skipped, note, answered }) {
|
|
2140
|
+
export function questionCardJson({ callId, questions, selections, skipped, note, answered, offset = 0, title }) {
|
|
2083
2141
|
const elements = []
|
|
2084
2142
|
questions.forEach((question, index) => {
|
|
2085
2143
|
const options = Array.isArray(question?.options) ? question.options : []
|
|
2144
|
+
const multi = question?.multiSelect === true
|
|
2086
2145
|
const lines = []
|
|
2087
2146
|
if (question?.header) lines.push(`**${question.header}**`)
|
|
2088
2147
|
if (question?.question) lines.push(question.question)
|
|
2089
2148
|
if (question?.detail) {
|
|
2090
2149
|
lines.push(`> ${String(question.detail).split('\n').join('\n> ')}`)
|
|
2091
2150
|
}
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2151
|
+
elements.push({ tag: 'markdown', content: lines.join('\n') })
|
|
2152
|
+
if (options.length === 0) {
|
|
2153
|
+
elements.push({
|
|
2154
|
+
tag: 'markdown',
|
|
2155
|
+
text_size: 'notation',
|
|
2156
|
+
content: '<font color="grey">直接回复文字作为答案;回复「跳过」表示不作答。</font>',
|
|
2098
2157
|
})
|
|
2099
|
-
|
|
2100
|
-
? '可多选:点选项切换选中,选好后点「提交」。'
|
|
2101
|
-
: '点下面的按钮,或直接回复序号 / 选项文字。'
|
|
2102
|
-
lines.push(`<font color="grey">${hint}</font>`)
|
|
2103
|
-
} else {
|
|
2104
|
-
lines.push('<font color="grey">直接回复文字作为答案;回复「跳过」表示不作答。</font>')
|
|
2158
|
+
return
|
|
2105
2159
|
}
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2160
|
+
|
|
2161
|
+
// One bare button per option: a top-level button is always its own block,
|
|
2162
|
+
// so the options queue vertically instead of sharing a squeezed row.
|
|
2163
|
+
const buttons = options.map((option, optionIndex) => {
|
|
2164
|
+
const chosen = selections?.[index]?.has?.(optionIndex) === true
|
|
2165
|
+
const label = String(option?.label ?? '').trim().slice(0, 40) || `选项${optionIndex + 1}`
|
|
2166
|
+
return {
|
|
2112
2167
|
tag: 'button',
|
|
2113
|
-
|
|
2168
|
+
width: 'fill',
|
|
2169
|
+
text: { tag: 'plain_text', content: `${optionIndex + 1}. ${label}${chosen ? ' ✓' : ''}` },
|
|
2114
2170
|
type: optionIndex === 0 ? 'primary' : 'default',
|
|
2115
|
-
behaviors: [{ type: 'callback', value: { c: callId, q: index, o: optionIndex } }],
|
|
2116
|
-
}
|
|
2117
|
-
})
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2171
|
+
behaviors: [{ type: 'callback', value: { c: callId, q: offset + index, o: optionIndex } }],
|
|
2172
|
+
}
|
|
2173
|
+
})
|
|
2174
|
+
elements.push(...buttons.slice(0, OPTION_ROWS_VISIBLE))
|
|
2175
|
+
if (buttons.length > OPTION_ROWS_VISIBLE) {
|
|
2176
|
+
// A long option list would push the question off the screen; fold the
|
|
2177
|
+
// tail into a panel the client expands without a server round trip. The
|
|
2178
|
+
// panel title stays neutral because the toggle never calls back, so the
|
|
2179
|
+
// channel cannot rewrite it to 收起 after an expansion.
|
|
2180
|
+
elements.push(collapsibleGroup(`其余 ${buttons.length - OPTION_ROWS_VISIBLE} 项(点击展开 / 收起)`, buttons.slice(OPTION_ROWS_VISIBLE)))
|
|
2181
|
+
}
|
|
2182
|
+
|
|
2183
|
+
// Multi-select ticks are invisible on the buttons themselves (a button's
|
|
2184
|
+
// text cannot be patched), so the card carries one status line per
|
|
2185
|
+
// multi-select question and the channel rewrites it on every click.
|
|
2186
|
+
if (multi) {
|
|
2187
|
+
elements.push({
|
|
2188
|
+
tag: 'markdown',
|
|
2189
|
+
element_id: `${QA_SELECTION_ELEMENT_PREFIX}${offset + index}`,
|
|
2190
|
+
text_size: 'notation',
|
|
2191
|
+
content: questionSelectionText(question, selections?.[index]),
|
|
2192
|
+
})
|
|
2193
|
+
}
|
|
2194
|
+
|
|
2195
|
+
const actions = []
|
|
2196
|
+
if (multi) {
|
|
2197
|
+
actions.push({
|
|
2198
|
+
tag: 'button',
|
|
2199
|
+
width: 'fill',
|
|
2200
|
+
text: { tag: 'plain_text', content: '提交' },
|
|
2201
|
+
type: 'primary',
|
|
2202
|
+
behaviors: [{ type: 'callback', value: { c: callId, q: offset + index, submit: true } }],
|
|
2128
2203
|
})
|
|
2129
2204
|
}
|
|
2130
2205
|
if (answered !== true) {
|
|
2131
|
-
|
|
2132
|
-
tag: '
|
|
2133
|
-
width: '
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
type: 'default',
|
|
2138
|
-
behaviors: [{ type: 'callback', value: { c: callId, q: index, skip: true } }],
|
|
2139
|
-
}],
|
|
2206
|
+
actions.push({
|
|
2207
|
+
tag: 'button',
|
|
2208
|
+
width: 'fill',
|
|
2209
|
+
text: { tag: 'plain_text', content: '跳过' },
|
|
2210
|
+
type: 'default',
|
|
2211
|
+
behaviors: [{ type: 'callback', value: { c: callId, q: offset + index, skip: true } }],
|
|
2140
2212
|
})
|
|
2141
2213
|
}
|
|
2142
|
-
elements.push(
|
|
2214
|
+
if (actions.length === 1) elements.push(actions[0])
|
|
2215
|
+
else if (actions.length > 1) elements.push(fillRow(actions))
|
|
2216
|
+
|
|
2217
|
+
// Fine print (option descriptions plus the reply hint) always closes the
|
|
2218
|
+
// block: mid-card notes read as if they described the buttons below them.
|
|
2219
|
+
const finePrint = []
|
|
2220
|
+
options.forEach((option, optionIndex) => {
|
|
2221
|
+
if (!option?.description) return
|
|
2222
|
+
finePrint.push(`<font color="grey">${optionIndex + 1} · ${option.description}</font>`)
|
|
2223
|
+
})
|
|
2224
|
+
finePrint.push(`<font color="grey">${multi
|
|
2225
|
+
? '可多选:点选项切换选中,选好后点「提交」。'
|
|
2226
|
+
: '点上面的按钮作答,也可直接回复序号 / 选项文字。'}</font>`)
|
|
2227
|
+
elements.push({ tag: 'markdown', text_size: 'notation', content: finePrint.join('\n') })
|
|
2143
2228
|
})
|
|
2144
2229
|
const noteText = note || '等待你的回答…'
|
|
2145
2230
|
if (note !== null && noteText !== '') {
|
|
@@ -2151,11 +2236,20 @@ export function questionCardJson({ callId, questions, selections, skipped, note,
|
|
|
2151
2236
|
})
|
|
2152
2237
|
}
|
|
2153
2238
|
const count = questions.length
|
|
2239
|
+
const headline = title || (count > 1 ? `有 ${count} 个问题` : '需要你的回答')
|
|
2154
2240
|
return {
|
|
2155
2241
|
schema: '2.0',
|
|
2156
|
-
|
|
2242
|
+
// streaming_mode keeps the CardKit entity open for element updates: without
|
|
2243
|
+
// it every status write fails with `300309 streaming mode is closed`. The
|
|
2244
|
+
// card is closed once the question settles.
|
|
2245
|
+
config: {
|
|
2246
|
+
streaming_mode: true,
|
|
2247
|
+
update_multi: true,
|
|
2248
|
+
width_mode: 'fill',
|
|
2249
|
+
summary: { content: headline },
|
|
2250
|
+
},
|
|
2157
2251
|
header: {
|
|
2158
|
-
title: { tag: 'plain_text', content:
|
|
2252
|
+
title: { tag: 'plain_text', content: headline },
|
|
2159
2253
|
template: answered === true ? 'green' : 'blue',
|
|
2160
2254
|
},
|
|
2161
2255
|
body: { elements },
|
|
@@ -2233,14 +2327,20 @@ export function applyQuestionSelection(pending, value) {
|
|
|
2233
2327
|
return true
|
|
2234
2328
|
}
|
|
2235
2329
|
|
|
2330
|
+
/** True when one question of the batch already holds a usable answer. */
|
|
2331
|
+
export function isStepSettled(pending, index) {
|
|
2332
|
+
const question = pending?.questions?.[index]
|
|
2333
|
+
if (!question) return false
|
|
2334
|
+
if (pending.skipped.has(index)) return true
|
|
2335
|
+
if (question.options.length === 0) return false
|
|
2336
|
+
const picked = pending.selections?.[index]
|
|
2337
|
+
if (!picked || picked.size === 0) return false
|
|
2338
|
+
return pending.submitted.has(index)
|
|
2339
|
+
}
|
|
2340
|
+
|
|
2236
2341
|
export function isQuestionComplete(pending) {
|
|
2237
2342
|
if (!pending || pending.questions.length === 0) return false
|
|
2238
|
-
return pending.questions.every((question, index) =>
|
|
2239
|
-
if (pending.skipped.has(index)) return true
|
|
2240
|
-
if (question.options.length === 0) return false
|
|
2241
|
-
if (question.multiSelect) return pending.submitted.has(index) && pending.selections[index].size > 0
|
|
2242
|
-
return pending.selections[index].size > 0 && pending.submitted.has(index)
|
|
2243
|
-
})
|
|
2343
|
+
return pending.questions.every((question, index) => isStepSettled(pending, index))
|
|
2244
2344
|
}
|
|
2245
2345
|
|
|
2246
2346
|
/** The batch the host accepts: one item per question, in order. */
|
|
@@ -2250,7 +2350,8 @@ export function questionAnswersOf(pending) {
|
|
|
2250
2350
|
const selected = chosen
|
|
2251
2351
|
.filter((optionIndex) => optionIndex >= 0 && optionIndex < question.options.length)
|
|
2252
2352
|
.map((optionIndex) => question.options[optionIndex].label)
|
|
2253
|
-
|
|
2353
|
+
const custom = pending.customs?.[index]
|
|
2354
|
+
return { id: question.id, selected, ...(custom ? { custom: String(custom) } : {}) }
|
|
2254
2355
|
})
|
|
2255
2356
|
}
|
|
2256
2357
|
|
|
@@ -2265,6 +2366,7 @@ function questionChatOf(config, sessionId) {
|
|
|
2265
2366
|
return ''
|
|
2266
2367
|
}
|
|
2267
2368
|
|
|
2369
|
+
/** Rewrite every live element of one question card (ticks, then the status line). */
|
|
2268
2370
|
async function refreshQuestionCard(config, pending, status, answers) {
|
|
2269
2371
|
if (!pending.cardId) return
|
|
2270
2372
|
const summary = status === 'answered'
|
|
@@ -2277,22 +2379,113 @@ async function refreshQuestionCard(config, pending, status, answers) {
|
|
|
2277
2379
|
? '已取消。'
|
|
2278
2380
|
: '等待你的回答…'
|
|
2279
2381
|
if (!summary) return
|
|
2382
|
+
// One card carries exactly one step, so only that step's elements exist to
|
|
2383
|
+
// rewrite: its multi-select tick line (when it has options) and the status line.
|
|
2384
|
+
const writes = []
|
|
2385
|
+
const index = pending.step ?? 0
|
|
2386
|
+
const question = pending.questions[index]
|
|
2387
|
+
if (question?.multiSelect === true) {
|
|
2388
|
+
writes.push({
|
|
2389
|
+
elementId: `${QA_SELECTION_ELEMENT_PREFIX}${index}`,
|
|
2390
|
+
content: questionSelectionText(question, pending.selections?.[index]),
|
|
2391
|
+
})
|
|
2392
|
+
}
|
|
2393
|
+
writes.push({ elementId: QA_STATUS_ELEMENT_ID, content: `<font color="grey">${summary}</font>` })
|
|
2394
|
+
for (const write of writes) {
|
|
2395
|
+
try {
|
|
2396
|
+
pending.sequence += 1
|
|
2397
|
+
await provider().card.update({
|
|
2398
|
+
cardId: pending.cardId,
|
|
2399
|
+
content: write.content,
|
|
2400
|
+
sequence: pending.sequence,
|
|
2401
|
+
elementId: write.elementId,
|
|
2402
|
+
})
|
|
2403
|
+
} catch (error) {
|
|
2404
|
+
logInbound(config, {
|
|
2405
|
+
event: 'question', result: 'card-update-failed',
|
|
2406
|
+
sessionId: pending.sessionId, callId: pending.callId, elementId: write.elementId,
|
|
2407
|
+
reason: errorText(error),
|
|
2408
|
+
})
|
|
2409
|
+
}
|
|
2410
|
+
}
|
|
2411
|
+
}
|
|
2412
|
+
|
|
2413
|
+
/** Settle the card: close its streaming mode so it stops accepting updates. */
|
|
2414
|
+
async function closeQuestionCard(config, pending, status, answers) {
|
|
2415
|
+
if (!pending.cardId) return
|
|
2280
2416
|
try {
|
|
2281
2417
|
pending.sequence += 1
|
|
2282
|
-
await provider().card.
|
|
2418
|
+
await provider().card.close({
|
|
2283
2419
|
cardId: pending.cardId,
|
|
2284
|
-
content: `<font color="grey">${summary}</font>`,
|
|
2285
2420
|
sequence: pending.sequence,
|
|
2286
|
-
|
|
2421
|
+
summary: status === 'answered'
|
|
2422
|
+
? `已选择:${(answers ?? []).map((item) => item.selected.join('、') || (item.custom ? `“${item.custom}”` : '跳过')).join(';')}`
|
|
2423
|
+
: status === 'timeout' ? '等待超时,已跳过。' : status === 'skipped' ? '已跳过。' : '已取消。',
|
|
2287
2424
|
})
|
|
2288
2425
|
} catch (error) {
|
|
2289
2426
|
logInbound(config, {
|
|
2290
|
-
event: 'question', result: 'card-
|
|
2427
|
+
event: 'question', result: 'card-close-failed',
|
|
2291
2428
|
sessionId: pending.sessionId, callId: pending.callId, reason: errorText(error),
|
|
2292
2429
|
})
|
|
2293
2430
|
}
|
|
2294
2431
|
}
|
|
2295
2432
|
|
|
2433
|
+
/** Fold one parsed text reply into the pending state for its step. */
|
|
2434
|
+
function recordTextAnswer(pending, index, answer) {
|
|
2435
|
+
const question = pending.questions[index]
|
|
2436
|
+
const picked = new Set()
|
|
2437
|
+
for (const label of answer?.selected ?? []) {
|
|
2438
|
+
const at = question.options.findIndex((option) => sameLabel(option.label, label))
|
|
2439
|
+
if (at >= 0) picked.add(at)
|
|
2440
|
+
}
|
|
2441
|
+
pending.selections[index] = picked
|
|
2442
|
+
pending.submitted.add(index)
|
|
2443
|
+
if (picked.size === 0) pending.skipped.add(index)
|
|
2444
|
+
if (answer?.custom !== undefined) {
|
|
2445
|
+
pending.customs = pending.customs ?? {}
|
|
2446
|
+
pending.customs[index] = String(answer.custom)
|
|
2447
|
+
}
|
|
2448
|
+
}
|
|
2449
|
+
|
|
2450
|
+
/**
|
|
2451
|
+
* Close the answered step's card and post the next question as its own card.
|
|
2452
|
+
*
|
|
2453
|
+
* A batch of questions is asked one at a time, so each card stays short and the
|
|
2454
|
+
* user only sees the question that is actually waiting for an answer.
|
|
2455
|
+
*/
|
|
2456
|
+
async function advanceQuestionCard(ctx, config, pending) {
|
|
2457
|
+
if (pending.advancing === true) return false
|
|
2458
|
+
pending.advancing = true
|
|
2459
|
+
try {
|
|
2460
|
+
const index = pending.step
|
|
2461
|
+
const question = pending.questions[index]
|
|
2462
|
+
const chosen = pending.selections[index] instanceof Set ? [...pending.selections[index]] : []
|
|
2463
|
+
const selected = chosen
|
|
2464
|
+
.sort((left, right) => left - right)
|
|
2465
|
+
.map((at) => question.options[at]?.label ?? '')
|
|
2466
|
+
.filter(Boolean)
|
|
2467
|
+
const settled = [{ id: question.id, selected }]
|
|
2468
|
+
await refreshQuestionCard(config, pending, 'answered', settled)
|
|
2469
|
+
await closeQuestionCard(config, pending, 'answered', settled)
|
|
2470
|
+
pending.step = index + 1
|
|
2471
|
+
pending.cardId = ''
|
|
2472
|
+
pending.messageId = ''
|
|
2473
|
+
pending.sequence = 1
|
|
2474
|
+
const sent = await sendQuestionCard(ctx, config, pending)
|
|
2475
|
+
if (!sent) {
|
|
2476
|
+
finishQuestion(config, pending, 'answered')
|
|
2477
|
+
} else if (pending.timeoutMs > 0) {
|
|
2478
|
+
// Every step gets the full answer window instead of sharing one deadline.
|
|
2479
|
+
if (pending.timer) clearTimeout(pending.timer)
|
|
2480
|
+
pending.timer = setTimeout(() => finishQuestion(config, pending, 'timeout'), pending.timeoutMs)
|
|
2481
|
+
pending.timer.unref?.()
|
|
2482
|
+
}
|
|
2483
|
+
return sent
|
|
2484
|
+
} finally {
|
|
2485
|
+
pending.advancing = false
|
|
2486
|
+
}
|
|
2487
|
+
}
|
|
2488
|
+
|
|
2296
2489
|
/**
|
|
2297
2490
|
* Settle one pending question exactly once.
|
|
2298
2491
|
* @param status - answered | skipped | timeout | aborted
|
|
@@ -2324,13 +2517,24 @@ function finishQuestion(config, pending, status, explicitAnswers) {
|
|
|
2324
2517
|
try { pending.resolve(answers) } catch { /* ignore */ }
|
|
2325
2518
|
}
|
|
2326
2519
|
void refreshQuestionCard(config, pending, status, answers)
|
|
2520
|
+
.then(() => closeQuestionCard(config, pending, status, answers))
|
|
2327
2521
|
return true
|
|
2328
2522
|
}
|
|
2329
2523
|
|
|
2330
2524
|
/** Send the question as a card; fall back to plain text if card creation fails. */
|
|
2331
2525
|
async function sendQuestionCard(ctx, config, pending) {
|
|
2526
|
+
const step = pending.step ?? 0
|
|
2527
|
+
const question = pending.questions[step]
|
|
2528
|
+
const total = pending.questions.length
|
|
2332
2529
|
try {
|
|
2333
|
-
const card = questionCardJson({
|
|
2530
|
+
const card = questionCardJson({
|
|
2531
|
+
callId: pending.callId,
|
|
2532
|
+
questions: [question],
|
|
2533
|
+
selections: [pending.selections[step]],
|
|
2534
|
+
offset: step,
|
|
2535
|
+
title: total > 1 ? `第 ${step + 1}/${total} 题` : undefined,
|
|
2536
|
+
note: total > 1 ? `第 ${step + 1}/${total} 题 · 等待你的回答…` : '等待你的回答…',
|
|
2537
|
+
})
|
|
2334
2538
|
const cardId = await provider().card.createRaw({ card })
|
|
2335
2539
|
const messageId = await provider().card.attach({ chatId: pending.chatId, cardId })
|
|
2336
2540
|
pending.cardId = cardId
|
|
@@ -2338,6 +2542,7 @@ async function sendQuestionCard(ctx, config, pending) {
|
|
|
2338
2542
|
logInbound(config, {
|
|
2339
2543
|
event: 'question', result: 'card-sent', sessionId: pending.sessionId,
|
|
2340
2544
|
callId: pending.callId, chatId: pending.chatId, cardId, messageId,
|
|
2545
|
+
step, count: total,
|
|
2341
2546
|
})
|
|
2342
2547
|
return true
|
|
2343
2548
|
} catch (error) {
|
|
@@ -2349,7 +2554,7 @@ async function sendQuestionCard(ctx, config, pending) {
|
|
|
2349
2554
|
})
|
|
2350
2555
|
}
|
|
2351
2556
|
try {
|
|
2352
|
-
await provider().sendText({ chatId: pending.chatId, text: questionText(
|
|
2557
|
+
await provider().sendText({ chatId: pending.chatId, text: questionText([question]) })
|
|
2353
2558
|
logInbound(config, {
|
|
2354
2559
|
event: 'question', result: 'text-sent', sessionId: pending.sessionId,
|
|
2355
2560
|
callId: pending.callId, chatId: pending.chatId,
|
|
@@ -2388,6 +2593,9 @@ export async function answerUserQuestion(ctx, config, request, next) {
|
|
|
2388
2593
|
selections: questions.map(() => new Set()),
|
|
2389
2594
|
skipped: new Set(),
|
|
2390
2595
|
submitted: new Set(),
|
|
2596
|
+
customs: {},
|
|
2597
|
+
step: 0,
|
|
2598
|
+
timeoutMs,
|
|
2391
2599
|
messageId: '', cardId: '', sequence: 1,
|
|
2392
2600
|
done: false, status: '', signal: request?.signal,
|
|
2393
2601
|
resolve: undefined, reject: undefined, timer: undefined, abortHandler: undefined,
|
|
@@ -2445,6 +2653,23 @@ export async function handleQuestionCardAction(ctx, config, raw) {
|
|
|
2445
2653
|
?? [...pendingQuestions.values()].find((item) => item.chatId === action.chatId && String(action.value?.c ?? '') === item.callId)
|
|
2446
2654
|
if (!pending) return { toast: { type: 'info', content: '这个问题已经结束。' } }
|
|
2447
2655
|
if (pending.chatId && action.chatId && action.chatId !== pending.chatId) return undefined
|
|
2656
|
+
// Each step owns its own card; a click on an earlier (or already answered)
|
|
2657
|
+
// card must not touch the question that is live now.
|
|
2658
|
+
const clickedStep = Number(action.value?.q)
|
|
2659
|
+
if (Number.isInteger(clickedStep) && clickedStep !== pending.step) {
|
|
2660
|
+
return { toast: { type: 'info', content: '这题已经答过了。' } }
|
|
2661
|
+
}
|
|
2662
|
+
if (pending.advancing === true) {
|
|
2663
|
+
return { toast: { type: 'info', content: '正在发送下一题…' } }
|
|
2664
|
+
}
|
|
2665
|
+
// 提交 with nothing ticked would only re-render the card; say so instead.
|
|
2666
|
+
if (action.value?.submit === true) {
|
|
2667
|
+
const question = pending.questions[pending.step]
|
|
2668
|
+
const picked = pending.selections?.[pending.step]
|
|
2669
|
+
if (question?.multiSelect === true && (!picked || picked.size === 0)) {
|
|
2670
|
+
return { toast: { type: 'warning', content: '先勾选至少一项,再点「提交」。' } }
|
|
2671
|
+
}
|
|
2672
|
+
}
|
|
2448
2673
|
if (!applyQuestionSelection(pending, action.value)) {
|
|
2449
2674
|
logInbound(config, {
|
|
2450
2675
|
event: 'question', result: 'card-bad-value', sessionId: pending.sessionId,
|
|
@@ -2452,7 +2677,12 @@ export async function handleQuestionCardAction(ctx, config, raw) {
|
|
|
2452
2677
|
})
|
|
2453
2678
|
return { toast: { type: 'error', content: '无法识别的选择。' } }
|
|
2454
2679
|
}
|
|
2455
|
-
if (
|
|
2680
|
+
if (isStepSettled(pending, pending.step)) {
|
|
2681
|
+
if (pending.step + 1 < pending.questions.length) {
|
|
2682
|
+
// Queue the next question as its own card once this one is answered.
|
|
2683
|
+
void advanceQuestionCard(ctx, config, pending)
|
|
2684
|
+
return { toast: { type: 'success', content: `第 ${pending.step + 2} 题已发出。` } }
|
|
2685
|
+
}
|
|
2456
2686
|
finishQuestion(config, pending, 'answered')
|
|
2457
2687
|
return { toast: { type: 'success', content: '已提交。' } }
|
|
2458
2688
|
}
|
|
@@ -2462,20 +2692,28 @@ export async function handleQuestionCardAction(ctx, config, raw) {
|
|
|
2462
2692
|
|
|
2463
2693
|
/**
|
|
2464
2694
|
* Consume a text reply as the answer to one pending question in that chat.
|
|
2465
|
-
* @returns true when the message was the answer and must not start a turn
|
|
2695
|
+
* @returns true when the message was the answer and must not start a turn
|
|
2466
2696
|
*/
|
|
2467
2697
|
export function consumeQuestionTextAnswer(ctx, config, { chatId, senderId, text }) {
|
|
2468
2698
|
if (config.questionCards === false) return false
|
|
2469
2699
|
const pending = [...pendingQuestions.values()].find((item) => item.chatId === chatId && !item.done)
|
|
2470
2700
|
if (!pending) return false
|
|
2471
|
-
|
|
2701
|
+
if (pending.advancing === true) return false
|
|
2702
|
+
const index = pending.step ?? 0
|
|
2703
|
+
const question = pending.questions[index]
|
|
2704
|
+
const answers = parseQuestionText(text, [question])
|
|
2472
2705
|
if (!answers) return false
|
|
2473
2706
|
lastInboundAt = Date.now()
|
|
2474
|
-
|
|
2707
|
+
recordTextAnswer(pending, index, answers[0])
|
|
2475
2708
|
logInbound(config, {
|
|
2476
2709
|
event: 'question', result: 'answered', via: 'text', sessionId: pending.sessionId,
|
|
2477
|
-
callId: pending.callId, senderId, chatId,
|
|
2710
|
+
callId: pending.callId, senderId, chatId, step: index,
|
|
2478
2711
|
})
|
|
2712
|
+
if (index + 1 < pending.questions.length) {
|
|
2713
|
+
void advanceQuestionCard(ctx, config, pending)
|
|
2714
|
+
} else {
|
|
2715
|
+
finishQuestion(config, pending, 'answered')
|
|
2716
|
+
}
|
|
2479
2717
|
return true
|
|
2480
2718
|
}
|
|
2481
2719
|
|
package/package.json
CHANGED