Skip to content

Commit daa1c6f

Browse files
committed
fix(agents-bind): generate peer format for feishu group bindings
The `agents add --bind` command was generating accountId format for all channel bindings, but feishu group chats require peer-based routing. For example: - `openclaw agents add --bind 'feishu:oc_test'` generated: ```json { "match": { "channel": "feishu", "accountId": "oc_test" } } ``` - But the router expects: ```json { "match": { "channel": "feishu", "peer": { "kind": "group", "id": "oc_test" } } } ``` This fix adds support for two binding syntaxes: 1. `<channel>:<peerId>` - For feishu, generates peer format (default) 2. `<channel>:account:<accountId>` - Explicit accountId syntax Examples: - `feishu:oc_test` → `{ peer: { kind: "group", id: "oc_test" } }` - `feishu:account:work` → `{ accountId: "work" }` - `telegram:default` → `{ accountId: "default" }` (existing behavior) Fixes issue #95734 ## Test Coverage Added tests in `src/commands/agents.bindings.test.ts`: - parseBindingSpecs: feishu group binding as peer format - parseBindingSpecs: feishu explicit accountId syntax - parseBindingSpecs: rejects invalid binding with too many colons - parseBindingSpecs: telegram binding as accountId (existing behavior) - buildChannelBindings: feishu group binding as peer format - buildChannelBindings: feishu explicit accountId binding - buildChannelBindings: telegram binding as accountId (existing behavior)
1 parent 42e22d4 commit daa1c6f

2 files changed

Lines changed: 202 additions & 27 deletions

File tree

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// Agents bindings tests cover parsing and building channel route bindings.
2+
import { describe, expect, it } from "vitest";
3+
import { buildChannelBindings, parseBindingSpecs } from "./agents.bindings.js";
4+
5+
describe("parseBindingSpecs", () => {
6+
it("parses feishu group binding as peer format", () => {
7+
const parsed = parseBindingSpecs({
8+
agentId: "feishu-agent",
9+
specs: ["feishu:oc_test"],
10+
config: {},
11+
});
12+
13+
expect(parsed.errors).toEqual([]);
14+
expect(parsed.bindings).toEqual([
15+
{
16+
type: "route",
17+
agentId: "feishu-agent",
18+
match: {
19+
channel: "feishu",
20+
peer: { kind: "group", id: "oc_test" },
21+
},
22+
},
23+
]);
24+
});
25+
26+
it("parses feishu explicit accountId syntax", () => {
27+
const parsed = parseBindingSpecs({
28+
agentId: "feishu-agent",
29+
specs: ["feishu:account:work"],
30+
config: {},
31+
});
32+
33+
expect(parsed.errors).toEqual([]);
34+
expect(parsed.bindings).toEqual([
35+
{
36+
type: "route",
37+
agentId: "feishu-agent",
38+
match: {
39+
channel: "feishu",
40+
accountId: "work",
41+
},
42+
},
43+
]);
44+
});
45+
46+
it("rejects invalid binding with too many colons", () => {
47+
const parsed = parseBindingSpecs({
48+
agentId: "agent",
49+
specs: ["feishu:oc_test:extra:segments"],
50+
config: {},
51+
});
52+
53+
expect(parsed.bindings).toEqual([]);
54+
expect(parsed.errors).toEqual([
55+
'Invalid binding "feishu:oc_test:extra:segments". Too many colon-separated segments. Use <channel>:<id> or <channel>:account:<id>.',
56+
]);
57+
});
58+
59+
it("parses telegram binding as accountId (existing behavior)", () => {
60+
const parsed = parseBindingSpecs({
61+
agentId: "telegram-agent",
62+
specs: ["telegram:default"],
63+
config: {},
64+
});
65+
66+
expect(parsed.errors).toEqual([]);
67+
// Telegram uses accountId by default
68+
expect(parsed.bindings[0]?.match.accountId).toBe("default");
69+
expect(parsed.bindings[0]?.match.peer).toBeUndefined();
70+
});
71+
});
72+
73+
describe("buildChannelBindings", () => {
74+
it("builds feishu group binding as peer format", () => {
75+
const bindings = buildChannelBindings({
76+
agentId: "feishu-agent",
77+
selection: ["feishu"],
78+
config: {},
79+
accountIds: { feishu: "oc_test" },
80+
});
81+
82+
expect(bindings).toEqual([
83+
{
84+
type: "route",
85+
agentId: "feishu-agent",
86+
match: {
87+
channel: "feishu",
88+
peer: { kind: "group", id: "oc_test" },
89+
},
90+
},
91+
]);
92+
});
93+
94+
it("builds feishu explicit accountId binding", () => {
95+
const bindings = buildChannelBindings({
96+
agentId: "feishu-agent",
97+
selection: ["feishu"],
98+
config: {},
99+
accountIds: { feishu: "account:work" },
100+
});
101+
102+
expect(bindings).toEqual([
103+
{
104+
type: "route",
105+
agentId: "feishu-agent",
106+
match: {
107+
channel: "feishu",
108+
accountId: "work",
109+
},
110+
},
111+
]);
112+
});
113+
114+
it("builds telegram binding as accountId (existing behavior)", () => {
115+
const bindings = buildChannelBindings({
116+
agentId: "telegram-agent",
117+
selection: ["telegram"],
118+
config: {},
119+
accountIds: { telegram: "default" },
120+
});
121+
122+
expect(bindings).toEqual([
123+
{
124+
type: "route",
125+
agentId: "telegram-agent",
126+
match: {
127+
channel: "telegram",
128+
accountId: "default",
129+
},
130+
},
131+
]);
132+
});
133+
});

‎src/commands/agents.bindings.ts‎

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,32 @@ export function buildChannelBindings(params: {
288288
const agentId = normalizeAgentId(params.agentId);
289289
for (const channel of params.selection) {
290290
const match: AgentRouteBinding["match"] = { channel };
291-
const accountId = resolveBindingAccountId({
292-
channel,
293-
config: params.config,
294-
agentId,
295-
explicitAccountId: params.accountIds?.[channel],
296-
});
297-
if (accountId) {
298-
match.accountId = accountId;
291+
const explicitAccountId = params.accountIds?.[channel];
292+
293+
// For feishu channel, treat explicit account IDs as peer IDs (group chats)
294+
// unless they are prefixed with "account:"
295+
if (channel === "feishu" && explicitAccountId) {
296+
if (explicitAccountId.startsWith("account:")) {
297+
// Explicit accountId syntax: feishu:account:xxx
298+
const accountId = explicitAccountId.slice("account:".length);
299+
if (accountId.trim()) {
300+
match.accountId = accountId.trim();
301+
}
302+
} else {
303+
// Default: treat as peer ID for group chats
304+
match.peer = { kind: "group" as const, id: explicitAccountId };
305+
}
306+
} else {
307+
// For other channels, use the existing logic
308+
const accountId = resolveBindingAccountId({
309+
channel,
310+
config: params.config,
311+
agentId,
312+
explicitAccountId,
313+
});
314+
if (accountId) {
315+
match.accountId = accountId;
316+
}
299317
}
300318
bindings.push({ type: "route", agentId, match });
301319
}
@@ -316,37 +334,61 @@ export function parseBindingSpecs(params: {
316334
if (!trimmed) {
317335
continue;
318336
}
319-
// Bind specs are exactly <channel> or <channel>:<account>; extra colon
320-
// segments would silently change the requested account if truncated.
321-
const [channelRaw, accountRaw, ...extraSegments] = trimmed.split(":");
337+
// Bind specs support two formats:
338+
// - <channel>:<peerId> for peer-based routing (e.g., feishu:oc_test for group chats)
339+
// - <channel>:account:<accountId> for explicit accountId routing (e.g., feishu:account:work)
340+
// Extra colon segments beyond these patterns are rejected.
341+
const parts = trimmed.split(":");
342+
const channelRaw = parts[0];
343+
const accountOrPeerRaw = parts[1];
344+
const accountPrefixRaw = parts[2];
345+
const extraSegments = parts.slice(3);
346+
322347
if (extraSegments.length > 0) {
323348
errors.push(
324-
`Invalid binding "${trimmed}". Account id cannot contain ":". Use <channel>:<account>, for example telegram:default.`,
349+
`Invalid binding "${trimmed}". Too many colon-separated segments. Use <channel>:<id> or <channel>:account:<id>.`,
325350
);
326351
continue;
327352
}
353+
328354
const channel = normalizeBindingChannelId(channelRaw, params.config);
329355
if (!channel) {
330356
errors.push(formatUnknownChannelMessage({ channel: channelRaw }));
331357
continue;
332358
}
333-
let accountId: string | undefined = accountRaw?.trim();
334-
if (accountRaw !== undefined && !accountId) {
335-
errors.push(
336-
`Invalid binding "${trimmed}". Account id is empty. Use <channel>:<account>, for example telegram:default.`,
337-
);
338-
continue;
339-
}
340-
accountId = resolveBindingAccountId({
341-
channel,
342-
config: params.config,
343-
agentId,
344-
explicitAccountId: accountId,
345-
});
359+
346360
const match: AgentRouteBinding["match"] = { channel };
347-
if (accountId) {
348-
match.accountId = accountId;
361+
362+
if (accountOrPeerRaw !== undefined) {
363+
// Check for explicit accountId syntax: <channel>:account:<id>
364+
if (accountOrPeerRaw.toLowerCase() === "account" && accountPrefixRaw !== undefined) {
365+
const accountId = accountPrefixRaw.trim();
366+
if (!accountId) {
367+
errors.push(
368+
`Invalid binding "${trimmed}". Account id is empty. Use <channel>:account:<id>.`,
369+
);
370+
continue;
371+
}
372+
match.accountId = accountId;
373+
} else if (accountOrPeerRaw.trim()) {
374+
// For feishu channel, treat as peer ID (group chat) by default
375+
if (channel === "feishu") {
376+
match.peer = { kind: "group" as const, id: accountOrPeerRaw.trim() };
377+
} else {
378+
// For other channels, use the existing accountId logic
379+
const accountId = resolveBindingAccountId({
380+
channel,
381+
config: params.config,
382+
agentId,
383+
explicitAccountId: accountOrPeerRaw.trim(),
384+
});
385+
if (accountId) {
386+
match.accountId = accountId;
387+
}
388+
}
389+
}
349390
}
391+
350392
bindings.push({ type: "route", agentId, match });
351393
}
352394
return { bindings, errors };

0 commit comments

Comments
 (0)