Skip to content

Commit f70c4ec

Browse files
XiaoLong LuoXiaoLong Luo
authored andcommitted
fix(feishu): scope sequential queue by group session
1 parent 51affb8 commit f70c4ec

4 files changed

Lines changed: 175 additions & 7 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Docs: https://docs.openclaw.ai
2424

2525
### Fixes
2626

27+
- Feishu: align group-message sequential queue keys with `groupSessionScope`, so separate topics or topic senders no longer block each other unnecessarily. (#72972) Thanks @AaronLuo00.
2728
- fix: block workspace CLOUDSDK_PYTHON override and always set trusted interpreter for gcloud. (#74492) Thanks @pgondhi987.
2829
- Providers/Z.AI: move the bundled GLM catalog and auth env metadata into the plugin manifest, so `models list --all --provider zai` shows the full known catalog without duplicated runtime seed data. Thanks @shakkernerd.
2930
- Providers/Qianfan and Providers/Stepfun: declare setup auth metadata (`api-key` method, `QIANFAN_API_KEY`, `STEPFUN_API_KEY`) in the plugin manifest so onboarding and `models setup` surface the expected env var without falling back to legacy `providerAuthEnvVars` runtime seed data. Thanks @shakkernerd.

‎extensions/feishu/src/monitor.account.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { getFeishuRuntime } from "./runtime.js";
2828
import { getMessageFeishu } from "./send.js";
2929
import { getFeishuSequentialKey } from "./sequential-key.js";
3030
import { createFeishuThreadBindingManager } from "./thread-bindings.js";
31-
import type { FeishuChatType, ResolvedFeishuAccount } from "./types.js";
31+
import type { FeishuChatType, FeishuConfig, ResolvedFeishuAccount } from "./types.js";
3232

3333
const FEISHU_REACTION_VERIFY_TIMEOUT_MS = 1_500;
3434

@@ -164,6 +164,7 @@ function normalizeFeishuChatType(value: unknown): FeishuChatType | undefined {
164164
type RegisterEventHandlersContext = {
165165
cfg: ClawdbotConfig;
166166
accountId: string;
167+
feishuCfg?: FeishuConfig;
167168
runtime?: RuntimeEnv;
168169
chatHistories: Map<string, HistoryEntry[]>;
169170
fireAndForget?: boolean;
@@ -242,7 +243,7 @@ function registerEventHandlers(
242243
eventDispatcher: Lark.EventDispatcher,
243244
context: RegisterEventHandlersContext,
244245
): void {
245-
const { cfg, accountId, runtime, chatHistories, fireAndForget } = context;
246+
const { cfg, accountId, feishuCfg, runtime, chatHistories, fireAndForget } = context;
246247
const log = runtime?.log ?? console.log;
247248
const error = runtime?.error ?? console.error;
248249
const runFeishuHandler = async (params: { task: () => Promise<void>; errorMessage: string }) => {
@@ -274,7 +275,7 @@ function registerEventHandlers(
274275
recordProcessedMessage: recordProcessedFeishuMessage,
275276
getBotOpenId: (id) => botOpenIds.get(id),
276277
getBotName: (id) => botNames.get(id),
277-
resolveSequentialKey: getFeishuSequentialKey,
278+
resolveSequentialKey: (params) => getFeishuSequentialKey({ ...params, feishuCfg }),
278279
}),
279280
"im.message.message_read_v1": async () => {
280281
// Ignore read receipts
@@ -453,6 +454,7 @@ export async function monitorSingleAccount(params: MonitorSingleAccountParams):
453454
registerEventHandlers(eventDispatcher, {
454455
cfg,
455456
accountId,
457+
feishuCfg: account.config,
456458
runtime,
457459
chatHistories,
458460
fireAndForget: params.fireAndForget ?? true,

‎extensions/feishu/src/sequential-key.test.ts‎

Lines changed: 147 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
import { describe, expect, it } from "vitest";
22
import type { FeishuMessageEvent } from "./bot.js";
33
import { getFeishuSequentialKey } from "./sequential-key.js";
4+
import type { FeishuConfig } from "./types.js";
45

56
function createTextEvent(params: {
67
text: string;
78
messageId?: string;
89
chatId?: string;
10+
chatType?: "p2p" | "group" | "topic_group";
11+
rootId?: string;
12+
threadId?: string;
13+
senderOpenId?: string;
14+
senderUserId?: string;
915
}): FeishuMessageEvent {
1016
return {
1117
sender: {
1218
sender_id: {
13-
open_id: "ou_sender_1",
14-
user_id: "ou_user_1",
19+
open_id: params.senderOpenId ?? "ou_sender_1",
20+
user_id: params.senderUserId ?? "ou_user_1",
1521
},
1622
sender_type: "user",
1723
},
1824
message: {
1925
message_id: params.messageId ?? "om_message_1",
2026
chat_id: params.chatId ?? "oc_dm_chat",
21-
chat_type: "p2p",
27+
chat_type: params.chatType ?? "p2p",
2228
message_type: "text",
2329
content: JSON.stringify({ text: params.text }),
30+
...(params.rootId ? { root_id: params.rootId } : {}),
31+
...(params.threadId ? { thread_id: params.threadId } : {}),
2432
},
2533
} as FeishuMessageEvent;
2634
}
2735

36+
function createFeishuConfig(params: Partial<FeishuConfig>): FeishuConfig {
37+
return params as unknown as FeishuConfig;
38+
}
2839
describe("getFeishuSequentialKey", () => {
2940
it.each([
3041
[createTextEvent({ text: "hello" }), "feishu:default:oc_dm_chat"],
@@ -40,6 +51,139 @@ describe("getFeishuSequentialKey", () => {
4051
).toBe(expected);
4152
});
4253

54+
it("keeps group messages on the per-chat lane by default", () => {
55+
const event = createTextEvent({
56+
text: "hello",
57+
chatId: "oc_group",
58+
chatType: "group",
59+
rootId: "om_root_1",
60+
});
61+
62+
expect(
63+
getFeishuSequentialKey({
64+
accountId: "default",
65+
event,
66+
feishuCfg: createFeishuConfig({}),
67+
}),
68+
).toBe("feishu:default:oc_group");
69+
});
70+
71+
it("scopes group_sender queues by sender", () => {
72+
const feishuCfg = createFeishuConfig({ groupSessionScope: "group_sender" });
73+
const first = createTextEvent({
74+
text: "one",
75+
chatId: "oc_group",
76+
chatType: "group",
77+
senderOpenId: "ou_sender_1",
78+
});
79+
const otherSender = createTextEvent({
80+
text: "two",
81+
chatId: "oc_group",
82+
chatType: "group",
83+
senderOpenId: "ou_sender_2",
84+
});
85+
86+
expect(getFeishuSequentialKey({ accountId: "default", event: first, feishuCfg })).toBe(
87+
"feishu:default:oc_group:sender:ou_sender_1",
88+
);
89+
expect(getFeishuSequentialKey({ accountId: "default", event: otherSender, feishuCfg })).toBe(
90+
"feishu:default:oc_group:sender:ou_sender_2",
91+
);
92+
});
93+
94+
it("honors legacy topicSessionMode when resolving group queue lanes", () => {
95+
const feishuCfg = createFeishuConfig({ topicSessionMode: "enabled" });
96+
const event = createTextEvent({
97+
text: "hello",
98+
chatId: "oc_group",
99+
chatType: "group",
100+
rootId: "om_root_1",
101+
});
102+
103+
expect(getFeishuSequentialKey({ accountId: "default", event, feishuCfg })).toBe(
104+
"feishu:default:oc_group:topic:om_root_1",
105+
);
106+
});
107+
108+
it("scopes group_topic queues by thread topic", () => {
109+
const feishuCfg = createFeishuConfig({ groupSessionScope: "group_topic" });
110+
const first = createTextEvent({
111+
text: "one",
112+
chatId: "oc_group",
113+
chatType: "group",
114+
rootId: "om_root_1",
115+
messageId: "om_message_1",
116+
});
117+
const sameTopic = createTextEvent({
118+
text: "two",
119+
chatId: "oc_group",
120+
chatType: "group",
121+
rootId: "om_root_1",
122+
messageId: "om_message_2",
123+
});
124+
const otherTopic = createTextEvent({
125+
text: "three",
126+
chatId: "oc_group",
127+
chatType: "group",
128+
rootId: "om_root_2",
129+
messageId: "om_message_3",
130+
});
131+
132+
expect(getFeishuSequentialKey({ accountId: "default", event: first, feishuCfg })).toBe(
133+
"feishu:default:oc_group:topic:om_root_1",
134+
);
135+
expect(getFeishuSequentialKey({ accountId: "default", event: sameTopic, feishuCfg })).toBe(
136+
"feishu:default:oc_group:topic:om_root_1",
137+
);
138+
expect(getFeishuSequentialKey({ accountId: "default", event: otherTopic, feishuCfg })).toBe(
139+
"feishu:default:oc_group:topic:om_root_2",
140+
);
141+
});
142+
143+
it("scopes group_topic_sender queues by thread topic and sender", () => {
144+
const feishuCfg = createFeishuConfig({ groupSessionScope: "group_topic_sender" });
145+
const first = createTextEvent({
146+
text: "one",
147+
chatId: "oc_group",
148+
chatType: "group",
149+
rootId: "om_root_1",
150+
senderOpenId: "ou_sender_1",
151+
});
152+
const otherSender = createTextEvent({
153+
text: "two",
154+
chatId: "oc_group",
155+
chatType: "group",
156+
rootId: "om_root_1",
157+
senderOpenId: "ou_sender_2",
158+
});
159+
160+
expect(getFeishuSequentialKey({ accountId: "default", event: first, feishuCfg })).toBe(
161+
"feishu:default:oc_group:topic:om_root_1:sender:ou_sender_1",
162+
);
163+
expect(getFeishuSequentialKey({ accountId: "default", event: otherSender, feishuCfg })).toBe(
164+
"feishu:default:oc_group:topic:om_root_1:sender:ou_sender_2",
165+
);
166+
});
167+
168+
it("honors per-group session scope overrides", () => {
169+
const feishuCfg = createFeishuConfig({
170+
groupSessionScope: "group",
171+
groups: {
172+
oc_group: { groupSessionScope: "group_topic" },
173+
},
174+
});
175+
const event = createTextEvent({
176+
text: "hello",
177+
chatId: "oc_group",
178+
chatType: "group",
179+
rootId: "om_root_1",
180+
});
181+
182+
expect(getFeishuSequentialKey({ accountId: "default", event, feishuCfg })).toBe(
183+
"feishu:default:oc_group:topic:om_root_1",
184+
);
185+
});
186+
43187
it("keeps /btw on a stable per-chat lane across different message ids", () => {
44188
const first = createTextEvent({ text: "/btw one", messageId: "om_message_1" });
45189
const second = createTextEvent({ text: "/btw two", messageId: "om_message_2" });

‎extensions/feishu/src/sequential-key.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ import {
22
isAbortRequestText,
33
isBtwRequestText,
44
} from "openclaw/plugin-sdk/command-primitives-runtime";
5+
import { resolveFeishuGroupSession } from "./bot-content.js";
56
import { parseFeishuMessageEvent, type FeishuMessageEvent } from "./bot.js";
7+
import { resolveFeishuGroupConfig } from "./policy.js";
8+
import { isFeishuGroupChatType, type FeishuConfig } from "./types.js";
69

710
export function getFeishuSequentialKey(params: {
811
accountId: string;
912
event: FeishuMessageEvent;
1013
botOpenId?: string;
1114
botName?: string;
15+
feishuCfg?: FeishuConfig;
1216
}): string {
13-
const { accountId, event, botOpenId, botName } = params;
17+
const { accountId, event, botOpenId, botName, feishuCfg } = params;
1418
const chatId = event.message.chat_id?.trim() || "unknown";
1519
const baseKey = `feishu:${accountId}:${chatId}`;
1620
const parsed = parseFeishuMessageEvent(event, botOpenId, botName);
@@ -24,5 +28,22 @@ export function getFeishuSequentialKey(params: {
2428
return `${baseKey}:btw`;
2529
}
2630

31+
if (isFeishuGroupChatType(event.message.chat_type)) {
32+
const senderOpenId =
33+
event.sender.sender_id.open_id?.trim() || event.sender.sender_id.user_id?.trim() || "unknown";
34+
const groupSession = resolveFeishuGroupSession({
35+
chatId,
36+
senderOpenId,
37+
messageId: event.message.message_id,
38+
rootId: event.message.root_id,
39+
threadId: event.message.thread_id,
40+
chatType: event.message.chat_type,
41+
groupConfig: resolveFeishuGroupConfig({ cfg: feishuCfg, groupId: chatId }),
42+
feishuCfg,
43+
});
44+
45+
return `feishu:${accountId}:${groupSession.peerId}`;
46+
}
47+
2748
return baseKey;
2849
}

0 commit comments

Comments
 (0)