-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[1/n] Break Agent into AgentBase+Agent #1068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+111
−61
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,27 @@ | ||
from typing import Any, Generic | ||
|
||
from .agent import Agent | ||
from typing_extensions import TypeVar | ||
|
||
from .agent import Agent, AgentBase | ||
from .run_context import RunContextWrapper, TContext | ||
from .tool import Tool | ||
|
||
TAgent = TypeVar("TAgent", bound=AgentBase, default=AgentBase) | ||
|
||
|
||
class RunHooks(Generic[TContext]): | ||
class RunHooksBase(Generic[TContext, TAgent]): | ||
"""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: | ||
async def on_agent_start(self, context: RunContextWrapper[TContext], agent: TAgent) -> 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], | ||
agent: TAgent, | ||
output: Any, | ||
) -> None: | ||
"""Called when the agent produces a final output.""" | ||
|
@@ -28,16 +30,16 @@ async def on_agent_end( | |
async def on_handoff( | ||
self, | ||
context: RunContextWrapper[TContext], | ||
from_agent: Agent[TContext], | ||
to_agent: Agent[TContext], | ||
from_agent: TAgent, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will this cause type checking errors for customers that relied on the type of context? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess not if TAgent is well typed |
||
to_agent: TAgent, | ||
) -> None: | ||
"""Called when a handoff occurs.""" | ||
pass | ||
|
||
async def on_tool_start( | ||
self, | ||
context: RunContextWrapper[TContext], | ||
agent: Agent[TContext], | ||
agent: TAgent, | ||
tool: Tool, | ||
) -> None: | ||
"""Called before a tool is invoked.""" | ||
|
@@ -46,30 +48,30 @@ async def on_tool_start( | |
async def on_tool_end( | ||
self, | ||
context: RunContextWrapper[TContext], | ||
agent: Agent[TContext], | ||
agent: TAgent, | ||
tool: Tool, | ||
result: str, | ||
) -> None: | ||
"""Called after a tool is invoked.""" | ||
pass | ||
|
||
|
||
class AgentHooks(Generic[TContext]): | ||
class AgentHooksBase(Generic[TContext, TAgent]): | ||
"""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: | ||
async def on_start(self, context: RunContextWrapper[TContext], agent: TAgent) -> 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], | ||
agent: TAgent, | ||
output: Any, | ||
) -> None: | ||
"""Called when the agent produces a final output.""" | ||
|
@@ -78,8 +80,8 @@ async def on_end( | |
async def on_handoff( | ||
self, | ||
context: RunContextWrapper[TContext], | ||
agent: Agent[TContext], | ||
source: Agent[TContext], | ||
agent: TAgent, | ||
source: TAgent, | ||
) -> None: | ||
"""Called when the agent is being handed off to. The `source` is the agent that is handing | ||
off to this agent.""" | ||
|
@@ -88,7 +90,7 @@ async def on_handoff( | |
async def on_tool_start( | ||
self, | ||
context: RunContextWrapper[TContext], | ||
agent: Agent[TContext], | ||
agent: TAgent, | ||
tool: Tool, | ||
) -> None: | ||
"""Called before a tool is invoked.""" | ||
|
@@ -97,9 +99,16 @@ async def on_tool_start( | |
async def on_tool_end( | ||
self, | ||
context: RunContextWrapper[TContext], | ||
agent: Agent[TContext], | ||
agent: TAgent, | ||
tool: Tool, | ||
result: str, | ||
) -> None: | ||
"""Called after a tool is invoked.""" | ||
pass | ||
|
||
|
||
RunHooks = RunHooksBase[TContext, Agent] | ||
"""Run hooks when using `Agent`.""" | ||
|
||
AgentHooks = AgentHooksBase[TContext, Agent] | ||
"""Agent hooks for `Agent`s.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.