Files
crewAI/src/crewai/agents/cache/cache_handler.py
Greyson LaLonde d5126d159b
Some checks failed
Notify Downstream / notify-downstream (push) Has been cancelled
Mark stale issues and pull requests / stale (push) Has been cancelled
chore: improve typing and docs in agents leaf files (#3461)
- Add typing and Google-style docstrings to agents leaf files
- Add TODO notes
2025-09-08 11:57:34 -04:00

46 lines
1.2 KiB
Python

"""Cache handler for tool usage results."""
from typing import Any
from pydantic import BaseModel, PrivateAttr
class CacheHandler(BaseModel):
"""Handles caching of tool execution results.
Provides in-memory caching for tool outputs based on tool name and input.
Notes:
- TODO: Make thread-safe.
"""
_cache: dict[str, Any] = PrivateAttr(default_factory=dict)
def add(self, tool: str, input: str, output: Any) -> None:
"""Add a tool result to the cache.
Args:
tool: Name of the tool.
input: Input string used for the tool.
output: Output result from tool execution.
Notes:
- TODO: Rename 'input' parameter to avoid shadowing builtin.
"""
self._cache[f"{tool}-{input}"] = output
def read(self, tool: str, input: str) -> Any | None:
"""Retrieve a cached tool result.
Args:
tool: Name of the tool.
input: Input string used for the tool.
Returns:
Cached result if found, None otherwise.
Notes:
- TODO: Rename 'input' parameter to avoid shadowing builtin.
"""
return self._cache.get(f"{tool}-{input}")