-
Notifications
You must be signed in to change notification settings - Fork 1
automatically set capabilities for wrapped agents #38
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c67ae54
add design
pmeier 19d794a
update design with resolved questions and remaining open item
pmeier 7883439
Implement auto-extraction of AG-UI agent capabilities
pmeier 80c7047
manual cleanup
pmeier 261e952
more cleanup
pmeier 765072e
Merge branch 'main' into auto-capabilities
pmeier 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import abc | ||
| import dataclasses | ||
| import textwrap | ||
| import uuid | ||
| from collections.abc import AsyncIterator | ||
|
|
@@ -13,6 +14,7 @@ | |
|
|
||
| if TYPE_CHECKING: | ||
| import agno.agent | ||
| import agno.tools | ||
| import pydantic_ai | ||
|
|
||
| from _ravnar.schema import QuickPrompt | ||
|
|
@@ -115,7 +117,16 @@ async def run(self, input: ag_ui.core.RunAgentInput) -> AsyncIterator[ag_ui.core | |
| yield ta.validate_json(sse.data) | ||
|
|
||
|
|
||
| class PydanticAiAgentWrapper(_AgentBase): | ||
| @dataclasses.dataclass | ||
| class _PydanticAiDynamicCapabilities: | ||
| tools: list[ag_ui.core.Tool] | ||
| reasoning_supported: bool | None | ||
| approvals: bool | None | ||
| image_output: bool | None | ||
| structured_output: bool | None | ||
|
|
||
|
|
||
| class PydanticAiAgentWrapper(Agent): | ||
| """Pydantic AI agent wrapper""" | ||
|
|
||
| def __init__( | ||
|
|
@@ -127,19 +138,125 @@ def __init__( | |
| ) -> None: | ||
| self._agent = agent | ||
|
|
||
| if capabilities is None: | ||
| capabilities = ag_ui.core.AgentCapabilities( | ||
| identity=ag_ui.core.IdentityCapabilities(name=agent.name), | ||
| transport=ag_ui.core.TransportCapabilities(streaming=True), | ||
| ) | ||
| self._capabilities: ag_ui.core.AgentCapabilities | ||
| if capabilities is not None: | ||
| self._capabilities = capabilities | ||
|
|
||
| super().__init__(capabilities=capabilities, quick_prompts=quick_prompts) | ||
| if quick_prompts is None: | ||
| quick_prompts = [] | ||
| self._quick_prompts = quick_prompts | ||
|
|
||
| async def setup(self) -> None: | ||
| if hasattr(self, "_capabilities"): | ||
| return | ||
|
|
||
| self._capabilities = await self.extract_capabilities(self._agent) | ||
|
|
||
| def run(self, input: ag_ui.core.RunAgentInput) -> AsyncIterator[ag_ui.core.Event]: | ||
| from pydantic_ai.ui.ag_ui import AGUIAdapter | ||
|
|
||
| return AGUIAdapter(agent=self._agent, run_input=input, accept="text/event-stream").run_stream() # type: ignore[return-value] | ||
|
|
||
| def get_capabilities(self) -> ag_ui.core.AgentCapabilities: | ||
| """The capabilities of the agent.""" | ||
| return self._capabilities | ||
|
|
||
| def get_quick_prompts(self) -> list[QuickPrompt]: | ||
| """The quick prompts of the agent.""" | ||
| return self._quick_prompts | ||
|
|
||
| @staticmethod | ||
| async def extract_capabilities( | ||
| agent: pydantic_ai.Agent, *, ctx: pydantic_ai.RunContext | None = None | ||
| ) -> ag_ui.core.AgentCapabilities: | ||
| import pydantic_ai.models | ||
| from pydantic_ai.usage import RunUsage | ||
|
|
||
| capabilities = ag_ui.core.AgentCapabilities( | ||
| identity=ag_ui.core.IdentityCapabilities( | ||
| name=agent.name, | ||
| description=agent.description, | ||
| type="pydantic-ai", | ||
| ), | ||
| transport=ag_ui.core.TransportCapabilities(streaming=True), | ||
| tools=ag_ui.core.ToolsCapabilities( | ||
| supported=True, | ||
| client_provided=True, | ||
| ), | ||
| ) | ||
|
|
||
| if ctx is None and isinstance(agent.model, pydantic_ai.models.Model): | ||
| ctx = pydantic_ai.RunContext(deps=None, model=agent.model, usage=RunUsage()) | ||
| if ctx is not None: | ||
| dynamic_capabilities = await PydanticAiAgentWrapper._extract_dynamic_capabilities(agent, ctx=ctx) | ||
|
|
||
| assert capabilities.tools is not None | ||
| capabilities.tools.items = dynamic_capabilities.tools | ||
| if dynamic_capabilities.reasoning_supported is not None: | ||
| capabilities.reasoning = ag_ui.core.ReasoningCapabilities( | ||
| supported=dynamic_capabilities.reasoning_supported | ||
| ) | ||
| if dynamic_capabilities.approvals is not None: | ||
| capabilities.human_in_the_loop = ag_ui.core.HumanInTheLoopCapabilities( | ||
| approvals=dynamic_capabilities.approvals | ||
| ) | ||
| if dynamic_capabilities.image_output is not None: | ||
| capabilities.multimodal = ag_ui.core.MultimodalCapabilities( | ||
| output=ag_ui.core.MultimodalOutputCapabilities(image=dynamic_capabilities.image_output) | ||
| ) | ||
| if dynamic_capabilities.structured_output is not None: | ||
| capabilities.output = ag_ui.core.OutputCapabilities( | ||
| structured_output=dynamic_capabilities.structured_output | ||
| ) | ||
|
|
||
| return capabilities | ||
|
|
||
| @staticmethod | ||
| async def _extract_dynamic_capabilities( | ||
| agent: pydantic_ai.Agent, *, ctx: pydantic_ai.RunContext | ||
| ) -> _PydanticAiDynamicCapabilities: | ||
| tools = [ | ||
| ag_ui.core.Tool( | ||
| name=(td := tool.tool_def).name, | ||
| description=td.description or "", | ||
| parameters=td.parameters_json_schema, | ||
| ) | ||
| for toolset in agent.toolsets | ||
| for tool in (await toolset.get_tools(ctx)).values() | ||
| ] | ||
|
|
||
| reasoning_supported: bool | None = None | ||
| approvals: bool | None = None | ||
| image_output: bool | None = None | ||
|
|
||
| if agent.root_capability is not None: | ||
| from pydantic_ai.capabilities import HandleDeferredToolCalls, ImageGeneration, Thinking | ||
|
|
||
| for capability in agent.root_capability.capabilities: | ||
| if isinstance(capability, Thinking): | ||
| reasoning_supported = True | ||
| elif isinstance(capability, HandleDeferredToolCalls): | ||
| approvals = True | ||
| elif isinstance(capability, ImageGeneration): | ||
| image_output = True | ||
|
|
||
| if not isinstance(agent.output_type, type): | ||
| structured_output = None | ||
| elif issubclass(agent.output_type, str): | ||
| structured_output = False | ||
| elif issubclass(agent.output_type, pydantic.BaseModel): | ||
| structured_output = True | ||
| else: | ||
| structured_output = None | ||
|
|
||
| return _PydanticAiDynamicCapabilities( | ||
| tools=tools, | ||
| reasoning_supported=reasoning_supported, | ||
| approvals=approvals, | ||
| image_output=image_output, | ||
| structured_output=structured_output, | ||
| ) | ||
|
|
||
|
|
||
| class AgnoAgentWrapper(_AgentBase): | ||
| """Agno agent wrapper""" | ||
|
|
@@ -154,14 +271,75 @@ def __init__( | |
| self._agent = agent | ||
|
|
||
| if capabilities is None: | ||
| capabilities = ag_ui.core.AgentCapabilities( | ||
| identity=ag_ui.core.IdentityCapabilities(name=agent.name), | ||
| transport=ag_ui.core.TransportCapabilities(streaming=True), | ||
| ) | ||
| capabilities = self.extract_capabilities(agent) | ||
|
|
||
| super().__init__(capabilities=capabilities, quick_prompts=quick_prompts) | ||
|
|
||
| def run(self, input: ag_ui.core.RunAgentInput) -> AsyncIterator[ag_ui.core.Event]: | ||
| from agno.os.interfaces.agui.router import run_agent | ||
|
|
||
| return run_agent(self._agent, input) # type: ignore[return-value] | ||
|
|
||
| @staticmethod | ||
| def extract_capabilities(agent: agno.agent.Agent) -> ag_ui.core.AgentCapabilities: | ||
|
Member
Author
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've opened agno-agi/agno#8127 to hopefully push this upstream. |
||
| from agno.tools import Function, Toolkit | ||
|
|
||
| tools: list[ag_ui.core.Tool] | None = None | ||
| approvals: bool | None = None | ||
| if isinstance(agent.tools, list): | ||
| tools = [] | ||
| approvals = False | ||
| for tool in agent.tools: | ||
| functions: list[Function] | ||
| if isinstance(tool, Toolkit): | ||
| functions = [fn for fn in tool.functions.values() if isinstance(fn, Function)] | ||
| elif isinstance(tool, Function): | ||
| functions = [tool] | ||
| elif callable(tool): | ||
| functions = [Function.from_callable(tool)] | ||
| else: | ||
| continue | ||
|
|
||
| for fn in functions: | ||
| tools.append( | ||
| ag_ui.core.Tool( | ||
| name=fn.name, | ||
| description=fn.description or "", | ||
| parameters=fn.parameters, | ||
| ) | ||
| ) | ||
| if fn.requires_confirmation or fn.requires_user_input: | ||
| approvals = True | ||
|
|
||
| return ag_ui.core.AgentCapabilities( | ||
| identity=ag_ui.core.IdentityCapabilities( | ||
| name=agent.name, | ||
| description=agent.description, | ||
| metadata=agent.metadata, | ||
| type="agno", | ||
| ), | ||
| transport=ag_ui.core.TransportCapabilities(streaming=True), | ||
| tools=ag_ui.core.ToolsCapabilities( | ||
| supported=True, | ||
| items=tools, | ||
| client_provided=False, | ||
|
Member
Author
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. |
||
| ), | ||
| human_in_the_loop=ag_ui.core.HumanInTheLoopCapabilities(approvals=approvals) | ||
| if approvals is not None | ||
| else None, | ||
| output=ag_ui.core.OutputCapabilities(structured_output=structured_output) | ||
| if (structured_output := agent.structured_outputs is True or agent.output_schema is not None) | ||
| else None, | ||
| reasoning=ag_ui.core.ReasoningCapabilities(supported=agent.reasoning), | ||
| multi_agent=ag_ui.core.MultiAgentCapabilities( | ||
| supported=True, | ||
| sub_agents=[ | ||
| ag_ui.core.SubAgentInfo( | ||
| name=agent.reasoning_agent.name, | ||
| description=agent.reasoning_agent.description, | ||
| ) | ||
| ], | ||
| ) | ||
| if agent.reasoning_agent is not None and agent.reasoning_agent.name is not None | ||
| else None, | ||
| ) | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've opened pydantic/pydantic-ai#5686 to hopefully push this upstream.