@@ -12,28 +12,14 @@ import type { OpenClawConfig } from "../../config/types.openclaw.js";
1212import { pickFirstExistingAgentId } from "../../routing/resolve-route.js" ;
1313import { resolveChannelConfiguredBindingProvider } from "./binding-provider.js" ;
1414import type { CompiledConfiguredBinding , ConfiguredBindingChannel } from "./binding-types.js" ;
15- import {
16- resolveConfiguredBindingConsumer ,
17- type ConfiguredBindingConsumer ,
18- } from "./configured-binding-consumers.js" ;
15+ import { resolveConfiguredBindingConsumer } from "./configured-binding-consumers.js" ;
1916import { getLoadedChannelPlugin } from "./index.js" ;
20- import type {
21- ChannelConfiguredBindingConversationRef ,
22- ChannelConfiguredBindingProvider ,
23- } from "./types.adapters.js" ;
17+ import type { ChannelConfiguredBindingProvider } from "./types.adapters.js" ;
2418
2519export type CompiledConfiguredBindingRegistry = {
2620 rulesByChannel : Map < ConfiguredBindingChannel , CompiledConfiguredBinding [ ] > ;
2721} ;
2822
29- function resolveLoadedChannelPlugin ( channel : string ) {
30- const normalized = normalizeOptionalLowercaseString ( channel ) ;
31- if ( ! normalized ) {
32- return undefined ;
33- }
34- return getLoadedChannelPlugin ( normalized as ConfiguredBindingChannel ) ;
35- }
36-
3723function resolveConfiguredBindingAdapter ( channel : string ) : {
3824 channel : ConfiguredBindingChannel ;
3925 provider : ChannelConfiguredBindingProvider ;
@@ -42,7 +28,7 @@ function resolveConfiguredBindingAdapter(channel: string): {
4228 if ( ! normalized ) {
4329 return null ;
4430 }
45- const plugin = resolveLoadedChannelPlugin ( normalized ) ;
31+ const plugin = getLoadedChannelPlugin ( normalized as ConfiguredBindingChannel ) ;
4632 const provider = resolveChannelConfiguredBindingProvider ( plugin ) ;
4733 if (
4834 ! plugin ||
@@ -58,81 +44,19 @@ function resolveConfiguredBindingAdapter(channel: string): {
5844 } ;
5945}
6046
61- function resolveBindingConversationId ( binding : {
62- match ?: { peer ?: { id ?: string } } ;
63- } ) : string | null {
64- return normalizeOptionalString ( binding . match ?. peer ?. id ) ?? null ;
65- }
66-
67- function compileConfiguredBindingTarget ( params : {
68- provider : ChannelConfiguredBindingProvider ;
69- binding : CompiledConfiguredBinding [ "binding" ] ;
70- conversationId : string ;
71- } ) : ChannelConfiguredBindingConversationRef | null {
72- return params . provider . compileConfiguredBinding ( {
73- binding : params . binding ,
74- conversationId : params . conversationId ,
75- } ) ;
76- }
77-
78- function compileConfiguredBindingRule ( params : {
79- cfg : OpenClawConfig ;
80- channel : ConfiguredBindingChannel ;
81- binding : CompiledConfiguredBinding [ "binding" ] ;
82- target : ChannelConfiguredBindingConversationRef ;
83- bindingConversationId : string ;
84- provider : ChannelConfiguredBindingProvider ;
85- consumer : ConfiguredBindingConsumer ;
86- } ) : CompiledConfiguredBinding | null {
87- const agentId = pickFirstExistingAgentId ( params . cfg , params . binding . agentId ?? "main" ) ;
88- const targetFactory = params . consumer . buildTargetFactory ( {
89- cfg : params . cfg ,
90- binding : params . binding ,
91- channel : params . channel ,
92- agentId,
93- target : params . target ,
94- bindingConversationId : params . bindingConversationId ,
95- } ) ;
96- if ( ! targetFactory ) {
97- return null ;
98- }
99- return {
100- channel : params . channel ,
101- accountPattern : normalizeOptionalString ( params . binding . match . accountId ) ,
102- binding : params . binding ,
103- bindingConversationId : params . bindingConversationId ,
104- target : params . target ,
105- agentId,
106- provider : params . provider ,
107- targetFactory,
108- } ;
109- }
110-
111- function pushCompiledRule (
112- target : Map < ConfiguredBindingChannel , CompiledConfiguredBinding [ ] > ,
113- rule : CompiledConfiguredBinding ,
114- ) {
115- const existing = target . get ( rule . channel ) ;
116- if ( existing ) {
117- existing . push ( rule ) ;
118- return ;
119- }
120- target . set ( rule . channel , [ rule ] ) ;
121- }
122-
123- function compileConfiguredBindingRegistry ( params : {
124- cfg : OpenClawConfig ;
125- } ) : CompiledConfiguredBindingRegistry {
47+ export function resolveCompiledBindingRegistry (
48+ cfg : OpenClawConfig ,
49+ ) : CompiledConfiguredBindingRegistry {
12650 const rulesByChannel = new Map < ConfiguredBindingChannel , CompiledConfiguredBinding [ ] > ( ) ;
12751
128- for ( const binding of listConfiguredBindings ( params . cfg ) ) {
52+ for ( const binding of listConfiguredBindings ( cfg ) ) {
12953 // Ordinary routing bindings share the config array but have no stateful target consumer.
13054 // Reject them before consulting the loaded channel registry on request-time route lookups.
13155 const consumer = resolveConfiguredBindingConsumer ( binding ) ;
13256 if ( ! consumer ) {
13357 continue ;
13458 }
135- const bindingConversationId = resolveBindingConversationId ( binding ) ;
59+ const bindingConversationId = normalizeOptionalString ( binding . match ?. peer ?. id ) ;
13660 if ( ! bindingConversationId ) {
13761 continue ;
13862 }
@@ -142,56 +66,43 @@ function compileConfiguredBindingRegistry(params: {
14266 continue ;
14367 }
14468
145- const target = compileConfiguredBindingTarget ( {
146- provider : resolvedChannel . provider ,
69+ const target = resolvedChannel . provider . compileConfiguredBinding ( {
14770 binding,
14871 conversationId : bindingConversationId ,
14972 } ) ;
15073 if ( ! target ) {
15174 continue ;
15275 }
15376
154- const rule = compileConfiguredBindingRule ( {
155- cfg : params . cfg ,
156- channel : resolvedChannel . channel ,
77+ const agentId = pickFirstExistingAgentId ( cfg , binding . agentId ?? "main" ) ;
78+ const targetFactory = consumer . buildTargetFactory ( {
79+ cfg ,
15780 binding,
81+ channel : resolvedChannel . channel ,
82+ agentId,
15883 target,
15984 bindingConversationId,
160- provider : resolvedChannel . provider ,
161- consumer,
16285 } ) ;
163- if ( ! rule ) {
86+ if ( ! targetFactory ) {
16487 continue ;
16588 }
166- pushCompiledRule ( rulesByChannel , rule ) ;
89+ const rule : CompiledConfiguredBinding = {
90+ channel : resolvedChannel . channel ,
91+ accountPattern : normalizeOptionalString ( binding . match . accountId ) ,
92+ binding,
93+ bindingConversationId,
94+ target,
95+ agentId,
96+ provider : resolvedChannel . provider ,
97+ targetFactory,
98+ } ;
99+ const existing = rulesByChannel . get ( rule . channel ) ;
100+ if ( existing ) {
101+ existing . push ( rule ) ;
102+ } else {
103+ rulesByChannel . set ( rule . channel , [ rule ] ) ;
104+ }
167105 }
168106
169- return {
170- rulesByChannel,
171- } ;
172- }
173-
174- export function resolveCompiledBindingRegistry (
175- cfg : OpenClawConfig ,
176- ) : CompiledConfiguredBindingRegistry {
177- return compileConfiguredBindingRegistry ( { cfg } ) ;
178- }
179-
180- export function primeCompiledBindingRegistry (
181- cfg : OpenClawConfig ,
182- ) : CompiledConfiguredBindingRegistry {
183- return compileConfiguredBindingRegistry ( { cfg } ) ;
184- }
185-
186- export function countCompiledBindingRegistry ( registry : CompiledConfiguredBindingRegistry ) : {
187- bindingCount : number ;
188- channelCount : number ;
189- } {
190- return {
191- bindingCount : [ ...registry . rulesByChannel . values ( ) ] . reduce (
192- ( sum , rules ) => sum + rules . length ,
193- 0 ,
194- ) ,
195- channelCount : registry . rulesByChannel . size ,
196- } ;
107+ return { rulesByChannel } ;
197108}
0 commit comments