Skip to content

feat(agui): add InputMessageDeduplicationMiddleware for resent transcripts - #4535

Merged
juruoO-o merged 4 commits into
agentscope-ai:mainfrom
xzxiaoshan:feat/input-message-dedup-middleware
Dec 2, 2026
Merged

juruoO-o merged 4 commits into
agentscope-ai:mainfrom
xzxiaoshan:feat/input-message-dedup-middleware

Conversation

@xzxiaoshan

Copy link
Copy Markdown
Collaborator

Summary

AG-UI front ends such as CopilotKit resend the whole conversation on every run. Against an agent that keeps its conversation across calls, each call then appends the history again. #4523 removed the old trimming and left a deduplicating MiddlewareBase to applications, because matching resent messages against the agent's state is easy to get wrong and the assistant message ids a client receives are not the persisted ones.

This PR ships that middleware, InputMessageDeduplicationMiddleware, so applications register it instead of writing their own. Assistant messages are matched by content, because the ids the stream gives them are not the persisted message ids.

Changes

  • InputMessageDeduplicationMiddleware (AG-UI extension) runs in onAgentStateReady and replaces the input with the messages the agent does not hold yet. It looks for the newest message of the agent's conversation that the input repeats and hands the agent only the input after its counterpart. A conversation message is recognized in the input by the tool call or tool result ids it carries, by its text, or by its id when it has no text. An incremental input is left unchanged. While the agent waits for tool results, results it is not waiting for are dropped. When the resent input carries nothing new, the last user message is handed over again under a new id, so the agent answers it again.
  • The AG-UI guide (EN and ZH) replaces the hand-written middleware outline with the registration:
import io.agentscope.core.agui.middleware.InputMessageDeduplicationMiddleware;

HarnessAgent.Builder agentBuilder = HarnessAgent.builder()
    .name("assistant")
    // ...
    .middleware(new InputMessageDeduplicationMiddleware());

Register it only on an agent that keeps its conversation across requests.

Refs #2023.

…ripts

## Summary

AG-UI front ends such as CopilotKit resend the whole conversation on
every run. Against an agent that keeps its conversation across calls,
each call then appends the history again. agentscope-ai#4523 removed the old trimming
and left a deduplicating `MiddlewareBase` to applications, because
matching resent messages against the agent's state is easy to get wrong
and the assistant message ids a client receives are not the persisted
ones.

This adds that middleware, `InputMessageDeduplicationMiddleware`, so
applications register it instead of writing their own. Assistant
messages are matched by content, because the ids the stream gives them
are not the persisted message ids.

## Changes

- `InputMessageDeduplicationMiddleware` (AG-UI extension) runs in
  `onAgentStateReady` and replaces the input with the messages the agent
  does not hold yet. It looks for the newest message of the agent's
  conversation that the input repeats and hands the agent only the input
  after its counterpart. A conversation message is recognized in the
  input by the tool call or tool result ids it carries, by its text, or
  by its id when it has no text. An incremental input is left unchanged.
  While the agent waits for tool results, results it is not waiting for
  are dropped. When the resent input carries nothing new, the last user
  message is handed over again under a new id, so the agent answers it
  again.
- The AG-UI guide (EN and ZH) replaces the hand-written middleware
  outline with the registration and its rules.

Register it only on an agent that keeps its conversation across
requests.

Refs agentscope-ai#2023
@mintlify

mintlify Bot commented Dec 1, 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 f809570.

Approve preview

The middleware lost input in three shapes: a new message with the text
of a question that got no answer, a result the agent waits for that
the client sent in front of its copy of the tool call, and a new
message followed by a stale tool result.

The text of a user message alone no longer counts as a repetition: it
takes the same message id, or messages in front of it that line up
with the conversation. A result the agent waits for reaches it
wherever the client put it, and so does the answer to a call that
waits for approval. A new user message while a tool call waits for its
result gets a cancelled result for that call, so that the agent goes
on instead of refusing the call. A question that is still the last
message of the conversation is not handed over again: the agent
answers it from the conversation.

The Javadoc is shortened and the AG-UI guide (EN and ZH) follows.

@juruoO-o juruoO-o 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.

I support shipping this as an opt-in middleware in agentscope-extensions-agui. The transcript matching is easy for applications to get wrong, and the existing onAgentStateReady hook is the right integration point. Please address these two correctness issues before merge.

  1. [P1] Filtering a pure replay can re-dispatch pending tools. When the request contains only replayed history and already-consumed results, InputDelta.extract() can leave an empty input list. With a tool still pending, ReActAgent treats empty input as a resume and calls resumeAgent(). Please keep the call suspended without dispatching it again. Add a regression through the agent or AG-UI entry point that checks TOOL_SUSPENDED, an unchanged dispatch count, and successful delivery of the real result afterward. See the regression scenario in #3766.
  2. [P1] Filtering framework messages can drop a real new user message. withoutFrameworkMessages() drops any incoming USER message whose normalized text matches a synthetic message anywhere in the stored conversation. A new user message with a fresh ID can therefore disappear even when there is no transcript overlap. Please require evidence that the message is a replay before dropping it, and add a regression that preserves a fresh user message with matching text.

There are also two policy choices to make explicit. withCancelledCalls() synthesizes a tool result when a new user message arrives, even with enablePendingToolRecovery disabled. Please keep cancellation behind an explicit application choice. restoreLastUserPrompt() can turn a retry of an already-answered transcript into another model run. Please clarify how callers request regeneration and how a replay or retry is handled.

Reviewed head 208290cfe437c45668c20e0cb0c3051a4b65e35a. Static source review. Local Java tests were not run.

@juruoO-o juruoO-o 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.

One additional data-loss case on the reviewed head: MessageFingerprint records attachment classes, so two messages with the same text and same attachment types can compare equal even when the image or document differs. Please preserve the new message when attachment identity cannot be established. Cover same-type attachments with different content and no reliable shared message ID.

A replay that leaves the agent waiting for a tool result ends as a
suspension naming the waiting calls, instead of requesting them again.

Only user messages that copy, in order, the messages the framework
added behind the anchor are left out. A new message that merely has the
same text is kept.

A new user message cancels a waiting call only on an agent built with
enablePendingToolRecovery(true). Without it the run fails as before.

The attachments of a user message are compared by what they hold, by
address, data or file, so the same words with another picture are a new
message.

A resend that starts at a reply the client splits into reasoning and
text is recognized.

Regenerate and retry are documented. The AG-UI guide (EN and ZH)
follows, and names the check a journaled agent makes and the failure
messages of a HarnessAgent.
@xzxiaoshan

Copy link
Copy Markdown
Collaborator Author

All five points from the two reviews are addressed.

  • Replay while a tool waits. The run now ends with TOOL_SUSPENDED and names the waiting calls; they are not requested again until their results arrive. Covered through the agent and the AG-UI processor.
  • Framework messages. Only user messages that copy, in order, the ones the framework added right behind the anchor are left out. A new message that merely has the same text is kept.
  • Cancellation. A new user message cancels a waiting call only on an agent built with enablePendingToolRecovery(true). Without it the run fails as before and the call keeps waiting.
  • Regenerate and retry. Documented rather than changed: AG-UI has no signal for it, so a resend with nothing new hands the last user message over again and regenerates the latest reply. A question that got no answer is not handed over again.
  • Attachments. Those of a user message are compared by content (address, data or file id), so the same words with another picture are a new message. An attachment that cannot be compared leaves it to the id.

@juruoO-o juruoO-o 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.

One remaining data-loss case in withoutFrameworkCopies(): isCopyOf() compares only role and collapsed text, bypassing the new attachment comparison. In leavesACopyOfAReminderOutWhileACallWaits, add a new image to the final incoming user message while keeping the reminder text. The stored reminder has no image, yet this branch still drops the entire incoming message before the agent can see it. Please retain messages with additional or different content blocks (including attachments) and add this anchored regression alongside the genuine text-only reminder-copy case.

withoutFrameworkCopies() took a user message for a copy of a
framework-added reminder when only its collapsed text matched. A new
message that kept the reminder's text and added a picture, a sound or
any other content block was dropped before the agent saw it, without
an error, while a tool call was waiting.

A copy now has the stored message's text, however it is split into
text blocks, and the same other content blocks. Media blocks are
compared by what they hold, as resent messages are, and a block that
cannot be compared does not make a copy. A tool result or an approval
is never taken for a copy.

The AG-UI guide (EN and ZH) follows.
@xzxiaoshan

Copy link
Copy Markdown
Collaborator Author

Fixed: a user message that keeps a reminder's text but carries other content is no longer dropped as a copy of it.

@juruoO-o juruoO-o 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.

Reviewed the reminder-copy fix and its regression coverage. Messages with new or changed attachments survive deduplication, and genuine reminder copies are still filtered.

@juruoO-o
juruoO-o merged commit 7b87b59 into agentscope-ai:main Dec 2, 2026
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.

2 participants