feat(adk): support returning directly based on tool result - #1237
Open
wqdqwqdqwdqwq wants to merge 2 commits into
Open
feat(adk): support returning directly based on tool result#1237wqdqwqdqwdqwq wants to merge 2 commits into
wqdqwqdqwdqwq wants to merge 2 commits into
Conversation
Add SetReturnDirectly, which lets a tool decide from its own execution result whether to stop the ReAct loop and return that result as the agent's final output. This is the runtime counterpart of the static ToolsConfig.ReturnDirectly. Previously the direct-return graph branch was only compiled when at least one tool was statically listed in ReturnDirectly, so a runtime decision could never take effect for an agent with no static configuration. The branch is now also compiled when the new ToolsConfig.AllowRuntimeReturnDirectly is set, which keeps agents using neither mechanism on their original topology. SetReturnDirectly reports an error when the agent has no direct-return path at all, instead of silently continuing the loop.
…tions The adk package plain errors with no package prefix; the three new errors carried an 'adk:' prefix unique in the package. Rework them to match, move the sentinel error next to ErrExceedMaxIterations, and use closure capture instead of a sentinel error through ProcessState for control flow.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Add
SetReturnDirectly, letting a tool decide from its own execution result whether the agent should stop and return that result.Enabled by a new
ToolsConfigfield:Closes #1187.
Why
Until now a tool could only influence the ReAct loop statically, by being listed in
ToolsConfig.ReturnDirectly. That cannot express "return directly for some arguments, keep the loop running for others" — a retriever whose recall is good enough this time, a query tool that hit a cache.The old
flow/agent/reactpackage already exposes exactly this asreact.SetReturnDirectly, so ADK was the one losing capability. Its doc comment even states the intended priority: "This setting has a higher priority than the AgentConfig.ToolReturnDirectly."There is also a gap that makes the naive version of this feature silently fail: the direct-return branch was only compiled when
len(ReturnDirectly) > 0, so an agent with no static configuration has no path to reach the end node, and a runtime request could never take effect.How
ReturnDirectlyorAllowRuntimeReturnDirectlyis set. Agents using neither keep their previous topology, so they pay nothing for this feature — no extra branch, no extra state read per tool iteration.ToolsConfig.ReturnDirectly/ChatModelAgentContext.ReturnDirectly. With parallel tool calls, the last request in an iteration wins.SetReturnDirectlyreturns an error rather than silently continuing the loop.*schema.Messageand*schema.AgenticMessagegraphs are covered; a tool cannot know which it runs under, so both concrete states are attempted.returnDirectlyAllowedstate flag is deliberately unexported so gob skips it and the checkpoint wire format is unchanged. It is therefore lost across a resume, and is refreshed from live config on every tool iteration.Test plan
go test -race ./adk/... -count=1golangci-lint run ./adk/...(v2.8.0, as pinned in CI) — 0 issuesTestChatModelParallelToolInterruptAndResumeandTestReturnDirectlyEventSentAfterResumeStreamableRuntool; AgenticMessage path; topology predicateWhat(中文)
新增
SetReturnDirectly,让 tool 根据自己的执行结果决定是否终止循环并把该结果作为最终输出,由新的ToolsConfig.AllowRuntimeReturnDirectly开启。Why(中文)
原先只能通过
ToolsConfig.ReturnDirectly静态指定,无法表达“这次参数下直接返回、其他情况继续循环”。老的flow/agent/react包本来就有react.SetReturnDirectly,ADK 反而缺失。另外 direct-return 分支此前仅在
len(ReturnDirectly) > 0时编入图,未做静态配置的 agent 根本没有通往结束节点的路径,运行时请求会静默失效。How(中文)
returnDirectlyAllowed特意使用非导出字段,gob 会跳过它,checkpoint 线上格式零变化;因此 resume 后会丢失,改为每轮从配置刷新。