mirror of
https://github.com/hansjone/dsh-im-ops.git
synced 2026-10-09 01:53:21 +08:00
feat: add private chat batch input commands
This commit is contained in:
parent
f6cc1cae30
commit
068cf57259
28 changed files with 2665 additions and 219 deletions
|
|
@ -1998,3 +1998,138 @@ test('only the actor who started a DingTalk group interaction can answer it', as
|
|||
await Promise.all([first, intruder]);
|
||||
assert.deepEqual(asked, ['甲发起交互', '乙试图代答']);
|
||||
});
|
||||
|
||||
test('DingTalk private batch input submits once, cancels cleanly, and restores normal chat', async () => {
|
||||
const fixture = stateFixture();
|
||||
const asked = [];
|
||||
const sent = [];
|
||||
const bridge = new DingtalkHarnessBridge({
|
||||
api: { sendText: async ({ text }) => sent.push(text) },
|
||||
clientId: 'ding-client',
|
||||
clientSecret: 'host-secret',
|
||||
harness: {
|
||||
createSession: async () => 'session-batch',
|
||||
sessionExists: async () => true,
|
||||
ask: async (_sessionId, prompt) => {
|
||||
asked.push(prompt);
|
||||
return asked.length === 1 ? '批量完成' : '普通完成';
|
||||
},
|
||||
},
|
||||
state: fixture.state,
|
||||
});
|
||||
|
||||
await bridge.accept(message('batch-start', '/batch'));
|
||||
await bridge.accept(message('batch-one', '第一条'));
|
||||
await bridge.accept(message('batch-two', '第二条'));
|
||||
assert.deepEqual(asked, []);
|
||||
assert.equal(sent.length, 1, 'only /batch acknowledges before submission');
|
||||
|
||||
await bridge.accept(message('batch-send', '/send'));
|
||||
assert.equal(asked.length, 1);
|
||||
assert.match(asked[0], /\[消息 1\]\n第一条/);
|
||||
assert.match(asked[0], /\[消息 2\]\n第二条/);
|
||||
assert.equal(sent.at(-1), '批量完成');
|
||||
|
||||
await bridge.accept(message('cancel-start', '/batch'));
|
||||
await bridge.accept(message('cancel-data', '不得提交'));
|
||||
await bridge.accept(message('cancel-command', '/cancel'));
|
||||
assert.equal(asked.length, 1);
|
||||
assert.match(sent.at(-1), /丢弃 1 条/);
|
||||
|
||||
await bridge.accept(message('normal-after-batch', '普通问题'));
|
||||
assert.equal(asked.at(-1), '普通问题');
|
||||
assert.equal(sent.at(-1), '普通完成');
|
||||
});
|
||||
|
||||
test('DingTalk retains a private batch after Harness ask fails and lets /send retry it', async () => {
|
||||
const fixture = stateFixture();
|
||||
const asked = [];
|
||||
const sent = [];
|
||||
const bridge = new DingtalkHarnessBridge({
|
||||
api: { sendText: async ({ text }) => sent.push(text) },
|
||||
clientId: 'ding-client',
|
||||
clientSecret: 'host-secret',
|
||||
harness: {
|
||||
createSession: async () => 'session-batch-retry',
|
||||
sessionExists: async () => true,
|
||||
ask: async (_sessionId, prompt) => {
|
||||
asked.push(prompt);
|
||||
if (asked.length === 1) throw new Error('temporary Harness failure');
|
||||
return '重试成功';
|
||||
},
|
||||
},
|
||||
state: fixture.state,
|
||||
logger: { warn() {}, error() {} },
|
||||
});
|
||||
|
||||
await bridge.accept(message('batch-retry-start', '/batch'));
|
||||
await bridge.accept(message('batch-retry-data', '需要重试的内容'));
|
||||
await bridge.accept(message('batch-retry-first-send', '/send'));
|
||||
|
||||
assert.equal(asked.length, 1);
|
||||
assert.match(sent.at(-1), /已保留 1 条消息/);
|
||||
assert.match(sent.at(-1), /再次发送 \/send 重试/);
|
||||
|
||||
await bridge.accept(message('batch-retry-second-send', '/send'));
|
||||
|
||||
assert.equal(asked.length, 2);
|
||||
assert.equal(asked[1], asked[0]);
|
||||
assert.equal(sent.at(-1), '重试成功');
|
||||
});
|
||||
|
||||
test('DingTalk clears a private batch after turn-stopped without suggesting a batch retry', async () => {
|
||||
const fixture = stateFixture();
|
||||
const sent = [];
|
||||
let asks = 0;
|
||||
const bridge = new DingtalkHarnessBridge({
|
||||
api: { sendText: async ({ text }) => sent.push(text) },
|
||||
clientId: 'ding-client',
|
||||
clientSecret: 'host-secret',
|
||||
harness: {
|
||||
createSession: async () => 'session-batch-stopped',
|
||||
sessionExists: async () => true,
|
||||
ask: async () => {
|
||||
asks += 1;
|
||||
const error = new Error('turn stopped');
|
||||
error.code = 'turn-stopped';
|
||||
throw error;
|
||||
},
|
||||
},
|
||||
state: fixture.state,
|
||||
logger: { warn() {}, error() {} },
|
||||
});
|
||||
|
||||
await bridge.accept(message('batch-stopped-start', '/batch'));
|
||||
await bridge.accept(message('batch-stopped-data', '停止后应清除'));
|
||||
await bridge.accept(message('batch-stopped-send', '/send'));
|
||||
|
||||
assert.equal(asks, 1);
|
||||
assert.equal(sent.some((text) => text.includes('已保留')), false);
|
||||
|
||||
await bridge.accept(message('batch-stopped-send-again', '/send'));
|
||||
|
||||
assert.equal(asks, 1);
|
||||
assert.match(sent.at(-1), /当前没有待提交的批量内容/);
|
||||
});
|
||||
|
||||
test('DingTalk group batch commands are rejected without reaching Harness', async () => {
|
||||
const fixture = stateFixture();
|
||||
const sent = [];
|
||||
let asks = 0;
|
||||
const bridge = new DingtalkHarnessBridge({
|
||||
api: { sendText: async ({ text }) => sent.push(text) },
|
||||
clientId: 'ding-client',
|
||||
clientSecret: 'host-secret',
|
||||
harness: { ask: async () => { asks += 1; } },
|
||||
state: fixture.state,
|
||||
});
|
||||
|
||||
await bridge.accept(message('group-batch-command', '/batch', {
|
||||
conversationType: '2',
|
||||
conversationId: 'batch-room',
|
||||
isInAtList: true,
|
||||
}));
|
||||
|
||||
assert.equal(asks, 0);
|
||||
assert.match(sent.at(-1), /仅支持私聊/);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -438,8 +438,27 @@ test('Discord normalizes DMs and only addressed server messages', () => {
|
|||
}, '1234567890123456789');
|
||||
assert.equal(direct.kind, 'direct');
|
||||
assert.equal(direct.addressed, true);
|
||||
assert.equal(direct.plainText, true);
|
||||
assert.deepEqual(direct.connectionTestTarget, { channelId: '222222222222222222' });
|
||||
|
||||
const sticker = normalizeDiscordMessage({
|
||||
id: '111111111111111115',
|
||||
channel_id: '222222222222222222',
|
||||
author: { id: '333333333333333333', bot: false },
|
||||
content: 'sticker caption',
|
||||
sticker_items: [{ id: '555555555555555555', name: 'wave' }],
|
||||
}, '1234567890123456789');
|
||||
assert.equal(sticker.plainText, false);
|
||||
|
||||
const poll = normalizeDiscordMessage({
|
||||
id: '111111111111111116',
|
||||
channel_id: '222222222222222222',
|
||||
author: { id: '333333333333333333', bot: false },
|
||||
content: 'poll caption',
|
||||
poll: { question: { text: 'choose one' } },
|
||||
}, '1234567890123456789');
|
||||
assert.equal(poll.plainText, false);
|
||||
|
||||
const group = normalizeDiscordMessage({
|
||||
id: '111111111111111112',
|
||||
channel_id: '222222222222222223',
|
||||
|
|
@ -988,6 +1007,7 @@ test('Discord keeps image attachments in images and exposes ordinary attachments
|
|||
});
|
||||
|
||||
assert.equal(message.content, '');
|
||||
assert.equal(message.plainText, false);
|
||||
assert.equal(message.images.length, 2);
|
||||
assert.deepEqual({
|
||||
name: message.images[0].name,
|
||||
|
|
|
|||
|
|
@ -389,7 +389,8 @@ test('Feishu lists models and presets without prompting and advertises fast comm
|
|||
const help = sent.at(-1);
|
||||
for (const command of [
|
||||
'/models', '/model', '/reasoninglist', '/reasonings', '/reasoning',
|
||||
'/presetlist', '/preset', '/preset --default', '/stop', '/steer',
|
||||
'/presetlist', '/preset', '/preset --default', '/batch', '/send', '/cancel',
|
||||
'/stop', '/steer',
|
||||
]) {
|
||||
assert.equal(help.includes(command), true, command);
|
||||
}
|
||||
|
|
@ -397,6 +398,280 @@ test('Feishu lists models and presets without prompting and advertises fast comm
|
|||
assert.match(help, /\/preset id:<ID>/);
|
||||
});
|
||||
|
||||
test('Feishu private batch input synchronously collects messages and submits one ordered prompt', async () => {
|
||||
const fixture = stateFixture([['p2p:ou_user', 'session-batch']]);
|
||||
const sent = [];
|
||||
const asked = [];
|
||||
const reactions = [];
|
||||
const bridge = new FeishuHarnessBridge({
|
||||
client: textClient(async ({ text }) => sent.push(text)),
|
||||
channel: {
|
||||
addReaction: async (messageId, emojiType) => {
|
||||
reactions.push({ messageId, emojiType });
|
||||
return `${messageId}-${emojiType}`;
|
||||
},
|
||||
removeReaction: async () => undefined,
|
||||
},
|
||||
harness: {
|
||||
sessionExists: async () => true,
|
||||
ask: async (_sessionId, text) => {
|
||||
asked.push(text);
|
||||
return '批量处理完成';
|
||||
},
|
||||
},
|
||||
state: fixture.state,
|
||||
status: bridgeStatus(),
|
||||
allowedSenderOpenIds: new Set(['ou_user']),
|
||||
});
|
||||
|
||||
const operations = [
|
||||
bridge.accept(event('batch-open', '/batch')),
|
||||
bridge.accept(event('batch-one', '第一条')),
|
||||
bridge.accept(event('batch-two', '第二条')),
|
||||
bridge.accept(event('batch-progress', '/batch')),
|
||||
bridge.accept(event('batch-send', '/send')),
|
||||
];
|
||||
await Promise.all(operations);
|
||||
await bridge.waitForIdle();
|
||||
|
||||
assert.deepEqual(asked, [
|
||||
'以下是用户通过批量输入模式发送的多条内容,请按顺序作为同一次输入统一处理。\n\n'
|
||||
+ '[消息 1]\n第一条\n\n[消息 2]\n第二条',
|
||||
]);
|
||||
assert.match(sent.join('\n'), /已进入批量输入模式/);
|
||||
assert.match(sent.join('\n'), /已收集 2\/10 条/);
|
||||
assert.match(sent.join('\n'), /批量处理完成/);
|
||||
for (const messageId of ['batch-open', 'batch-one', 'batch-two', 'batch-progress', 'batch-send']) {
|
||||
assert.equal(fixture.seen.has(messageId), true, messageId);
|
||||
assert.deepEqual(
|
||||
reactions.filter((reaction) => reaction.messageId === messageId).map(({ emojiType }) => emojiType),
|
||||
['OnIt', 'DONE'],
|
||||
);
|
||||
}
|
||||
|
||||
await bridge.accept(event('batch-after-success', '恢复普通聊天'));
|
||||
await bridge.waitForIdle();
|
||||
assert.equal(asked.at(-1), '恢复普通聊天');
|
||||
});
|
||||
|
||||
test('Feishu batch input caps at ten, rejects non-text content, and can be cancelled', async () => {
|
||||
const fixture = stateFixture([['p2p:ou_user', 'session-batch-limit']]);
|
||||
const sent = [];
|
||||
let asks = 0;
|
||||
const bridge = new FeishuHarnessBridge({
|
||||
client: textClient(async ({ text }) => sent.push(text)),
|
||||
channel: {},
|
||||
harness: {
|
||||
sessionExists: async () => true,
|
||||
ask: async () => {
|
||||
asks += 1;
|
||||
return '不应调用';
|
||||
},
|
||||
},
|
||||
state: fixture.state,
|
||||
status: bridgeStatus(),
|
||||
allowedSenderOpenIds: new Set(['ou_user']),
|
||||
});
|
||||
|
||||
await bridge.accept(event('batch-limit-open', '/batch'));
|
||||
for (let index = 1; index <= 10; index += 1) {
|
||||
await bridge.accept(event(`batch-limit-${index}`, `内容 ${index}`));
|
||||
}
|
||||
await bridge.accept(event('batch-limit-overflow', '第十一条'));
|
||||
await bridge.accept(event('batch-limit-cancel', '/cancel'));
|
||||
assert.equal(asks, 0);
|
||||
assert.match(sent.join('\n'), /已收集 10\/10 条/);
|
||||
assert.match(sent.join('\n'), /当前批次已满,这条消息未收录/);
|
||||
assert.match(sent.join('\n'), /共丢弃 10 条消息/);
|
||||
|
||||
await bridge.accept(event('batch-nontext-open', '/batch'));
|
||||
await bridge.accept(event('batch-nontext-image', '', {
|
||||
message_type: 'image',
|
||||
content: JSON.stringify({ image_key: 'img_batch_rejected' }),
|
||||
}));
|
||||
await bridge.accept(event('batch-nontext-cancel', '/cancel'));
|
||||
assert.equal(asks, 0);
|
||||
assert.match(sent.join('\n'), /目前仅支持文字,这条消息未收录/);
|
||||
assert.match(sent.at(-1), /已取消批量输入/);
|
||||
assert.doesNotMatch(sent.at(-1), /丢弃 1 条/);
|
||||
});
|
||||
|
||||
test('Feishu group batch commands follow the response policy but never reach Harness', async () => {
|
||||
const fixture = stateFixture([['group:oc_batch_group', 'session-group-batch']]);
|
||||
const sent = [];
|
||||
let asks = 0;
|
||||
const bridge = new FeishuHarnessBridge({
|
||||
client: textClient(async ({ text }) => sent.push(text)),
|
||||
channel: {},
|
||||
harness: {
|
||||
sessionExists: async () => true,
|
||||
ask: async () => {
|
||||
asks += 1;
|
||||
return '不应调用';
|
||||
},
|
||||
},
|
||||
state: fixture.state,
|
||||
status: bridgeStatus(),
|
||||
allowedSenderOpenIds: new Set(['ou_user']),
|
||||
botOpenId: 'ou_bot',
|
||||
groupResponseMode: 'mention',
|
||||
});
|
||||
|
||||
for (const [messageId, command] of [
|
||||
['group-batch', '/batch'],
|
||||
['group-send', '/send'],
|
||||
['group-cancel', '/cancel'],
|
||||
]) {
|
||||
await bridge.accept(event(messageId, `@_bot ${command}`, {
|
||||
chat_type: 'group',
|
||||
chat_id: 'oc_batch_group',
|
||||
mentions: [{ key: '@_bot', id: { open_id: 'ou_bot' } }],
|
||||
}));
|
||||
}
|
||||
await bridge.accept(event('group-batch-unaddressed', '/batch', {
|
||||
chat_type: 'group',
|
||||
chat_id: 'oc_batch_group',
|
||||
}));
|
||||
bridge.setGroupResponseMode('all');
|
||||
await bridge.accept(event('group-batch-all-mode', '/batch', {
|
||||
chat_type: 'group',
|
||||
chat_id: 'oc_batch_group',
|
||||
}));
|
||||
|
||||
assert.equal(asks, 0);
|
||||
assert.equal(sent.length, 4);
|
||||
assert.equal(sent.every((text) => /仅支持私聊/.test(text)), true);
|
||||
assert.equal(fixture.seen.has('group-batch-unaddressed'), false);
|
||||
assert.equal(fixture.seen.has('group-batch-all-mode'), true);
|
||||
});
|
||||
|
||||
test('Feishu batch start is busy behind a turn and submitting lets later messages use the normal queue', async () => {
|
||||
const fixture = stateFixture([['p2p:ou_user', 'session-batch-queue']]);
|
||||
const firstStarted = deferred();
|
||||
const releaseFirst = deferred();
|
||||
const batchStarted = deferred();
|
||||
const releaseBatch = deferred();
|
||||
const asked = [];
|
||||
const sent = [];
|
||||
const bridge = new FeishuHarnessBridge({
|
||||
client: textClient(async ({ text }) => sent.push(text)),
|
||||
channel: {},
|
||||
harness: {
|
||||
sessionExists: async () => true,
|
||||
ask: async (_sessionId, text) => {
|
||||
asked.push(text);
|
||||
if (text === '运行中的普通任务') {
|
||||
firstStarted.resolve();
|
||||
await releaseFirst.promise;
|
||||
} else if (text.includes('[消息 1]\n批量内容')) {
|
||||
batchStarted.resolve();
|
||||
await releaseBatch.promise;
|
||||
}
|
||||
return `完成:${text}`;
|
||||
},
|
||||
},
|
||||
state: fixture.state,
|
||||
status: bridgeStatus(),
|
||||
allowedSenderOpenIds: new Set(['ou_user']),
|
||||
});
|
||||
|
||||
const activeTurn = bridge.accept(event('batch-busy-turn', '运行中的普通任务'));
|
||||
await firstStarted.promise;
|
||||
await bridge.accept(event('batch-busy-open', '/batch'));
|
||||
assert.match(sent.at(-1), /正在运行的任务/);
|
||||
releaseFirst.resolve();
|
||||
await activeTurn;
|
||||
|
||||
await bridge.accept(event('batch-queue-open', '/batch'));
|
||||
await bridge.accept(event('batch-queue-content', '批量内容'));
|
||||
const submission = bridge.accept(event('batch-queue-send', '/send'));
|
||||
await batchStarted.promise;
|
||||
const laterMessage = bridge.accept(event('batch-queue-later', '提交后的普通消息'));
|
||||
await bridge.accept(event('batch-queue-cancel-late', '/cancel'));
|
||||
assert.match(sent.at(-1), /已经提交,无法取消/);
|
||||
releaseBatch.resolve();
|
||||
await Promise.all([submission, laterMessage]);
|
||||
await bridge.waitForIdle();
|
||||
|
||||
assert.equal(asked.length, 3);
|
||||
assert.equal(asked[0], '运行中的普通任务');
|
||||
assert.match(asked[1], /\[消息 1\]\n批量内容/);
|
||||
assert.equal(asked[2], '提交后的普通消息');
|
||||
});
|
||||
|
||||
test('Feishu retains a batch after an ask failure and retries the same snapshot', async () => {
|
||||
const fixture = stateFixture([['p2p:ou_user', 'session-batch-retry']]);
|
||||
const sent = [];
|
||||
const asked = [];
|
||||
const finalReactions = [];
|
||||
const bridge = new FeishuHarnessBridge({
|
||||
client: textClient(async ({ text }) => sent.push(text)),
|
||||
channel: {
|
||||
addReaction: async (_messageId, emojiType) => {
|
||||
finalReactions.push(emojiType);
|
||||
return `reaction-${emojiType}`;
|
||||
},
|
||||
removeReaction: async () => undefined,
|
||||
},
|
||||
harness: {
|
||||
sessionExists: async () => true,
|
||||
ask: async (_sessionId, text) => {
|
||||
asked.push(text);
|
||||
if (asked.length === 1) throw new Error('transient batch failure');
|
||||
return '重试成功';
|
||||
},
|
||||
},
|
||||
state: fixture.state,
|
||||
status: bridgeStatus(),
|
||||
allowedSenderOpenIds: new Set(['ou_user']),
|
||||
logger: { info() {}, warn() {}, error() {} },
|
||||
});
|
||||
|
||||
await bridge.accept(event('batch-retry-open', '/batch'));
|
||||
await bridge.accept(event('batch-retry-content', '需要重试'));
|
||||
await bridge.accept(event('batch-retry-first-send', '/send'));
|
||||
assert.match(sent.at(-1), /提交失败,已保留 1 条消息/);
|
||||
assert.equal(finalReactions.at(-1), 'ERROR');
|
||||
|
||||
await bridge.accept(event('batch-retry-status', '/batch'));
|
||||
assert.match(sent.at(-1), /已收集 1\/10 条/);
|
||||
await bridge.accept(event('batch-retry-second-send', '/send'));
|
||||
assert.equal(asked.length, 2);
|
||||
assert.equal(asked[0], asked[1]);
|
||||
assert.equal(sent.at(-1), '重试成功');
|
||||
|
||||
await bridge.accept(event('batch-retry-cancel-after-success', '/cancel'));
|
||||
assert.match(sent.at(-1), /没有正在进行的批量输入/);
|
||||
});
|
||||
|
||||
test('Feishu clears a submitted batch when the Harness turn is stopped', async () => {
|
||||
const fixture = stateFixture([['p2p:ou_user', 'session-batch-stopped']]);
|
||||
const sent = [];
|
||||
const bridge = new FeishuHarnessBridge({
|
||||
client: textClient(async ({ text }) => sent.push(text)),
|
||||
channel: {},
|
||||
harness: {
|
||||
sessionExists: async () => true,
|
||||
ask: async () => {
|
||||
const error = new Error('turn stopped');
|
||||
error.code = 'turn-stopped';
|
||||
throw error;
|
||||
},
|
||||
},
|
||||
state: fixture.state,
|
||||
status: bridgeStatus(),
|
||||
allowedSenderOpenIds: new Set(['ou_user']),
|
||||
});
|
||||
|
||||
await bridge.accept(event('stopped-batch-open', '/batch'));
|
||||
await bridge.accept(event('stopped-batch-content', '不要重试'));
|
||||
await bridge.accept(event('stopped-batch-send', '/send'));
|
||||
assert.doesNotMatch(sent.join('\n'), /已保留/);
|
||||
|
||||
await bridge.accept(event('stopped-batch-cancel', '/cancel'));
|
||||
assert.match(sent.at(-1), /没有正在进行的批量输入/);
|
||||
});
|
||||
|
||||
test('bridge maps a Feishu conversation to a persistent Harness session and replies', async () => {
|
||||
const sent = [];
|
||||
const reactions = [];
|
||||
|
|
@ -786,7 +1061,7 @@ test('bridge sends Feishu post text and all embedded images as one structured pr
|
|||
assert.deepEqual(sent, ['两张图片都已收到']);
|
||||
});
|
||||
|
||||
test('text inside a Feishu post is a model prompt rather than a local command', async () => {
|
||||
test('text inside a Feishu post is a model prompt rather than a local or batch command', async () => {
|
||||
const fixture = stateFixture([['p2p:ou_user', 'session-post-command']]);
|
||||
const asked = [];
|
||||
const sent = [];
|
||||
|
|
@ -809,11 +1084,18 @@ test('text inside a Feishu post is a model prompt rather than a local command',
|
|||
message_type: 'post',
|
||||
content: JSON.stringify({ content: [[{ tag: 'text', text: '/new' }]] }),
|
||||
}));
|
||||
await bridge.accept(event('om_post_batch_command', '', {
|
||||
message_type: 'post',
|
||||
content: JSON.stringify({ content: [[{ tag: 'text', text: '/batch' }]] }),
|
||||
}));
|
||||
await bridge.waitForIdle();
|
||||
|
||||
assert.equal(fixture.sessions.get('p2p:ou_user'), 'session-post-command');
|
||||
assert.deepEqual(asked, [{ sessionId: 'session-post-command', content: '/new' }]);
|
||||
assert.deepEqual(sent, ['按普通内容处理']);
|
||||
assert.deepEqual(asked, [
|
||||
{ sessionId: 'session-post-command', content: '/new' },
|
||||
{ sessionId: 'session-post-command', content: '/batch' },
|
||||
]);
|
||||
assert.deepEqual(sent, ['按普通内容处理', '按普通内容处理']);
|
||||
});
|
||||
|
||||
test('a threaded Feishu reply answers a pending Harness question before the original turn queue', async () => {
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ test('menu exposes the increased command set and keeps repair number-only', () =
|
|||
assert.equal(actions.includes('repair'), false);
|
||||
});
|
||||
|
||||
test('menu and card help advertise Agent Preset and reasoning commands', () => {
|
||||
test('menu and card help advertise Agent Preset, reasoning, and batch commands', () => {
|
||||
const help = menuHelpText();
|
||||
assert.match(help, /\/presetlist/);
|
||||
assert.match(help, /\/preset \[序号或完整ID\]/);
|
||||
|
|
@ -73,12 +73,18 @@ test('menu and card help advertise Agent Preset and reasoning commands', () => {
|
|||
assert.match(help, /\/reasoninglist 或 \/reasonings/);
|
||||
assert.match(help, /\/reasoning \[序号、等级ID或 --default\]/);
|
||||
assert.match(help, /\/model \[序号或完整模型ID\] \[推理等级ID\]/);
|
||||
assert.match(help, /\/batch/);
|
||||
assert.match(help, /\/send/);
|
||||
assert.match(help, /\/cancel/);
|
||||
|
||||
const card = helpCard();
|
||||
assert.match(card, /\/reasoninglist/);
|
||||
assert.match(card, /\/reasonings/);
|
||||
assert.match(card, /\/reasoning \[序号、等级ID或 --default\]/);
|
||||
assert.match(card, /\/model \[序号或完整模型ID\] \[推理等级ID\]/);
|
||||
assert.match(card, /\/batch/);
|
||||
assert.match(card, /\/send/);
|
||||
assert.match(card, /\/cancel/);
|
||||
});
|
||||
|
||||
test('card-action probe carries only its action and opaque nonce', () => {
|
||||
|
|
|
|||
|
|
@ -2246,3 +2246,154 @@ test('QQ cancellation prevents SDK sendFile', async (t) => {
|
|||
await cancelledBridge.accept(message({ messageId: 'qq-artifact-cancelled' }));
|
||||
assert.equal(fileCalls, 0);
|
||||
});
|
||||
|
||||
test('QQ private batch input submits once, cancels cleanly, and restores normal chat', async () => {
|
||||
const fixture = stateFixture();
|
||||
const asked = [];
|
||||
const sent = [];
|
||||
const bridge = new QqHarnessBridge({
|
||||
bot: { sendText: async (_target, text) => sent.push(text) },
|
||||
ownerUserOpenid: 'owner-openid',
|
||||
harness: {
|
||||
createSession: async () => 'session-batch',
|
||||
sessionExists: async () => true,
|
||||
ask: async (_sessionId, prompt) => {
|
||||
asked.push(prompt);
|
||||
return asked.length === 1 ? '批量完成' : '普通完成';
|
||||
},
|
||||
},
|
||||
state: fixture.state,
|
||||
});
|
||||
const inbound = (messageId, content, extra = {}) => message({
|
||||
messageId,
|
||||
content,
|
||||
replyTarget: { scope: 'c2c', targetId: 'owner-openid', msgId: messageId },
|
||||
...extra,
|
||||
});
|
||||
|
||||
await bridge.accept(inbound('qq-batch-start', '/batch'));
|
||||
await bridge.accept(inbound('qq-batch-one', '第一条'));
|
||||
await bridge.accept(inbound('qq-batch-two', '第二条'));
|
||||
assert.deepEqual(asked, []);
|
||||
assert.equal(sent.length, 1);
|
||||
|
||||
await bridge.accept(inbound('qq-batch-send', '/send'));
|
||||
assert.equal(asked.length, 1);
|
||||
assert.match(asked[0], /\[消息 1\]\n第一条/);
|
||||
assert.match(asked[0], /\[消息 2\]\n第二条/);
|
||||
assert.equal(sent.at(-1), '批量完成');
|
||||
|
||||
await bridge.accept(inbound('qq-cancel-start', '/batch'));
|
||||
await bridge.accept(inbound('qq-cancel-data', '不得提交'));
|
||||
await bridge.accept(inbound('qq-cancel-command', '/cancel'));
|
||||
assert.equal(asked.length, 1);
|
||||
assert.match(sent.at(-1), /丢弃 1 条/);
|
||||
|
||||
await bridge.accept(inbound('qq-normal-after-batch', '普通问题'));
|
||||
assert.equal(asked.at(-1), '普通问题');
|
||||
assert.equal(sent.at(-1), '普通完成');
|
||||
});
|
||||
|
||||
test('QQ retains a private batch after Harness ask fails and lets /send retry it', async () => {
|
||||
const fixture = stateFixture();
|
||||
const asked = [];
|
||||
const sent = [];
|
||||
const bridge = new QqHarnessBridge({
|
||||
bot: { sendText: async (_target, text) => sent.push(text) },
|
||||
ownerUserOpenid: 'owner-openid',
|
||||
harness: {
|
||||
createSession: async () => 'session-batch-retry',
|
||||
sessionExists: async () => true,
|
||||
ask: async (_sessionId, prompt) => {
|
||||
asked.push(prompt);
|
||||
if (asked.length === 1) throw new Error('temporary Harness failure');
|
||||
return '重试成功';
|
||||
},
|
||||
},
|
||||
state: fixture.state,
|
||||
logger: { warn() {}, error() {} },
|
||||
});
|
||||
const inbound = (messageId, content) => message({
|
||||
messageId,
|
||||
content,
|
||||
replyTarget: { scope: 'c2c', targetId: 'owner-openid', msgId: messageId },
|
||||
});
|
||||
|
||||
await bridge.accept(inbound('qq-batch-retry-start', '/batch'));
|
||||
await bridge.accept(inbound('qq-batch-retry-data', '需要重试的内容'));
|
||||
await bridge.accept(inbound('qq-batch-retry-first-send', '/send'));
|
||||
|
||||
assert.equal(asked.length, 1);
|
||||
assert.match(sent.at(-1), /已保留 1 条消息/);
|
||||
assert.match(sent.at(-1), /再次发送 \/send 重试/);
|
||||
|
||||
await bridge.accept(inbound('qq-batch-retry-second-send', '/send'));
|
||||
|
||||
assert.equal(asked.length, 2);
|
||||
assert.equal(asked[1], asked[0]);
|
||||
assert.equal(sent.at(-1), '重试成功');
|
||||
});
|
||||
|
||||
test('QQ clears a private batch after turn-stopped without suggesting a batch retry', async () => {
|
||||
const fixture = stateFixture();
|
||||
const sent = [];
|
||||
let asks = 0;
|
||||
const bridge = new QqHarnessBridge({
|
||||
bot: { sendText: async (_target, text) => sent.push(text) },
|
||||
ownerUserOpenid: 'owner-openid',
|
||||
harness: {
|
||||
createSession: async () => 'session-batch-stopped',
|
||||
sessionExists: async () => true,
|
||||
ask: async () => {
|
||||
asks += 1;
|
||||
const error = new Error('turn stopped');
|
||||
error.code = 'turn-stopped';
|
||||
throw error;
|
||||
},
|
||||
},
|
||||
state: fixture.state,
|
||||
logger: { warn() {}, error() {} },
|
||||
});
|
||||
const inbound = (messageId, content) => message({
|
||||
messageId,
|
||||
content,
|
||||
replyTarget: { scope: 'c2c', targetId: 'owner-openid', msgId: messageId },
|
||||
});
|
||||
|
||||
await bridge.accept(inbound('qq-batch-stopped-start', '/batch'));
|
||||
await bridge.accept(inbound('qq-batch-stopped-data', '停止后应清除'));
|
||||
await bridge.accept(inbound('qq-batch-stopped-send', '/send'));
|
||||
|
||||
assert.equal(asks, 1);
|
||||
assert.equal(sent.at(-1), '已停止。');
|
||||
assert.equal(sent.some((text) => text.includes('已保留')), false);
|
||||
|
||||
await bridge.accept(inbound('qq-batch-stopped-send-again', '/send'));
|
||||
|
||||
assert.equal(asks, 1);
|
||||
assert.match(sent.at(-1), /当前没有待提交的批量内容/);
|
||||
});
|
||||
|
||||
test('QQ group batch commands are rejected without reaching Harness', async () => {
|
||||
const fixture = stateFixture();
|
||||
const sent = [];
|
||||
let asks = 0;
|
||||
const bridge = new QqHarnessBridge({
|
||||
bot: { sendText: async (_target, text) => sent.push(text) },
|
||||
ownerUserOpenid: 'owner-openid',
|
||||
harness: { ask: async () => { asks += 1; } },
|
||||
state: fixture.state,
|
||||
});
|
||||
|
||||
await bridge.accept(message({
|
||||
kind: 'group',
|
||||
rawEventType: 'GROUP_AT_MESSAGE_CREATE',
|
||||
groupOpenid: 'batch-group',
|
||||
content: '/batch',
|
||||
messageId: 'qq-group-batch',
|
||||
replyTarget: { scope: 'group', targetId: 'batch-group', msgId: 'qq-group-batch' },
|
||||
}));
|
||||
|
||||
assert.equal(asks, 0);
|
||||
assert.match(sent.at(-1), /仅支持私聊/);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -215,6 +215,341 @@ test('all four shared text channels execute /compact outside the model prompt pa
|
|||
}
|
||||
});
|
||||
|
||||
test('all four shared text channels collect a private batch and submit it in one ordered ask', async () => {
|
||||
for (const [name, Bridge] of [
|
||||
['slack', SlackHarnessBridge],
|
||||
['telegram', TelegramHarnessBridge],
|
||||
['discord', DiscordHarnessBridge],
|
||||
['whatsapp', WhatsappHarnessBridge],
|
||||
]) {
|
||||
const fixture = stateFixture();
|
||||
const sent = [];
|
||||
const asks = [];
|
||||
const bridge = new Bridge({
|
||||
bot: { sendText: async (_target, text) => sent.push(text) },
|
||||
state: fixture.state,
|
||||
harness: {
|
||||
createSession: async () => `session-batch-${name}`,
|
||||
sessionExists: async () => true,
|
||||
ask: async (sessionId, text) => {
|
||||
asks.push({ sessionId, text });
|
||||
return `batch reply ${name}`;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const accepted = [
|
||||
bridge.accept(message(`batch-start-${name}`, '/batch')),
|
||||
bridge.accept(message(`batch-first-${name}`, 'first line')),
|
||||
bridge.accept(message(`batch-last-${name}`, 'last line')),
|
||||
bridge.accept(message(`batch-send-${name}`, '/send')),
|
||||
];
|
||||
await Promise.all(accepted);
|
||||
|
||||
assert.equal(asks.length, 1, `${name} sends one ask`);
|
||||
assert.equal(asks[0].sessionId, `session-batch-${name}`, name);
|
||||
assert.match(asks[0].text, /\[消息 1\]\nfirst line/, name);
|
||||
assert.match(asks[0].text, /\[消息 2\]\nlast line/, name);
|
||||
assert.ok(
|
||||
asks[0].text.indexOf('first line') < asks[0].text.indexOf('last line'),
|
||||
`${name} preserves order`,
|
||||
);
|
||||
assert.equal(sent.filter((text) => text === `batch reply ${name}`).length, 1, name);
|
||||
assert.equal(fixture.seen.has(`batch-first-${name}`), true, `${name} records first item`);
|
||||
assert.equal(fixture.seen.has(`batch-last-${name}`), true, `${name} records last item`);
|
||||
|
||||
await bridge.accept(message(`normal-after-batch-${name}`, 'ordinary message'));
|
||||
assert.equal(asks.length, 2, `${name} resumes ordinary chat`);
|
||||
assert.equal(asks[1].text, 'ordinary message', `${name} ordinary prompt stays unchanged`);
|
||||
}
|
||||
});
|
||||
|
||||
test('shared text channels reserve addressed group batch commands without changing group chat', async () => {
|
||||
const commands = ['/batch', '/BATCH now', '/send', '/send later', '/cancel', '/cancel all'];
|
||||
for (const [name, Bridge] of [
|
||||
['slack', SlackHarnessBridge],
|
||||
['telegram', TelegramHarnessBridge],
|
||||
['discord', DiscordHarnessBridge],
|
||||
['whatsapp', WhatsappHarnessBridge],
|
||||
]) {
|
||||
const fixture = stateFixture();
|
||||
const sent = [];
|
||||
const asks = [];
|
||||
const bridge = new Bridge({
|
||||
bot: { sendText: async (_target, text) => sent.push(text) },
|
||||
state: fixture.state,
|
||||
harness: {
|
||||
createSession: async () => `group-session-${name}`,
|
||||
sessionExists: async () => true,
|
||||
ask: async (_sessionId, text) => {
|
||||
asks.push(text);
|
||||
return `${name} group reply`;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
for (const [index, command] of commands.entries()) {
|
||||
await bridge.accept(message(`group-batch-${name}-${index}`, command, {
|
||||
kind: 'group',
|
||||
conversationId: `group-${name}`,
|
||||
addressed: true,
|
||||
}));
|
||||
}
|
||||
assert.equal(asks.length, 0, `${name} never submits a group batch command`);
|
||||
assert.equal(sent.length, commands.length, `${name} replies to every addressed form`);
|
||||
assert.equal(
|
||||
sent.every((text) => text.includes('仅支持私聊')),
|
||||
true,
|
||||
`${name} explains the private-only boundary`,
|
||||
);
|
||||
|
||||
const beforeUnaddressed = sent.length;
|
||||
await bridge.accept(message(`group-unaddressed-${name}`, '/batch', {
|
||||
kind: 'group',
|
||||
conversationId: `group-${name}`,
|
||||
addressed: false,
|
||||
}));
|
||||
assert.equal(sent.length, beforeUnaddressed, `${name} keeps mention rules intact`);
|
||||
assert.equal(asks.length, 0, `${name} ignores an unaddressed group command`);
|
||||
|
||||
await bridge.accept(message(`group-normal-${name}`, 'ordinary addressed group message', {
|
||||
kind: 'group',
|
||||
conversationId: `group-${name}`,
|
||||
addressed: true,
|
||||
}));
|
||||
assert.deepEqual(asks, ['ordinary addressed group message'], `${name} group chat is unchanged`);
|
||||
}
|
||||
});
|
||||
|
||||
test('private batch input enforces text-only collection, command blocking, the limit, and cancel', async () => {
|
||||
const fixture = stateFixture();
|
||||
const sent = [];
|
||||
let asks = 0;
|
||||
const bridge = createBridge({
|
||||
state: fixture.state,
|
||||
bot: { sendText: async (_target, text) => sent.push(text) },
|
||||
harness: {
|
||||
createSession: async () => 'unused-batch-session',
|
||||
ask: async () => { asks += 1; return 'unexpected'; },
|
||||
},
|
||||
});
|
||||
|
||||
await bridge.accept(message('limited-start', '/batch'));
|
||||
await bridge.accept(message('limited-image', 'image caption', {
|
||||
images: [{ data: Buffer.from('image') }],
|
||||
}));
|
||||
await bridge.accept(message('limited-file', 'file caption', {
|
||||
files: [{ name: 'data.txt', data: Buffer.from('data') }],
|
||||
}));
|
||||
await bridge.accept(message('limited-unsupported-media', 'video caption', {
|
||||
plainText: false,
|
||||
}));
|
||||
await bridge.accept(message('limited-command', '/new'));
|
||||
|
||||
const itemPromises = [];
|
||||
for (let index = 1; index <= 11; index += 1) {
|
||||
itemPromises.push(bridge.accept(message(`limited-item-${index}`, `item ${index}`)));
|
||||
}
|
||||
await Promise.all(itemPromises);
|
||||
await bridge.accept(message('limited-item-10', 'item 10 replay'));
|
||||
await bridge.accept(message('limited-cancel', '/cancel'));
|
||||
|
||||
assert.equal(asks, 0, 'collection and cancellation never call Harness');
|
||||
assert.equal(sent.filter((text) => text.includes('目前仅支持文字')).length, 3);
|
||||
assert.equal(sent.some((text) => text.includes('先发送 /send 提交或 /cancel 取消')), true);
|
||||
assert.equal(sent.some((text) => /10\/10.*已满/.test(text)), true);
|
||||
assert.equal(sent.some((text) => text.includes('这条消息未收录')), true);
|
||||
assert.equal(sent.at(-1).includes('共丢弃 10 条消息'), true);
|
||||
assert.equal(fixture.seen.has('limited-item-11'), true, 'a rejected eleventh item is recorded');
|
||||
|
||||
await bridge.accept(message('normal-after-cancel', 'normal after cancel'));
|
||||
assert.equal(asks, 1, 'cancel restores the ordinary prompt path');
|
||||
});
|
||||
|
||||
test('batch commands cannot answer a pending Harness question or approval', async () => {
|
||||
for (const interactionKind of ['question', 'approval']) {
|
||||
const fixture = stateFixture();
|
||||
const sent = [];
|
||||
const interactionDone = deferred();
|
||||
let interactionResponses = 0;
|
||||
const bridge = createBridge({
|
||||
state: fixture.state,
|
||||
bot: { sendText: async (_target, text) => sent.push(text) },
|
||||
harness: {
|
||||
createSession: async () => `session-pending-${interactionKind}`,
|
||||
ask: async (sessionId, _text, options) => {
|
||||
const interaction = interactionKind === 'question'
|
||||
? questionInteraction({
|
||||
id: 'pending-batch-question',
|
||||
sessionId,
|
||||
questions: [{ id: 'answer', question: 'question waiting' }],
|
||||
respond: async () => {
|
||||
interactionResponses += 1;
|
||||
interactionDone.resolve();
|
||||
return { accepted: true };
|
||||
},
|
||||
})
|
||||
: approvalInteraction({
|
||||
id: 'pending-batch-approval',
|
||||
sessionId,
|
||||
reason: 'approval waiting',
|
||||
respond: async () => {
|
||||
interactionResponses += 1;
|
||||
interactionDone.resolve();
|
||||
return { accepted: true };
|
||||
},
|
||||
});
|
||||
await options.onInteraction(interaction);
|
||||
await interactionDone.promise;
|
||||
return `${interactionKind} complete`;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const processing = bridge.accept(message(
|
||||
`pending-${interactionKind}-start`,
|
||||
`start ${interactionKind}`,
|
||||
));
|
||||
await eventually(() => sent.some((text) => text.includes(`${interactionKind} waiting`)));
|
||||
|
||||
await within(
|
||||
Promise.all([
|
||||
bridge.accept(message(`pending-${interactionKind}-batch`, '/batch')),
|
||||
bridge.accept(message(`pending-${interactionKind}-send`, '/send')),
|
||||
bridge.accept(message(`pending-${interactionKind}-cancel`, '/cancel')),
|
||||
]),
|
||||
250,
|
||||
`batch commands waited behind a pending ${interactionKind}`,
|
||||
);
|
||||
assert.equal(interactionResponses, 0, `${interactionKind} was not answered by a batch command`);
|
||||
assert.equal(sent.some((text) => text.includes('先完成当前交互')), true, interactionKind);
|
||||
assert.equal(sent.some((text) => text.includes('没有待提交')), true, interactionKind);
|
||||
assert.equal(sent.some((text) => text.includes('没有正在进行')), true, interactionKind);
|
||||
|
||||
await bridge.accept(message(
|
||||
`pending-${interactionKind}-answer`,
|
||||
interactionKind === 'question' ? 'yes' : '批准',
|
||||
));
|
||||
await processing;
|
||||
assert.equal(interactionResponses, 1, `${interactionKind} accepts its real answer`);
|
||||
}
|
||||
});
|
||||
|
||||
test('a submitting batch rejects duplicate controls while a later ordinary message stays ordinary', async () => {
|
||||
const fixture = stateFixture();
|
||||
const sent = [];
|
||||
const batchStarted = deferred();
|
||||
const releaseBatch = deferred();
|
||||
const asks = [];
|
||||
const bridge = createBridge({
|
||||
state: fixture.state,
|
||||
bot: { sendText: async (_target, text) => sent.push(text) },
|
||||
harness: {
|
||||
createSession: async () => 'session-submitting-batch',
|
||||
sessionExists: async () => true,
|
||||
ask: async (_sessionId, text) => {
|
||||
asks.push(text);
|
||||
if (asks.length === 1) {
|
||||
batchStarted.resolve();
|
||||
await releaseBatch.promise;
|
||||
return 'batch complete';
|
||||
}
|
||||
return 'ordinary complete';
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await bridge.accept(message('submitting-start', '/batch'));
|
||||
await bridge.accept(message('submitting-item', 'batched item'));
|
||||
const submission = bridge.accept(message('submitting-send', '/send'));
|
||||
await batchStarted.promise;
|
||||
|
||||
await within(
|
||||
Promise.all([
|
||||
bridge.accept(message('submitting-send-again', '/send')),
|
||||
bridge.accept(message('submitting-cancel', '/cancel')),
|
||||
]),
|
||||
250,
|
||||
'submitting controls waited behind the batch ask',
|
||||
);
|
||||
const ordinary = bridge.accept(message('submitting-ordinary', 'ordinary after send'));
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
assert.equal(asks.length, 1, 'the later ordinary message waits in the existing queue');
|
||||
assert.equal(sent.some((text) => text.includes('请勿重复发送 /send')), true);
|
||||
assert.equal(sent.some((text) => text.includes('已经提交,无法取消')), true);
|
||||
|
||||
releaseBatch.resolve();
|
||||
await Promise.all([submission, ordinary]);
|
||||
assert.equal(asks.length, 2);
|
||||
assert.equal(asks[1], 'ordinary after send');
|
||||
});
|
||||
|
||||
test('a failed batch submission is retained with an explicit retry and can be sent once again', async () => {
|
||||
const fixture = stateFixture();
|
||||
const sent = [];
|
||||
const asks = [];
|
||||
let attempt = 0;
|
||||
const bridge = createBridge({
|
||||
state: fixture.state,
|
||||
bot: { sendText: async (_target, text) => sent.push(text) },
|
||||
harness: {
|
||||
createSession: async () => 'session-retry-batch',
|
||||
sessionExists: async () => true,
|
||||
ask: async (_sessionId, text) => {
|
||||
asks.push(text);
|
||||
attempt += 1;
|
||||
if (attempt === 1) throw new Error('transient Harness failure');
|
||||
return 'retry complete';
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await bridge.accept(message('retry-batch-start', '/batch'));
|
||||
await bridge.accept(message('retry-batch-item', 'keep this item'));
|
||||
await bridge.accept(message('retry-batch-send-one', '/send'));
|
||||
|
||||
assert.equal(asks.length, 1);
|
||||
assert.equal(sent.at(-1).includes('已保留 1 条消息'), true);
|
||||
assert.equal(sent.at(-1).includes('/send 重试'), true);
|
||||
|
||||
await bridge.accept(message('retry-batch-send-two', '/send'));
|
||||
assert.equal(asks.length, 2);
|
||||
assert.equal(asks[1], asks[0], 'retry reuses the retained immutable batch content');
|
||||
assert.equal(sent.at(-1), 'retry complete');
|
||||
|
||||
await bridge.accept(message('retry-no-batch', '/send'));
|
||||
assert.equal(sent.at(-1).includes('没有待提交'), true, 'successful retry clears the batch');
|
||||
});
|
||||
|
||||
test('stopping a submitted batch clears it instead of offering a duplicate retry', async () => {
|
||||
const fixture = stateFixture();
|
||||
const sent = [];
|
||||
let asks = 0;
|
||||
const stopped = new Error('stopped by user');
|
||||
stopped.code = 'turn-stopped';
|
||||
const bridge = createBridge({
|
||||
state: fixture.state,
|
||||
bot: { sendText: async (_target, text) => sent.push(text) },
|
||||
harness: {
|
||||
createSession: async () => 'session-stopped-batch',
|
||||
ask: async () => {
|
||||
asks += 1;
|
||||
throw stopped;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await bridge.accept(message('stopped-batch-start', '/batch'));
|
||||
await bridge.accept(message('stopped-batch-item', 'do this once'));
|
||||
await bridge.accept(message('stopped-batch-send', '/send'));
|
||||
assert.equal(asks, 1);
|
||||
assert.equal(sent.some((text) => text.includes('已保留')), false);
|
||||
|
||||
await bridge.accept(message('stopped-batch-send-again', '/send'));
|
||||
assert.equal(asks, 1, 'a stopped submission cannot be retried as the same batch');
|
||||
assert.equal(sent.at(-1).includes('没有待提交'), true);
|
||||
});
|
||||
|
||||
test('shared text bridge passes a file-only message through askOptions.files', async () => {
|
||||
const fixture = stateFixture();
|
||||
const source = Object.freeze({
|
||||
|
|
|
|||
|
|
@ -462,6 +462,7 @@ test('Enterprise WeChat lists models and presets without prompting and advertise
|
|||
for (const command of [
|
||||
'/models', '/model', '/reasoninglist', '/reasonings', '/reasoning',
|
||||
'/presetlist', '/preset', '/preset --default', '/stop', '/steer',
|
||||
'/batch', '/send', '/cancel',
|
||||
]) {
|
||||
assert.equal(help.includes(command), true, command);
|
||||
}
|
||||
|
|
@ -1890,3 +1891,135 @@ test('Enterprise WeChat cancellation prevents SDK upload', async (t) => {
|
|||
await cancelledBridge.accept(frame({ msgid: 'wecom-artifact-cancelled' }));
|
||||
assert.equal(uploads, 0);
|
||||
});
|
||||
|
||||
test('Enterprise WeChat batch input collects ten texts and submits one ordered Harness turn', async () => {
|
||||
const transport = testClient();
|
||||
const prompts = [];
|
||||
const bridge = new WecomHarnessBridge({
|
||||
client: transport.client,
|
||||
generateStreamId: () => 'batch-stream',
|
||||
harness: {
|
||||
sessionExists: async () => true,
|
||||
ask: async (_sessionId, prompt) => {
|
||||
prompts.push(prompt);
|
||||
return '批量完成';
|
||||
},
|
||||
},
|
||||
state: state(),
|
||||
});
|
||||
|
||||
await bridge.accept(frame({ msgid: 'batch-start', text: { content: '/batch' } }));
|
||||
for (let index = 1; index <= 10; index += 1) {
|
||||
await bridge.accept(frame({
|
||||
msgid: `batch-item-${index}`,
|
||||
text: { content: `企微内容 ${index}` },
|
||||
}));
|
||||
}
|
||||
await bridge.accept(frame({ msgid: 'batch-overflow', text: { content: '不会收录' } }));
|
||||
|
||||
assert.equal(prompts.length, 0);
|
||||
assert.equal(transport.streamed.some(({ content }) => /10\/10.*已满/.test(content)), true);
|
||||
assert.equal(transport.streamed.some(({ content }) => /这条消息未收录/.test(content)), true);
|
||||
|
||||
await bridge.accept(frame({ msgid: 'batch-send', text: { content: '/send' } }));
|
||||
assert.equal(prompts.length, 1);
|
||||
assert.match(prompts[0], /\[消息 1\]\n企微内容 1/);
|
||||
assert.match(prompts[0], /\[消息 10\]\n企微内容 10/);
|
||||
assert.doesNotMatch(prompts[0], /不会收录/);
|
||||
assert.equal(transport.streamed.at(-1).content, '批量完成');
|
||||
});
|
||||
|
||||
test('Enterprise WeChat rejects batch commands in groups without invoking Harness', async () => {
|
||||
const transport = testClient();
|
||||
let asks = 0;
|
||||
const bridge = new WecomHarnessBridge({
|
||||
client: transport.client,
|
||||
harness: { ask: async () => { asks += 1; return 'unexpected'; } },
|
||||
state: state(),
|
||||
});
|
||||
|
||||
for (const [index, command] of ['/batch', '/send', '/cancel'].entries()) {
|
||||
await bridge.accept(frame({
|
||||
msgid: `group-batch-${index}`,
|
||||
chattype: 'group',
|
||||
chatid: 'group-batch',
|
||||
text: { content: `@机器人 ${command}` },
|
||||
}));
|
||||
}
|
||||
|
||||
assert.equal(asks, 0);
|
||||
assert.equal(transport.streamed.length, 3);
|
||||
assert.equal(transport.streamed.every(({ content }) => /仅支持私聊/.test(content)), true);
|
||||
});
|
||||
|
||||
test('Enterprise WeChat keeps a failed batch for retry and queues later ordinary messages normally', async () => {
|
||||
const transport = testClient();
|
||||
const firstBatch = deferred();
|
||||
const prompts = [];
|
||||
let failFirst = true;
|
||||
const bridge = new WecomHarnessBridge({
|
||||
client: transport.client,
|
||||
generateStreamId: (() => { let index = 0; return () => `batch-retry-${++index}`; })(),
|
||||
harness: {
|
||||
sessionExists: async () => true,
|
||||
ask: async (_sessionId, prompt) => {
|
||||
prompts.push(prompt);
|
||||
if (failFirst) {
|
||||
failFirst = false;
|
||||
throw new Error('temporary failure');
|
||||
}
|
||||
if (/\[消息 1\]/.test(prompt)) await firstBatch.promise;
|
||||
return `完成:${prompt}`;
|
||||
},
|
||||
},
|
||||
state: state(),
|
||||
logger: { warn() {}, error() {} },
|
||||
});
|
||||
|
||||
await bridge.accept(frame({ msgid: 'retry-start', text: { content: '/batch' } }));
|
||||
await bridge.accept(frame({ msgid: 'retry-content', text: { content: '需要重试' } }));
|
||||
await bridge.accept(frame({ msgid: 'retry-send-1', text: { content: '/send' } }));
|
||||
assert.match(transport.streamed.at(-1).content, /消息处理失败.*已保留 1 条消息/s);
|
||||
|
||||
const retry = bridge.accept(frame({ msgid: 'retry-send-2', text: { content: '/send' } }));
|
||||
await eventually(() => prompts.length === 2);
|
||||
const ordinary = bridge.accept(frame({
|
||||
msgid: 'ordinary-after-send',
|
||||
text: { content: '提交期间的普通消息' },
|
||||
}));
|
||||
assert.equal(prompts.length, 2);
|
||||
|
||||
firstBatch.resolve();
|
||||
await Promise.all([retry, ordinary]);
|
||||
assert.equal(prompts.length, 3);
|
||||
assert.equal(prompts[2], '提交期间的普通消息');
|
||||
|
||||
await bridge.accept(frame({ msgid: 'cancel-start', text: { content: '/batch' } }));
|
||||
await bridge.accept(frame({ msgid: 'cancel-content', text: { content: '丢弃' } }));
|
||||
await bridge.accept(frame({ msgid: 'cancel-command', text: { content: '/cancel' } }));
|
||||
assert.match(transport.streamed.at(-1).content, /已取消批量输入.*丢弃 1 条消息/s);
|
||||
});
|
||||
|
||||
test('Enterprise WeChat clears a submitted batch when the Harness turn is stopped', async () => {
|
||||
const transport = testClient();
|
||||
const bridge = new WecomHarnessBridge({
|
||||
client: transport.client,
|
||||
harness: {
|
||||
sessionExists: async () => true,
|
||||
ask: async () => {
|
||||
const error = new Error('turn stopped');
|
||||
error.code = 'turn-stopped';
|
||||
throw error;
|
||||
},
|
||||
},
|
||||
state: state(),
|
||||
});
|
||||
|
||||
await bridge.accept(frame({ msgid: 'stopped-start', text: { content: '/batch' } }));
|
||||
await bridge.accept(frame({ msgid: 'stopped-content', text: { content: '不要重试' } }));
|
||||
await bridge.accept(frame({ msgid: 'stopped-send', text: { content: '/send' } }));
|
||||
assert.equal(transport.streamed.some(({ content }) => /已保留/.test(content)), false);
|
||||
|
||||
await bridge.accept(frame({ msgid: 'stopped-cancel', text: { content: '/cancel' } }));
|
||||
assert.match(transport.streamed.at(-1).content, /没有正在进行的批量输入/);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -778,6 +778,7 @@ test('Weixin lists models and presets without prompting and advertises fast comm
|
|||
for (const command of [
|
||||
'/models', '/model', '/reasoninglist', '/reasonings', '/reasoning',
|
||||
'/presetlist', '/preset', '/preset --default', '/stop', '/steer',
|
||||
'/batch', '/send', '/cancel',
|
||||
]) {
|
||||
assert.equal(help.includes(command), true, command);
|
||||
}
|
||||
|
|
@ -1650,3 +1651,154 @@ test('bridge commands are local and internal failures return a generic message',
|
|||
});
|
||||
assert.doesNotMatch(JSON.stringify(status.lastMessageError), /private path|secret|token-shaped/);
|
||||
});
|
||||
|
||||
test('Weixin batch input collects up to ten native text messages and submits one ordered turn', async () => {
|
||||
const fixture = stateFixture();
|
||||
fixture.sessions.set('p2p:owner-user', 'session-batch');
|
||||
const sent = [];
|
||||
const prompts = [];
|
||||
const bridge = new WeixinHarnessBridge({
|
||||
api: { sendText: async (request) => sent.push(request) },
|
||||
baseUrl: 'https://ilinkai.weixin.qq.com/',
|
||||
token: 'host-token',
|
||||
ownerUserId: 'owner-user',
|
||||
harness: {
|
||||
sessionExists: async () => true,
|
||||
ask: async (sessionId, prompt) => {
|
||||
prompts.push({ sessionId, prompt });
|
||||
return '批量完成';
|
||||
},
|
||||
},
|
||||
state: fixture.state,
|
||||
});
|
||||
|
||||
await bridge.accept(message('batch-start', '/batch'));
|
||||
await bridge.accept(message('batch-voice', '', {
|
||||
item_list: [{ type: 3, voice_item: { text: '语音转写不能收录' } }],
|
||||
}));
|
||||
await bridge.accept(message('batch-image', '', {
|
||||
item_list: [{ type: 2, image_item: { media: {} } }],
|
||||
}));
|
||||
for (let index = 1; index <= 10; index += 1) {
|
||||
await bridge.accept(message(`batch-item-${index}`, `内容 ${index}`));
|
||||
}
|
||||
await bridge.accept(message('batch-overflow', '不会收录'));
|
||||
|
||||
assert.equal(prompts.length, 0);
|
||||
assert.equal(sent.some(({ text }) => /目前仅支持文字.*未收录/s.test(text)), true);
|
||||
assert.equal(sent.some(({ text }) => /10\/10.*已满/.test(text)), true);
|
||||
assert.equal(sent.some(({ text }) => /这条消息未收录/.test(text)), true);
|
||||
|
||||
await bridge.accept(message('batch-send', '/send'));
|
||||
|
||||
assert.equal(prompts.length, 1);
|
||||
assert.equal(prompts[0].sessionId, 'session-batch');
|
||||
assert.match(prompts[0].prompt, /\[消息 1\]\n内容 1/);
|
||||
assert.match(prompts[0].prompt, /\[消息 10\]\n内容 10/);
|
||||
assert.doesNotMatch(prompts[0].prompt, /语音转写不能收录|不会收录/);
|
||||
assert.equal(sent.at(-1).text, '批量完成');
|
||||
|
||||
await bridge.accept(message('after-batch', '恢复普通聊天'));
|
||||
assert.equal(prompts.length, 2);
|
||||
assert.equal(prompts[1].prompt, '恢复普通聊天');
|
||||
});
|
||||
|
||||
test('Weixin batch cancellation is local and a failed submission remains retryable', async () => {
|
||||
const fixture = stateFixture();
|
||||
fixture.sessions.set('p2p:owner-user', 'session-batch-retry');
|
||||
const sent = [];
|
||||
let attempts = 0;
|
||||
const bridge = new WeixinHarnessBridge({
|
||||
api: { sendText: async ({ text }) => sent.push(text) },
|
||||
baseUrl: 'https://ilinkai.weixin.qq.com/',
|
||||
token: 'host-token',
|
||||
ownerUserId: 'owner-user',
|
||||
harness: {
|
||||
sessionExists: async () => true,
|
||||
ask: async () => {
|
||||
attempts += 1;
|
||||
if (attempts === 1) throw new Error('temporary failure');
|
||||
return '重试成功';
|
||||
},
|
||||
},
|
||||
state: fixture.state,
|
||||
logger: { error() {} },
|
||||
});
|
||||
|
||||
await bridge.accept(message('retry-start', '/batch'));
|
||||
await bridge.accept(message('retry-content', '需要重试'));
|
||||
await bridge.accept(message('retry-send-1', '/send'));
|
||||
assert.match(sent.at(-1), /消息处理失败.*已保留 1 条消息/s);
|
||||
|
||||
await bridge.accept(message('retry-send-2', '/send'));
|
||||
assert.equal(attempts, 2);
|
||||
assert.equal(sent.at(-1), '重试成功');
|
||||
|
||||
await bridge.accept(message('cancel-start', '/batch'));
|
||||
await bridge.accept(message('cancel-content', '丢弃我'));
|
||||
await bridge.accept(message('cancel-command', '/cancel'));
|
||||
assert.match(sent.at(-1), /已取消批量输入.*丢弃 1 条消息/s);
|
||||
assert.equal(attempts, 2);
|
||||
});
|
||||
|
||||
test('Weixin clears a submitted batch when the Harness turn is stopped', async () => {
|
||||
const fixture = stateFixture();
|
||||
fixture.sessions.set('p2p:owner-user', 'session-batch-stopped');
|
||||
const sent = [];
|
||||
const bridge = new WeixinHarnessBridge({
|
||||
api: { sendText: async ({ text }) => sent.push(text) },
|
||||
baseUrl: 'https://ilinkai.weixin.qq.com/',
|
||||
token: 'host-token',
|
||||
ownerUserId: 'owner-user',
|
||||
harness: {
|
||||
sessionExists: async () => true,
|
||||
ask: async () => {
|
||||
const error = new Error('turn stopped');
|
||||
error.code = 'turn-stopped';
|
||||
throw error;
|
||||
},
|
||||
},
|
||||
state: fixture.state,
|
||||
});
|
||||
|
||||
await bridge.accept(message('stopped-start', '/batch'));
|
||||
await bridge.accept(message('stopped-content', '不要重试'));
|
||||
await bridge.accept(message('stopped-send', '/send'));
|
||||
assert.doesNotMatch(sent.join('\n'), /已保留/);
|
||||
|
||||
await bridge.accept(message('stopped-cancel', '/cancel'));
|
||||
assert.match(sent.at(-1), /没有正在进行的批量输入/);
|
||||
});
|
||||
|
||||
test('Weixin refuses /batch while the existing conversation queue is running', async () => {
|
||||
const fixture = stateFixture();
|
||||
fixture.sessions.set('p2p:owner-user', 'session-batch-busy');
|
||||
const running = deferred();
|
||||
const sent = [];
|
||||
let asks = 0;
|
||||
const bridge = new WeixinHarnessBridge({
|
||||
api: { sendText: async ({ text }) => sent.push(text) },
|
||||
baseUrl: 'https://ilinkai.weixin.qq.com/',
|
||||
token: 'host-token',
|
||||
ownerUserId: 'owner-user',
|
||||
harness: {
|
||||
sessionExists: async () => true,
|
||||
ask: async () => {
|
||||
asks += 1;
|
||||
await running.promise;
|
||||
return '原任务完成';
|
||||
},
|
||||
},
|
||||
state: fixture.state,
|
||||
});
|
||||
|
||||
const turn = bridge.accept(message('busy-turn', '正在运行'));
|
||||
await eventually(() => asks === 1);
|
||||
await bridge.accept(message('busy-batch', '/batch'));
|
||||
assert.match(sent.at(-1), /正在运行的任务.*\/stop.*\/batch/s);
|
||||
|
||||
running.resolve();
|
||||
await turn;
|
||||
await bridge.accept(message('busy-send', '/send'));
|
||||
assert.match(sent.at(-1), /没有待提交的批量内容/);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -2,7 +2,10 @@ import assert from 'node:assert/strict';
|
|||
import test from 'node:test';
|
||||
|
||||
import { WeixinApiError } from '../../../src/channels/weixin/weixin-api.mjs';
|
||||
import { WeixinRuntime } from '../../../src/channels/weixin/weixin-runtime.mjs';
|
||||
import {
|
||||
orderWeixinMessages,
|
||||
WeixinRuntime,
|
||||
} from '../../../src/channels/weixin/weixin-runtime.mjs';
|
||||
|
||||
const flush = () => new Promise((resolve) => setImmediate(resolve));
|
||||
|
||||
|
|
@ -50,6 +53,90 @@ function abortable(promise, signal) {
|
|||
});
|
||||
}
|
||||
|
||||
test('Weixin orders one update batch by sequence instead of its unrelated message id', () => {
|
||||
const first = { seq: 101, message_id: 900 };
|
||||
const second = { seq: '102', message_id: 100 };
|
||||
const third = { seq: 103, message_id: 500 };
|
||||
assert.deepEqual(orderWeixinMessages([third, first, second]), [first, second, third]);
|
||||
|
||||
const earlier = { create_time_ms: 1_000 };
|
||||
const later = { create_time_ms: 2_000 };
|
||||
assert.deepEqual(orderWeixinMessages([later, earlier]), [earlier, later]);
|
||||
|
||||
const withoutOrder = { client_id: 'local-only' };
|
||||
assert.deepEqual(orderWeixinMessages([third, withoutOrder, first]), [third, withoutOrder, first]);
|
||||
assert.deepEqual(orderWeixinMessages(null), []);
|
||||
});
|
||||
|
||||
test('runtime preserves rapid batch command order when getUpdates returns newest first', async () => {
|
||||
const sends = [];
|
||||
const seen = new Set();
|
||||
let polls = 0;
|
||||
const stopped = deferred();
|
||||
const inbound = (seq, messageId, text) => ({
|
||||
seq,
|
||||
message_id: messageId,
|
||||
message_type: 1,
|
||||
from_user_id: 'owner',
|
||||
context_token: `context-${messageId}`,
|
||||
item_list: [{ type: 1, text_item: { text } }],
|
||||
});
|
||||
const runtime = new WeixinRuntime({
|
||||
api: {
|
||||
notifyStart: async () => {},
|
||||
notifyStop: async () => {},
|
||||
sendText: async ({ text }) => sends.push(text),
|
||||
getUpdates: async ({ signal }) => {
|
||||
polls += 1;
|
||||
if (polls === 1) {
|
||||
return {
|
||||
ret: 0,
|
||||
get_updates_buf: 'ordered-batch',
|
||||
msgs: [
|
||||
inbound(303, 200, '/cancel'),
|
||||
inbound(302, 100, '这条内容必须被取消'),
|
||||
inbound(301, 900, '/batch'),
|
||||
],
|
||||
};
|
||||
}
|
||||
return abortable(stopped.promise, signal);
|
||||
},
|
||||
},
|
||||
config: {
|
||||
botId: 'wx_ordered_batch',
|
||||
baseUrl: 'https://ilinkai.weixin.qq.com/',
|
||||
ownerUserId: 'owner',
|
||||
},
|
||||
token: 'bot-token',
|
||||
harness: {
|
||||
ensureRunning: async () => true,
|
||||
ask: async () => assert.fail('cancelled batch content must not reach Harness'),
|
||||
},
|
||||
state: {
|
||||
getUpdatesBuf: () => '',
|
||||
setGetUpdatesBuf: async () => {},
|
||||
hasSeen: (id) => seen.has(id),
|
||||
markSeen: async (id) => seen.add(id),
|
||||
sessionFor: () => null,
|
||||
setSession: async () => {},
|
||||
clearSession: async () => {},
|
||||
},
|
||||
logger: { warn() {}, error() {} },
|
||||
});
|
||||
|
||||
await runtime.start();
|
||||
try {
|
||||
await eventually(
|
||||
() => sends.some((text) => /丢弃 1 条消息/.test(text)),
|
||||
'the reversed provider batch should be processed in sequence order',
|
||||
);
|
||||
assert.equal(sends.some((text) => /仅支持文字|没有正在进行/.test(text)), false);
|
||||
} finally {
|
||||
await runtime.stop();
|
||||
stopped.resolve({ ret: 0, msgs: [] });
|
||||
}
|
||||
});
|
||||
|
||||
test('runtime sends a connection test to the bound Weixin owner without reply context', async () => {
|
||||
const sends = [];
|
||||
const runtime = new WeixinRuntime({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue