mirror of
https://github.com/microsoft/autogen.git
synced 2025-10-21 21:10:22 +00:00

<!-- Thank you for your contribution! Please review https://microsoft.github.io/autogen/docs/Contribute before opening a pull request. --> <!-- Please add a reviewer to the assignee section when you create a PR. If you don't have the access to it, we will shortly find a reviewer and assign them to your PR. --> ## Why are these changes needed? Make AssistantAgent and Handoff use BaseTool. This ensures that they can be made declarative/serialized <!-- Please give a short summary of the change and the problem this solves. --> ## Related issue number <!-- For example: "Closes #1234" --> ## Checks - [ ] I've included any doc changes needed for https://microsoft.github.io/autogen/. See https://microsoft.github.io/autogen/docs/Contribute#documentation to build and test documentation locally. - [ ] I've added tests (if relevant) corresponding to the changes introduced in this PR. - [ ] I've made sure all auto checks have passed.
57 lines
2.1 KiB
Python
57 lines
2.1 KiB
Python
import logging
|
|
from typing import Any, Dict
|
|
|
|
from autogen_core.tools import FunctionTool, BaseTool
|
|
from pydantic import BaseModel, Field, model_validator
|
|
|
|
from .. import EVENT_LOGGER_NAME
|
|
|
|
event_logger = logging.getLogger(EVENT_LOGGER_NAME)
|
|
|
|
|
|
class Handoff(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."""
|
|
|
|
@model_validator(mode="before")
|
|
@classmethod
|
|
def set_defaults(cls, values: Dict[str, Any]) -> Dict[str, Any]:
|
|
if not values.get("description"):
|
|
values["description"] = f"Handoff to {values['target']}."
|
|
if not values.get("name"):
|
|
values["name"] = f"transfer_to_{values['target']}".lower()
|
|
else:
|
|
name = values["name"]
|
|
if not isinstance(name, str):
|
|
raise ValueError(f"Handoff name must be a string: {values['name']}")
|
|
# Check if name is a valid identifier.
|
|
if not name.isidentifier():
|
|
raise ValueError(f"Handoff name must be a valid identifier: {values['name']}")
|
|
if not values.get("message"):
|
|
values["message"] = (
|
|
f"Transferred to {values['target']}, adopting the role of {values['target']} immediately."
|
|
)
|
|
return values
|
|
|
|
@property
|
|
def handoff_tool(self) -> BaseTool[BaseModel, BaseModel]:
|
|
"""Create a handoff tool from this handoff configuration."""
|
|
|
|
def _handoff_tool() -> str:
|
|
return self.message
|
|
|
|
return FunctionTool(_handoff_tool, name=self.name, description=self.description)
|