From 1b814b4dfacca66322b98a2d5e8abd39cc72d551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=85=E8=86=80?= Date: Fri, 23 Oct 2026 23:23:16 +0800 Subject: [PATCH] fix(feishu): retry sends with stable uuids An unreadable response does not prove a send failed. Reuse the same uuid for retries so recovering the result cannot create a second Feishu message. --- server/lib/drivers/feishu/rest.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/server/lib/drivers/feishu/rest.ts b/server/lib/drivers/feishu/rest.ts index f424483..93cb30b 100644 --- a/server/lib/drivers/feishu/rest.ts +++ b/server/lib/drivers/feishu/rest.ts @@ -79,6 +79,12 @@ async function feishuRequest( } const data = (await res.json().catch(() => null)) as FeishuResponse | null; + // A 200 whose body cannot be read (or carries no result code) is not + // known to have succeeded: tried again (the request's uuid keeps a + // send from doubling). + if (res.ok && (!data || typeof data.code !== "number")) { + throw new Error("Feishu answered without a result code"); + } if (!res.ok) { lastError = data?.msg ?? data?.error?.message ?? `HTTP ${res.status}`; @@ -197,12 +203,17 @@ export async function sendMessage( token: string, chatId: string, content: Record, + sendKey?: string, ): Promise { const url = `${FEISHU_API}/open-apis/im/v1/messages?receive_id_type=chat_id`; + // One `uuid` for every attempt of this send (of the delivery, given a + // `sendKey`): Feishu delivers at most one message per uuid within an hour, + // so a retry after a lost response never posts a duplicate. const body: Record = { receive_id: chatId, msg_type: "interactive", content: JSON.stringify(content), + uuid: (sendKey ?? crypto.randomUUID()).slice(0, 50), }; return feishuRequest(url, "POST", token, body, chatId); } @@ -223,6 +234,7 @@ export async function sendText(token: string, chatId: string, text: string): Pro receive_id: chatId, msg_type: "text", content: JSON.stringify({ text }), + uuid: crypto.randomUUID(), }; return feishuRequest(url, "POST", token, body, chatId); }