fix(feishu): preserve explicit target routing hints (#31594) (thanks @liuxiaopai-ai)

This commit is contained in:
Peter Steinberger
2026-03-02 13:40:55 +00:00
parent 07b419a0e7
commit 05b84e718b
4 changed files with 85 additions and 2 deletions

View File

@@ -0,0 +1,74 @@
import type { ClawdbotConfig } from "openclaw/plugin-sdk";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { resolveFeishuSendTarget } from "./send-target.js";
const resolveFeishuAccountMock = vi.hoisted(() => vi.fn());
const createFeishuClientMock = vi.hoisted(() => vi.fn());
vi.mock("./accounts.js", () => ({
resolveFeishuAccount: resolveFeishuAccountMock,
}));
vi.mock("./client.js", () => ({
createFeishuClient: createFeishuClientMock,
}));
describe("resolveFeishuSendTarget", () => {
const cfg = {} as ClawdbotConfig;
const client = { id: "client" };
beforeEach(() => {
resolveFeishuAccountMock.mockReset().mockReturnValue({
accountId: "default",
enabled: true,
configured: true,
});
createFeishuClientMock.mockReset().mockReturnValue(client);
});
it("keeps explicit group targets as chat_id even when ID shape is ambiguous", () => {
const result = resolveFeishuSendTarget({
cfg,
to: "feishu:group:group_room_alpha",
});
expect(result.receiveId).toBe("group_room_alpha");
expect(result.receiveIdType).toBe("chat_id");
expect(result.client).toBe(client);
});
it("maps dm-prefixed open IDs to open_id", () => {
const result = resolveFeishuSendTarget({
cfg,
to: "lark:dm:ou_123",
});
expect(result.receiveId).toBe("ou_123");
expect(result.receiveIdType).toBe("open_id");
});
it("maps dm-prefixed non-open IDs to user_id", () => {
const result = resolveFeishuSendTarget({
cfg,
to: " feishu:dm:user_123 ",
});
expect(result.receiveId).toBe("user_123");
expect(result.receiveIdType).toBe("user_id");
});
it("throws when target account is not configured", () => {
resolveFeishuAccountMock.mockReturnValue({
accountId: "default",
enabled: true,
configured: false,
});
expect(() =>
resolveFeishuSendTarget({
cfg,
to: "feishu:group:oc_123",
}),
).toThrow('Feishu account "default" not configured');
});
});

View File

@@ -8,18 +8,22 @@ export function resolveFeishuSendTarget(params: {
to: string;
accountId?: string;
}) {
const target = params.to.trim();
const account = resolveFeishuAccount({ cfg: params.cfg, accountId: params.accountId });
if (!account.configured) {
throw new Error(`Feishu account "${account.accountId}" not configured`);
}
const client = createFeishuClient(account);
const receiveId = normalizeFeishuTarget(params.to);
const receiveId = normalizeFeishuTarget(target);
if (!receiveId) {
throw new Error(`Invalid Feishu target: ${params.to}`);
}
// Preserve explicit routing prefixes (chat/group/user/dm/open_id) when present.
// normalizeFeishuTarget strips these prefixes, so infer type from the raw target first.
const withoutProviderPrefix = target.replace(/^(feishu|lark):/i, "");
return {
client,
receiveId,
receiveIdType: resolveReceiveIdType(receiveId),
receiveIdType: resolveReceiveIdType(withoutProviderPrefix),
};
}

View File

@@ -17,6 +17,10 @@ describe("resolveReceiveIdType", () => {
it("treats explicit group targets as chat_id", () => {
expect(resolveReceiveIdType("group:oc_123")).toBe("chat_id");
});
it("treats dm-prefixed open IDs as open_id", () => {
expect(resolveReceiveIdType("dm:ou_123")).toBe("open_id");
});
});
describe("normalizeFeishuTarget", () => {