Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/openagents/agents/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
OpenAgents agent classes and utilities.
"""

from .pydantic_ai_agent import PydanticAIAgentRunner
from .runner import AgentRunner
from .worker_agent import WorkerAgent
from .project_echo_agent import ProjectEchoAgentRunner
Expand Down
42 changes: 42 additions & 0 deletions src/openagents/agents/pydantic_ai_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from typing import Any, Optional, Type, TypeVar
from pydantic import BaseModel
from pydantic_ai import Agent
from .base import BaseAgentRunner # Replace with actual base class name

# Generic type for the Pydantic model response
ResultSchema = TypeVar("ResultSchema", bound=BaseModel)

class PydanticAIAgentRunner(BaseAgentRunner):
"""
Runner for PydanticAI agents to integrate with OpenAgents networks.
"""
def __init__(
self,
agent: Agent[Any, ResultSchema],
deps: Any = None,
result_type: Optional[Type[ResultSchema]] = None
):
self.agent = agent
self.deps = deps
self.result_type = result_type

async def handle_request(self, message: str, context: Optional[dict] = None) -> Any:
"""
Translates a network message into a PydanticAI run call.
"""
# PydanticAI's run method handles the internal LLM calls and tool loops
result = await self.agent.run(
message,
deps=self.deps,
# If the network provides history, map it here
message_history=context.get("history") if context else None
)

# Return the structured data back to the OpenAgents network
return {
"content": result.data,
"metadata": {
"usage": result.usage(),
"all_messages": result.all_messages()
}
}
Loading