Hi Guys, Is it possible to add hook functions in a...
# questions
l
Hi Guys, Is it possible to add hook functions in agent delegation? I want certain function to execute before triggering a particular type of agent.
m
https://github.com/OpenHands/software-agent-sdk/pull/2347 🙂 How are you using sub-agents—via the
SDK
or the
CLI
? The change I sent you is currently only available in the `SDK`'s main branch. Re-reading your question, I’m not sure if I fully understood what you're trying to do. Could you provide an example or a concrete use case of what you would like to accomplish?
l
I'm using SDK.. Thanks Vasco.. This is exactly what I was looking for..
excited 1
I want to do some workspace cleanup before triggering the delegation agent.
m
Ok now I see 🙂 I believe, you can do that in 2 different ways: 1. PRE_TOOL_USE hook on the parent agent, matching the delegate tool Since subagents are spawned via the delegate tool, you can attach a
pre_tool_use
hook on the parent conversation that matches "delegate". This fires before the subagent is created:
Copy code
HookConfig(
   pre_tool_use=[
     HookMatcher(
       matcher="delegate", 
       hooks=[
         HookDefinition(command="./cleanup_workspace.sh")
       ]
     )
   ]
 )
2. SESSION_START hook on the subagent definition This fires when the subagent's conversation begins (right after creation). Define it in the agent's .md frontmatter or programmatically:
Copy code
---
 name: my-worker-agent
 hooks:
  session_start:
   - matcher: "*"
    hooks:
     - command: "./cleanup_workspace.sh"
 ---
Or programmatically in the AgentDefinition:
Copy code
AgentDefinition(
   hooks=HookConfig(
     session_start=[
       HookMatcher(matcher="*", hooks=[
         HookDefinition(command="./cleanup_workspace.sh")
       ])
     ]
   )
 )
The first solution is already available in the last release of the sdk. The second just in sdk main.