Files
agent-framework/python/agent_framework/_agents.py
T
Dmytro Struk 35c938fb5b .Python: Added Agent and AgentThread abstractions (#130)
* Added Agent and AgentThread classes

* Addressed PR feedback

* Converted Agent to protocol

* Removed thread deletion logic

* Small update

* Small updates to the Agent protocol
2025-07-07 17:47:14 +00:00

141 lines
4.6 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
from abc import ABC, abstractmethod
from collections.abc import AsyncIterable, Awaitable, Callable
from typing import Any, Protocol, TypeVar, runtime_checkable
from ._types import ChatMessage, ChatResponse, ChatResponseUpdate
TThreadType = TypeVar("TThreadType", bound="AgentThread")
# region AgentThread
class AgentThread(ABC):
"""Base class for agent threads."""
def __init__(self) -> None:
"""Initialize the agent thread."""
self._id: str | None = None # type: ignore
@property
def id(self) -> str | None:
"""Returns the ID of the current thread (if any)."""
return self._id
async def create(self) -> str | None:
"""Starts the thread and returns the thread ID."""
# If the thread ID is already set, we're done, just return the Id.
if self.id is not None:
return self.id
# Otherwise, create the thread.
self._id = await self._create()
return self.id
async def delete(self) -> None:
"""Ends the current thread."""
await self._delete()
self._id = None
async def on_new_message(
self,
new_message: ChatMessage,
) -> None:
"""Invoked when a new message has been contributed to the chat by any participant."""
# If the thread is not created yet, create it.
if self.id is None:
await self.create()
await self._on_new_message(new_message)
@abstractmethod
async def _create(self) -> str:
"""Starts the thread and returns the thread ID."""
raise NotImplementedError
@abstractmethod
async def _delete(self) -> None:
"""Ends the current thread."""
raise NotImplementedError
@abstractmethod
async def _on_new_message(
self,
new_message: ChatMessage,
) -> None:
"""Invoked when a new message has been contributed to the chat by any participant."""
raise NotImplementedError
# region Agent Protocol
@runtime_checkable
class Agent(Protocol):
"""A protocol for an agent that can be invoked."""
async def run(
self,
messages: str | ChatMessage | list[str | ChatMessage] | None = None,
*,
arguments: dict[str, Any] | None = None,
thread: AgentThread | None = None,
**kwargs: Any,
) -> ChatResponse:
"""Get a response from the agent.
This method returns the final result of the agent's execution
as a single ChatResponse object. The caller is blocked until
the final result is available.
Note: For streaming responses, use the run_stream method, which returns
intermediate steps and the final result as a stream of ChatResponseUpdate
objects. Streaming only the final result is not feasible because the timing of
the final result's availability is unknown, and blocking the caller until then
is undesirable in streaming scenarios.
Args:
messages: The message(s) to send to the agent.
arguments: Additional arguments to pass to the agent.
thread: The conversation thread associated with the message(s).
kwargs: Additional keyword arguments.
Returns:
An agent response item.
"""
...
async def run_stream(
self,
messages: str | ChatMessage | list[str | ChatMessage] | None = None,
*,
arguments: dict[str, Any] | None = None,
thread: AgentThread | None = None,
on_intermediate_message: Callable[[ChatMessage], Awaitable[None]] | None = None,
**kwargs: Any,
) -> AsyncIterable[ChatResponseUpdate]:
"""Run the agent as a stream.
This method will return the intermediate steps and final results of the
agent's execution as a stream of ChatResponseUpdate objects to the caller.
To get the intermediate steps of the agent's execution as fully formed messages,
use the on_intermediate_message callback.
Note: A ChatResponseUpdate object contains a chunk of a message.
Args:
messages: The message(s) to send to the agent.
arguments: Additional arguments to pass to the agent.
thread: The conversation thread associated with the message(s).
on_intermediate_message: A callback function to handle intermediate steps of the
agent's execution as fully formed messages.
kwargs: Additional keyword arguments.
Yields:
An agent response item.
"""
...