Files
openclaw/src/routing/bindings.ts
Bob 6a705a37f2 ACP: add persistent Discord channel and Telegram topic bindings (#34873)
* docs: add ACP persistent binding experiment plan

* docs: align ACP persistent binding spec to channel-local config

* docs: scope Telegram ACP bindings to forum topics only

* docs: lock bound /new and /reset behavior to in-place ACP reset

* ACP: add persistent discord/telegram conversation bindings

* ACP: fix persistent binding reuse and discord thread parent context

* docs: document channel-specific persistent ACP bindings

* ACP: split persistent bindings and share conversation id helpers

* ACP: defer configured binding init until preflight passes

* ACP: fix discord thread parent fallback and explicit disable inheritance

* ACP: keep bound /new and /reset in-place

* ACP: honor configured bindings in native command flows

* ACP: avoid configured fallback after runtime bind failure

* docs: refine ACP bindings experiment config examples

* acp: cut over to typed top-level persistent bindings

* ACP bindings: harden reset recovery and native command auth

* Docs: add ACP bound command auth proposal

* Tests: normalize i18n registry zh-CN assertion encoding

* ACP bindings: address review findings for reset and fallback routing

* ACP reset: gate hooks on success and preserve /new arguments

* ACP bindings: fix auth and binding-priority review findings

* Telegram ACP: gate ensure on auth and accepted messages

* ACP bindings: fix session-key precedence and unavailable handling

* ACP reset/native commands: honor fallback targets and abort on bootstrap failure

* Config schema: validate ACP binding channel and Telegram topic IDs

* Discord ACP: apply configured DM bindings to native commands

* ACP reset tails: dispatch through ACP after command handling

* ACP tails/native reset auth: fix target dispatch and restore full auth

* ACP reset detection: fallback to active ACP keys for DM contexts

* Tests: type runTurn mock input in ACP dispatch test

* ACP: dedup binding route bootstrap and reset target resolution

* reply: align ACP reset hooks with bound session key

* docs: replace personal discord ids with placeholders

* fix: add changelog entry for ACP persistent bindings (#34873) (thanks @dutifulbob)

---------

Co-authored-by: Onur <2453968+osolmaz@users.noreply.github.com>
2026-03-05 09:38:12 +01:00

115 lines
3.4 KiB
TypeScript

import { resolveDefaultAgentId } from "../agents/agent-scope.js";
import { normalizeChatChannelId } from "../channels/registry.js";
import { listRouteBindings } from "../config/bindings.js";
import type { OpenClawConfig } from "../config/config.js";
import type { AgentRouteBinding } from "../config/types.agents.js";
import { normalizeAccountId, normalizeAgentId } from "./session-key.js";
function normalizeBindingChannelId(raw?: string | null): string | null {
const normalized = normalizeChatChannelId(raw);
if (normalized) {
return normalized;
}
const fallback = (raw ?? "").trim().toLowerCase();
return fallback || null;
}
export function listBindings(cfg: OpenClawConfig): AgentRouteBinding[] {
return listRouteBindings(cfg);
}
function resolveNormalizedBindingMatch(binding: AgentRouteBinding): {
agentId: string;
accountId: string;
channelId: string;
} | null {
if (!binding || typeof binding !== "object") {
return null;
}
const match = binding.match;
if (!match || typeof match !== "object") {
return null;
}
const channelId = normalizeBindingChannelId(match.channel);
if (!channelId) {
return null;
}
const accountId = typeof match.accountId === "string" ? match.accountId.trim() : "";
if (!accountId || accountId === "*") {
return null;
}
return {
agentId: normalizeAgentId(binding.agentId),
accountId: normalizeAccountId(accountId),
channelId,
};
}
export function listBoundAccountIds(cfg: OpenClawConfig, channelId: string): string[] {
const normalizedChannel = normalizeBindingChannelId(channelId);
if (!normalizedChannel) {
return [];
}
const ids = new Set<string>();
for (const binding of listBindings(cfg)) {
const resolved = resolveNormalizedBindingMatch(binding);
if (!resolved || resolved.channelId !== normalizedChannel) {
continue;
}
ids.add(resolved.accountId);
}
return Array.from(ids).toSorted((a, b) => a.localeCompare(b));
}
export function resolveDefaultAgentBoundAccountId(
cfg: OpenClawConfig,
channelId: string,
): string | null {
const normalizedChannel = normalizeBindingChannelId(channelId);
if (!normalizedChannel) {
return null;
}
const defaultAgentId = normalizeAgentId(resolveDefaultAgentId(cfg));
for (const binding of listBindings(cfg)) {
const resolved = resolveNormalizedBindingMatch(binding);
if (
!resolved ||
resolved.channelId !== normalizedChannel ||
resolved.agentId !== defaultAgentId
) {
continue;
}
return resolved.accountId;
}
return null;
}
export function buildChannelAccountBindings(cfg: OpenClawConfig) {
const map = new Map<string, Map<string, string[]>>();
for (const binding of listBindings(cfg)) {
const resolved = resolveNormalizedBindingMatch(binding);
if (!resolved) {
continue;
}
const byAgent = map.get(resolved.channelId) ?? new Map<string, string[]>();
const list = byAgent.get(resolved.agentId) ?? [];
if (!list.includes(resolved.accountId)) {
list.push(resolved.accountId);
}
byAgent.set(resolved.agentId, list);
map.set(resolved.channelId, byAgent);
}
return map;
}
export function resolvePreferredAccountId(params: {
accountIds: string[];
defaultAccountId: string;
boundAccounts: string[];
}): string {
if (params.boundAccounts.length > 0) {
return params.boundAccounts[0];
}
return params.defaultAccountId;
}