@@ -144,6 +144,8 @@ pub trait AgentDyn<C>: Send + Sync
144144where
145145 C : AgentContext + Send + Sync ,
146146{
147+ fn label ( & self ) -> & str ;
148+
147149 fn name ( & self ) -> String ;
148150
149151 fn definition ( & self ) -> FunctionDefinition ;
@@ -163,34 +165,43 @@ where
163165}
164166
165167/// Adapter for converting static Agent to dynamic dispatch.
166- struct AgentWrapper < T , C > ( Arc < T > , PhantomData < C > )
168+ struct AgentWrapper < T , C >
167169where
168170 T : Agent < C > + ' static ,
169- C : AgentContext + Send + Sync + ' static ;
171+ C : AgentContext + Send + Sync + ' static ,
172+ {
173+ inner : Arc < T > ,
174+ label : String ,
175+ _phantom : PhantomData < C > ,
176+ }
170177
171178impl < T , C > AgentDyn < C > for AgentWrapper < T , C >
172179where
173180 T : Agent < C > + ' static ,
174181 C : AgentContext + Send + Sync + ' static ,
175182{
183+ fn label ( & self ) -> & str {
184+ & self . label
185+ }
186+
176187 fn name ( & self ) -> String {
177- self . 0 . name ( )
188+ self . inner . name ( )
178189 }
179190
180191 fn definition ( & self ) -> FunctionDefinition {
181- self . 0 . definition ( )
192+ self . inner . definition ( )
182193 }
183194
184195 fn tool_dependencies ( & self ) -> Vec < String > {
185- self . 0 . tool_dependencies ( )
196+ self . inner . tool_dependencies ( )
186197 }
187198
188199 fn supported_resource_tags ( & self ) -> Vec < String > {
189- self . 0 . supported_resource_tags ( )
200+ self . inner . supported_resource_tags ( )
190201 }
191202
192203 fn init ( & self , ctx : C ) -> BoxPinFut < Result < ( ) , BoxError > > {
193- let agent = self . 0 . clone ( ) ;
204+ let agent = self . inner . clone ( ) ;
194205 Box :: pin ( async move { agent. init ( ctx) . await } )
195206 }
196207
@@ -200,7 +211,7 @@ where
200211 prompt : String ,
201212 resources : Vec < Resource > ,
202213 ) -> BoxPinFut < Result < AgentOutput , BoxError > > {
203- let agent = self . 0 . clone ( ) ;
214+ let agent = self . inner . clone ( ) ;
204215 Box :: pin ( async move { agent. run ( ctx, prompt, resources) . await } )
205216 }
206217}
@@ -275,11 +286,13 @@ where
275286 /// # Returns
276287 /// - Vec<[`Function`]>: Vector of agent functions.
277288 pub fn functions ( & self , names : Option < & [ & str ] > ) -> Vec < Function > {
289+ let names: Option < Vec < String > > =
290+ names. map ( |names| names. iter ( ) . map ( |n| n. to_ascii_lowercase ( ) ) . collect ( ) ) ;
278291 self . set
279292 . iter ( )
280- . filter_map ( |( name, agent) | match names {
293+ . filter_map ( |( name, agent) | match & names {
281294 Some ( names) => {
282- if names. contains ( & name. as_str ( ) ) {
295+ if names. contains ( name) {
283296 Some ( Function {
284297 definition : agent. definition ( ) ,
285298 supported_resource_tags : agent. supported_resource_tags ( ) ,
@@ -299,7 +312,7 @@ where
299312 /// Extracts resources from the provided list based on the agent's supported tags.
300313 pub fn select_resources ( & self , name : & str , resources : & mut Vec < Resource > ) -> Vec < Resource > {
301314 self . set
302- . get ( name)
315+ . get ( & name. to_ascii_lowercase ( ) )
303316 . map ( |agent| {
304317 let supported_tags = agent. supported_resource_tags ( ) ;
305318 select_resources ( resources, & supported_tags)
@@ -311,7 +324,7 @@ where
311324 ///
312325 /// # Arguments
313326 /// - `agent`: The agent to register, must implement [`Agent`] trait.
314- pub fn add < T > ( & mut self , agent : T ) -> Result < ( ) , BoxError >
327+ pub fn add < T > ( & mut self , agent : T , label : Option < String > ) -> Result < ( ) , BoxError >
315328 where
316329 T : Agent < C > + Send + Sync + ' static ,
317330 {
@@ -321,13 +334,17 @@ where
321334 }
322335
323336 validate_function_name ( & name) ?;
324- let agent_dyn = AgentWrapper ( Arc :: new ( agent) , PhantomData ) ;
337+ let agent_dyn = AgentWrapper {
338+ inner : Arc :: new ( agent) ,
339+ label : label. unwrap_or_else ( || name. clone ( ) ) ,
340+ _phantom : PhantomData ,
341+ } ;
325342 self . set . insert ( name, Box :: new ( agent_dyn) ) ;
326343 Ok ( ( ) )
327344 }
328345
329346 /// Retrieves an agent by name.
330347 pub fn get ( & self , name : & str ) -> Option < & dyn AgentDyn < C > > {
331- self . set . get ( name) . map ( |v| & * * v)
348+ self . set . get ( & name. to_ascii_lowercase ( ) ) . map ( |v| & * * v)
332349 }
333350}
0 commit comments