A2A 时间戳扩展的服务端与客户端接入方式
该示例包为 A2A SDK 的 Python 实现提供时间戳扩展,通过包装执行器在服务端为消息与产物附加 UTC ISO 时间戳,并通过包装客户端工厂在请求中声明扩展并读取时间戳元数据。
# A2A 时间戳扩展显示套件 此套件执行 **时间戳扩展** 为Python中的A2A SDK. 该扩展展示了如何以模块化,脱钩,高度自动化的方式,用遵守时间戳丰富输出的A2A消息和文物. --- ## 架构套件结构( P) 软件包的结构将库关注与可运行的演示代码隔离: * **`timestamp_ext/`**:核心库包. * **`core.py`**: 包含核心元数据(`URI`, `TIMESTAMP_FIELD`)和主要数据 `TimestampExtension` 助学课. * **`server.py`**: 豪斯服务器侧装饰器( U)`_TimestampingAgentExecutor`, `_TimestampingEventQueue`) 与公众 `wrap_executor` 函数。 * **`client.py`**: 房屋客户端拦截器、装饰器和工厂包装器(`wrap_client_factory`, `client_interceptor`),它隔离了所有针对客户的Protobuf进口. * **`__init__.py`**: 仅展示用于一般用途的核心公共出口. * **`__main__.py`**:配置并发射Starlette ASGI网络服务器,在Echo代理上显示时间戳扩展. * **`agent_executor.py`**: 执行A2A代理执行器类(`EchoExecutor`处理和回响文本请求。 * **`test_client.py`**: 一个交互式的CLI测试客户端,演示如何包装客户端工厂并检查盖章的响应元数据. --- ## 使用指南 ### 1. 服务器系统设置 为了在您的 A2A 代理上启用时间戳扩展, 请在 `AgentCard` 并使用 `wrap_executor`: ```python from a2a.types.a2a_pb2 import AgentCard from timestamp_ext.core import TimestampExtension from timestamp_ext.server import wrap_executor # 1. Initialize the extension ext = TimestampExtension() # 2. Advertise support on the agent card card = AgentCard(...) ext.add_to_card(card=card) # 3. Decorate your agent executor handler = DefaultRequestHandler( agent_executor=wrap_executor(executor=MyExecutor(), ext=ext), agent_card=card, ... ) ``` 用这个包件,任何信息或文物 由 `MyExecutor` 当客户端请求时,会自动用当前的UTC ISO乘数戳印. ### 2. 客户端设置 要向服务器请求扩展并读取时间戳, 请将您 `ClientFactory` 使用 `wrap_client_factory`: ```python from a2a.client import ClientConfig, ClientFactory from timestamp_ext.core import TimestampExtension from timestamp_ext.client import wrap_client_factory # 1. Initialize the extension ext = TimestampExtension() # 2. Wrap the client factory factory = wrap_client_factory( factory=ClientFactory(config=ClientConfig(httpx_client=httpx_client)), ext=ext ) client = factory.create(card=card) ``` 包裹的工厂安装一个客户端截取器,自动添加 `A2A-Extensions: <uri>` 每一封发送的电话和邮票客户端信件的页眉。 --- ## 运行显示大小写测试( T) ### 先决条件 - **Python 键**:版本为3.10或更高. ### 快速启动 1. **建立虚拟环境并安装依赖** 创建和激活虚拟环境,然后安装所需的软件包: ```bash python -m venv .venv source .venv/bin/activate pip install -r requirements.txt ``` 2. **启动服务器** 在端口本地运行 A2A 代理服务器 `9998`: ```bash python __main__.py ``` 3. **运行交互式演示客户端** 在单独的终端中,激活虚拟环境并运行客户端与服务器互动: ```bash source .venv/bin/activate python test_client.py ``` ### 运行集成测试 运行 end-to-end 集成测试( 它在子进程中旋转服务器并执行客户端查询) : ```bash pytest -s -v ```
查看原始来源