dsh-channel-feishu 0.8.2 → 0.8.4
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 +1 -1
- package/index.js +305 -65
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -82,7 +82,7 @@ dsh plugin add dsh-channel-feishu
|
|
|
82
82
|
|
|
83
83
|
```bash
|
|
84
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.
|
|
85
|
+
dsh plugin add https://github.com/Poy-Coder/dsh-channel-feishu/releases/download/v0.8.3/dsh-channel-feishu-0.8.3.tgz
|
|
86
86
|
```
|
|
87
87
|
|
|
88
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,145 @@ 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
|
+
// Fine print (option descriptions plus the reply hint) sits right above the
|
|
2196
|
+
// action row: explanations must not split the options from their buttons.
|
|
2197
|
+
const finePrint = []
|
|
2198
|
+
options.forEach((option, optionIndex) => {
|
|
2199
|
+
if (!option?.description) return
|
|
2200
|
+
finePrint.push(`<font color="grey">${optionIndex + 1} · ${option.description}</font>`)
|
|
2201
|
+
})
|
|
2202
|
+
finePrint.push(`<font color="grey">${multi
|
|
2203
|
+
? '可多选:点选项切换选中,选好后点「提交」。'
|
|
2204
|
+
: '点上面的按钮作答,也可直接回复序号 / 选项文字。'}</font>`)
|
|
2205
|
+
elements.push({ tag: 'markdown', text_size: 'notation', content: finePrint.join('\n') })
|
|
2206
|
+
|
|
2207
|
+
// The action row closes the block, so 提交 / 跳过 always sit at the very
|
|
2208
|
+
// bottom no matter how many options the fold reveals.
|
|
2209
|
+
const actions = []
|
|
2210
|
+
if (multi) {
|
|
2211
|
+
actions.push({
|
|
2212
|
+
tag: 'button',
|
|
2213
|
+
width: 'fill',
|
|
2214
|
+
text: { tag: 'plain_text', content: '提交' },
|
|
2215
|
+
type: 'primary',
|
|
2216
|
+
behaviors: [{ type: 'callback', value: { c: callId, q: offset + index, submit: true } }],
|
|
2128
2217
|
})
|
|
2129
2218
|
}
|
|
2130
2219
|
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
|
-
}],
|
|
2220
|
+
actions.push({
|
|
2221
|
+
tag: 'button',
|
|
2222
|
+
width: 'fill',
|
|
2223
|
+
text: { tag: 'plain_text', content: '跳过' },
|
|
2224
|
+
type: 'default',
|
|
2225
|
+
behaviors: [{ type: 'callback', value: { c: callId, q: offset + index, skip: true } }],
|
|
2140
2226
|
})
|
|
2141
2227
|
}
|
|
2142
|
-
elements.push(
|
|
2228
|
+
if (actions.length === 1) elements.push(actions[0])
|
|
2229
|
+
else if (actions.length > 1) elements.push(fillRow(actions))
|
|
2143
2230
|
})
|
|
2144
2231
|
const noteText = note || '等待你的回答…'
|
|
2145
2232
|
if (note !== null && noteText !== '') {
|
|
@@ -2151,11 +2238,20 @@ export function questionCardJson({ callId, questions, selections, skipped, note,
|
|
|
2151
2238
|
})
|
|
2152
2239
|
}
|
|
2153
2240
|
const count = questions.length
|
|
2241
|
+
const headline = title || (count > 1 ? `有 ${count} 个问题` : '需要你的回答')
|
|
2154
2242
|
return {
|
|
2155
2243
|
schema: '2.0',
|
|
2156
|
-
|
|
2244
|
+
// streaming_mode keeps the CardKit entity open for element updates: without
|
|
2245
|
+
// it every status write fails with `300309 streaming mode is closed`. The
|
|
2246
|
+
// card is closed once the question settles.
|
|
2247
|
+
config: {
|
|
2248
|
+
streaming_mode: true,
|
|
2249
|
+
update_multi: true,
|
|
2250
|
+
width_mode: 'fill',
|
|
2251
|
+
summary: { content: headline },
|
|
2252
|
+
},
|
|
2157
2253
|
header: {
|
|
2158
|
-
title: { tag: 'plain_text', content:
|
|
2254
|
+
title: { tag: 'plain_text', content: headline },
|
|
2159
2255
|
template: answered === true ? 'green' : 'blue',
|
|
2160
2256
|
},
|
|
2161
2257
|
body: { elements },
|
|
@@ -2233,14 +2329,20 @@ export function applyQuestionSelection(pending, value) {
|
|
|
2233
2329
|
return true
|
|
2234
2330
|
}
|
|
2235
2331
|
|
|
2332
|
+
/** True when one question of the batch already holds a usable answer. */
|
|
2333
|
+
export function isStepSettled(pending, index) {
|
|
2334
|
+
const question = pending?.questions?.[index]
|
|
2335
|
+
if (!question) return false
|
|
2336
|
+
if (pending.skipped.has(index)) return true
|
|
2337
|
+
if (question.options.length === 0) return false
|
|
2338
|
+
const picked = pending.selections?.[index]
|
|
2339
|
+
if (!picked || picked.size === 0) return false
|
|
2340
|
+
return pending.submitted.has(index)
|
|
2341
|
+
}
|
|
2342
|
+
|
|
2236
2343
|
export function isQuestionComplete(pending) {
|
|
2237
2344
|
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
|
-
})
|
|
2345
|
+
return pending.questions.every((question, index) => isStepSettled(pending, index))
|
|
2244
2346
|
}
|
|
2245
2347
|
|
|
2246
2348
|
/** The batch the host accepts: one item per question, in order. */
|
|
@@ -2250,7 +2352,8 @@ export function questionAnswersOf(pending) {
|
|
|
2250
2352
|
const selected = chosen
|
|
2251
2353
|
.filter((optionIndex) => optionIndex >= 0 && optionIndex < question.options.length)
|
|
2252
2354
|
.map((optionIndex) => question.options[optionIndex].label)
|
|
2253
|
-
|
|
2355
|
+
const custom = pending.customs?.[index]
|
|
2356
|
+
return { id: question.id, selected, ...(custom ? { custom: String(custom) } : {}) }
|
|
2254
2357
|
})
|
|
2255
2358
|
}
|
|
2256
2359
|
|
|
@@ -2265,6 +2368,7 @@ function questionChatOf(config, sessionId) {
|
|
|
2265
2368
|
return ''
|
|
2266
2369
|
}
|
|
2267
2370
|
|
|
2371
|
+
/** Rewrite every live element of one question card (ticks, then the status line). */
|
|
2268
2372
|
async function refreshQuestionCard(config, pending, status, answers) {
|
|
2269
2373
|
if (!pending.cardId) return
|
|
2270
2374
|
const summary = status === 'answered'
|
|
@@ -2277,22 +2381,113 @@ async function refreshQuestionCard(config, pending, status, answers) {
|
|
|
2277
2381
|
? '已取消。'
|
|
2278
2382
|
: '等待你的回答…'
|
|
2279
2383
|
if (!summary) return
|
|
2384
|
+
// One card carries exactly one step, so only that step's elements exist to
|
|
2385
|
+
// rewrite: its multi-select tick line (when it has options) and the status line.
|
|
2386
|
+
const writes = []
|
|
2387
|
+
const index = pending.step ?? 0
|
|
2388
|
+
const question = pending.questions[index]
|
|
2389
|
+
if (question?.multiSelect === true) {
|
|
2390
|
+
writes.push({
|
|
2391
|
+
elementId: `${QA_SELECTION_ELEMENT_PREFIX}${index}`,
|
|
2392
|
+
content: questionSelectionText(question, pending.selections?.[index]),
|
|
2393
|
+
})
|
|
2394
|
+
}
|
|
2395
|
+
writes.push({ elementId: QA_STATUS_ELEMENT_ID, content: `<font color="grey">${summary}</font>` })
|
|
2396
|
+
for (const write of writes) {
|
|
2397
|
+
try {
|
|
2398
|
+
pending.sequence += 1
|
|
2399
|
+
await provider().card.update({
|
|
2400
|
+
cardId: pending.cardId,
|
|
2401
|
+
content: write.content,
|
|
2402
|
+
sequence: pending.sequence,
|
|
2403
|
+
elementId: write.elementId,
|
|
2404
|
+
})
|
|
2405
|
+
} catch (error) {
|
|
2406
|
+
logInbound(config, {
|
|
2407
|
+
event: 'question', result: 'card-update-failed',
|
|
2408
|
+
sessionId: pending.sessionId, callId: pending.callId, elementId: write.elementId,
|
|
2409
|
+
reason: errorText(error),
|
|
2410
|
+
})
|
|
2411
|
+
}
|
|
2412
|
+
}
|
|
2413
|
+
}
|
|
2414
|
+
|
|
2415
|
+
/** Settle the card: close its streaming mode so it stops accepting updates. */
|
|
2416
|
+
async function closeQuestionCard(config, pending, status, answers) {
|
|
2417
|
+
if (!pending.cardId) return
|
|
2280
2418
|
try {
|
|
2281
2419
|
pending.sequence += 1
|
|
2282
|
-
await provider().card.
|
|
2420
|
+
await provider().card.close({
|
|
2283
2421
|
cardId: pending.cardId,
|
|
2284
|
-
content: `<font color="grey">${summary}</font>`,
|
|
2285
2422
|
sequence: pending.sequence,
|
|
2286
|
-
|
|
2423
|
+
summary: status === 'answered'
|
|
2424
|
+
? `已选择:${(answers ?? []).map((item) => item.selected.join('、') || (item.custom ? `“${item.custom}”` : '跳过')).join(';')}`
|
|
2425
|
+
: status === 'timeout' ? '等待超时,已跳过。' : status === 'skipped' ? '已跳过。' : '已取消。',
|
|
2287
2426
|
})
|
|
2288
2427
|
} catch (error) {
|
|
2289
2428
|
logInbound(config, {
|
|
2290
|
-
event: 'question', result: 'card-
|
|
2429
|
+
event: 'question', result: 'card-close-failed',
|
|
2291
2430
|
sessionId: pending.sessionId, callId: pending.callId, reason: errorText(error),
|
|
2292
2431
|
})
|
|
2293
2432
|
}
|
|
2294
2433
|
}
|
|
2295
2434
|
|
|
2435
|
+
/** Fold one parsed text reply into the pending state for its step. */
|
|
2436
|
+
function recordTextAnswer(pending, index, answer) {
|
|
2437
|
+
const question = pending.questions[index]
|
|
2438
|
+
const picked = new Set()
|
|
2439
|
+
for (const label of answer?.selected ?? []) {
|
|
2440
|
+
const at = question.options.findIndex((option) => sameLabel(option.label, label))
|
|
2441
|
+
if (at >= 0) picked.add(at)
|
|
2442
|
+
}
|
|
2443
|
+
pending.selections[index] = picked
|
|
2444
|
+
pending.submitted.add(index)
|
|
2445
|
+
if (picked.size === 0) pending.skipped.add(index)
|
|
2446
|
+
if (answer?.custom !== undefined) {
|
|
2447
|
+
pending.customs = pending.customs ?? {}
|
|
2448
|
+
pending.customs[index] = String(answer.custom)
|
|
2449
|
+
}
|
|
2450
|
+
}
|
|
2451
|
+
|
|
2452
|
+
/**
|
|
2453
|
+
* Close the answered step's card and post the next question as its own card.
|
|
2454
|
+
*
|
|
2455
|
+
* A batch of questions is asked one at a time, so each card stays short and the
|
|
2456
|
+
* user only sees the question that is actually waiting for an answer.
|
|
2457
|
+
*/
|
|
2458
|
+
async function advanceQuestionCard(ctx, config, pending) {
|
|
2459
|
+
if (pending.advancing === true) return false
|
|
2460
|
+
pending.advancing = true
|
|
2461
|
+
try {
|
|
2462
|
+
const index = pending.step
|
|
2463
|
+
const question = pending.questions[index]
|
|
2464
|
+
const chosen = pending.selections[index] instanceof Set ? [...pending.selections[index]] : []
|
|
2465
|
+
const selected = chosen
|
|
2466
|
+
.sort((left, right) => left - right)
|
|
2467
|
+
.map((at) => question.options[at]?.label ?? '')
|
|
2468
|
+
.filter(Boolean)
|
|
2469
|
+
const settled = [{ id: question.id, selected }]
|
|
2470
|
+
await refreshQuestionCard(config, pending, 'answered', settled)
|
|
2471
|
+
await closeQuestionCard(config, pending, 'answered', settled)
|
|
2472
|
+
pending.step = index + 1
|
|
2473
|
+
pending.cardId = ''
|
|
2474
|
+
pending.messageId = ''
|
|
2475
|
+
pending.sequence = 1
|
|
2476
|
+
const sent = await sendQuestionCard(ctx, config, pending)
|
|
2477
|
+
if (!sent) {
|
|
2478
|
+
finishQuestion(config, pending, 'answered')
|
|
2479
|
+
} else if (pending.timeoutMs > 0) {
|
|
2480
|
+
// Every step gets the full answer window instead of sharing one deadline.
|
|
2481
|
+
if (pending.timer) clearTimeout(pending.timer)
|
|
2482
|
+
pending.timer = setTimeout(() => finishQuestion(config, pending, 'timeout'), pending.timeoutMs)
|
|
2483
|
+
pending.timer.unref?.()
|
|
2484
|
+
}
|
|
2485
|
+
return sent
|
|
2486
|
+
} finally {
|
|
2487
|
+
pending.advancing = false
|
|
2488
|
+
}
|
|
2489
|
+
}
|
|
2490
|
+
|
|
2296
2491
|
/**
|
|
2297
2492
|
* Settle one pending question exactly once.
|
|
2298
2493
|
* @param status - answered | skipped | timeout | aborted
|
|
@@ -2324,13 +2519,24 @@ function finishQuestion(config, pending, status, explicitAnswers) {
|
|
|
2324
2519
|
try { pending.resolve(answers) } catch { /* ignore */ }
|
|
2325
2520
|
}
|
|
2326
2521
|
void refreshQuestionCard(config, pending, status, answers)
|
|
2522
|
+
.then(() => closeQuestionCard(config, pending, status, answers))
|
|
2327
2523
|
return true
|
|
2328
2524
|
}
|
|
2329
2525
|
|
|
2330
2526
|
/** Send the question as a card; fall back to plain text if card creation fails. */
|
|
2331
2527
|
async function sendQuestionCard(ctx, config, pending) {
|
|
2528
|
+
const step = pending.step ?? 0
|
|
2529
|
+
const question = pending.questions[step]
|
|
2530
|
+
const total = pending.questions.length
|
|
2332
2531
|
try {
|
|
2333
|
-
const card = questionCardJson({
|
|
2532
|
+
const card = questionCardJson({
|
|
2533
|
+
callId: pending.callId,
|
|
2534
|
+
questions: [question],
|
|
2535
|
+
selections: [pending.selections[step]],
|
|
2536
|
+
offset: step,
|
|
2537
|
+
title: total > 1 ? `第 ${step + 1}/${total} 题` : undefined,
|
|
2538
|
+
note: total > 1 ? `第 ${step + 1}/${total} 题 · 等待你的回答…` : '等待你的回答…',
|
|
2539
|
+
})
|
|
2334
2540
|
const cardId = await provider().card.createRaw({ card })
|
|
2335
2541
|
const messageId = await provider().card.attach({ chatId: pending.chatId, cardId })
|
|
2336
2542
|
pending.cardId = cardId
|
|
@@ -2338,6 +2544,7 @@ async function sendQuestionCard(ctx, config, pending) {
|
|
|
2338
2544
|
logInbound(config, {
|
|
2339
2545
|
event: 'question', result: 'card-sent', sessionId: pending.sessionId,
|
|
2340
2546
|
callId: pending.callId, chatId: pending.chatId, cardId, messageId,
|
|
2547
|
+
step, count: total,
|
|
2341
2548
|
})
|
|
2342
2549
|
return true
|
|
2343
2550
|
} catch (error) {
|
|
@@ -2349,7 +2556,7 @@ async function sendQuestionCard(ctx, config, pending) {
|
|
|
2349
2556
|
})
|
|
2350
2557
|
}
|
|
2351
2558
|
try {
|
|
2352
|
-
await provider().sendText({ chatId: pending.chatId, text: questionText(
|
|
2559
|
+
await provider().sendText({ chatId: pending.chatId, text: questionText([question]) })
|
|
2353
2560
|
logInbound(config, {
|
|
2354
2561
|
event: 'question', result: 'text-sent', sessionId: pending.sessionId,
|
|
2355
2562
|
callId: pending.callId, chatId: pending.chatId,
|
|
@@ -2388,6 +2595,9 @@ export async function answerUserQuestion(ctx, config, request, next) {
|
|
|
2388
2595
|
selections: questions.map(() => new Set()),
|
|
2389
2596
|
skipped: new Set(),
|
|
2390
2597
|
submitted: new Set(),
|
|
2598
|
+
customs: {},
|
|
2599
|
+
step: 0,
|
|
2600
|
+
timeoutMs,
|
|
2391
2601
|
messageId: '', cardId: '', sequence: 1,
|
|
2392
2602
|
done: false, status: '', signal: request?.signal,
|
|
2393
2603
|
resolve: undefined, reject: undefined, timer: undefined, abortHandler: undefined,
|
|
@@ -2445,6 +2655,23 @@ export async function handleQuestionCardAction(ctx, config, raw) {
|
|
|
2445
2655
|
?? [...pendingQuestions.values()].find((item) => item.chatId === action.chatId && String(action.value?.c ?? '') === item.callId)
|
|
2446
2656
|
if (!pending) return { toast: { type: 'info', content: '这个问题已经结束。' } }
|
|
2447
2657
|
if (pending.chatId && action.chatId && action.chatId !== pending.chatId) return undefined
|
|
2658
|
+
// Each step owns its own card; a click on an earlier (or already answered)
|
|
2659
|
+
// card must not touch the question that is live now.
|
|
2660
|
+
const clickedStep = Number(action.value?.q)
|
|
2661
|
+
if (Number.isInteger(clickedStep) && clickedStep !== pending.step) {
|
|
2662
|
+
return { toast: { type: 'info', content: '这题已经答过了。' } }
|
|
2663
|
+
}
|
|
2664
|
+
if (pending.advancing === true) {
|
|
2665
|
+
return { toast: { type: 'info', content: '正在发送下一题…' } }
|
|
2666
|
+
}
|
|
2667
|
+
// 提交 with nothing ticked would only re-render the card; say so instead.
|
|
2668
|
+
if (action.value?.submit === true) {
|
|
2669
|
+
const question = pending.questions[pending.step]
|
|
2670
|
+
const picked = pending.selections?.[pending.step]
|
|
2671
|
+
if (question?.multiSelect === true && (!picked || picked.size === 0)) {
|
|
2672
|
+
return { toast: { type: 'warning', content: '先勾选至少一项,再点「提交」。' } }
|
|
2673
|
+
}
|
|
2674
|
+
}
|
|
2448
2675
|
if (!applyQuestionSelection(pending, action.value)) {
|
|
2449
2676
|
logInbound(config, {
|
|
2450
2677
|
event: 'question', result: 'card-bad-value', sessionId: pending.sessionId,
|
|
@@ -2452,7 +2679,12 @@ export async function handleQuestionCardAction(ctx, config, raw) {
|
|
|
2452
2679
|
})
|
|
2453
2680
|
return { toast: { type: 'error', content: '无法识别的选择。' } }
|
|
2454
2681
|
}
|
|
2455
|
-
if (
|
|
2682
|
+
if (isStepSettled(pending, pending.step)) {
|
|
2683
|
+
if (pending.step + 1 < pending.questions.length) {
|
|
2684
|
+
// Queue the next question as its own card once this one is answered.
|
|
2685
|
+
void advanceQuestionCard(ctx, config, pending)
|
|
2686
|
+
return { toast: { type: 'success', content: `第 ${pending.step + 2} 题已发出。` } }
|
|
2687
|
+
}
|
|
2456
2688
|
finishQuestion(config, pending, 'answered')
|
|
2457
2689
|
return { toast: { type: 'success', content: '已提交。' } }
|
|
2458
2690
|
}
|
|
@@ -2462,20 +2694,28 @@ export async function handleQuestionCardAction(ctx, config, raw) {
|
|
|
2462
2694
|
|
|
2463
2695
|
/**
|
|
2464
2696
|
* 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
|
|
2697
|
+
* @returns true when the message was the answer and must not start a turn
|
|
2466
2698
|
*/
|
|
2467
2699
|
export function consumeQuestionTextAnswer(ctx, config, { chatId, senderId, text }) {
|
|
2468
2700
|
if (config.questionCards === false) return false
|
|
2469
2701
|
const pending = [...pendingQuestions.values()].find((item) => item.chatId === chatId && !item.done)
|
|
2470
2702
|
if (!pending) return false
|
|
2471
|
-
|
|
2703
|
+
if (pending.advancing === true) return false
|
|
2704
|
+
const index = pending.step ?? 0
|
|
2705
|
+
const question = pending.questions[index]
|
|
2706
|
+
const answers = parseQuestionText(text, [question])
|
|
2472
2707
|
if (!answers) return false
|
|
2473
2708
|
lastInboundAt = Date.now()
|
|
2474
|
-
|
|
2709
|
+
recordTextAnswer(pending, index, answers[0])
|
|
2475
2710
|
logInbound(config, {
|
|
2476
2711
|
event: 'question', result: 'answered', via: 'text', sessionId: pending.sessionId,
|
|
2477
|
-
callId: pending.callId, senderId, chatId,
|
|
2712
|
+
callId: pending.callId, senderId, chatId, step: index,
|
|
2478
2713
|
})
|
|
2714
|
+
if (index + 1 < pending.questions.length) {
|
|
2715
|
+
void advanceQuestionCard(ctx, config, pending)
|
|
2716
|
+
} else {
|
|
2717
|
+
finishQuestion(config, pending, 'answered')
|
|
2718
|
+
}
|
|
2479
2719
|
return true
|
|
2480
2720
|
}
|
|
2481
2721
|
|
package/package.json
CHANGED