1- import { getLoadedChannelPluginForRead } from "../../channels/plugins/registry-loaded-read.js" ;
21import type { ChannelId } from "../../channels/plugins/types.public.js" ;
32import { resolveAgentMainSessionKey } from "../../config/sessions/main-session.js" ;
43import { resolveStorePath } from "../../config/sessions/paths.js" ;
@@ -9,9 +8,6 @@ import { maybeResolveIdLikeTarget } from "../../infra/outbound/target-id-resolut
98import { tryResolveLoadedOutboundTarget } from "../../infra/outbound/targets-loaded.js" ;
109import { resolveSessionDeliveryTarget } from "../../infra/outbound/targets-session.js" ;
1110import type { OutboundChannel } from "../../infra/outbound/targets.js" ;
12- import { readChannelAllowFromStoreEntriesSync } from "../../pairing/allow-from-store-read.js" ;
13- import { mapAllowFromEntries } from "../../plugin-sdk/channel-config-helpers.js" ;
14- import { resolveFirstBoundAccountId } from "../../routing/bound-account-read.js" ;
1511import { normalizeAccountId } from "../../routing/session-key.js" ;
1612
1713export type DeliveryTargetResolution =
@@ -56,11 +52,19 @@ async function resolveOutboundTargetWithRuntime(
5652let channelSelectionRuntimePromise :
5753 | Promise < typeof import ( "../../infra/outbound/channel-selection.runtime.js" ) >
5854 | undefined ;
55+ let deliveryTargetRuntimePromise :
56+ | Promise < typeof import ( "./delivery-target.runtime.js" ) >
57+ | undefined ;
5958
6059async function loadChannelSelectionRuntime ( ) {
6160 channelSelectionRuntimePromise ??= import ( "../../infra/outbound/channel-selection.runtime.js" ) ;
6261 return await channelSelectionRuntimePromise ;
6362}
63+
64+ async function loadDeliveryTargetRuntime ( ) {
65+ deliveryTargetRuntimePromise ??= import ( "./delivery-target.runtime.js" ) ;
66+ return await deliveryTargetRuntimePromise ;
67+ }
6468export async function resolveDeliveryTarget (
6569 cfg : OpenClawConfig ,
6670 agentId : string ,
@@ -140,6 +144,7 @@ export async function resolveDeliveryTarget(
140144 : undefined ;
141145 let accountId = explicitAccountId ?? resolved . accountId ;
142146 if ( ! accountId && channel ) {
147+ const { resolveFirstBoundAccountId } = await loadDeliveryTargetRuntime ( ) ;
143148 accountId = resolveFirstBoundAccountId ( { cfg, channelId : channel , agentId } ) ;
144149 }
145150
@@ -172,34 +177,42 @@ export async function resolveDeliveryTarget(
172177 } ;
173178 }
174179
175- const channelPlugin = getLoadedChannelPluginForRead ( channel ) ;
176- const resolvedAccountId = normalizeAccountId ( accountId ) ;
177- const configuredAllowFromRaw = channelPlugin ?. config . resolveAllowFrom ?.( {
178- cfg,
179- accountId : resolvedAccountId ,
180- } ) ;
181- const configuredAllowFrom = configuredAllowFromRaw
182- ? mapAllowFromEntries ( configuredAllowFromRaw )
183- : [ ] ;
184- const storeAllowFrom = readChannelAllowFromStoreEntriesSync (
185- channel ,
186- process . env ,
187- resolvedAccountId ,
188- ) ;
189- const allowFromOverride = [ ...new Set ( [ ...configuredAllowFrom , ...storeAllowFrom ] ) ] ;
190- const effectiveAllowFrom = mode === "implicit" ? allowFromOverride : undefined ;
191-
192- if ( toCandidate && mode === "implicit" && allowFromOverride . length > 0 ) {
193- const currentTargetResolution = await resolveOutboundTargetWithRuntime ( {
194- channel,
195- to : toCandidate ,
180+ let effectiveAllowFrom : string [ ] | undefined ;
181+ if ( mode === "implicit" ) {
182+ const {
183+ getLoadedChannelPluginForRead,
184+ mapAllowFromEntries,
185+ readChannelAllowFromStoreEntriesSync,
186+ } = await loadDeliveryTargetRuntime ( ) ;
187+ const channelPlugin = getLoadedChannelPluginForRead ( channel ) ;
188+ const resolvedAccountId = normalizeAccountId ( accountId ) ;
189+ const configuredAllowFromRaw = channelPlugin ?. config . resolveAllowFrom ?.( {
196190 cfg,
197- accountId,
198- mode,
199- allowFrom : effectiveAllowFrom ,
191+ accountId : resolvedAccountId ,
200192 } ) ;
201- if ( ! currentTargetResolution . ok ) {
202- toCandidate = allowFromOverride [ 0 ] ;
193+ const configuredAllowFrom = configuredAllowFromRaw
194+ ? mapAllowFromEntries ( configuredAllowFromRaw )
195+ : [ ] ;
196+ const storeAllowFrom = readChannelAllowFromStoreEntriesSync (
197+ channel ,
198+ process . env ,
199+ resolvedAccountId ,
200+ ) ;
201+ const allowFromOverride = [ ...new Set ( [ ...configuredAllowFrom , ...storeAllowFrom ] ) ] ;
202+ effectiveAllowFrom = allowFromOverride ;
203+
204+ if ( toCandidate && allowFromOverride . length > 0 ) {
205+ const currentTargetResolution = await resolveOutboundTargetWithRuntime ( {
206+ channel,
207+ to : toCandidate ,
208+ cfg,
209+ accountId,
210+ mode,
211+ allowFrom : effectiveAllowFrom ,
212+ } ) ;
213+ if ( ! currentTargetResolution . ok ) {
214+ toCandidate = allowFromOverride [ 0 ] ;
215+ }
203216 }
204217 }
205218
0 commit comments