Skip to content

Hooks

junjo.Hooks

Kind: Class

SDK version: 0.64.0

Documentation channel: Next source preview

Hooks()

View source

Registry for optional Junjo lifecycle callbacks.

Hooks are observers. They do not create spans or control Workflow or Agent execution. If a hook callback raises, Junjo keeps execution isolated and continues dispatching the remaining callbacks for that hook.

To use them, register one or more callbacks and pass the registry to a Workflow, Subflow, or Agent definition.

Every on_* registration method returns an unsubscribe callback. Call the returned function when the callback should no longer receive events.

.. rubric:: Example

``python import logging

hooks = Hooks() logger = logging.getLogger(name)

def log_completed(event: WorkflowCompletedEvent[MyState]) -> None: logger.info(“%s %s”, event.hook_name, event.result.state.model_dump())

unsubscribe = hooks.on_workflow_completed(log_completed)

workflow = Workflow(…, hooks=hooks)

Later, if this callback should no longer run:

Section titled “Later, if this callback should no longer run:”

unsubscribe() ``

on_workflow_started(callback: HookCallback[WorkflowStartedEvent]) -> Callable[[], None]

View source

Register a callback for the start of a top-level workflow run.

on_workflow_completed(callback: HookCallback[WorkflowCompletedEvent[StateT]]) -> Callable[[], None]

View source

Register a callback for successful workflow completion.

on_workflow_failed(callback: HookCallback[WorkflowFailedEvent[StateT]]) -> Callable[[], None]

View source

Register a callback for workflow failures.

on_workflow_cancelled(callback: HookCallback[WorkflowCancelledEvent[StateT]]) -> Callable[[], None]

View source

Register a callback for workflow cancellation.

on_subflow_started(callback: HookCallback[SubflowStartedEvent]) -> Callable[[], None]

View source

Register a callback for subflow start events.

on_subflow_completed(callback: HookCallback[SubflowCompletedEvent[StateT]]) -> Callable[[], None]

View source

Register a callback for successful subflow completion.

on_subflow_failed(callback: HookCallback[SubflowFailedEvent[StateT]]) -> Callable[[], None]

View source

Register a callback for subflow failures.

on_subflow_cancelled(callback: HookCallback[SubflowCancelledEvent[StateT]]) -> Callable[[], None]

View source

Register a callback for subflow cancellation.

on_node_started(callback: HookCallback[NodeStartedEvent]) -> Callable[[], None]

View source

Register a callback for node start events.

on_node_completed(callback: HookCallback[NodeCompletedEvent]) -> Callable[[], None]

View source

Register a callback for successful node completion.

on_node_failed(callback: HookCallback[NodeFailedEvent]) -> Callable[[], None]

View source

Register a callback for node failures.

on_node_cancelled(callback: HookCallback[NodeCancelledEvent]) -> Callable[[], None]

View source

Register a callback for node cancellation.

on_run_concurrent_started(callback: HookCallback[RunConcurrentStartedEvent]) -> Callable[[], None]

View source

Register a callback for RunConcurrent start events.

on_run_concurrent_completed(callback: HookCallback[RunConcurrentCompletedEvent]) -> Callable[[], None]

View source

Register a callback for successful RunConcurrent completion.

on_run_concurrent_failed(callback: HookCallback[RunConcurrentFailedEvent]) -> Callable[[], None]

View source

Register a callback for RunConcurrent failures.

on_run_concurrent_cancelled(callback: HookCallback[RunConcurrentCancelledEvent]) -> Callable[[], None]

View source

Register a callback for RunConcurrent cancellation.

on_state_changed(callback: HookCallback[StateChangedEvent[StateT]]) -> Callable[[], None]

View source

Register a callback for committed state updates.

on_agent_started(callback: HookCallback[AgentStartedEvent]) -> Callable[[], None]

View source

Register a callback for admitted Agent execution start.

on_agent_completed(callback: HookCallback[AgentCompletedEvent]) -> Callable[[], None]

View source

Register a callback for successful Agent completion.

on_agent_failed(callback: HookCallback[AgentFailedEvent]) -> Callable[[], None]

View source

Register a callback for admitted Agent execution failure.

on_agent_cancelled(callback: HookCallback[AgentCancelledEvent]) -> Callable[[], None]

View source

Register a callback for admitted Agent execution cancellation.