[docs]classHandoff(BaseModel):"""Handoff configuration."""target:str"""The name of the target agent to handoff to."""description:str=Field(default="")"""The description of the handoff such as the condition under which it should happen and the target agent's ability. If not provided, it is generated from the target agent's name."""name:str=Field(default="")"""The name of this handoff configuration. If not provided, it is generated from the target agent's name."""message:str=Field(default="")"""The message to the target agent. If not provided, it is generated from the target agent's name."""
[docs]@model_validator(mode="before")@classmethoddefset_defaults(cls,values:Dict[str,Any])->Dict[str,Any]:ifnotvalues.get("description"):values["description"]=f"Handoff to {values['target']}."ifnotvalues.get("name"):values["name"]=f"transfer_to_{values['target']}".lower()else:name=values["name"]ifnotisinstance(name,str):raiseValueError(f"Handoff name must be a string: {values['name']}")# Check if name is a valid identifier.ifnotname.isidentifier():raiseValueError(f"Handoff name must be a valid identifier: {values['name']}")ifnotvalues.get("message"):values["message"]=(f"Transferred to {values['target']}, adopting the role of {values['target']} immediately.")returnvalues
@propertydefhandoff_tool(self)->BaseTool[BaseModel,BaseModel]:"""Create a handoff tool from this handoff configuration."""def_handoff_tool()->str:returnself.messagereturnFunctionTool(_handoff_tool,name=self.name,description=self.description,strict=True)