Skip to content

Commit 1cf8060

Browse files
authored
refactor(agui): delimit the outbound message id and its suffix (#4554)
## Summary This is an optimization of the outbound message ID format, not a bug fix. `AguiMessageConverter.toAguiMessages` builds the ID of an outbound AG-UI message as `agui::<sourceId>-<UUID>`. That format is usable: a reader who knows that the suffix is a UUID can strip it by its length. It is simply less reliable to depend on in the long run. The separator is a hyphen, which the UUID itself and many source ids also contain, so the source id can only be recovered by counting the fixed length of the UUID rather than by finding a boundary the format guarantees. This changes the tail to `::<UUID without hyphens>`. The suffix no longer contains the separator, so the source id is whatever precedes the last `::`. Reading the source id back then no longer depends on the length of the suffix, and source ids that contain hyphens or colons need no special handling. The prefix and the rule that reuses an id which already carries it are unchanged. The id format introduced in #3239 changes for callers of these conversion methods; the adapter's run path streams events and does not call them. ## Changes - `generatedMessageId` appends `::` and a hyphen-free UUID instead of `-` and a UUID. - The Javadoc of `toAguiMessages` and `toAguiMessageList` states the format and how to read the source id back. - The converter tests assert the full shape of the id and cover source ids that contain colons.
1 parent 4c63e6c commit 1cf8060

2 files changed

Lines changed: 67 additions & 14 deletions

File tree

‎agentscope-extensions/agentscope-extensions-protocol/agentscope-extensions-agui/src/main/java/io/agentscope/core/agui/converter/AguiMessageConverter.java‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ public AguiMessage toAguiMessage(Msg msg) {
127127
* error state.
128128
*
129129
* <p>Each generated AG-UI message receives a unique ID in the format
130-
* {@code agui::sourceId-UUID}. An ID that already carries the prefix is reused, so repeated
131-
* conversions do not append another marker.
130+
* {@code agui::sourceId::uuid}, where {@code uuid} is a random UUID without hyphens. After the
131+
* prefix, the source ID is everything before the last {@code ::}. An ID that already carries
132+
* the prefix is reused, so repeated conversions do not append another marker.
132133
*
133134
* @param msg The AgentScope message to convert
134135
* @return The converted AG-UI messages, never empty
@@ -301,8 +302,9 @@ private static InputContent toDocumentInputContent(DocumentBlock block) {
301302
* Convert a list of AgentScope messages to AG-UI messages.
302303
*
303304
* <p>Each generated AG-UI message receives a unique ID in the format
304-
* {@code agui::sourceId-UUID}. An ID that already carries the prefix is reused, so repeated
305-
* conversions do not append another marker.
305+
* {@code agui::sourceId::uuid}, where {@code uuid} is a random UUID without hyphens. After the
306+
* prefix, the source ID is everything before the last {@code ::}. An ID that already carries
307+
* the prefix is reused, so repeated conversions do not append another marker.
306308
*
307309
* <p>The output list may contain more messages than the input list because messages that mix
308310
* reasoning with other content or contain multiple tool results are expanded.
@@ -630,7 +632,9 @@ private AguiMessage toolResultMessage(Msg msg, ToolResultBlock result) {
630632

631633
private static String generatedMessageId(Msg msg) {
632634
String id = msg.getId();
633-
return id.startsWith("agui::") ? id : "agui::" + id + "-" + UUID.randomUUID();
635+
return id.startsWith("agui::")
636+
? id
637+
: "agui::" + id + "::" + UUID.randomUUID().toString().replace("-", "");
634638
}
635639

636640
/**

‎agentscope-extensions/agentscope-extensions-protocol/agentscope-extensions-agui/src/test/java/io/agentscope/core/agui/converter/AguiMessageConverterTest.java‎

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
import java.util.Collections;
6363
import java.util.List;
6464
import java.util.Map;
65+
import java.util.regex.Matcher;
66+
import java.util.regex.Pattern;
6567
import org.junit.jupiter.api.BeforeEach;
6668
import org.junit.jupiter.api.Test;
6769

@@ -70,6 +72,9 @@
7072
*/
7173
class AguiMessageConverterTest {
7274

75+
/** Shape of a generated outbound ID; the UUID has no hyphens. */
76+
private static final Pattern OUTBOUND_ID = Pattern.compile("^agui::(.+)::([0-9a-f]{32})$");
77+
7378
private AguiMessageConverter converter;
7479

7580
@BeforeEach
@@ -159,7 +164,7 @@ void testConvertMsgToAguiMessage() {
159164

160165
AguiMessage aguiMsg = converter.toAguiMessage(msg);
161166

162-
assertTrue(aguiMsg.getId().startsWith("agui::msg-5-"));
167+
assertGeneratedId("msg-5", aguiMsg.getId());
163168
assertEquals("user", aguiMsg.getRole());
164169
assertEquals("Test message", aguiMsg.getTextContent());
165170
}
@@ -182,7 +187,7 @@ void testConvertMsgWithToolUseToAguiMessage() {
182187

183188
AguiMessage aguiMsg = converter.toAguiMessage(msg);
184189

185-
assertTrue(aguiMsg.getId().startsWith("agui::msg-6-"));
190+
assertGeneratedId("msg-6", aguiMsg.getId());
186191
assertEquals("assistant", aguiMsg.getRole());
187192
assertEquals("Calling tool...", aguiMsg.getTextContent());
188193
assertTrue(aguiMsg.hasToolCalls());
@@ -217,13 +222,50 @@ void testRoundTripConversion() {
217222
AguiMessage converted = converter.toAguiMessage(msg);
218223
AguiMessage convertedAgain = converter.toAguiMessage(converter.toMsg(converted));
219224

220-
assertTrue(converted.getId().startsWith("agui::" + original.getId() + "-"));
225+
assertGeneratedId(original.getId(), converted.getId());
221226
assertNotEquals(original.getId(), converted.getId());
222227
assertEquals(converted.getId(), convertedAgain.getId());
223228
assertEquals(original.getRole(), converted.getRole());
224229
assertEquals(original.getTextContent(), converted.getTextContent());
225230
}
226231

232+
@Test
233+
void testGeneratedIdKeepsSourceIdContainingColon() {
234+
// A resumed tool result is named "agui-resume-<messageId>:<toolCallId>"
235+
String sourceId = "agui-resume-3f2a9c1e-aaaa-4bbb-8ccc-dddddddddddd:call_1";
236+
Msg msg =
237+
Msg.builder()
238+
.id(sourceId)
239+
.role(MsgRole.TOOL)
240+
.content(
241+
ToolResultBlock.builder()
242+
.id("call_1")
243+
.output(TextBlock.builder().text("Result: 42").build())
244+
.build())
245+
.build();
246+
247+
AguiMessage aguiMsg = converter.toAguiMessage(msg);
248+
249+
assertGeneratedId(sourceId, aguiMsg.getId());
250+
assertEquals("call_1", aguiMsg.getToolCallId());
251+
}
252+
253+
@Test
254+
void testGeneratedIdKeepsSourceIdContainingSeparator() {
255+
// The tail of the source ID looks like a suffix; only the last "::" introduces one
256+
String sourceId = "call::0123456789abcdef0123456789abcdef";
257+
Msg msg =
258+
Msg.builder()
259+
.id(sourceId)
260+
.role(MsgRole.USER)
261+
.content(TextBlock.builder().text("Test message").build())
262+
.build();
263+
264+
AguiMessage aguiMsg = converter.toAguiMessage(msg);
265+
266+
assertGeneratedId(sourceId, aguiMsg.getId());
267+
}
268+
227269
@Test
228270
void testConvertToolMessageToMsg() {
229271
AguiMessage aguiMsg = AguiMessage.toolMessage("msg-t1", "tc-1", "Tool result here");
@@ -690,7 +732,7 @@ void testConvertMsgWithToolResultToAguiMessage() {
690732

691733
AguiMessage aguiMsg = converter.toAguiMessage(msg);
692734

693-
assertTrue(aguiMsg.getId().startsWith("agui::msg-tr1-"));
735+
assertGeneratedId("msg-tr1", aguiMsg.getId());
694736
assertEquals("tool", aguiMsg.getRole());
695737
assertEquals("tc-1", aguiMsg.getToolCallId());
696738
assertEquals("Result: 42", aguiMsg.getTextContent());
@@ -710,8 +752,8 @@ void testToAguiMessageListExpandsMultipleToolResults() {
710752
List<AguiMessage> messages = converter.toAguiMessageList(List.of(msg));
711753

712754
assertEquals(2, messages.size());
713-
assertTrue(messages.get(0).getId().startsWith("agui::msg-tr2-"));
714-
assertTrue(messages.get(1).getId().startsWith("agui::msg-tr2-"));
755+
assertGeneratedId("msg-tr2", messages.get(0).getId());
756+
assertGeneratedId("msg-tr2", messages.get(1).getId());
715757
assertNotEquals(messages.get(0).getId(), messages.get(1).getId());
716758
assertEquals("tc-1", messages.get(0).getToolCallId());
717759
assertEquals("tc-2", messages.get(1).getToolCallId());
@@ -748,7 +790,7 @@ void testConvertThinkingBlockToReasoningMessage() {
748790

749791
AguiMessage result = converter.toAguiMessage(msg);
750792

751-
assertTrue(result.getId().startsWith("agui::msg-thinking-"));
793+
assertGeneratedId("msg-thinking", result.getId());
752794
assertEquals("reasoning", result.getRole());
753795
assertEquals("Need to inspect the weather", result.getTextContent());
754796
}
@@ -773,10 +815,10 @@ void testConvertMixedAssistantTurnToReasoningAndAssistantMessages() {
773815
List<AguiMessage> results = converter.toAguiMessages(msg);
774816

775817
assertEquals(2, results.size());
776-
assertTrue(results.get(0).getId().startsWith("agui::msg-mixed-"));
818+
assertGeneratedId("msg-mixed", results.get(0).getId());
777819
assertTrue(results.get(0).isReasoningMessage());
778820
assertEquals("Need a tool", results.get(0).getTextContent());
779-
assertTrue(results.get(1).getId().startsWith("agui::msg-mixed-"));
821+
assertGeneratedId("msg-mixed", results.get(1).getId());
780822
assertNotEquals(results.get(0).getId(), results.get(1).getId());
781823
assertTrue(results.get(1).isAssistantMessage());
782824
assertEquals("Let me check", results.get(1).getTextContent());
@@ -2448,4 +2490,11 @@ private static String resultText(ToolResultBlock result) {
24482490
.findFirst()
24492491
.orElse("");
24502492
}
2493+
2494+
/** Asserts that {@code id} is a generated outbound ID whose source ID is {@code sourceId}. */
2495+
private static void assertGeneratedId(String sourceId, String id) {
2496+
Matcher matcher = OUTBOUND_ID.matcher(id);
2497+
assertTrue(matcher.matches(), "unexpected outbound id: " + id);
2498+
assertEquals(sourceId, matcher.group(1));
2499+
}
24512500
}

0 commit comments

Comments
 (0)