Skip to content

Commit 1f649f4

Browse files
Jasmine ZhangJasmine Zhang
authored andcommitted
fix: treat feishu bind conversation ids as peers
Fixes #95734
1 parent f80f4a8 commit 1f649f4

2 files changed

Lines changed: 100 additions & 2 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { afterEach, describe, expect, it } from "vitest";
2+
import { setActivePluginRegistry } from "../plugins/runtime.js";
3+
import {
4+
createBindingResolverTestPlugin,
5+
createTestRegistry,
6+
} from "../test-utils/channel-plugins.js";
7+
import { parseBindingSpecs } from "./agents.bindings.js";
8+
9+
const feishuBindingPlugin = createBindingResolverTestPlugin({
10+
id: "feishu",
11+
resolveBindingAccountId: () => undefined,
12+
});
13+
14+
describe("agents bind feishu integration", () => {
15+
it("parses feishu group chat bindings into peer matches", () => {
16+
setActivePluginRegistry(
17+
createTestRegistry([{ pluginId: "feishu", plugin: feishuBindingPlugin, source: "test" }]),
18+
);
19+
20+
const parsed = parseBindingSpecs({ agentId: "main", specs: ["feishu:oc_test"], config: {} });
21+
22+
expect(parsed.errors).toStrictEqual([]);
23+
expect(parsed.bindings).toEqual([
24+
{
25+
type: "route",
26+
agentId: "main",
27+
match: { channel: "feishu", peer: { kind: "group", id: "oc_test" } },
28+
},
29+
]);
30+
});
31+
32+
it("preserves full feishu topic conversation ids after the channel separator", () => {
33+
setActivePluginRegistry(
34+
createTestRegistry([{ pluginId: "feishu", plugin: feishuBindingPlugin, source: "test" }]),
35+
);
36+
37+
const parsed = parseBindingSpecs({
38+
agentId: "main",
39+
specs: ["feishu:oc_group_chat:topic:om_topic_root"],
40+
config: {},
41+
});
42+
43+
expect(parsed.errors).toStrictEqual([]);
44+
expect(parsed.bindings).toEqual([
45+
{
46+
type: "route",
47+
agentId: "main",
48+
match: {
49+
channel: "feishu",
50+
peer: { kind: "group", id: "oc_group_chat:topic:om_topic_root" },
51+
},
52+
},
53+
]);
54+
});
55+
56+
afterEach(() => {
57+
setActivePluginRegistry(createTestRegistry());
58+
});
59+
});

‎src/commands/agents.bindings.ts‎

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,36 @@ function getBindingChannelPlugin(channel: ChannelId) {
247247
return getLoadedChannelPlugin(channel) ?? getBundledChannelSetupPlugin(channel);
248248
}
249249

250+
function resolveFeishuConversationBindingPeer(
251+
raw: string,
252+
): { kind: "direct" | "group"; id: string } | undefined {
253+
const trimmed = raw.trim();
254+
if (!trimmed) {
255+
return undefined;
256+
}
257+
const lowered = trimmed.toLowerCase();
258+
if (
259+
lowered.startsWith("user:") ||
260+
lowered.startsWith("dm:") ||
261+
lowered.startsWith("open_id:") ||
262+
lowered.startsWith("ou_") ||
263+
lowered.startsWith("on_")
264+
) {
265+
const id = trimmed.replace(/^(user|dm|open_id):/i, "").trim();
266+
return id ? { kind: "direct", id } : undefined;
267+
}
268+
if (
269+
lowered.startsWith("chat:") ||
270+
lowered.startsWith("group:") ||
271+
lowered.startsWith("channel:") ||
272+
lowered.startsWith("oc_")
273+
) {
274+
const id = trimmed.replace(/^(chat|group|channel):/i, "").trim();
275+
return id ? { kind: "group", id } : undefined;
276+
}
277+
return undefined;
278+
}
279+
250280
function resolveBindingAccountId(params: {
251281
channel: ChannelId;
252282
config: OpenClawConfig;
@@ -316,7 +346,9 @@ export function parseBindingSpecs(params: {
316346
if (!trimmed) {
317347
continue;
318348
}
319-
const [channelRaw, accountRaw] = trimmed.split(":", 2);
349+
const separatorIndex = trimmed.indexOf(":");
350+
const channelRaw = separatorIndex >= 0 ? trimmed.slice(0, separatorIndex) : trimmed;
351+
const accountRaw = separatorIndex >= 0 ? trimmed.slice(separatorIndex + 1) : undefined;
320352
const channel = normalizeBindingChannelId(channelRaw, params.config);
321353
if (!channel) {
322354
errors.push(formatUnknownChannelMessage({ channel: channelRaw }));
@@ -329,16 +361,23 @@ export function parseBindingSpecs(params: {
329361
);
330362
continue;
331363
}
364+
const feishuPeer =
365+
channel === "feishu" && accountId
366+
? resolveFeishuConversationBindingPeer(accountId)
367+
: undefined;
332368
accountId = resolveBindingAccountId({
333369
channel,
334370
config: params.config,
335371
agentId,
336-
explicitAccountId: accountId,
372+
explicitAccountId: feishuPeer ? undefined : accountId,
337373
});
338374
const match: AgentRouteBinding["match"] = { channel };
339375
if (accountId) {
340376
match.accountId = accountId;
341377
}
378+
if (feishuPeer) {
379+
match.peer = feishuPeer;
380+
}
342381
bindings.push({ type: "route", agentId, match });
343382
}
344383
return { bindings, errors };

0 commit comments

Comments
 (0)