|
| 1 | +"""Ledger context provider for comprehensive agent activity tracking. |
| 2 | +
|
| 3 | +Tracks complete agent activity ledger including tool calls, conversation history, |
| 4 | +and timing information. This comprehensive audit trail enables steering handlers |
| 5 | +to make informed guidance decisions based on agent behavior patterns and history. |
| 6 | +
|
| 7 | +Data captured: |
| 8 | +
|
| 9 | + - Tool call history with inputs, outputs, timing, success/failure |
| 10 | + - Conversation messages and agent responses |
| 11 | + - Session metadata and timing information |
| 12 | + - Error patterns and recovery attempts |
| 13 | +
|
| 14 | +Usage: |
| 15 | + Use as context provider functions or mix into steering handlers. |
| 16 | +""" |
| 17 | + |
| 18 | +import logging |
| 19 | +from datetime import datetime |
| 20 | +from typing import Any |
| 21 | + |
| 22 | +from ....hooks.events import AfterToolCallEvent, BeforeToolCallEvent |
| 23 | +from ..core.context import SteeringContext, SteeringContextCallback, SteeringContextProvider |
| 24 | + |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | + |
| 27 | + |
| 28 | +class LedgerBeforeToolCall(SteeringContextCallback[BeforeToolCallEvent]): |
| 29 | + """Context provider for ledger tracking before tool calls.""" |
| 30 | + |
| 31 | + def __init__(self) -> None: |
| 32 | + """Initialize the ledger provider.""" |
| 33 | + self.session_start = datetime.now().isoformat() |
| 34 | + |
| 35 | + def __call__(self, event: BeforeToolCallEvent, steering_context: SteeringContext, **kwargs: Any) -> None: |
| 36 | + """Update ledger before tool call.""" |
| 37 | + ledger = steering_context.data.get("ledger") or {} |
| 38 | + |
| 39 | + if not ledger: |
| 40 | + ledger = { |
| 41 | + "session_start": self.session_start, |
| 42 | + "tool_calls": [], |
| 43 | + "conversation_history": [], |
| 44 | + "session_metadata": {}, |
| 45 | + } |
| 46 | + |
| 47 | + tool_call_entry = { |
| 48 | + "timestamp": datetime.now().isoformat(), |
| 49 | + "tool_name": event.tool_use.get("name"), |
| 50 | + "tool_args": event.tool_use.get("arguments", {}), |
| 51 | + "status": "pending", |
| 52 | + } |
| 53 | + ledger["tool_calls"].append(tool_call_entry) |
| 54 | + steering_context.data.set("ledger", ledger) |
| 55 | + |
| 56 | + |
| 57 | +class LedgerAfterToolCall(SteeringContextCallback[AfterToolCallEvent]): |
| 58 | + """Context provider for ledger tracking after tool calls.""" |
| 59 | + |
| 60 | + def __call__(self, event: AfterToolCallEvent, steering_context: SteeringContext, **kwargs: Any) -> None: |
| 61 | + """Update ledger after tool call.""" |
| 62 | + ledger = steering_context.data.get("ledger") or {} |
| 63 | + |
| 64 | + if ledger.get("tool_calls"): |
| 65 | + last_call = ledger["tool_calls"][-1] |
| 66 | + last_call.update( |
| 67 | + { |
| 68 | + "completion_timestamp": datetime.now().isoformat(), |
| 69 | + "status": event.result["status"], |
| 70 | + "result": event.result["content"], |
| 71 | + "error": str(event.exception) if event.exception else None, |
| 72 | + } |
| 73 | + ) |
| 74 | + steering_context.data.set("ledger", ledger) |
| 75 | + |
| 76 | + |
| 77 | +class LedgerProvider(SteeringContextProvider): |
| 78 | + """Combined ledger context provider for both before and after tool calls.""" |
| 79 | + |
| 80 | + def context_providers(self, **kwargs: Any) -> list[SteeringContextCallback]: |
| 81 | + """Return ledger context providers with shared state.""" |
| 82 | + return [ |
| 83 | + LedgerBeforeToolCall(), |
| 84 | + LedgerAfterToolCall(), |
| 85 | + ] |
0 commit comments