Skip to main content

merge_messages

Function merge_messages 

Source
pub fn merge_messages(ours: &[Message], theirs: &[Message]) -> Vec<Message>
Expand description

Merges two copies of one conversation’s messages, by message id.

AG-UI 1.0 makes a message id unique within its thread, so two lists that hold the same conversation agree on which message is which. They differ in what each holds that the other lacks, and that is all this adds: the result is ours with the messages of theirs it does not hold.

A messageIn the result
only ours holdsas it is, in the order ours has them
both hold, the copies equalonce, in the place ours gives it
both hold, the copies differentthe copy of ours, in the place ours gives it
only theirs holdsafter every message of ours, in the order theirs has them
a tool result, of either list, whose call a message before it holdsdirectly behind that message, after the tool results and activity messages already behind it

Two lists that started from the same messages and each grew at the end therefore merge to: the common start, what ours added, what theirs added. The result depends on the two lists and nothing else, so the same arguments always give the same list. It is not symmetric: with the arguments swapped, the other list’s additions come first and the other list’s copies are the ones kept.

No message of ours is changed or left out, so a message one list saw complete and the other only in part stays as ours has it. Pass as ours the list whose copies should stand.

The last row is the one rule about a role. A tool result is placed by the call it answers and not by where a list kept it, so that in the result it follows the message that holds its call whichever way the two lists were built: one that arrived after other messages and was kept at the end is put behind its call, in ours as in theirs. A list whose results stand behind their calls already comes through in its own order. The message that holds a call is the assistant message with that call among its tool_calls. Where several hold the id, as with a provider that numbers its calls from zero in every turn, it is the newest one whose call has no result yet, or the newest one when each has. A result whose call no earlier message holds stays in the order of its list.

A reasoning message stays in front of the reply it led to because the order of each list is kept, and an activity message is merged like any other.

The function reads ids and nothing else. It does not know which conversation a list belongs to, and it does not refuse a tool call both lists answered: both results are in the list. Use it for a view of two threads while either is still running. For the thread that will send the next request, Thread::merge_messages_from applies the same rules and checks what a list cannot say.

Each list is expected to hold an id once, as Thread::messages does. Of a repeated id the first message is kept.

use ag_ui::Message;
use ag_ui::client::merge_messages;

let start = vec![Message::user("u-1", "Hello"), Message::assistant("a-1", "Hi")];
// Two copies of the conversation, each of which went on by itself.
let mut ours = start.clone();
ours.push(Message::user("u-2", "Write the report"));
ours.push(Message::assistant("a-2", "Here it is."));
let mut theirs = start.clone();
theirs.push(Message::user("u-3", "What time is it?"));
theirs.push(Message::assistant("a-3", "Noon."));

let merged = merge_messages(&ours, &theirs);
let ids: Vec<_> = merged.iter().map(|message| message.id().as_str()).collect();
assert_eq!(ids, ["u-1", "a-1", "u-2", "a-2", "u-3", "a-3"]);