c56bef871b
Sync docs with Docusaurus / sync (push) Waiting to run
Tests / Check if changed (push) Waiting to run
Tests / format (push) Blocked by required conditions
Tests / check-imports (push) Blocked by required conditions
Tests / Unit / macos-latest (push) Blocked by required conditions
Tests / Unit / ubuntu-latest (push) Blocked by required conditions
Tests / Unit / windows-latest (push) Blocked by required conditions
Tests / mypy (push) Blocked by required conditions
Tests / Integration / ubuntu-latest (push) Blocked by required conditions
Tests / Integration / macos-latest (push) Blocked by required conditions
Tests / Integration / windows-latest (push) Blocked by required conditions
Tests / notify-slack-on-failure (push) Blocked by required conditions
Tests / Mark tests as completed (push) Blocked by required conditions
Docker image release / Build base image (push) Waiting to run
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
51 lines
2.1 KiB
Python
51 lines
2.1 KiB
Python
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
#
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
from typing import Any, Literal, Protocol, get_args
|
|
|
|
from haystack.components.agents.state.state import State
|
|
|
|
# Points in the Agent's run loop at which hooks can be registered.
|
|
HookPoint = Literal["before_llm", "before_tool", "after_tool", "on_exit"]
|
|
|
|
BEFORE_LLM: HookPoint = "before_llm"
|
|
BEFORE_TOOL: HookPoint = "before_tool"
|
|
AFTER_TOOL: HookPoint = "after_tool"
|
|
ON_EXIT: HookPoint = "on_exit"
|
|
VALID_HOOK_POINTS: tuple[HookPoint, ...] = get_args(HookPoint)
|
|
|
|
|
|
class Hook(Protocol):
|
|
"""
|
|
A callable the Agent invokes at a point in its run loop, receiving the live `State`.
|
|
|
|
A hook influences the run only by mutating `State` in place. At least `messages` (the conversation),
|
|
`step_count`, `token_usage` and `tool_call_counts` are available; any additional keys defined in the Agent's
|
|
`state_schema` are available too. The same hook object can be registered under multiple hook points.
|
|
|
|
Implement this protocol directly for stateful hooks (e.g. one wrapping a component), or use the `@hook` decorator to
|
|
wrap a plain `(State) -> None` function.
|
|
|
|
A hook may additionally define `async def run_async(self, state: State) -> None` for true async behavior; when
|
|
absent, the Agent calls `run` during async runs. It is left off this protocol on purpose so sync-only hooks
|
|
don't have to implement it.
|
|
|
|
A hook may also implement the optional lifecycle methods `warm_up` / `warm_up_async` and `close` / `close_async`.
|
|
The Agent calls them from its own `warm_up` / `warm_up_async` and `close` / `close_async`, so a hook can defer
|
|
opening clients or reading credentials until warm-up and release them on close.
|
|
"""
|
|
|
|
def run(self, state: State) -> None:
|
|
"""Run the hook against the live `State`, mutating it in place."""
|
|
...
|
|
|
|
def to_dict(self) -> dict[str, Any]:
|
|
"""Serialize the hook to a dictionary."""
|
|
...
|
|
|
|
@classmethod
|
|
def from_dict(cls, data: dict[str, Any]) -> "Hook":
|
|
"""Deserialize the hook from a dictionary."""
|
|
...
|