@@ -57,40 +57,6 @@ class AgentFactory(NamedTuple):
5757_registry_lock = RLock ()
5858
5959
60- def _definition_from_factory (
61- name : str ,
62- factory_func : Callable [["LLM" ], "Agent" ],
63- description : str ,
64- ) -> AgentDefinition :
65- """Introspect *factory_func* with a dummy LLM to build a full AgentDefinition.
66-
67- This lets register_agent() accept just (name, factory_func, description)
68- while still producing a complete definition (tools, system_prompt, model)
69- that can be forwarded to a remote server. Falls back to a minimal
70- definition if the factory raises.
71- """
72- _model_placeholder = "__introspect__"
73- try :
74- from openhands .sdk .llm .llm import LLM as _LLM
75-
76- agent = factory_func (_LLM (model = _model_placeholder , api_key = SecretStr ("n/a" )))
77- tools = [t .name for t in agent .tools ]
78- system_prompt = ""
79- if agent .agent_context and agent .agent_context .system_message_suffix :
80- system_prompt = agent .agent_context .system_message_suffix
81- model = agent .llm .model if agent .llm .model != _model_placeholder else "inherit"
82- return AgentDefinition (
83- name = name ,
84- description = description ,
85- tools = tools ,
86- system_prompt = system_prompt ,
87- model = model ,
88- )
89- except Exception :
90- logger .debug (f"Could not introspect factory for agent '{ name } '" )
91- return AgentDefinition (name = name , description = description )
92-
93-
9460def register_agent (
9561 name : str ,
9662 factory_func : Callable [["LLM" ], "Agent" ],
@@ -107,13 +73,28 @@ def register_agent(
10773 Raises:
10874 ValueError: If an agent with the same name already exists
10975 """
76+ try :
77+ from openhands .sdk .llm .llm import LLM as _LLM
78+
79+ _model_placeholder = "__introspect__"
80+ agent = factory_func (_LLM (model = _model_placeholder , api_key = SecretStr ("n/a" )))
81+ definition = AgentDefinition .from_agent (
82+ agent , name = name , description = description
83+ )
84+ # If the model was our placeholder, the factory didn't set one explicitly
85+ if definition .model == _model_placeholder :
86+ definition = definition .model_copy (update = {"model" : "inherit" })
87+ except Exception :
88+ logger .debug (f"Could not introspect factory for agent '{ name } '" )
89+ definition = AgentDefinition (name = name , description = description )
90+
11091 with _registry_lock :
11192 if name in _agent_factories :
11293 raise ValueError (f"Agent '{ name } ' already registered" )
11394
11495 _agent_factories [name ] = AgentFactory (
11596 factory_func = factory_func ,
116- definition = _definition_from_factory ( name , factory_func , description ) ,
97+ definition = definition ,
11798 )
11899
119100
0 commit comments