Skip to content

feat(a2a): run middlewares in A2aAgent - #3695

Merged
jujn merged 1 commit into
agentscope-ai:mainfrom
guslegend0510:fix/issue-2612-a2a-middleware
Oct 21, 2026
Merged

jujn merged 1 commit into
agentscope-ai:mainfrom
guslegend0510:fix/issue-2612-a2a-middleware

Conversation

@guslegend0510

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT (main)

Description

Fixes #2612

A2A client 去掉弃用 API 的第三步(前两步是 #3663、#3674):A2aAgent 支持 MiddlewareBase。之前只能用已弃用的 Hook 扩展 A2A 调用。

另:#2612 的认领人提过一个 draft #2953,基于 A2A 0.3 的代码,9/3 之后没有更新,#3590 升级到 1.0 后已无法合入。本 PR 在当前 main 上实现

改动

  • A2aAgent.Builder 加 middleware(...) / middlewares(...),排序(order())和按 activePoints() 筛选都和 ReActAgent 一致。
  • onAgent:包住整次调用。call() 和 streamEvents() 都经过它,middleware 能看到并改写每个事件,包括 call() 返回的结果。
  • onReasoning:包住和远端 Agent 的交互。输入消息就是发给远端的消息(改写后远端收到的就是改写后的),事件是远端流式返回的文本、思考增量。tools 为空,options 为默认值。
  • 其他扩展点(onActing、onModelCall、onSystemPrompt、onAgentStateReady)不适用于远端 Agent,Javadoc 和文档里写明。
  • Hook(包括 Pre/Chunk/PostReasoning)和弃用的 stream() 行为不变;hook() 和 ReActAgent 一样不额外标弃用。
  • 文档:中英文 a2a.md 加了 middleware 的说明和示例。

测试

  • A2aAgentTest:
    • 两个 onAgent 按 order() 由外到内执行,外层改写的结果就是 call() 的返回值;
    • onReasoning 拿到发给远端的消息,改写后远端收到改写后的消息,并能看到远端流式返回的增量,streamEvents 也照常收到;
    • 只在 ON_ACTING 生效的 middleware 不会在 onAgent 执行。
    • 前两条用只保存不执行的骨架确认先失败。
  • 原有测试全部通过:A2A client 模块 127 个;Starter 端到端 15 个里 14 个通过、1 个跳过(等 fix(a2a): 取消任务后结束服务端执行,并释放服务端线程池 #3578 的中断用例);Nacos A2A 模块全部通过。
  • 本地(JDK 17,macOS):mvn -B -T1C clean verify 99 个模块全部通过(11,084 个测试,0 失败,200 跳过)。补丁覆盖率:行 47/49,分支 8/12(未覆盖的两行是批量添加的 middlewares(List))。

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn -B -T1C clean verify)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

A2aAgent only took hooks, which are deprecated for removal, so A2A calls
could not be extended with MiddlewareBase.

The builder now takes middlewares, ordered and filtered by active points
like in ReActAgent. onAgent wraps the whole call, so it sees and can
change every event of streamEvents() and the result of call().
onReasoning wraps the exchange with the remote agent: its input messages
are the messages sent, and its events are the text and thinking the
remote agent streams. The other extension points do not apply to a
remote agent. Hooks work as before.
@mintlify

mintlify Bot commented Oct 21, 2026

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit f747a9b.

Approve preview

@jujn
jujn merged commit 8cc6795 into agentscope-ai:main Oct 21, 2026
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A2aAgent 不支持 Middleware 机制, 仅能通过 deprecated Hook API 拦截事件流 — 请求支持 Middleware

3 participants