Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(im): 用 Discord 稳定身份恢复后台监听

Dash3f575c6d84f9
diff --git a/apps/desktop/src/main/im/discord/__tests__/discordAdapter.test.ts b/apps/desktop/src/main/im/discord/__tests__/discordAdapter.test.ts
index 6b1e531ddb..23e9f330ec 100644
--- a/apps/desktop/src/main/im/discord/__tests__/discordAdapter.test.ts
+++ b/apps/desktop/src/main/im/discord/__tests__/discordAdapter.test.ts
@@ -62,6 +62,18 @@ describe('discord ImChannelAdapter characterization', () => {
});
});
+ it('reads the current routing identity rather than the status display tag', () => {
+ let appId = 'application-1';
+ const im = {
+ get botContextId() { return appId; },
+ getStatus: () => ({ kind: 'connected', appId: 'display#0000' }),
+ } as unknown as DiscordIM;
+ const current = buildDiscordAdapter(im, CONFIG);
+ expect(current.getBotContextId?.()).toBe('application-1');
+ appId = 'application-2';
+ expect(current.getBotContextId?.()).toBe('application-2');
+ });
+
it('vendorOptions inject discordChatId + source=discord', () => {
expect(adapter.buildVendorOptions('9876543210')).toEqual({
discordChatId: '9876543210',
diff --git a/apps/desktop/src/main/im/discord/adapter.ts b/apps/desktop/src/main/im/discord/adapter.ts
index e198404fc2..401e1b234d 100644
--- a/apps/desktop/src/main/im/discord/adapter.ts
+++ b/apps/desktop/src/main/im/discord/adapter.ts
@@ -28,6 +28,7 @@ export function buildDiscordAdapter(
return {
channel: 'discord',
im: discordIm,
+ getBotContextId: () => discordIm.botContextId,
output: { kind: 'rich-card', im: discordIm },
config,
ui,
diff --git a/apps/desktop/src/main/im/shared/__tests__/orchestratorOutput.test.ts b/apps/desktop/src/main/im/shared/__tests__/orchestratorOutput.test.ts
index aaf6e8825b..1174ff1d13 100644
--- a/apps/desktop/src/main/im/shared/__tests__/orchestratorOutput.test.ts
+++ b/apps/desktop/src/main/im/shared/__tests__/orchestratorOutput.test.ts
@@ -9,6 +9,7 @@ const mocks = vi.hoisted(() => ({
current: vi.fn(),
binding: vi.fn<() => IdentityKey | null>(),
status: vi.fn(),
+ botContextId: vi.fn(),
attach: vi.fn(),
}));
vi.mock('../../../logger', () => ({ createLogger: () => ({ info: vi.fn(), warn: vi.fn() }) }));
@@ -35,10 +36,11 @@ vi.mock('../cardActionHandler', () => ({ createCardActionHandler: () => vi.fn()
import { createImOrchestrator } from '../orchestrator';
import { publishChannelTurn } from '../../../maker-ipc/channelTurnSignal';
-for (const channel of ['telegram', 'feishu'] as const) {
+for (const channel of ['telegram', 'feishu', 'discord', 'wechat', 'wecom', 'dingtalk'] as const) {
createImOrchestrator({
channel, im: { getStatus: mocks.status }, sessions: { source: channel },
output: { kind: 'chunked-text' },
+ ...(channel === 'discord' ? { getBotContextId: mocks.botContextId } : {}),
} as unknown as ImChannelAdapter);
}
const session = { id: 'desktop-task' } as Session;
@@ -55,6 +57,7 @@ beforeEach(() => {
mocks.current.mockReturnValue(true);
mocks.status.mockReturnValue({ kind: 'connected', appId: 'bot' });
mocks.binding.mockReturnValue(null);
+ mocks.botContextId.mockReturnValue('bot');
mocks.limit.mockResolvedValue([nativeRow]);
});
@@ -66,12 +69,35 @@ describe('background output route recovery', () => {
expect(mocks.attach).toHaveBeenCalledExactlyOnceWith(session, 'group-user', { attached: true, scopeKey: 'topic' });
});
- it.each(['feishu', 'telegram'])('falls back to native %s identity without a binding', async (source) => {
+ it.each(['feishu', 'telegram', 'wechat', 'wecom', 'dingtalk'])('falls back to native %s identity without a binding', async (source) => {
mocks.limit.mockResolvedValue([{ ...nativeRow, source, imBotContextId: 'bot', imUserId: 'native-user' }]);
await publishChannelTurn(session, 'starting');
expect(mocks.attach).toHaveBeenCalledExactlyOnceWith(session, 'native-user', { attached: false, scopeKey: undefined });
});
+ it.each([false, true])('uses stable Discord identity for cold route (takeover: %s)', async (attached) => {
+ mocks.status.mockReturnValue({ kind: 'connected', appId: 'display#0000' });
+ mocks.limit.mockResolvedValue([{ source: 'discord', status: 'active', imBotContextId: 'bot', imUserId: 'user' }]);
+ if (attached) mocks.binding.mockReturnValue({ ...binding, channel: 'discord' });
+ await publishChannelTurn(session, 'starting');
+ expect(mocks.attach).toHaveBeenCalledExactlyOnceWith(session, attached ? 'group-user' : 'user', {
+ attached, scopeKey: attached ? 'topic' : undefined,
+ });
+ mocks.attach.mockClear();
+ mocks.botContextId.mockReturnValue('other-bot');
+ await publishChannelTurn(session, 'starting');
+ expect(mocks.attach).not.toHaveBeenCalled();
+ // An unavailable identity must not fall back to a matching display label.
+ mocks.status.mockReturnValue({ kind: 'connected', appId: 'bot' });
+ mocks.botContextId.mockReturnValue('');
+ await publishChannelTurn(session, 'starting');
+ expect(mocks.attach).not.toHaveBeenCalled();
+ mocks.botContextId.mockReturnValue('bot');
+ mocks.status.mockReturnValue({ kind: 'connecting' });
+ await publishChannelTurn(session, 'starting');
+ expect(mocks.attach).not.toHaveBeenCalled();
+ });
+
it.each(['deleted', 'archived', 'missing', 'disconnected', 'wrong-bot', 'wrong-account', 'unbound-desktop', 'unsupported-binding'])(
'does not attach for %s', async (state) => {
mocks.binding.mockReturnValue(binding);
diff --git a/apps/desktop/src/main/im/shared/orchestrator.ts b/apps/desktop/src/main/im/shared/orchestrator.ts
index 33663605ae..bf3c04ef91 100644
--- a/apps/desktop/src/main/im/shared/orchestrator.ts
+++ b/apps/desktop/src/main/im/shared/orchestrator.ts
@@ -62,7 +62,9 @@ onChannelTurn(async (session, phase) => {
if (!orchestrator) return;
const status = orchestrator.adapter.im.getStatus();
const botId = binding?.botContextId ?? (row.source === 'feishu' ? row.feishuBotAppId : row.imBotContextId);
- if (status.kind !== 'connected' || status.appId !== botId) return;
+ if (status.kind !== 'connected') return;
+ const connectedBotId = orchestrator.adapter.getBotContextId?.() ?? status.appId;
+ if (!connectedBotId || connectedBotId !== botId) return;
const userId = binding?.userId ?? (row.source === 'feishu' ? row.feishuOpenId : row.imUserId);
if (userId) orchestrator.turnRunner.attachSessionOutput(session, userId, {
attached: binding !== null,
diff --git a/apps/desktop/src/main/im/shared/types.ts b/apps/desktop/src/main/im/shared/types.ts
index 2186044381..5e96b5040d 100644
--- a/apps/desktop/src/main/im/shared/types.ts
+++ b/apps/desktop/src/main/im/shared/types.ts
@@ -147,6 +147,8 @@ export interface ImChannelAdapter {
messageSourceIm?(): string;
/** 所有渠道共有的文本收发能力;富卡片能力由 output.kind 显式收窄。 */
im: TextChannelIM;
+ /** Stable inbound contextId when connected status.appId is only a display label. */
+ getBotContextId?(): string;
/** Terminal output strategy; existing channels use rich-card. */
output: ImOutputDriver;
config: ImOrchestratorConfig;
diff --git a/docs/product-rules/telegram-bot-parity.md b/docs/product-rules/telegram-bot-parity.md
index f6bc60084d..9d5bbcd9e8 100644
--- a/docs/product-rules/telegram-bot-parity.md
+++ b/docs/product-rules/telegram-bot-parity.md
@@ -258,6 +258,7 @@ SSH 任务不把远端路径或托管媒体引用当作本地文件上传;个
每轮执行前从当前账号库恢复输出监听,核对 bot 身份和任务状态,因此重启后无需先从 IM 发一条消息。
恢复路由优先读取持久化接管绑定的渠道、bot、用户和话题,无接管绑定时才使用渠道原生任务的来源;
接管监听保留 scheduler-only 判据,解除接管沿用原清理入口。
+bot 身份按入站使用的稳定 ID 校验;Discord 取 gateway application id,连接状态中的显示名不用于路由。
飞书/Lark 话题缺少内存锚点时,从原话题查询合法消息恢复;查询失败不降级到群主流。
切换执行实例仅换绑事件监听,保留该任务已排队的 IM 消息。
个人 IM 的排队入站消息在已有后台结果的发送尝试结束后再派发;发送失败同样释放队列,
diff --git a/packages/lizi-im/src/discord/__tests__/inbound.test.ts b/packages/lizi-im/src/discord/__tests__/inbound.test.ts
index cdaed8ae6e..ce1f5b003e 100644
--- a/packages/lizi-im/src/discord/__tests__/inbound.test.ts
+++ b/packages/lizi-im/src/discord/__tests__/inbound.test.ts
@@ -14,6 +14,23 @@ afterEach(() => {
vi.unstubAllGlobals();
});
+describe('Discord routing identity', () => {
+ it('keeps gateway application id separate from the display tag across bot changes', () => {
+ const gateway = makeGateway();
+ const im = new DiscordIM(makeHost(), { gatewayFactory: (handlers) => {
+ gateway.setHandlers(handlers);
+ return gateway;
+ } });
+ gateway.emitStatus({ kind: 'connected', appId: 'bot#0000' });
+ expect(im.getStatus()).toEqual({ kind: 'connected', appId: 'bot#0000' });
+ expect(im.botContextId).toBe('app-1');
+ gateway.setAppId('app-2');
+ expect(im.botContextId).toBe('app-2');
+ gateway.setAppId('');
+ expect(im.botContextId).toBe('');
+ });
+});
+
describe('normalizeDmMessage', () => {
it('normalizes plain DM text fields', async () => {
const event = await normalizeDmMessage(message({ content: 'hello' }), {
diff --git a/packages/lizi-im/src/discord/index.ts b/packages/lizi-im/src/discord/index.ts
index 7035f6d052..b490b883f3 100644
--- a/packages/lizi-im/src/discord/index.ts
+++ b/packages/lizi-im/src/discord/index.ts
@@ -444,6 +444,11 @@ export class DiscordIM extends BaseIM implements ChannelIM {
return this.status;
}
+ /** Routing identity used by inbound contextId; status.appId is a display tag. */
+ get botContextId(): string {
+ return this.gateway.appId;
+ }
+
private restoreSecret(key: string, previousValue: string | null): void {
if (previousValue === null) {
this.host.secrets.remove(key);