MCPcopy
hub / github.com/langchain-ai/deepagents / dispatch_hook

Function dispatch_hook

libs/code/deepagents_code/hooks.py:161–187  ·  view source on GitHub ↗

Fire matching hook commands with `payload` serialized as JSON on stdin. The `event` name is automatically injected into the payload under the `"event"` key so callers don't need to duplicate it. The blocking subprocess work is offloaded to a thread so the caller's event loop is nev

(event: str, payload: dict[str, Any])

Source from the content-addressed store, hash-verified

159
160
161async def dispatch_hook(event: str, payload: dict[str, Any]) -> None:
162 """Fire matching hook commands with `payload` serialized as JSON on stdin.
163
164 The `event` name is automatically injected into the payload under the
165 `"event"` key so callers don't need to duplicate it.
166
167 The blocking subprocess work is offloaded to a thread so the caller's
168 event loop is never stalled. Matching hooks run concurrently, each with
169 a 5-second timeout. Errors are logged and never propagated.
170
171 Args:
172 event: Dotted event name (e.g. `'session.start'`).
173 payload: Arbitrary JSON-serializable dict sent on the command's stdin.
174 """
175 try:
176 hooks = _load_hooks()
177 if not hooks:
178 return
179
180 payload_bytes = json.dumps({"event": event, **payload}).encode()
181 await asyncio.to_thread(_dispatch_hook_sync, event, payload_bytes, hooks)
182 except Exception:
183 logger.warning(
184 "Unexpected error in dispatch_hook for event %s",
185 event,
186 exc_info=True,
187 )
188
189
190def dispatch_hook_fire_and_forget(event: str, payload: dict[str, Any]) -> None:

Callers 5

execute_task_textualFunction · 0.90
_run_agent_loopFunction · 0.90
_handle_offloadMethod · 0.90

Calls 1

_load_hooksFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…