@@ -231,11 +231,9 @@ async def create_channel(
231
231
if thread .id is None :
232
232
await thread .create ()
233
233
234
- chat_history = ChatHistory ()
235
- async for message in thread .get_messages ():
236
- chat_history .add_message (message )
234
+ messages = [message async for message in thread .get_messages ()]
237
235
238
- return ChatHistoryChannel (messages = chat_history . messages , thread = thread )
236
+ return ChatHistoryChannel (messages = messages , thread = thread )
239
237
240
238
@trace_agent_get_response
241
239
@override
@@ -280,9 +278,7 @@ async def get_response(
280
278
if not responses :
281
279
raise AgentInvokeException ("No response from agent." )
282
280
283
- response = responses [- 1 ]
284
- await thread .on_new_message (response )
285
- return AgentResponseItem (message = response , thread = thread )
281
+ return AgentResponseItem (message = responses [- 1 ], thread = thread )
286
282
287
283
@trace_agent_invocation
288
284
@override
@@ -321,7 +317,6 @@ async def invoke(
321
317
chat_history .add_message (message )
322
318
323
319
async for response in self ._inner_invoke (thread , chat_history , arguments , kernel , ** kwargs ):
324
- await thread .on_new_message (response )
325
320
yield AgentResponseItem (message = response , thread = thread )
326
321
327
322
@trace_agent_invocation
@@ -411,6 +406,9 @@ async def invoke_stream(
411
406
412
407
await self ._capture_mutated_messages (agent_chat_history , message_count_before_completion , thread )
413
408
if role != AuthorRole .TOOL :
409
+ # Tool messages will be automatically added to the chat history by the auto function invocation loop
410
+ # if it's the response (i.e. terminated by a filter), thus we need to avoid notifying the thread about
411
+ # them multiple times.
414
412
await thread .on_new_message (
415
413
ChatMessageContent (
416
414
role = role if role else AuthorRole .ASSISTANT , content = "" .join (response_builder ), name = self .name
@@ -468,6 +466,11 @@ async def _inner_invoke(
468
466
469
467
for response in responses :
470
468
response .name = self .name
469
+ if response .role != AuthorRole .TOOL :
470
+ # Tool messages will be automatically added to the chat history by the auto function invocation loop
471
+ # if it's the response (i.e. terminated by a filter),, thus we need to avoid notifying the thread about
472
+ # them multiple times.
473
+ await thread .on_new_message (response )
471
474
yield response
472
475
473
476
async def _prepare_agent_chat_history (
0 commit comments