mirror of
https://github.com/hansjone/dsh-im-ops.git
synced 2026-10-14 05:50:47 +08:00
fix(feishu): preserve interaction card reply semantics
This commit is contained in:
parent
3d25137824
commit
2b6f4bdfb0
3 changed files with 112 additions and 7 deletions
File diff suppressed because one or more lines are too long
|
|
@ -1677,10 +1677,15 @@ export class FeishuHarnessBridge {
|
||||||
}
|
}
|
||||||
const conversationType = route.key.startsWith('p2p:') ? 'direct'
|
const conversationType = route.key.startsWith('p2p:') ? 'direct'
|
||||||
: route.key.startsWith('group:') ? 'group' : null;
|
: route.key.startsWith('group:') ? 'group' : null;
|
||||||
|
const isInteractionResponse = resolvedAction.startsWith('approve:')
|
||||||
|
|| resolvedAction.startsWith('reject:')
|
||||||
|
|| resolvedAction.startsWith('answer:');
|
||||||
const access = evaluateInboundAccess(this.#accessPolicy, {
|
const access = evaluateInboundAccess(this.#accessPolicy, {
|
||||||
conversationType,
|
conversationType,
|
||||||
senderIds: operatorOpenId,
|
senderIds: operatorOpenId,
|
||||||
isCommand: true,
|
// These buttons are the card equivalent of an ordinary approval or
|
||||||
|
// question reply. Every other card action remains command-gated.
|
||||||
|
isCommand: !isInteractionResponse,
|
||||||
});
|
});
|
||||||
if (!access.allowed) {
|
if (!access.allowed) {
|
||||||
if (access.reason === 'command-not-allowed') {
|
if (access.reason === 'command-not-allowed') {
|
||||||
|
|
@ -3878,7 +3883,7 @@ export class FeishuHarnessBridge {
|
||||||
// Fall back to the plain-text question if the card cannot be sent.
|
// Fall back to the plain-text question if the card cannot be sent.
|
||||||
// If the text send also fails, let the error propagate so the pending
|
// If the text send also fails, let the error propagate so the pending
|
||||||
// question is not marked as presented and the existing retry logic runs.
|
// question is not marked as presented and the existing retry logic runs.
|
||||||
await this.#send(
|
return this.#send(
|
||||||
pending.chatId,
|
pending.chatId,
|
||||||
harnessQuestionText(question, pending.index, pending.questions.length, {
|
harnessQuestionText(question, pending.index, pending.questions.length, {
|
||||||
requiresMention: pending.requiresMention,
|
requiresMention: pending.requiresMention,
|
||||||
|
|
|
||||||
|
|
@ -1966,7 +1966,9 @@ test('an approval is presented as an interactive card with approve and reject bu
|
||||||
},
|
},
|
||||||
state: fixture.state,
|
state: fixture.state,
|
||||||
status: bridgeStatus(),
|
status: bridgeStatus(),
|
||||||
allowedSenderOpenIds: new Set(['ou_user']),
|
accessPolicy: directAccessPolicy({
|
||||||
|
users: [{ id: 'ou_user', canExecuteCommands: false }],
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
const turn = bridge.accept(event('approval-card-start', '执行危险命令'));
|
const turn = bridge.accept(event('approval-card-start', '执行危险命令'));
|
||||||
|
|
@ -1987,6 +1989,10 @@ test('an approval is presented as an interactive card with approve and reject bu
|
||||||
assert.equal(decisions.length, 0);
|
assert.equal(decisions.length, 0);
|
||||||
|
|
||||||
await bridge.onCardAction(cardActionEvent('om_card_1', 'approve:approval-card-id', 'ou_user'));
|
await bridge.onCardAction(cardActionEvent('om_card_1', 'approve:approval-card-id', 'ou_user'));
|
||||||
|
await eventually(
|
||||||
|
() => decisions.length === 1,
|
||||||
|
'an allowed ordinary-message user could not approve their own interaction',
|
||||||
|
);
|
||||||
await turn;
|
await turn;
|
||||||
assert.deepEqual(decisions, [{
|
assert.deepEqual(decisions, [{
|
||||||
ok: true,
|
ok: true,
|
||||||
|
|
@ -2039,7 +2045,9 @@ test('approval card reject button submits a rejected outcome', async () => {
|
||||||
},
|
},
|
||||||
state: fixture.state,
|
state: fixture.state,
|
||||||
status: bridgeStatus(),
|
status: bridgeStatus(),
|
||||||
allowedSenderOpenIds: new Set(['ou_user']),
|
accessPolicy: directAccessPolicy({
|
||||||
|
users: [{ id: 'ou_user', canExecuteCommands: false }],
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
const turn = bridge.accept(event('approval-reject-start', '覆盖文件'));
|
const turn = bridge.accept(event('approval-reject-start', '覆盖文件'));
|
||||||
|
|
@ -2049,6 +2057,10 @@ test('approval card reject button submits a rejected outcome', async () => {
|
||||||
);
|
);
|
||||||
|
|
||||||
await bridge.onCardAction(cardActionEvent('om_card_1', 'reject:approval-reject-id', 'ou_user'));
|
await bridge.onCardAction(cardActionEvent('om_card_1', 'reject:approval-reject-id', 'ou_user'));
|
||||||
|
await eventually(
|
||||||
|
() => decisions.length === 1,
|
||||||
|
'an allowed ordinary-message user could not reject their own interaction',
|
||||||
|
);
|
||||||
await turn;
|
await turn;
|
||||||
assert.deepEqual(decisions, [{
|
assert.deepEqual(decisions, [{
|
||||||
ok: true,
|
ok: true,
|
||||||
|
|
@ -2064,6 +2076,7 @@ test('a single-choice question is presented as a card with option buttons by def
|
||||||
const fixture = stateFixture([['p2p:ou_user', 'session-question-card']]);
|
const fixture = stateFixture([['p2p:ou_user', 'session-question-card']]);
|
||||||
const sent = [];
|
const sent = [];
|
||||||
const submitted = deferred();
|
const submitted = deferred();
|
||||||
|
let submittedResult;
|
||||||
const bridge = new FeishuHarnessBridge({
|
const bridge = new FeishuHarnessBridge({
|
||||||
client: cardClient(async (outgoing) => sent.push(outgoing)),
|
client: cardClient(async (outgoing) => sent.push(outgoing)),
|
||||||
harness: {
|
harness: {
|
||||||
|
|
@ -2086,6 +2099,7 @@ test('a single-choice question is presented as a card with option buttons by def
|
||||||
}],
|
}],
|
||||||
},
|
},
|
||||||
respond: async (result) => {
|
respond: async (result) => {
|
||||||
|
submittedResult = result;
|
||||||
submitted.resolve(result);
|
submitted.resolve(result);
|
||||||
return { accepted: true };
|
return { accepted: true };
|
||||||
},
|
},
|
||||||
|
|
@ -2096,7 +2110,9 @@ test('a single-choice question is presented as a card with option buttons by def
|
||||||
},
|
},
|
||||||
state: fixture.state,
|
state: fixture.state,
|
||||||
status: bridgeStatus(),
|
status: bridgeStatus(),
|
||||||
allowedSenderOpenIds: new Set(['ou_user']),
|
accessPolicy: directAccessPolicy({
|
||||||
|
users: [{ id: 'ou_user', canExecuteCommands: false }],
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
const turn = bridge.accept(event('question-card-start', '请先调用 ask_user_question'));
|
const turn = bridge.accept(event('question-card-start', '请先调用 ask_user_question'));
|
||||||
|
|
@ -2115,6 +2131,10 @@ test('a single-choice question is presented as a card with option buttons by def
|
||||||
await bridge.onCardAction(
|
await bridge.onCardAction(
|
||||||
cardActionEvent('om_card_1', 'answer:question-card-id:0:测试环境', 'ou_user'),
|
cardActionEvent('om_card_1', 'answer:question-card-id:0:测试环境', 'ou_user'),
|
||||||
);
|
);
|
||||||
|
await eventually(
|
||||||
|
() => submittedResult !== undefined,
|
||||||
|
'an allowed ordinary-message user could not answer their own interaction',
|
||||||
|
);
|
||||||
await turn;
|
await turn;
|
||||||
assert.deepEqual(await submitted.promise, {
|
assert.deepEqual(await submitted.promise, {
|
||||||
ok: true,
|
ok: true,
|
||||||
|
|
@ -2203,6 +2223,86 @@ test('an interaction card falls back to plain text when the card send fails', as
|
||||||
}]);
|
}]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('a resolved question remembers the text fallback message after its card send fails', async () => {
|
||||||
|
const fixture = stateFixture([['p2p:ou_user', 'session-question-fallback-thread']]);
|
||||||
|
const sent = [];
|
||||||
|
const asked = [];
|
||||||
|
const resolved = deferred();
|
||||||
|
let resolveInteraction;
|
||||||
|
const client = {
|
||||||
|
im: { v1: { message: {
|
||||||
|
create: async (request) => {
|
||||||
|
if (request.data.msg_type === 'interactive') throw new Error('card disabled');
|
||||||
|
const messageId = `om_question_fallback_${sent.length + 1}`;
|
||||||
|
sent.push({
|
||||||
|
messageId,
|
||||||
|
text: JSON.parse(request.data.content).text,
|
||||||
|
});
|
||||||
|
return { code: 0, data: { message_id: messageId } };
|
||||||
|
},
|
||||||
|
} } },
|
||||||
|
};
|
||||||
|
const bridge = new FeishuHarnessBridge({
|
||||||
|
client,
|
||||||
|
harness: {
|
||||||
|
sessionExists: async () => true,
|
||||||
|
createSession: async () => assert.fail('the existing session should be reused'),
|
||||||
|
ask: async (sessionId, text, options) => {
|
||||||
|
asked.push(text);
|
||||||
|
if (asked.length > 1) return 'late fallback reply was handled as a new prompt';
|
||||||
|
await options.onInteraction({
|
||||||
|
kind: 'question',
|
||||||
|
interactionId: 'question-fallback-thread',
|
||||||
|
rpcId: 'question-fallback-thread',
|
||||||
|
sessionId,
|
||||||
|
payload: {
|
||||||
|
type: 'question/requested',
|
||||||
|
sessionId,
|
||||||
|
questions: [{
|
||||||
|
id: 'environment',
|
||||||
|
question: '请选择环境',
|
||||||
|
options: [{ label: '测试环境' }, { label: '生产环境' }],
|
||||||
|
}],
|
||||||
|
},
|
||||||
|
respond: async () => ({ accepted: true }),
|
||||||
|
});
|
||||||
|
resolveInteraction = async () => {
|
||||||
|
await options.onInteractionResolved({
|
||||||
|
kind: 'question',
|
||||||
|
interactionId: 'question-fallback-thread',
|
||||||
|
sessionId,
|
||||||
|
outcome: 'answered',
|
||||||
|
});
|
||||||
|
resolved.resolve();
|
||||||
|
};
|
||||||
|
await resolved.promise;
|
||||||
|
return '已由其他客户端完成';
|
||||||
|
},
|
||||||
|
},
|
||||||
|
state: fixture.state,
|
||||||
|
status: bridgeStatus(),
|
||||||
|
allowedSenderOpenIds: new Set(['ou_user']),
|
||||||
|
});
|
||||||
|
|
||||||
|
const first = bridge.accept(event('question-fallback-start', '启动卡片降级问题'));
|
||||||
|
await eventually(() => (
|
||||||
|
typeof resolveInteraction === 'function'
|
||||||
|
&& sent.some(({ text }) => text.includes('请选择环境'))
|
||||||
|
));
|
||||||
|
const questionMessageId = sent.find(({ text }) => text.includes('请选择环境')).messageId;
|
||||||
|
await resolveInteraction();
|
||||||
|
await first;
|
||||||
|
|
||||||
|
await bridge.accept(event('question-fallback-late', '1', {
|
||||||
|
root_id: 'question-fallback-start',
|
||||||
|
parent_id: questionMessageId,
|
||||||
|
thread_id: 'omt_question_fallback',
|
||||||
|
}));
|
||||||
|
|
||||||
|
assert.deepEqual(asked, ['启动卡片降级问题']);
|
||||||
|
assert.equal(sent.some(({ text }) => text.includes('已在其他客户端处理')), true);
|
||||||
|
});
|
||||||
|
|
||||||
test('a different allowed group member cannot approve or answer an interaction card', async () => {
|
test('a different allowed group member cannot approve or answer an interaction card', async () => {
|
||||||
const fixture = stateFixture([['group:oc_group', 'session-group-actor']]);
|
const fixture = stateFixture([['group:oc_group', 'session-group-actor']]);
|
||||||
const sent = [];
|
const sent = [];
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue