[docs]defagent(name:str|None=None,description:str|None=None,*,metadata:Metadata|None=None,)->Callable[[Callable],AgentManifest]:"""Decorator to create an agent."""defdecorator(fn:Callable)->AgentManifest:signature=inspect.signature(fn)parameters=list(signature.parameters.values())iflen(parameters)==0:raiseTypeError("The agent function must have at least 'input' argument")iflen(parameters)>2:raiseTypeError("The agent function must have only 'input' and 'context' arguments")iflen(parameters)==2andparameters[1].name!="context":raiseTypeError("The second argument of the agent function must be 'context'")has_context_param=len(parameters)==2classDecoratorAgentBase(AgentManifest):@propertydefname(self)->str:returnnameorfn.__name__@propertydefdescription(self)->str:returndescriptionorinspect.getdoc(fn)or""@propertydefmetadata(self)->Metadata:returnmetadataorMetadata()agent:AgentManifestifinspect.isasyncgenfunction(fn):classAsyncGenDecoratorAgent(DecoratorAgentBase):asyncdefrun(self,input:list[Message],context:Context)->AsyncGenerator[RunYield,RunYieldResume]:try:gen:AsyncGenerator[RunYield,RunYieldResume]=(fn(input,context)ifhas_context_paramelsefn(input))value=NonewhileTrue:value=yieldawaitgen.asend(value)exceptStopAsyncIteration:passagent=AsyncGenDecoratorAgent()elifinspect.iscoroutinefunction(fn):classCoroDecoratorAgent(DecoratorAgentBase):asyncdefrun(self,input:list[Message],context:Context)->Coroutine[RunYield]:returnawait(fn(input,context)ifhas_context_paramelsefn(input))agent=CoroDecoratorAgent()elifinspect.isgeneratorfunction(fn):classGenDecoratorAgent(DecoratorAgentBase):defrun(self,input:list[Message],context:Context)->Generator[RunYield,RunYieldResume]:yield from(fn(input,context)ifhas_context_paramelsefn(input))agent=GenDecoratorAgent()else:classFuncDecoratorAgent(DecoratorAgentBase):defrun(self,input:list[Message],context:Context)->RunYield:returnfn(input,context)ifhas_context_paramelsefn(input)agent=FuncDecoratorAgent()returnagentreturndecorator