-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathlifecycle.py
105 lines (89 loc) · 2.88 KB
/
lifecycle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from typing import Any, Generic
from .agent import Agent
from .run_context import RunContextWrapper, TContext
from .tool import Tool, Action
class RunHooks(Generic[TContext]):
"""A class that receives callbacks on various lifecycle events in an agent run. Subclass and
override the methods you need.
"""
async def on_agent_start(
self, context: RunContextWrapper[TContext], agent: Agent[TContext]
) -> None:
"""Called before the agent is invoked. Called each time the current agent changes."""
pass
async def on_agent_end(
self,
context: RunContextWrapper[TContext],
agent: Agent[TContext],
output: Any,
) -> None:
"""Called when the agent produces a final output."""
pass
async def on_handoff(
self,
context: RunContextWrapper[TContext],
from_agent: Agent[TContext],
to_agent: Agent[TContext],
) -> None:
"""Called when a handoff occurs."""
pass
async def on_tool_start(
self,
context: RunContextWrapper[TContext],
agent: Agent[TContext],
action: Action,
) -> None:
"""Called before a tool is invoked."""
pass
async def on_tool_end(
self,
context: RunContextWrapper[TContext],
agent: Agent[TContext],
tool: Tool,
result: str,
) -> None:
"""Called after a tool is invoked."""
pass
class AgentHooks(Generic[TContext]):
"""A class that receives callbacks on various lifecycle events for a specific agent. You can
set this on `agent.hooks` to receive events for that specific agent.
Subclass and override the methods you need.
"""
async def on_start(self, context: RunContextWrapper[TContext], agent: Agent[TContext]) -> None:
"""Called before the agent is invoked. Called each time the running agent is changed to this
agent."""
pass
async def on_end(
self,
context: RunContextWrapper[TContext],
agent: Agent[TContext],
output: Any,
) -> None:
"""Called when the agent produces a final output."""
pass
async def on_handoff(
self,
context: RunContextWrapper[TContext],
agent: Agent[TContext],
source: Agent[TContext],
) -> None:
"""Called when the agent is being handed off to. The `source` is the agent that is handing
off to this agent."""
pass
async def on_tool_start(
self,
context: RunContextWrapper[TContext],
agent: Agent[TContext],
action: Action,
) -> None:
"""Called before a tool is invoked."""
pass
async def on_tool_end(
self,
context: RunContextWrapper[TContext],
agent: Agent[TContext],
tool: Tool,
result: str,
) -> None:
"""Called after a tool is invoked."""
pass