@@ -43,7 +43,11 @@ public static IEndpointConventionBuilder MapAGUI(
4343 var jsonOptions = context . RequestServices . GetRequiredService < IOptions < Microsoft . AspNetCore . Http . Json . JsonOptions > > ( ) ;
4444 var jsonSerializerOptions = jsonOptions . Value . SerializerOptions ;
4545
46- var messages = input . Messages . AsChatMessages ( jsonSerializerOptions ) ;
46+ // Normalize assistant/tool ordering before we map to ChatMessage
47+ var aguiMessages = input . Messages ? . ToList ( ) ?? new List < AGUIMessage > ( ) ;
48+ FixToolMessageOrdering ( aguiMessages ) ;
49+
50+ var messages = aguiMessages . AsChatMessages ( jsonSerializerOptions ) ;
4751 var clientTools = input . Tools ? . AsAITools ( ) . ToList ( ) ;
4852
4953 // Create run options with AG-UI context in AdditionalProperties
@@ -80,4 +84,100 @@ public static IEndpointConventionBuilder MapAGUI(
8084 return new AGUIServerSentEventsResult ( events , sseLogger ) ;
8185 } ) ;
8286 }
87+
88+ private static void FixToolMessageOrdering ( List < AGUIMessage > messages )
89+ {
90+ if ( messages == null || messages . Count == 0 )
91+ {
92+ return ;
93+ }
94+
95+ // Collect all tool messages by ToolCallId
96+ var toolsByCallId = new Dictionary < string , Queue < AGUIToolMessage > > ( ) ;
97+ var toolsWithoutCallId = new List < AGUIToolMessage > ( ) ;
98+
99+ foreach ( var msg in messages )
100+ {
101+ if ( msg is AGUIToolMessage toolMsg )
102+ {
103+ if ( ! string . IsNullOrWhiteSpace ( toolMsg . ToolCallId ) )
104+ {
105+ if ( ! toolsByCallId . TryGetValue ( toolMsg . ToolCallId , out var queue ) )
106+ {
107+ queue = new Queue < AGUIToolMessage > ( ) ;
108+ toolsByCallId [ toolMsg . ToolCallId ] = queue ;
109+ }
110+
111+ queue . Enqueue ( toolMsg ) ;
112+ }
113+ else
114+ {
115+ toolsWithoutCallId . Add ( toolMsg ) ;
116+ }
117+ }
118+ }
119+
120+ var reordered = new List < AGUIMessage > ( messages . Count ) ;
121+
122+ foreach ( var msg in messages )
123+ {
124+ // Reinsert tool messages next to their assistant, so skip them in this pass.
125+ if ( msg is AGUIToolMessage )
126+ {
127+ continue ;
128+ }
129+
130+ reordered . Add ( msg ) ;
131+
132+ if ( msg is AGUIAssistantMessage assistant &&
133+ assistant . ToolCalls is { Length : > 0 } )
134+ {
135+ // For each tool call in this assistant message, append
136+ // the corresponding tool result message(s) immediately after.
137+ foreach ( var toolCall in assistant . ToolCalls )
138+ {
139+ if ( toolCall ? . Id is null )
140+ {
141+ continue ;
142+ }
143+
144+ if ( toolsByCallId . TryGetValue ( toolCall . Id , out var queue ) )
145+ {
146+ while ( queue . Count > 0 )
147+ {
148+ var toolMsg = queue . Dequeue ( ) ;
149+ reordered . Add ( toolMsg ) ;
150+ }
151+
152+ if ( queue . Count == 0 )
153+ {
154+ toolsByCallId . Remove ( toolCall . Id ) ;
155+ }
156+ }
157+ }
158+ }
159+ }
160+
161+ // Any remaining tool messages (without matching assistant toolCalls)
162+ // are appended at the end so nothing is lost.
163+ foreach ( var remainingQueue in toolsByCallId . Values )
164+ {
165+ while ( remainingQueue . Count > 0 )
166+ {
167+ reordered . Add ( remainingQueue . Dequeue ( ) ) ;
168+ }
169+ }
170+
171+ foreach ( var toolMsg in toolsWithoutCallId )
172+ {
173+ reordered . Add ( toolMsg ) ;
174+ }
175+
176+ // Replace the original list contents
177+ messages . Clear ( ) ;
178+ foreach ( var msg in reordered )
179+ {
180+ messages . Add ( msg ) ;
181+ }
182+ }
83183}
0 commit comments