Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(im): handle Discord messages without a channel id (#3009)
Junshan7ebef1cde960
diff --git a/packages/lizi-im/src/discord/__tests__/inbound.test.ts b/packages/lizi-im/src/discord/__tests__/inbound.test.tsindex ecba9f5c68..cdaed8ae6e 100644--- a/packages/lizi-im/src/discord/__tests__/inbound.test.ts+++ b/packages/lizi-im/src/discord/__tests__/inbound.test.ts@@ -36,13 +36,40 @@ describe('normalizeDmMessage', () => {});});+ it('returns null when channel id is missing so the transport suppresses it', async () => {+ // `channelId: ''` flows through the helper because `'' ?? 'dm-1'` is `''`+ // (only null/undefined triggers the default). Asserts an explicit empty+ // string is treated as a missing channel.+ const emptyString = await normalizeDmMessage(+ message({ content: 'hello', channelId: '' }),+ { contextId: 'app-1', mediaDir: tempDir(), download: vi.fn() },+ );+ expect(emptyString).toBeNull();++ // A genuinely channel-less event (no channelId and no channel.id) —+ // e.g. a partial presence update or a guild event leaking past the DM+ // filter — must also be suppressed. The `message()` helper defaults a+ // missing channelId to 'dm-1', so construct the raw object directly.+ const channelLess = await normalizeDmMessage(+ {+ id: 'msg-x',+ content: 'leaked guild text',+ author: { id: 'user-1' },+ attachments: [],+ stickers: [],+ } as Parameters<typeof normalizeDmMessage>[0],+ { contextId: 'app-1', mediaDir: tempDir(), download: vi.fn() },+ );+ expect(channelLess).toBeNull();+ });+it('downloads image attachments into the media dir', async () => {const mediaDir = tempDir();const download = vi.fn(async (_url: string, dest: string) => {fs.writeFileSync(dest, 'image');});- const event = await normalizeDmMessage(+ const event = (await normalizeDmMessage(message({attachments: [{@@ -55,7 +82,7 @@ describe('normalizeDmMessage', () => {],}),{ contextId: 'app-1', mediaDir, download },- );+ ))!;expect(download).toHaveBeenCalledWith('https://cdn.example/photo.png',@@ -87,7 +114,7 @@ describe('normalizeDmMessage', () => {resolveMediaUrl: vi.fn(() => null),};- const event = await normalizeDmMessage(+ const event = (await normalizeDmMessage(message({attachments: [{ id: 'att-1', name: 'photo.png', url: 'https://cdn.example/photo.png', size: 1024, contentType: 'image/png' },@@ -95,7 +122,7 @@ describe('normalizeDmMessage', () => {],}),{ contextId: 'app-1', mediaDir, download, media },- );+ ))!;expect(cacheImage).toHaveBeenCalledTimes(1);expect(cacheImage.mock.calls[0][0]).toMatchObject({@@ -131,14 +158,14 @@ describe('normalizeDmMessage', () => {resolveMediaUrl: vi.fn(() => null),};- const event = await normalizeDmMessage(+ const event = (await normalizeDmMessage(message({attachments: [{ id: 'att-1', name: 'photo.png', url: 'https://cdn.example/photo.png', size: 1024, contentType: 'image/png' },],}),{ contextId: 'app-1', mediaDir, download, media },- );+ ))!;expect(event.attachments[0]).toMatchObject({kind: 'image',@@ -151,7 +178,7 @@ describe('normalizeDmMessage', () => {it('marks attachments over 50MiB unsupported without downloading', async () => {const download = vi.fn();- const event = await normalizeDmMessage(+ const event = (await normalizeDmMessage(message({attachments: [{@@ -164,7 +191,7 @@ describe('normalizeDmMessage', () => {],}),{ contextId: 'app-1', mediaDir: tempDir(), download },- );+ ))!;expect(download).not.toHaveBeenCalled();expect(event.attachments).toEqual([]);@@ -172,10 +199,10 @@ describe('normalizeDmMessage', () => {});it('marks stickers unsupported', async () => {- const event = await normalizeDmMessage(+ const event = (await normalizeDmMessage(message({ stickers: [{ id: 'sticker-1', name: 'wave' }] }),{ contextId: 'app-1', mediaDir: tempDir(), download: vi.fn() },- );+ ))!;expect(event.unsupported).toEqual([{ type: 'sticker', label: 'wave' }]);});@@ -222,6 +249,7 @@ describe('normalizeDmMessage', () => {),]);+ if (!first || !second) throw new Error("expected both messages");expect(destinations).toEqual([path.join(mediaDir, 'msg-a-image.png'),path.join(mediaDir, 'msg-b-image.png'),@@ -238,7 +266,7 @@ describe('normalizeDmMessage', () => {fs.writeFileSync(dest, 'image');});- const event = await normalizeDmMessage(+ const event = (await normalizeDmMessage(message({id: 'msg-a',attachments: [@@ -259,7 +287,7 @@ describe('normalizeDmMessage', () => {],}),{ contextId: 'app-1', mediaDir, download },- );+ ))!;expect(event.attachments.map((a) => a.absPath)).toEqual([path.join(mediaDir, 'msg-a-att-1-image.png'),diff --git a/packages/lizi-im/src/discord/inbound.ts b/packages/lizi-im/src/discord/inbound.tsindex 4a49c7a964..1b553367cb 100644--- a/packages/lizi-im/src/discord/inbound.ts+++ b/packages/lizi-im/src/discord/inbound.ts@@ -40,8 +40,15 @@ export async function normalizeDmMessage(/** host 媒体总仓(可选):入站图片提升进内容寻址仓,缺省回落 mediaDir。 */media?: IMHostMediaCache;},-): Promise<IMMessageEvent> {+): Promise<IMMessageEvent | null> {const chatId = m.channelId ?? m.channel?.id ?? '';+ if (!chatId) {+ // Malformed event without a resolvable channel (e.g. partial presence+ // or guild event leaking through the DM filter). Return null so the+ // transport suppresses it entirely — emitting an event with an empty+ // chatId could inject non-DM content into the owner's agent session.+ return null;+ }const attachments: IMAttachment[] = [];const unsupported: IMUnsupportedEntry[] = [];diff --git a/packages/lizi-im/src/discord/index.ts b/packages/lizi-im/src/discord/index.tsindex 12382ba526..083ad46843 100644--- a/packages/lizi-im/src/discord/index.ts+++ b/packages/lizi-im/src/discord/index.ts@@ -855,6 +855,7 @@ export class DiscordIM extends BaseIM implements ChannelIM {download: downloadUrl,media: this.host.media,});+ if (!event) return;if (this.disposing ||this.configVersion !== acceptedContext.configVersion ||