




















@@ -6,6 +6,7 @@ import {
66readStringParam,
77} from "../../agents/tools/common.js";
88import { parseReplyDirectives } from "../../auto-reply/reply/reply-directives.js";
9+import { normalizeChatType, type ChatType } from "../../channels/chat-type.js";
910import { getChannelPlugin } from "../../channels/plugins/index.js";
1011import { dispatchChannelMessageAction } from "../../channels/plugins/message-action-dispatch.js";
1112import type {
@@ -25,8 +26,7 @@ import { getAgentScopedMediaLocalRoots } from "../../media/local-roots.js";
2526import { resolveAgentScopedOutboundMediaAccess } from "../../media/read-capability.js";
2627import { hasPollCreationParams } from "../../poll-params.js";
2728import { resolvePollMaxSelections } from "../../polls.js";
28-import { buildChannelAccountBindings } from "../../routing/bindings.js";
29-import { normalizeAgentId } from "../../routing/session-key.js";
29+import { resolveFirstBoundAccountId } from "../../routing/bound-account-read.js";
3030import {
3131normalizeOptionalLowercaseString,
3232normalizeOptionalString,
@@ -72,6 +72,7 @@ import {
7272} from "./outbound-policy.js";
7373import { executePollAction, executeSendAction } from "./outbound-send-service.js";
7474import { ensureOutboundSessionEntry, resolveOutboundSessionRoute } from "./outbound-session.js";
75+import { normalizeTargetForProvider } from "./target-normalization.js";
7576import { resolveChannelTarget, type ResolvedMessagingTarget } from "./target-resolver.js";
7677import { extractToolPayload } from "./tool-payload.js";
7778@@ -286,6 +287,80 @@ async function resolveChannel(
286287return selection.channel;
287288}
288289290+function addCandidateAndUnprefixedAlias(candidates: Set<string>, value?: string | null) {
291+const normalized = normalizeOptionalString(value);
292+if (!normalized) {
293+return;
294+}
295+candidates.add(normalized);
296+const unprefixed = normalized.replace(/^(channel|group|user):/i, "").trim();
297+if (unprefixed && unprefixed !== normalized) {
298+candidates.add(unprefixed);
299+}
300+}
301+302+function normalizeTargetForAccountBinding(channel: ChannelId, target: string): string | undefined {
303+try {
304+return normalizeTargetForProvider(channel, target);
305+} catch {
306+return undefined;
307+}
308+}
309+310+function inferPeerKindForAccountBinding(channel: ChannelId, target: string): ChatType | undefined {
311+const inferred = normalizeChatType(
312+getChannelPlugin(channel)?.messaging?.inferTargetChatType?.({ to: target }),
313+);
314+if (inferred) {
315+return inferred;
316+}
317+const normalized = normalizeTargetForAccountBinding(channel, target);
318+const candidates = [target, normalized].filter((value): value is string => Boolean(value));
319+if (candidates.some((value) => /^user:/i.test(value))) {
320+return "direct";
321+}
322+if (candidates.some((value) => /^(channel|group):/i.test(value))) {
323+return "channel";
324+}
325+return undefined;
326+}
327+328+function resolveTargetBoundAccountId(params: {
329+cfg: OpenClawConfig;
330+channel: ChannelId;
331+args: Record<string, unknown>;
332+agentId?: string;
333+}): string | undefined {
334+if (!params.agentId) {
335+return undefined;
336+}
337+const target =
338+normalizeOptionalString(params.args.to) ?? normalizeOptionalString(params.args.channelId) ?? "";
339+if (!target) {
340+return resolveFirstBoundAccountId({
341+cfg: params.cfg,
342+channelId: params.channel,
343+agentId: params.agentId,
344+});
345+}
346+347+const candidates = new Set<string>();
348+addCandidateAndUnprefixedAlias(candidates, target);
349+addCandidateAndUnprefixedAlias(
350+candidates,
351+normalizeTargetForAccountBinding(params.channel, target),
352+);
353+const [peerId, ...exactPeerIdAliases] = Array.from(candidates);
354+return resolveFirstBoundAccountId({
355+cfg: params.cfg,
356+channelId: params.channel,
357+agentId: params.agentId,
358+ peerId,
359+ exactPeerIdAliases,
360+peerKind: inferPeerKindForAccountBinding(params.channel, target),
361+});
362+}
363+289364async function resolveActionTarget(params: {
290365cfg: OpenClawConfig;
291366channel: ChannelId;
@@ -941,11 +1016,12 @@ export async function runMessageAction(
9411016const channel = await resolveChannel(cfg, params, input.toolContext);
9421017let accountId = readStringParam(params, "accountId") ?? input.defaultAccountId;
9431018if (!accountId && resolvedAgentId) {
944-const byAgent = buildChannelAccountBindings(cfg).get(channel);
945-const boundAccountIds = byAgent?.get(normalizeAgentId(resolvedAgentId));
946-if (boundAccountIds && boundAccountIds.length > 0) {
947-accountId = boundAccountIds[0];
948-}
1019+accountId = resolveTargetBoundAccountId({
1020+ cfg,
1021+ channel,
1022+args: params,
1023+agentId: resolvedAgentId,
1024+});
9491025}
9501026if (accountId) {
9511027params.accountId = accountId;
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。