diff --git a/.changeset/discord-thread-160004-recovery.md b/.changeset/discord-thread-160004-recovery.md new file mode 100644 index 00000000..d32c3ace --- /dev/null +++ b/.changeset/discord-thread-160004-recovery.md @@ -0,0 +1,5 @@ +--- +"@chat-adapter/discord": patch +--- + +Fix silent thread creation failure when Discord returns error code 160004 ("A thread has already been created for this message"). The adapter now recovers by reusing the existing thread instead of falling back to a standalone channel message. diff --git a/packages/adapter-discord/src/index.test.ts b/packages/adapter-discord/src/index.test.ts index c20e6118..41e5096e 100644 --- a/packages/adapter-discord/src/index.test.ts +++ b/packages/adapter-discord/src/index.test.ts @@ -3968,3 +3968,70 @@ describe("mentionRoleIds handling", () => { fetchSpy.mockRestore(); }); }); + +// ============================================================================ +// createDiscordThread 160004 Recovery Tests +// ============================================================================ + +describe("createDiscordThread 160004 recovery", () => { + const adapter = createDiscordAdapter({ + botToken: "test-token", + publicKey: testPublicKey, + applicationId: "test-app-id", + logger: mockLogger, + }); + + it("should recover when Discord returns 160004 (thread already exists)", async () => { + const { NetworkError } = await import("@chat-adapter/shared"); + + const spy = vi + .spyOn(adapter as any, "discordFetch") + .mockRejectedValue( + new NetworkError( + "discord", + 'Discord API error: 400 {"code": 160004, "message": "A thread has already been created for this message"}' + ) + ); + + const result = await (adapter as any).createDiscordThread( + "channel123", + "msg456" + ); + + expect(result.id).toBe("msg456"); + expect(result.name).toContain("Thread "); + + spy.mockRestore(); + }); + + it("should propagate non-160004 NetworkErrors", async () => { + const { NetworkError } = await import("@chat-adapter/shared"); + + const spy = vi + .spyOn(adapter as any, "discordFetch") + .mockRejectedValue( + new NetworkError( + "discord", + 'Discord API error: 403 {"code": 50001, "message": "Missing Access"}' + ) + ); + + await expect( + (adapter as any).createDiscordThread("channel123", "msg456") + ).rejects.toThrow(NetworkError); + + spy.mockRestore(); + }); + + it("should propagate non-NetworkError errors", async () => { + const spy = vi + .spyOn(adapter as any, "discordFetch") + .mockRejectedValue(new Error("Connection failed")); + + await expect( + (adapter as any).createDiscordThread("channel123", "msg456") + ).rejects.toThrow("Connection failed"); + + spy.mockRestore(); + }); +}); diff --git a/packages/adapter-discord/src/index.ts b/packages/adapter-discord/src/index.ts index 1b6f84a4..ea302485 100644 --- a/packages/adapter-discord/src/index.ts +++ b/packages/adapter-discord/src/index.ts @@ -959,23 +959,41 @@ export class DiscordAdapter implements Adapter { threadName, }); - const response = await this.discordFetch( - `/channels/${channelId}/messages/${messageId}/threads`, - "POST", - { - name: threadName, - auto_archive_duration: 1440, // 24 hours - } - ); + try { + const response = await this.discordFetch( + `/channels/${channelId}/messages/${messageId}/threads`, + "POST", + { + name: threadName, + auto_archive_duration: 1440, // 24 hours + } + ); - const result = (await response.json()) as { id: string; name: string }; + const result = (await response.json()) as { id: string; name: string }; - this.logger.debug("Discord API: POST thread response", { - threadId: result.id, - threadName: result.name, - }); + this.logger.debug("Discord API: POST thread response", { + threadId: result.id, + threadName: result.name, + }); - return result; + return result; + } catch (error) { + // Discord error 160004: "A thread has already been created for this message" + // Recover by using the existing thread (its ID equals the parent message ID). + if ( + error instanceof NetworkError && + typeof error.message === "string" && + error.message.includes('"code"') && + error.message.includes("160004") + ) { + this.logger.debug( + "Thread already exists for message, reusing existing thread", + { channelId, messageId } + ); + return { id: messageId, name: threadName }; + } + throw error; + } } /**