mirror of
https://github.com/hansjone/dsh-im-ops.git
synced 2026-10-10 05:03:15 +08:00
feat: support compact command across IM channels
This commit is contained in:
parent
4545cfe9c5
commit
1cc7a26f30
40 changed files with 728 additions and 136 deletions
|
|
@ -76,6 +76,31 @@ function stateFixture() {
|
|||
};
|
||||
}
|
||||
|
||||
test('DingTalk executes /compact for the bound Session without prompting the model', async () => {
|
||||
const fixture = stateFixture();
|
||||
fixture.sessions.set('p2p:staff-approved', 'session-compact');
|
||||
const sent = [];
|
||||
const executed = [];
|
||||
const bridge = new DingtalkHarnessBridge({
|
||||
api: { sendText: async (request) => sent.push(request) },
|
||||
clientId: 'ding-client',
|
||||
clientSecret: 'host-secret',
|
||||
harness: {
|
||||
executeCommand: async (sessionId, line) => {
|
||||
executed.push({ sessionId, line });
|
||||
return { commandId: 'compact-dingtalk', result: { kind: 'success', text: 'No compactable history yet.' } };
|
||||
},
|
||||
ask: async () => assert.fail('/compact must not be submitted to the model'),
|
||||
},
|
||||
state: fixture.state,
|
||||
});
|
||||
|
||||
await bridge.accept(message('compact-dingtalk', '/compact'));
|
||||
|
||||
assert.deepEqual(executed, [{ sessionId: 'session-compact', line: '/compact' }]);
|
||||
assert.equal(sent.at(-1).text, '暂无可压缩的历史记录。');
|
||||
});
|
||||
|
||||
test('bridge maps a DingTalk direct conversation to one persistent Harness session', async () => {
|
||||
const fixture = stateFixture();
|
||||
const sent = [];
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ function controller() {
|
|||
test('Host exports the DingTalk plugin identity and required services', () => {
|
||||
const plugin = createDingtalkHostPlugin({ controller: controller() });
|
||||
assert.equal(name, 'dsh-dingtalk-host');
|
||||
assert.deepEqual(inject, ['connection', 'credentials', 'webServer']);
|
||||
assert.deepEqual(inject, ['connection', 'credentials', 'webServer', 'typertGateway']);
|
||||
assert.equal(plugin.name, name);
|
||||
assert.deepEqual(plugin.inject, inject);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -79,6 +79,32 @@ function textClient(sendText) {
|
|||
};
|
||||
}
|
||||
|
||||
test('Feishu executes /compact for the bound Session without prompting the model', async () => {
|
||||
const fixture = stateFixture([['p2p:ou_user', 'session-compact']]);
|
||||
const sent = [];
|
||||
const executed = [];
|
||||
const bridge = new FeishuHarnessBridge({
|
||||
client: textClient(async ({ text }) => sent.push(text)),
|
||||
channel: {},
|
||||
harness: {
|
||||
executeCommand: async (sessionId, line) => {
|
||||
executed.push({ sessionId, line });
|
||||
return { commandId: 'compact-feishu', result: { kind: 'success', text: 'No compactable history yet.' } };
|
||||
},
|
||||
ask: async () => assert.fail('/compact must not be submitted to the model'),
|
||||
},
|
||||
state: fixture.state,
|
||||
status: bridgeStatus(),
|
||||
allowedSenderOpenIds: new Set(['ou_user']),
|
||||
});
|
||||
|
||||
await bridge.accept(event('compact-feishu', '/compact'));
|
||||
await bridge.waitForIdle();
|
||||
|
||||
assert.deepEqual(executed, [{ sessionId: 'session-compact', line: '/compact' }]);
|
||||
assert.deepEqual(sent, ['暂无可压缩的历史记录。']);
|
||||
});
|
||||
|
||||
test('bridge maps a Feishu conversation to a persistent Harness session and replies', async () => {
|
||||
const sent = [];
|
||||
const reactions = [];
|
||||
|
|
|
|||
|
|
@ -51,6 +51,30 @@ function message(overrides = {}) {
|
|||
};
|
||||
}
|
||||
|
||||
test('QQ executes /compact for the bound Session without prompting the model', async () => {
|
||||
const fixture = stateFixture([['c2c:owner-openid', 'session-compact']]);
|
||||
const sent = [];
|
||||
const executed = [];
|
||||
const bridge = new QqHarnessBridge({
|
||||
bot: { sendText: async (_target, text) => sent.push(text) },
|
||||
ownerUserOpenid: 'owner-openid',
|
||||
harness: {
|
||||
executeCommand: async (sessionId, line) => {
|
||||
executed.push({ sessionId, line });
|
||||
return { commandId: 'compact-qq', result: { kind: 'success', text: 'Compacted 3 history items (~900 tokens).' } };
|
||||
},
|
||||
ask: async () => assert.fail('/compact must not be submitted to the model'),
|
||||
},
|
||||
state: fixture.state,
|
||||
});
|
||||
|
||||
await bridge.accept(message({ messageId: 'compact-qq', content: '/compact' }));
|
||||
|
||||
assert.deepEqual(executed, [{ sessionId: 'session-compact', line: '/compact' }]);
|
||||
assert.deepEqual(sent, ['已压缩 3 条历史记录(约 900 个 token)。']);
|
||||
assert.equal(fixture.seen.has('compact-qq'), true);
|
||||
});
|
||||
|
||||
test('QQ private messages stream Harness snapshots and finalize once', async () => {
|
||||
const frames = [];
|
||||
const sent = [];
|
||||
|
|
|
|||
|
|
@ -117,6 +117,38 @@ function createBridge({ harness, state, bot, signal, logger } = {}) {
|
|||
});
|
||||
}
|
||||
|
||||
test('all four shared text channels execute /compact outside the model prompt path', async () => {
|
||||
for (const [name, Bridge] of [
|
||||
['slack', SlackHarnessBridge],
|
||||
['telegram', TelegramHarnessBridge],
|
||||
['discord', DiscordHarnessBridge],
|
||||
['whatsapp', WhatsappHarnessBridge],
|
||||
]) {
|
||||
const fixture = stateFixture({ 'direct:chat-a': `session-${name}` });
|
||||
const sent = [];
|
||||
const executed = [];
|
||||
const bridge = new Bridge({
|
||||
bot: { sendText: async (_target, text) => sent.push(text) },
|
||||
state: fixture.state,
|
||||
harness: {
|
||||
executeCommand: async (sessionId, line) => {
|
||||
executed.push({ sessionId, line });
|
||||
return {
|
||||
commandId: `command-${name}`,
|
||||
result: { kind: 'success', text: 'Compacted 12 history items (~3456 tokens).' },
|
||||
};
|
||||
},
|
||||
ask: async () => assert.fail('/compact must not be submitted to the model'),
|
||||
},
|
||||
});
|
||||
|
||||
await bridge.accept(message(`compact-${name}`, '/compact'));
|
||||
|
||||
assert.deepEqual(executed, [{ sessionId: `session-${name}`, line: '/compact' }]);
|
||||
assert.deepEqual(sent, ['已压缩 12 条历史记录(约 3456 个 token)。']);
|
||||
}
|
||||
});
|
||||
|
||||
test('answers a multi-question interaction on the fast lane with canonical values', async () => {
|
||||
const fixture = stateFixture();
|
||||
const sent = [];
|
||||
|
|
|
|||
|
|
@ -91,6 +91,31 @@ function questionInteraction({
|
|||
};
|
||||
}
|
||||
|
||||
test('Enterprise WeChat executes /compact for the bound Session without prompting the model', async () => {
|
||||
const store = state();
|
||||
const transport = testClient();
|
||||
const executed = [];
|
||||
const bridge = new WecomHarnessBridge({
|
||||
client: transport.client,
|
||||
generateStreamId: () => 'unused-stream',
|
||||
harness: {
|
||||
executeCommand: async (sessionId, line) => {
|
||||
executed.push({ sessionId, line });
|
||||
return { commandId: 'compact-wecom', result: { kind: 'success', text: 'No compactable history yet.' } };
|
||||
},
|
||||
ask: async () => assert.fail('/compact must not be submitted to the model'),
|
||||
},
|
||||
state: store,
|
||||
});
|
||||
|
||||
await bridge.accept(frame({ msgid: 'compact-wecom', text: { content: '/compact' } }));
|
||||
|
||||
assert.deepEqual(executed, [{ sessionId: 'session-existing', line: '/compact' }]);
|
||||
assert.equal(transport.streamed.at(-1).content, '暂无可压缩的历史记录。');
|
||||
assert.equal(transport.streamed.at(-1).finish, true);
|
||||
assert.deepEqual(transport.active, []);
|
||||
});
|
||||
|
||||
test('Enterprise WeChat messages stream Harness progress and finalize once', async () => {
|
||||
const replies = [];
|
||||
const active = [];
|
||||
|
|
|
|||
|
|
@ -63,6 +63,33 @@ function stateFixture() {
|
|||
};
|
||||
}
|
||||
|
||||
test('Weixin executes /compact for the bound Session without prompting the model', async () => {
|
||||
const fixture = stateFixture();
|
||||
fixture.sessions.set('p2p:owner-user', 'session-compact');
|
||||
const sent = [];
|
||||
const executed = [];
|
||||
const bridge = new WeixinHarnessBridge({
|
||||
api: { sendText: async (request) => sent.push(request) },
|
||||
baseUrl: 'https://ilinkai.weixin.qq.com/',
|
||||
token: 'host-token',
|
||||
ownerUserId: 'owner-user',
|
||||
harness: {
|
||||
executeCommand: async (sessionId, line) => {
|
||||
executed.push({ sessionId, line });
|
||||
return { commandId: 'compact-weixin', result: { kind: 'success', text: 'No compactable history yet.' } };
|
||||
},
|
||||
ask: async () => assert.fail('/compact must not be submitted to the model'),
|
||||
},
|
||||
state: fixture.state,
|
||||
});
|
||||
|
||||
await bridge.accept(message('compact-weixin', '/compact'));
|
||||
|
||||
assert.deepEqual(executed, [{ sessionId: 'session-compact', line: '/compact' }]);
|
||||
assert.equal(sent.at(-1).text, '暂无可压缩的历史记录。');
|
||||
assert.equal(fixture.seen.has('compact-weixin'), true);
|
||||
});
|
||||
|
||||
test('bridge maps the scanning Weixin user to one persistent Harness session and echoes context_token', async () => {
|
||||
const sent = [];
|
||||
const asked = [];
|
||||
|
|
|
|||
171
test/compact-command.test.mjs
Normal file
171
test/compact-command.test.mjs
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
import assert from 'node:assert/strict';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import test from 'node:test';
|
||||
|
||||
import { runCompactCommand } from '../src/channels/shared/compact-command.mjs';
|
||||
import {
|
||||
HarnessClient,
|
||||
HarnessRpcError,
|
||||
} from '../src/channels/shared/harness-client.mjs';
|
||||
import { createHarnessCommandExecutor } from '../plugin-src/host/harness-command-executor.mjs';
|
||||
|
||||
function state(sessionId = 'session-one') {
|
||||
return { sessionFor: () => sessionId };
|
||||
}
|
||||
|
||||
test('compact command validates syntax and requires an existing conversation Session', async () => {
|
||||
assert.equal(await runCompactCommand('hello', {}, state(), 'direct:one'), null);
|
||||
assert.match(
|
||||
(await runCompactCommand('/compact now', {}, state(), 'direct:one')).message,
|
||||
/不带参数/,
|
||||
);
|
||||
assert.match(
|
||||
(await runCompactCommand('/COMPACT', {}, state(null), 'direct:one')).message,
|
||||
/还没有可压缩的会话/,
|
||||
);
|
||||
assert.match(
|
||||
(await runCompactCommand('/compact', {}, state(), 'direct:one')).message,
|
||||
/暂不支持/,
|
||||
);
|
||||
});
|
||||
|
||||
test('compact command renders Harness outcomes and never changes the command line', async () => {
|
||||
const calls = [];
|
||||
const harness = {
|
||||
executeCommand: async (sessionId, line, options) => {
|
||||
calls.push({ sessionId, line, options });
|
||||
return {
|
||||
commandId: 'command-one',
|
||||
result: { kind: 'error', text: 'Compaction cancelled.' },
|
||||
};
|
||||
},
|
||||
};
|
||||
const signal = new AbortController().signal;
|
||||
const result = await runCompactCommand(
|
||||
' /COMPACT ',
|
||||
harness,
|
||||
state(),
|
||||
'direct:one',
|
||||
{ signal },
|
||||
);
|
||||
|
||||
assert.equal(result.message, '上下文压缩已取消。');
|
||||
assert.deepEqual(calls, [{
|
||||
sessionId: 'session-one',
|
||||
line: '/compact',
|
||||
options: { signal },
|
||||
}]);
|
||||
});
|
||||
|
||||
test('compact command contains unavailable, busy, stale, and invalid command failures', async () => {
|
||||
for (const [failure, pattern] of [
|
||||
[{ code: 'session-not-found' }, /会话已不存在/],
|
||||
[{ code: 'agent-busy' }, /正在生成回复/],
|
||||
[{ code: 'workspace-session-stale' }, /状态已发生变化/],
|
||||
[{ code: 'commands-unavailable' }, /暂不支持/],
|
||||
[new Error('private internal detail'), /压缩失败/],
|
||||
]) {
|
||||
const result = await runCompactCommand('/compact', {
|
||||
executeCommand: async () => { throw failure; },
|
||||
}, state(), 'direct:one');
|
||||
assert.match(result.message, pattern);
|
||||
assert.doesNotMatch(result.message, /private internal detail/);
|
||||
}
|
||||
|
||||
assert.match((await runCompactCommand('/compact', {
|
||||
executeCommand: async () => undefined,
|
||||
}, state(), 'direct:one')).message, /未注册/);
|
||||
assert.match((await runCompactCommand('/compact', {
|
||||
executeCommand: async () => ({ commandId: 'bad', result: { kind: 'other' } }),
|
||||
}, state(), 'direct:one')).message, /压缩失败/);
|
||||
});
|
||||
|
||||
test('HarnessClient delegates command execution and normalizes Typert lookup failures', async () => {
|
||||
const calls = [];
|
||||
const client = new HarnessClient({
|
||||
baseUrl: 'http://127.0.0.1:1',
|
||||
workspace: '/tmp',
|
||||
commandExecutor: async (...args) => {
|
||||
calls.push(args);
|
||||
return { commandId: 'command-one', result: { kind: 'success' } };
|
||||
},
|
||||
});
|
||||
const signal = new AbortController().signal;
|
||||
assert.deepEqual(await client.executeCommand('session-one', '/compact', { signal }), {
|
||||
commandId: 'command-one',
|
||||
result: { kind: 'success' },
|
||||
});
|
||||
assert.deepEqual(calls, [['session-one', '/compact', { signal }]]);
|
||||
|
||||
const unavailable = new HarnessClient({ baseUrl: 'http://127.0.0.1:1', workspace: '/tmp' });
|
||||
await assert.rejects(unavailable.executeCommand('session-one', '/compact'), {
|
||||
code: 'commands-unavailable',
|
||||
});
|
||||
|
||||
const rejected = new HarnessClient({
|
||||
baseUrl: 'http://127.0.0.1:1',
|
||||
workspace: '/tmp',
|
||||
commandExecutor: async () => {
|
||||
const error = new Error('lookup rejected');
|
||||
error.failure = { code: 'agent-busy', message: 'busy', details: { reason: 'turn active' } };
|
||||
throw error;
|
||||
},
|
||||
});
|
||||
await assert.rejects(
|
||||
rejected.executeCommand('session-one', '/compact'),
|
||||
(error) => error instanceof HarnessRpcError && error.code === 'agent-busy',
|
||||
);
|
||||
});
|
||||
|
||||
test('Host command executor invokes the commands Typert endpoint with the Session identity', async () => {
|
||||
const requests = [];
|
||||
const executor = createHarnessCommandExecutor({
|
||||
typertGateway: { invoke: async (request) => {
|
||||
requests.push(request);
|
||||
return { commandId: 'command-one', result: { kind: 'success' } };
|
||||
} },
|
||||
});
|
||||
const signal = new AbortController().signal;
|
||||
|
||||
assert.deepEqual(await executor('session-one', '/compact', { signal }), {
|
||||
commandId: 'command-one',
|
||||
result: { kind: 'success' },
|
||||
});
|
||||
assert.deepEqual(requests, [{
|
||||
namespace: 'commands',
|
||||
method: 'execute',
|
||||
args: { agentId: 'session-one', line: '/compact' },
|
||||
signal,
|
||||
}]);
|
||||
assert.equal(createHarnessCommandExecutor({}), undefined);
|
||||
assert.throws(() => createHarnessCommandExecutor({}, 'invalid'), /must be a function/);
|
||||
});
|
||||
|
||||
test('all nine production channels receive the Host command executor', async () => {
|
||||
const productionFiles = [
|
||||
'plugin-src/host/channels/feishu/production.mjs',
|
||||
'plugin-src/host/channels/weixin/production.mjs',
|
||||
'plugin-src/host/channels/dingtalk/production.mjs',
|
||||
'plugin-src/host/channels/wecom/production.mjs',
|
||||
'plugin-src/host/channels/qq/production.mjs',
|
||||
'plugin-src/host/channels/slack/production.mjs',
|
||||
'plugin-src/host/channels/shared/production.mjs',
|
||||
'plugin-src/host/channels/whatsapp/production.mjs',
|
||||
];
|
||||
for (const path of productionFiles) {
|
||||
const source = await readFile(new URL(`../${path}`, import.meta.url), 'utf8');
|
||||
assert.match(source, /createHarnessCommandExecutor\(ctx, internals\.commandExecutor\)/, path);
|
||||
assert.match(source, /commandExecutor \? \{ commandExecutor \} : \{\}/, path);
|
||||
}
|
||||
|
||||
for (const channel of [
|
||||
'feishu', 'weixin', 'dingtalk', 'wecom', 'qq',
|
||||
'slack', 'telegram', 'discord', 'whatsapp',
|
||||
]) {
|
||||
const source = await readFile(
|
||||
new URL(`../plugin-src/host/channels/${channel}/index.mjs`, import.meta.url),
|
||||
'utf8',
|
||||
);
|
||||
assert.match(source, /'typertGateway'/, channel);
|
||||
}
|
||||
});
|
||||
|
|
@ -33,7 +33,7 @@ test('Host composes all nine IM channels inside one plugin context', async () =>
|
|||
await plugin.apply(ctx, config);
|
||||
|
||||
assert.equal(name, 'dsh-im-host');
|
||||
assert.deepEqual(inject, ['connection', 'credentials', 'webServer']);
|
||||
assert.deepEqual(inject, ['connection', 'credentials', 'webServer', 'typertGateway']);
|
||||
assert.deepEqual(calls, [
|
||||
['feishu', ctx, { ...config.feishu, rpcAuthority: 'trusted-host' }],
|
||||
['weixin', ctx, { ...config.weixin, rpcAuthority: 'trusted-host' }],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue