refactor: share acp binding resolution helper

This commit is contained in:
Peter Steinberger 2026-03-13 20:55:26 +00:00
parent 94531fa237
commit 9b6790e3a6
1 changed files with 100 additions and 103 deletions

View File

@ -117,6 +117,70 @@ function toConfiguredBindingSpec(params: {
}; };
} }
function resolveConfiguredBindingRecord(params: {
cfg: OpenClawConfig;
bindings: AgentAcpBinding[];
channel: ConfiguredAcpBindingChannel;
accountId: string;
selectConversation: (
binding: AgentAcpBinding,
) => { conversationId: string; parentConversationId?: string } | null;
}): ResolvedConfiguredAcpBinding | null {
let wildcardMatch: {
binding: AgentAcpBinding;
conversationId: string;
parentConversationId?: string;
} | null = null;
for (const binding of params.bindings) {
if (normalizeBindingChannel(binding.match.channel) !== params.channel) {
continue;
}
const accountMatchPriority = resolveAccountMatchPriority(
binding.match.accountId,
params.accountId,
);
if (accountMatchPriority === 0) {
continue;
}
const conversation = params.selectConversation(binding);
if (!conversation) {
continue;
}
const spec = toConfiguredBindingSpec({
cfg: params.cfg,
channel: params.channel,
accountId: params.accountId,
conversationId: conversation.conversationId,
parentConversationId: conversation.parentConversationId,
binding,
});
if (accountMatchPriority === 2) {
return {
spec,
record: toConfiguredAcpBindingRecord(spec),
};
}
if (!wildcardMatch) {
wildcardMatch = { binding, ...conversation };
}
}
if (!wildcardMatch) {
return null;
}
const spec = toConfiguredBindingSpec({
cfg: params.cfg,
channel: params.channel,
accountId: params.accountId,
conversationId: wildcardMatch.conversationId,
parentConversationId: wildcardMatch.parentConversationId,
binding: wildcardMatch.binding,
});
return {
spec,
record: toConfiguredAcpBindingRecord(spec),
};
}
export function resolveConfiguredAcpBindingSpecBySessionKey(params: { export function resolveConfiguredAcpBindingSpecBySessionKey(params: {
cfg: OpenClawConfig; cfg: OpenClawConfig;
sessionKey: string; sessionKey: string;
@ -207,57 +271,20 @@ export function resolveConfiguredAcpBindingRecord(params: {
if (channel === "discord") { if (channel === "discord") {
const bindings = listAcpBindings(params.cfg); const bindings = listAcpBindings(params.cfg);
const resolveDiscordBindingForConversation = ( const resolveDiscordBindingForConversation = (targetConversationId: string) =>
targetConversationId: string, resolveConfiguredBindingRecord({
): ResolvedConfiguredAcpBinding | null => { cfg: params.cfg,
let wildcardMatch: AgentAcpBinding | null = null; bindings,
for (const binding of bindings) { channel: "discord",
if (normalizeBindingChannel(binding.match.channel) !== "discord") { accountId,
continue; selectConversation: (binding) => {
} const bindingConversationId = resolveBindingConversationId(binding);
const accountMatchPriority = resolveAccountMatchPriority( if (!bindingConversationId || bindingConversationId !== targetConversationId) {
binding.match.accountId, return null;
accountId, }
); return { conversationId: targetConversationId };
if (accountMatchPriority === 0) { },
continue; });
}
const bindingConversationId = resolveBindingConversationId(binding);
if (!bindingConversationId || bindingConversationId !== targetConversationId) {
continue;
}
if (accountMatchPriority === 2) {
const spec = toConfiguredBindingSpec({
cfg: params.cfg,
channel: "discord",
accountId,
conversationId: targetConversationId,
binding,
});
return {
spec,
record: toConfiguredAcpBindingRecord(spec),
};
}
if (!wildcardMatch) {
wildcardMatch = binding;
}
}
if (wildcardMatch) {
const spec = toConfiguredBindingSpec({
cfg: params.cfg,
channel: "discord",
accountId,
conversationId: targetConversationId,
binding: wildcardMatch,
});
return {
spec,
record: toConfiguredAcpBindingRecord(spec),
};
}
return null;
};
const directMatch = resolveDiscordBindingForConversation(conversationId); const directMatch = resolveDiscordBindingForConversation(conversationId);
if (directMatch) { if (directMatch) {
@ -280,61 +307,31 @@ export function resolveConfiguredAcpBindingRecord(params: {
if (!parsed || !parsed.chatId.startsWith("-")) { if (!parsed || !parsed.chatId.startsWith("-")) {
return null; return null;
} }
let wildcardMatch: AgentAcpBinding | null = null; return resolveConfiguredBindingRecord({
for (const binding of listAcpBindings(params.cfg)) { cfg: params.cfg,
if (normalizeBindingChannel(binding.match.channel) !== "telegram") { bindings: listAcpBindings(params.cfg),
continue; channel: "telegram",
} accountId,
const accountMatchPriority = resolveAccountMatchPriority(binding.match.accountId, accountId); selectConversation: (binding) => {
if (accountMatchPriority === 0) { const targetConversationId = resolveBindingConversationId(binding);
continue; if (!targetConversationId) {
} return null;
const targetConversationId = resolveBindingConversationId(binding); }
if (!targetConversationId) { const targetParsed = parseTelegramTopicConversation({
continue; conversationId: targetConversationId,
} });
const targetParsed = parseTelegramTopicConversation({ if (!targetParsed || !targetParsed.chatId.startsWith("-")) {
conversationId: targetConversationId, return null;
}); }
if (!targetParsed || !targetParsed.chatId.startsWith("-")) { if (targetParsed.canonicalConversationId !== parsed.canonicalConversationId) {
continue; return null;
} }
if (targetParsed.canonicalConversationId !== parsed.canonicalConversationId) { return {
continue;
}
if (accountMatchPriority === 2) {
const spec = toConfiguredBindingSpec({
cfg: params.cfg,
channel: "telegram",
accountId,
conversationId: parsed.canonicalConversationId, conversationId: parsed.canonicalConversationId,
parentConversationId: parsed.chatId, parentConversationId: parsed.chatId,
binding,
});
return {
spec,
record: toConfiguredAcpBindingRecord(spec),
}; };
} },
if (!wildcardMatch) { });
wildcardMatch = binding;
}
}
if (wildcardMatch) {
const spec = toConfiguredBindingSpec({
cfg: params.cfg,
channel: "telegram",
accountId,
conversationId: parsed.canonicalConversationId,
parentConversationId: parsed.chatId,
binding: wildcardMatch,
});
return {
spec,
record: toConfiguredAcpBindingRecord(spec),
};
}
return null;
} }
return null; return null;