fix(feishu): make interactive card menus reliable

This commit is contained in:
xmanrui 2026-08-21 11:27:33 +08:00
parent 60a364118e
commit 0e62c69382
8 changed files with 232 additions and 71 deletions

View file

@ -131,7 +131,7 @@ export class FeishuHarnessBridge {
#signal;
/** Number-tappable menus: conversation key → menu state. */
#menus = new Map();
/** Interactive-card message id → { key, chatId } for button callbacks. */
/** Interactive-card message id → route context for button callbacks. */
#cardKeys = new Map();
constructor({
@ -514,9 +514,15 @@ export class FeishuHarnessBridge {
* session binding, workspace switches or other card actions.
*/
onCardAction(event) {
const operatorOpenId = nonEmptyString(event?.operator?.operator_id?.open_id)
const operatorOpenId = nonEmptyString(event?.operator?.open_id)
?? nonEmptyString(event?.operator?.user_id)
// Keep accepting the legacy nested shape while preferring the current
// card.action.trigger v2 payload used by the official SDK.
?? nonEmptyString(event?.operator?.operator_id?.open_id)
?? nonEmptyString(event?.operator?.operator_id?.user_id);
if (operatorOpenId === null || !this.#allowedSenderOpenIds.has(operatorOpenId)) {
const operatorAllowed = operatorOpenId !== null
&& (this.#allowedSenderOpenIds.has('*') || this.#allowedSenderOpenIds.has(operatorOpenId));
if (!operatorAllowed) {
this.#logger.warn?.('[dsh-feishu] ignoring card action from an unallowed sender');
return Promise.resolve();
}
@ -534,10 +540,10 @@ export class FeishuHarnessBridge {
});
}
async #handleCardAction(action, { chatId, key }) {
async #handleCardAction(action, { chatId, key, sessionWorkspace = null }) {
if (action === 'sessions' || /^sessions:\d+$/.test(action)) {
const page = action === 'sessions' ? 0 : Number(action.slice('sessions:'.length));
await this.#showSessions({ chatId, key }, null, page);
await this.#showSessions({ chatId, key }, sessionWorkspace, page);
return;
}
if (action === 'workspaces') {
@ -635,7 +641,12 @@ export class FeishuHarnessBridge {
kind: 'sessions',
sessions: sessions.slice(safePage * MENU_PAGE_SIZE, (safePage + 1) * MENU_PAGE_SIZE),
});
await this.#sendCard(chatId, sessionListCard(workspace, sessions, safePage, sessions.length), { key });
await this.#sendCard(chatId, sessionListCard(workspace, sessions, safePage, sessions.length), {
key,
// Keep the canonical selector result for later page callbacks. The
// list response's workspace is display data and is not authoritative.
sessionWorkspace: resolved.workspace,
});
} catch (error) {
this.#logger.warn?.('[dsh-feishu] session list failed:', error.message);
await this.#send(chatId, '暂时无法获取会话列表,请稍后重试。');
@ -682,7 +693,13 @@ export class FeishuHarnessBridge {
}
const messageId = nonEmptyString(response?.data?.message_id);
if (options.key && messageId) {
this.#cardKeys.set(messageId, { key: options.key, chatId });
this.#cardKeys.set(messageId, {
key: options.key,
chatId,
sessionWorkspace: typeof options.sessionWorkspace === 'string' && options.sessionWorkspace
? options.sessionWorkspace
: null,
});
if (this.#cardKeys.size > 200) {
const oldest = this.#cardKeys.keys().next().value;
if (oldest !== undefined) this.#cardKeys.delete(oldest);

View file

@ -2,13 +2,12 @@
* Feishu interactive-card builders for the dsh-im menu / session-list /
* workspace-list UX. All builders return the JSON string the
* `im.message.create` API expects as `content` for `msg_type: interactive`
* (card schema 2.0; buttons are plain body elements — the `action` container
* is gone in V2).
* (card schema 2.0; callback buttons live inside a column_set/column layout).
*
* Buttons carry a small `{ action }` value object that `card.action.trigger`
* events echo back (when the app subscribes that event); every button also
* carries a numeric label so the number-reply fallback stays usable without
* button callbacks.
* Buttons carry a small `{ action }` callback behavior that
* `card.action.trigger` events echo back (when the app subscribes that
* callback); every button also carries a numeric label so the number-reply
* fallback stays usable without button callbacks.
*/
export const MENU_PAGE_SIZE = 10;
@ -23,10 +22,20 @@ function markdown(content) {
function button(content, actionValue) {
return {
tag: 'button',
text: plainText(content),
type: 'default',
value: { action: actionValue },
tag: 'column_set',
flex_mode: 'none',
columns: [{
tag: 'column',
width: 'weighted',
weight: 1,
elements: [{
tag: 'button',
text: plainText(content),
type: 'default',
width: 'fill',
behaviors: [{ type: 'callback', value: { action: actionValue } }],
}],
}],
};
}
@ -66,7 +75,7 @@ export function sessionListCard(workspace, sessions, page, total) {
const elements = [
{ tag: 'div', text: markdown(`**工作区**:\`${workspace}\`\n共 **${total}** 个会话${total > MENU_PAGE_SIZE ? `(第 ${page + 1}/${pageCount} 页)` : ''}`) },
...slice.map((session, offset) => button(
`${start + offset + 1}. ${safeTitle(session.title)}`,
`${offset + 1}. ${safeTitle(session.title)}`,
`use:${session.sessionId}`,
)),
];

View file

@ -186,7 +186,8 @@ export class MultiBotDshFeishuController {
addons: {
preset: false,
scopes: { tenant: [...REQUIRED_TENANT_SCOPES] },
events: { items: { tenant: ['im.message.receive_v1', 'card.action.trigger'] } },
events: { items: { tenant: ['im.message.receive_v1'] } },
callbacks: { items: ['card.action.trigger'] },
},
});
this.#touch();

View file

@ -102,7 +102,8 @@ export class DshFeishuController {
addons: {
preset: false,
scopes: { tenant: [...REQUIRED_TENANT_SCOPES] },
events: { items: { tenant: ['im.message.receive_v1', 'card.action.trigger'] } },
events: { items: { tenant: ['im.message.receive_v1'] } },
callbacks: { items: ['card.action.trigger'] },
},
});
return this.status();