Hook System Deep Dive¶
Overview¶
mozaiksai uses AG2 hook points plus declarative registration from hooks.yaml.
Canonical hook declarations live in:
app/workflows/{workflow}/hooks.yaml
Builder workflows use the same contract under factory_app/workflows/{workflow}/hooks.yaml.
No workflow hook JSON files are used in the canonical runtime contract.
Supported Hook Types¶
hooks.yaml supports:
process_message_before_sendupdate_agent_stateprocess_last_received_messageprocess_all_messages_before_reply
Declarative Contract¶
hooks:
- hook_type: update_agent_state
hook_agent: PlannerAgent
filename: hook_inject_plan.py
function: inject_plan_state
Fields are required per entry:
hook_typehook_agentfilenamefunction
Runtime Registration Model¶
load_hook_entries()reads and validateshooks.yaml.- For
update_agent_stateentries: - functions are pre-loaded before
ConversableAgent(...)construction. - hooks are passed through
update_agent_state_before_reply. - Other hook types are registered via
register_hook(...)post-construction.
This split exists because AG2 update_agent_state_before_reply behavior is bound during agent setup.
Execution Timing¶
update_agent_state: before agent reply generation.process_all_messages_before_reply: before reply using full message list.process_last_received_message: before reply, focused on latest message.process_message_before_send: right before outbound send.
Troubleshooting¶
If a hook does not fire:
- Verify the
hooks.yamlentry validates. - Confirm
hook_agentexactly matches the runtime agent name. - Confirm
filenameandfunctionresolve to an importable callable. - Check agent hook snapshots from runtime logs (
register_hooksdiagnostics).