AS AgentScopeAPI 手册 / API Reference
类class agentscope.agent

A2AAgent

class A2AAgent
源码Source agentscope/agent/_a2a_agent.py:189–661

A2A 智能体,驱动智能体的推理、工具调用与会话执行。位于 agentscope.agent 包。核心成员包括 aclose、reply_stream、reply、compress_context、observe。

This class intentionally provides Agent-like interaction methods without inheriting :class:agentscope.agent.Agent. A local Agent owns a model, toolkit, state, and reasoning loop; this adapter delegates those concerns to the remote A2A server and owns only the remote conversation (context_id) and the Task the next message continues (task_id), both held in :class:agentscope.state.A2AAgentState.

Each A2A Part of a response becomes one content block: text Parts become :class:~agentscope.message.TextBlock, raw byte and URL Parts become :class:~agentscope.message.DataBlock.

The adapter owns its A2A client and closes it in :meth:aclose, so it is single-use: once closed it cannot be reopened.

构造器摘要Constructor Summary

构造器Constructor说明Description
def __init__(agent_card: AgentCard, *, client: Client | None = None, state: A2AAgentState | None = None) -> None
初始化 `A2AAgent` 实例。
Initialize the A2A agent adapter.

方法摘要Method Summary

方法Method说明Description
async def __aenter__() -> A2AAgentasync
进入异步上下文并返回自身。
Enter the asynchronous context manager.
async def __aexit__(*_: Any) -> Noneasync
退出异步上下文并清理资源。
Close the owned client when leaving the context manager.
async def aclose() -> Noneasync
返回 `aclose` 结果。
Close the owned A2A client. Repeated calls are safe.
async def reply_stream(inputs: Msg | list[Msg] | None = None, yield_final_msg: bool = False) -> AsyncGenerator[AgentEve…async
流式回复输入消息,并逐个产出事件或消息。
Send input and stream the remote reply as AgentScope events.
async def reply(inputs: Msg | list[Msg] | None = None) -> Msgasync
回复输入消息并返回最终消息。
Send input and return the final assistant message.
async def compress_context(*args: Any, **kwargs: Any) -> Noneasync
在上下文超过阈值时压缩历史消息。
Do nothing because the remote A2A server owns its context.
async def observe(msgs: Msg | list[Msg] | None = None) -> Noneasync
接收外部观察消息并写入上下文。
Cache messages to send ahead of the next reply's own input.

构造器详情Constructor Detail

A2AAgent

def __init__(agent_card: AgentCard, *, client: Client | None = None, state: A2AAgentState | None = None) -> None

初始化 A2AAgent 实例。

Initialize the A2A agent adapter.

参数Parameters
名称Name类型Type说明Description
agent_cardAgentCard智能体卡片(类型 `AgentCard`)。The remote Agent Card, used both to identify the peer (its `name` becomes this adapter's `name`) and to select a transport. The SDK picks the newest protocol version the card advertises for the chosen binding, falling back to its A2A 0.3 compatibility transport when that is all the peer offers.
clientkw默认default NoneClient | None客户端(类型 `Client | None`)。An official SDK client, e.g. one configured for gRPC or with custom auth. If omitted, a streaming client is built from the card, which then requires a `JSONRPC` or `HTTP+JSON` interface. The adapter owns the client either way and closes it in :meth:`aclose`.
statekw默认default NoneA2AAgentState | None状态对象(类型 `A2AAgentState | None`)。An existing state to resume, e.g. one saved from an earlier adapter, so its `context_id` continues the same remote conversation. A fresh state is created when omitted.
源码Source agentscope/agent/_a2a_agent.py:207–261

方法详情Method Detail

__aenter__

async def __aenter__() -> A2AAgent
async

进入异步上下文并返回自身。

Leaving the block closes the owned client, and nothing reopens it, so an adapter can only be entered before it is closed.

返回Returns A2AAgent
返回A2AAgent。Returns A2A agent.
源码Source agentscope/agent/_a2a_agent.py:263–271

__aexit__

async def __aexit__(*_: Any) -> None
async

退出异步上下文并清理资源。

Close the owned client when leaving the context manager.

参数Parameters
名称Name类型Type说明Description
_Any组件(类型 `Any`)。The component.
返回Returns None
无返回值。No value is returned.
源码Source agentscope/agent/_a2a_agent.py:273–275

aclose

async def aclose() -> None
async

返回 aclose 结果。

Close the owned A2A client.

返回Returns None
无返回值。No value is returned.
源码Source agentscope/agent/_a2a_agent.py:277–281

reply_stream

async def reply_stream(inputs: Msg | list[Msg] | None = None, yield_final_msg: bool = False) -> AsyncGenerator[AgentEvent | Msg, None]
async

流式回复输入消息,并逐个产出事件或消息。

Send input and stream the remote reply as AgentScope events.

参数Parameters
名称Name类型Type说明Description
inputs默认default NoneMsg | list[Msg] | None输入消息(类型 `Msg | list[Msg] | None`)。The messages to send, preceded by the observed ones.
yield_final_msg默认default Falseboolyield 最终消息(类型 `bool`)。Whether to yield the final reply message after the events.
返回Returns AsyncGenerator[AgentEvent | Msg, None]
以异步流的形式返回AgentEvent|消息,None。`AgentEvent | Msg`:
源码Source agentscope/agent/_a2a_agent.py:283–303

reply

async def reply(inputs: Msg | list[Msg] | None = None) -> Msg
async

回复输入消息并返回最终消息。

Send input and return the final assistant message.

参数Parameters
名称Name类型Type说明Description
inputs默认default NoneMsg | list[Msg] | None输入消息(类型 `Msg | list[Msg] | None`)。The messages to send, preceded by the observed ones.
返回Returns Msg
返回消息。`Msg`:
源码Source agentscope/agent/_a2a_agent.py:305–322

compress_context

async def compress_context(*args: Any, **kwargs: Any) -> None
async

在上下文超过阈值时压缩历史消息。

The arguments are accepted for interface compatibility with :class:agentscope.agent.Agent.

参数Parameters
名称Name类型Type说明Description
argsAny参数(类型 `Any`)。The args.
kwargsAny关键字参数(类型 `Any`)。The kwargs.
返回Returns None
无返回值。No value is returned.
源码Source agentscope/agent/_a2a_agent.py:630–644

observe

async def observe(msgs: Msg | list[Msg] | None = None) -> None
async

接收外部观察消息并写入上下文。

Cache messages to send ahead of the next reply's own input.

参数Parameters
名称Name类型Type说明Description
msgs默认default NoneMsg | list[Msg] | None消息列表(类型 `Msg | list[Msg] | None`)。The messages to remember. `None` is ignored.
返回Returns None
无返回值。No value is returned.
源码Source agentscope/agent/_a2a_agent.py:646–661
基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.