Open
Description
I would like to give varying degrees of control to my agents. For example I would like a certain tool to not be available until another tool is called. Or, similarly, I don't want the agents to be able to handoff without calling a tool first.
Is there a way around this without something like this where we return from an agent?
I tried something like this:
class PlannerHooks(AgentHooks):
def __init__(self):
self._saved_handoffs: List[Handoff] | None = None
async def on_start(self, ctx, agent):
# remove the handoff
if self._saved_handoffs is None:
self._saved_handoffs = list(agent.handoffs)
agent.handoffs = [] # special line
agent.tools = [t for t in agent.tools if t.name != "transfer_to_executor"]
async def on_tool_end(self, ctx, agent, tool, result):
if tool.name != "specific_tool":
return
agent.handoffs = self._saved_handoffs
agent.tools.append(FunctionTool(name="transfer_to_executor", description="Transfer to the executor agent.", function=lambda x: x))
agent.model_settings = ModelSettings(tool_choice="transfer_to_executor")
agent.reset_tool_choice = False
async def on_handoff(self, context: RunContextWrapper, agent: Agent, source: Agent) -> None:
agent.model_settings = ModelSettings(tool_choice=STARTING_TOOL_CHOICE[agent.name])
agent.reset_tool_choice = False
This used to work but it no longer does. In particular, this code used to work without the special line
above; then it it broke, meaning the agent would attempt to call the tool I removed, the handoff. I then added the special line
and this gets me an OpenAI server error with a request ID. So it seems that changes made to tools during on_start
do not take effect.