[docs]def_load_agent(self,entrypoint:str)->Agent:"""Load agent from entrypoint with proper error handling."""if":"notinentrypoint:raiseValueError(f"Invalid entrypoint format: {entrypoint}. Expected 'module:Class'")module_name,class_name=entrypoint.split(":",1)try:module=importlib.import_module(module_name)exceptImportErrorase:raiseImportError(f"Could not import module '{module_name}': {e}")try:agent_class=getattr(module,class_name)exceptAttributeErrorase:raiseAttributeError(f"Module '{module_name}' has no class '{class_name}': {e}")ifnotissubclass(agent_class,Agent):raiseTypeError(f"{agent_class} is not a subclass of Agent")# Try to instantiate with minimal configfromhaive.core.engine.aug_llmimportAugLLMConfigtry:# First try with no args (for agents with defaults)returnagent_class()exceptException:try:# Try with name onlyreturnagent_class(name=class_name.lower())exceptException:try:# Try with name and enginereturnagent_class(name=class_name.lower(),engine=AugLLMConfig())exceptExceptionase:raiseRuntimeError(f"Could not instantiate {agent_class}: {e}")
[docs]asyncdefrun(self,initial_context:Dict[str,Any])->HAPContext:"""Execute the graph asynchronously."""returnawaitself.graph.execute(initial_context)
[docs]defrun_sync(self,initial_context:Dict[str,Any])->HAPContext:"""Execute the graph synchronously (for backward compatibility)."""importasyncio# Create context if neededifnotisinstance(initial_context,HAPContext):context=HAPContext()context.update(initial_context)else:context=initial_context# Simple synchronous execution for backward compatibilityexecution_order=self.graph.topological_order()fornode_idinexecution_order:node=self.graph.nodes[node_id]agent=node.load_agent()# Update execution pathcontext.execution_path.append(node_id)# Execute agent synchronouslyresult=agent.run(context.model_dump())# Update contextifisinstance(result,dict):context.update(result)else:context.outputs[node_id]=resultreturncontext