Skip to content

feat(ag-ui): support frontend tools - #5122

Open
deka wants to merge 14 commits into
docker:mainfrom
la-fabrique:feat/ag-ui-frontend-tools
Open

deka wants to merge 14 commits into
docker:mainfrom
la-fabrique:feat/ag-ui-frontend-tools

Conversation

@deka

@deka deka commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Closes #5119: frontend tools for serve ag-ui, step 4 of #5066 (step 1: #5067, step 2: #5087 / #5091, step 3: #5103).

What changes

  • Offered tools: RunAgentInput.tools are offered to the thread's agent (entry agent and its handoff targets), not to sub-agents or background agents. A name that is already a tool of those agents, or an invalid definition, is rejected with 400.
  • The call: a frontend call streams TOOL_CALL_START, TOOL_CALL_ARGS and TOOL_CALL_END with no TOOL_CALL_RESULT. Server calls in the same batch run as usual. The run then ends with RUN_FINISHED, outcome: {type: "success", pendingToolCallIds: [...]}, and the model is not called again.
  • The result: the next run sends tool messages with the call's toolCallId. They are stored as tool results (error maps to an error result), the run needs no new user message, and the model continues the turn.
    • A result may sit right after its call's message rather than at the end of the history, which is where @ag-ui/client puts it.
    • Results the thread already holds are history.
    • An unknown call, or one that is no longer pending, gets a 400 and the state is unchanged.
    • A pending call left unanswered (for example, the user sends a new message instead) gets an error result, which is streamed and logged without its content.
  • With an interrupt: a turn that mixes a frontend call with a tool approval or an elicitation pauses on the interrupt, and the resume carries the tool messages. If the interrupt is cancelled after it expires, the frontend call stays pending unless it is answered.
  • Wire IDs: the thread stores the wire ID of each call, so a pending call can be answered after a restart. Wire IDs are unique on the thread: a call ID the model reuses in a later run (the XML fallback's call_0) gets a suffix (call_0-2). @ag-ui/client merges calls that share an ID, and it would otherwise leave the thread stuck on a duplicate result. Records name the agent of each call, since every agent numbers its own calls (a sub-agent's call_0 doesn't hide the root's pending call_0). A failed or canceled run stores its calls too, because the client keeps the results it got.
  • Safety (pkg/runtime/toolexec):
    • The server never runs a frontend call: the safety mode doesn't ask, deny or skip hooks for it, and it goes to the client without an approval prompt (approval_source: client_executed).
    • Permission deny rules and pre_tool_use hooks still apply, under every policy.
    • The dispatcher decides client calls before the other calls of a batch. A refused frontend call streams its error before an approval pauses the run, and the client gets its calls whichever call asks first.
  • Docs:
    • docs/features/ag-ui/index.md: new "Frontend tools" section, plus updates to tool safety, input validation and limitations.
    • docs/configuration/permissions/index.md and docs/configuration/hooks/index.md: client-executed calls.

Tests

  • Event sequences: exact sequence for one call, two calls, an error result, and a frontend call next to a server call.

  • Answer placement: an answer placed before a later call's message (the reference client's order), then a new user turn after it.

  • Reused call IDs: three rounds through the Go reference client, in the same turn and across turns, get c1, c1-2 and c1-3, and the model gets every answer.

  • With an interrupt: a tool approval in the same turn (answered and unanswered), a call refused before an approval, an elicitation in the same turn, and cancelling an expired interrupt.

  • Invalid answers: each one gets a 400, the state is unchanged and a valid retry works. A run that fails after storing results can be retried, and so can a write of the results that fails part way.

  • Persistence and scope: answer after a restart (SQLite store), with the call ID reused after the restart (c1-2); a failed run whose suffixed call the client resends; a sub-agent reusing the root's call ID; reused call_0, policy coverage (every mode leaves the call to the client), and no frontend tools for sub-agents.

  • Interop: the Go reference client (ag-ui/sdks/community/go) replays the stream and its pending calls. It places results after their call's message, as @ag-ui/client does.

  • Runtime and dispatcher: unit tests for client-executed calls (pkg/runtime/client_tools_test.go, pkg/runtime/toolexec/client_executed_test.go).

  • Manual check against the JS reference client, @ag-ui/client 1.0.5 (HttpAgent):

    • a frontend call next to a server call: pending ["c1"], the answered run finishes with "Booked", and a resend of the whole history gets a 400;
    • a call ID reused over three rounds, in one turn and across turns: every round is answered.

    The second case is committed as examples/ag-ui/replay (pinned with a lockfile, scripted model, run by hand with AGUI_TS_REPLAY=1 go test -run TestTSReplay ./pkg/agui/, not in CI); with the wire ID suffix disabled it fails. The demo page examples/ag-ui/index.html now offers frontend tools (pick_date, confirm, get_browser_context) and answers pending calls; checked in a browser against a scripted model, including a call left unanswered.

    It found two bugs, both fixed here and covered by the tests above. The client inserts a result after its call's message rather than at the end. It also merges calls that share an ID, so a per-run wire ID stuck the thread.

  • Mutations checked (fix disabled, test fails):

    • the old parser: the interop test and the answer-placement test;
    • no thread-wide seeding, or records replaced per run: the reused-ID test;
    • records keyed without the agent: the sub-agent test;
    • no save on a failed run: the failed-run test;
    • no client-first dispatch: the denied-call test;
    • unansweredCalls counting frontend calls: the elicitation test;
    • no client-tool turn end: 8 tests.

Checks

  • task isn't installed here, so I ran the equivalent commands by hand.
    • go build ./... passes, on the final commit like the checks below.
    • go test ./... fails only in cmd/root (TestResume*, TestOpenStore*) and pkg/programresume, with "unsafe instance path ownership or permissions" from the dev environment. The PR touches neither area.
    • go test -race passes on pkg/agui, pkg/runtime, pkg/session and pkg/runtime/toolexec.
  • golangci-lint v2.14.0: 0 issues; go run ./lint .: no offenses; go mod tidy --diff clean; GOOS=windows go vet OK.
  • task check-ts equivalent: no diff in sdk/typescript (run before the last fixups, which change no API type).

Out of scope

  • Records grow with the thread: the thread keeps one small record (wire ID, agent, call ID) per streamed tool call. They make wire IDs unique and are not pruned (listed in the docs' limitations).
  • Restart during an interrupt: interrupts are not kept across a restart. A frontend call in a parked turn then gets the "unanswered" error result.
  • Tool definitions: they are not persisted; the client sends them on every run, as the protocol expects.
  • Sub-agents and background agents don't get frontend tools.
  • Not covered by this step: shared state, and snapshots before RUN_FINISHED. They remain follow-ups under feat: AG-UI protocol support, step 1 (serve ag-ui streaming chat) #5066.

@deka
deka force-pushed the feat/ag-ui-frontend-tools branch from f22156c to 8bb9b1a Compare December 4, 2026 07:57
@aheritier aheritier added area/agent For work that has to do with the general agent loop/agentic features of the app area/api For features/issues/fixes related to the usage of the cagent API area/docs Documentation changes area/runtime Runtime engine, agent loop execution, tool dispatch, loop detection area/sessions For features/issues/fixes related to session lifecycle (resume, persistence, export) area/tools For features/issues/fixes related to the usage of built-in and MCP tools kind/feat PR adds a new feature (maps to feat:). Use on PRs only. labels Dec 4, 2026
@deka
deka force-pushed the feat/ag-ui-frontend-tools branch from 8bb9b1a to 3f4288d Compare December 4, 2026 08:07
@deka
deka marked this pull request as ready for review December 4, 2026 09:05
@deka
deka requested a review from a team as a code owner December 4, 2026 09:06
@trungutt
trungutt self-requested a review December 4, 2026 11:44

@aheritier aheritier left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8 blocking issues before merging: frontend calls can diverge from what hooks approved, and some handoff, history, and retry paths leave the client and server out of sync.

Open questions: #5119 asks for replay through the TypeScript reference client. The PR reports a manual check, but the script isn’t included; can you add a reproducible replay through the actual SDK alongside the Go interop tests?

Comment thread pkg/agui/translator.go Resolved
Comment thread pkg/runtime/toolexec/dispatcher.go Resolved
Comment thread pkg/agui/frontend_tools.go Resolved
Comment thread pkg/agui/server.go Resolved
Comment thread pkg/agui/frontend_tools.go Resolved
Comment thread pkg/agui/server.go Resolved
Comment thread pkg/agui/frontend_tools.go Resolved
Comment thread pkg/agui/server.go Resolved
@aheritier
aheritier marked this pull request as draft December 4, 2026 12:44
@aheritier

Copy link
Copy Markdown
Collaborator

Please mark this PR ready for review once you’ve addressed the requested changes. Thanks!

@deka
deka force-pushed the feat/ag-ui-frontend-tools branch from 8ca9c6c to a956eff Compare December 4, 2026 13:09
deka added a commit to la-fabrique/docker-agent that referenced this pull request Dec 4, 2026
Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
deka added a commit to la-fabrique/docker-agent that referenced this pull request Dec 4, 2026
Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
deka added a commit to la-fabrique/docker-agent that referenced this pull request Dec 4, 2026
…ing targets

Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
deka added a commit to la-fabrique/docker-agent that referenced this pull request Dec 4, 2026
Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
deka added a commit to la-fabrique/docker-agent that referenced this pull request Dec 4, 2026
…ords

Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
deka added a commit to la-fabrique/docker-agent that referenced this pull request Dec 4, 2026
Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
deka added a commit to la-fabrique/docker-agent that referenced this pull request Dec 4, 2026
Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
deka added a commit to la-fabrique/docker-agent that referenced this pull request Dec 4, 2026
Addresses review comments docker#5122 (comment) and docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
@deka
deka force-pushed the feat/ag-ui-frontend-tools branch from 3ffe274 to 483a3ea Compare December 4, 2026 13:44
@deka

deka commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @aheritier 🙏 Addressed in fcb2fb3..ba82849, details in the threads.
The @ag-ui/client replay is committed in c39cc82 as examples/ag-ui/replay (pinned 1.0.5, scripted model, run by hand with AGUI_TS_REPLAY=1, not in CI), and the demo page now offers frontend tools (ba82849).

@deka
deka marked this pull request as ready for review December 4, 2026 14:04
@deka
deka requested a review from aheritier December 4, 2026 14:06

@aheritier aheritier left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three blocking issues remain: a frontend call can still expose arguments before redaction, upgraded threads can reuse a wire ID the client already knows, and an answer-only run can route away from the agent it should resume.

Comment thread pkg/agui/translator.go Resolved
Comment thread pkg/agui/frontend_tools.go Resolved
Comment thread pkg/agui/server.go Resolved
@aheritier
aheritier marked this pull request as draft December 4, 2026 15:12
deka added a commit to la-fabrique/docker-agent that referenced this pull request Dec 4, 2026
…ing targets

Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
deka added a commit to la-fabrique/docker-agent that referenced this pull request Dec 4, 2026
Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
deka added a commit to la-fabrique/docker-agent that referenced this pull request Dec 4, 2026
…ords

Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
@deka
deka force-pushed the feat/ag-ui-frontend-tools branch from ba82849 to bfc89d1 Compare December 4, 2026 15:20
deka added a commit to la-fabrique/docker-agent that referenced this pull request Dec 4, 2026
Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
deka added a commit to la-fabrique/docker-agent that referenced this pull request Dec 4, 2026
Addresses review comments docker#5122 (comment) and docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
deka added a commit to la-fabrique/docker-agent that referenced this pull request Dec 4, 2026
A malformed name the runtime repairs before dispatch is now repaired before its partial call is emitted, so a frontend call's raw arguments wait for the hooks and the client sees only the canonical name.

Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
deka added a commit to la-fabrique/docker-agent that referenced this pull request Dec 4, 2026
A thread stored before call records gets its calls' possible wire IDs (X, X-2, ...) reserved, so a new call reusing a runtime ID never takes an ID the client already holds.

Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
@deka

deka commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @aheritier 🙏 Addressed in 88267fa..7a0867d, details in the threads.

@deka
deka marked this pull request as ready for review December 4, 2026 15:47
@deka
deka requested a review from aheritier December 4, 2026 15:49
deka added 14 commits December 5, 2026 07:24
A client offers tools in RunAgentInput.tools; the model calls them like any
other tool and the application runs them. Step 4 of AG-UI support (docker#5119).

- The tools of a run are offered to the thread's agent (entry agent and its
  handoff targets), not to sub-agents. A name that is already a tool of
  those agents is rejected with 400.
- A frontend call streams START, ARGS and END without a result; the run ends
  with RUN_FINISHED, outcome success and pendingToolCallIds.
- The next run sends tool messages, stored as tool results (error set maps
  to an error result); it needs no new user message. A result may sit after
  its call's message rather than at the end, as @ag-ui/client places it. A pending
  call a run leaves unanswered gets an error result, streamed and logged.
- A turn mixing a frontend call and an approval pauses on the interrupt; the
  resume carries the tool messages. Cancelling an expired interrupt leaves
  the frontend call pending unless it is answered.
- The thread stores the wire IDs of its calls, so a pending call can be
  answered after a restart. Wire IDs are unique on the thread: a call ID the
  model reuses in a later run (call_0) gets a suffix, since @ag-ui/client
  merges calls that share an ID. Records name the agent of each call, as
  every agent numbers its own, and a failed run stores its calls too.
- The server runs no frontend call: the safety mode neither asks, denies nor
  skips hooks for it. Permission deny rules and pre_tool_use hooks apply.
- The dispatcher decides client calls before the other calls of a batch:
  a refused frontend call streams its error before an approval pauses the
  run, and the client gets its calls whichever call asks first.

Closes docker#5119

Signed-off-by: deka <dimitri.rayer@gmail.com>
- Store frontend tool results first on resume, so a failed store leaves
  no grant persisted and no elicitation answer delivered, and the same
  resume replayed goes through.
- Reject frontend tool names that collide with the agent's catalog tools
  or with the reserved structured output tool.
- Compile tool parameter schemas at the boundary (invalid schemas and
  remote $refs get a 400) and accept an empty schema as no parameters,
  as AG-UI specifies.
- Document that ask rules don't prompt for client-executed calls and
  that allow/ask rules skip pre_tool_use hooks for them.

Signed-off-by: deka <dimitri.rayer@gmail.com>
Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
…ing targets

Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
…ords

Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
Addresses review comments docker#5122 (comment) and docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
A manual check in examples/ag-ui/replay, pinned to @ag-ui/client 1.0.5 and run with AGUI_TS_REPLAY=1; it is skipped otherwise. The examples walk skips node_modules.

Signed-off-by: deka <dimitri.rayer@gmail.com>
Signed-off-by: deka <dimitri.rayer@gmail.com>
A malformed name the runtime repairs before dispatch is now repaired before its partial call is emitted, so a frontend call's raw arguments wait for the hooks and the client sees only the canonical name.

Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
A thread stored before call records gets its calls' possible wire IDs (X, X-2, ...) reserved, so a new call reusing a runtime ID never takes an ID the client already holds.

Addresses review comment docker#5122 (comment).

Signed-off-by: deka <dimitri.rayer@gmail.com>
…calls

A run that only answers client-executed calls takes up the saved routing invocation instead of starting a new one, so the calling agent's before_agent_run hook does not fire again. New user input still starts a new invocation.

Addresses review comment 4801467222.

Signed-off-by: deka <dimitri.rayer@gmail.com>
@trungutt

trungutt commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

@deka Sorry I'd need a bit of time for this PR

@trungutt trungutt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two bugs need fixing before this can merge. Both leave a conversation permanently stuck: every request after the first failure is rejected with 400, so the user can't continue it.

1. A tool call that gets cut off breaks the conversation permanently

What happens

The app offers a tool such as pick_date, and the model starts calling it. Then the model gets cut off before it finishes. The runtime correctly throws the half-written call away, but the AG-UI server still treats it as a call the app has to answer. One of two things follows:

  • The server sends TOOL_CALL_RESULT "(retracted: the call was not executed)" for it. The client stores that as a tool message and sends it back with the next request. The server then rejects its own result with 400 tool call "c1" is not pending.
  • Or the server lists the call in pendingToolCallIds. The client answers it, as the spec requires, and the server rejects that answer with the same 400.

The client resends its full history on every request, so every later request fails the same way, even ones that only add a new user message.

When it happens

Any of these, while the model is in the middle of a frontend tool call:

  • it hits its output-token limit (finish_reason: length);
  • it refuses (finish_reason: refusal);
  • the provider hands the turn to another model mid-stream;
  • the stream fails with a temporary error (503, overloaded, connection reset) and the runtime retries the same model;
  • the primary model fails and a fallback model answers.

The first and fourth are ordinary events. The same cases with a normal server-side tool work fine, so this is new in this PR.

Reproduce

Use @ag-ui/client 1.0.5 (HttpAgent) with a scripted model. In the first run, the model streams a pick_date call, then the stream ends with finish_reason: length. Then send one more user message. It gets a 400, and so does every request after it.

Cause

The translator marks a call as a frontend call as soon as its first fragment streams (pkg/agui/translator.go:359, call.frontend = true on PartialToolCallEvent), before the runtime decides whether to dispatch it. That flag drives pendingToolCallIds in finish() and the Frontend: true record saved in ag-ui.tool-calls. matchToolResults (pkg/agui/frontend_tools.go:146-151) then rejects any result for a frontend record that has no stored result.

2. Upgrading breaks conversations started on v1.168.0–v1.170.1, even without frontend tools

What happens

serve ag-ui shipped in v1.168.0, and those versions saved no call records. Take a conversation started on one of those versions whose last turn used a sub-agent (transfer_task) that called a tool. The old server streamed the sub-agent's call and result, so the client keeps a tool message for it. After upgrading, the client's next request includes that tool message, and the server answers 400 tool call "<id>" is unknown on this thread. It does the same on every request after that.

The client doesn't need to offer any frontend tools for this to happen.

Reproduce

  1. Run a turn where the root agent calls transfer_task and the sub-agent calls a tool.
  2. Delete the session's ag-ui.tool-calls attribute. That leaves the session as an older version stored it.
  3. Send the history the client holds, plus a new user message. It gets a 400.

The same thing happens when an older conversation's last turn holds a (retracted…) or (no result) result the old server streamed.

Cause

For a thread with no call records, matchToolResults falls back to checking stored (pkg/agui/frontend_tools.go:146). But stored is built from sess.OwnMessages() (:214), which leaves out sub-session results. Results that were only ever streamed and never stored are missing too.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent For work that has to do with the general agent loop/agentic features of the app area/api For features/issues/fixes related to the usage of the cagent API area/docs Documentation changes area/runtime Runtime engine, agent loop execution, tool dispatch, loop detection area/sessions For features/issues/fixes related to session lifecycle (resume, persistence, export) area/tools For features/issues/fixes related to the usage of built-in and MCP tools kind/feat PR adds a new feature (maps to feat:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: AG-UI protocol support, step 4 (frontend tools)

3 participants