Added author name logic to ChatClientAgent (#313)

This commit is contained in:
Dmytro Struk
2025-08-04 22:47:52 -07:00
committed by GitHub
Unverified
parent f5b35d8403
commit 544d2ba48f
2 changed files with 54 additions and 2 deletions
@@ -491,6 +491,7 @@ class ChatClientAgent(AgentBase):
"""
input_messages = self._normalize_messages(messages)
thread, thread_messages = await self._prepare_thread_and_messages(thread=thread, input_messages=input_messages)
agent_name = self._get_agent_name()
response = await self.chat_client.get_response(
messages=thread_messages,
@@ -519,6 +520,11 @@ class ChatClientAgent(AgentBase):
self._update_thread_with_type_and_conversation_id(thread, response.conversation_id)
# Ensure that the author name is set for each message in the response.
for message in response.messages:
if message.author_name is None:
message.author_name = agent_name
# Only notify the thread of new messages if the chatResponse was successful
# to avoid inconsistent messages state in the thread.
await self._notify_thread_of_new_messages(thread, input_messages)
@@ -595,6 +601,7 @@ class ChatClientAgent(AgentBase):
"""
input_messages = self._normalize_messages(messages)
thread, thread_messages = await self._prepare_thread_and_messages(thread=thread, input_messages=input_messages)
agent_name = self._get_agent_name()
response_updates: list[ChatResponseUpdate] = []
async for update in self.chat_client.get_streaming_response(
@@ -622,6 +629,10 @@ class ChatClientAgent(AgentBase):
**kwargs,
):
response_updates.append(update)
if update.author_name is None:
update.author_name = agent_name
yield AgentRunResponseUpdate(
contents=update.contents,
role=update.role,
@@ -720,3 +731,6 @@ class ChatClientAgent(AgentBase):
return [messages]
return [ChatMessage(role=ChatRole.USER, text=msg) if isinstance(msg, str) else msg for msg in messages]
def _get_agent_name(self) -> str:
return self.name or "UnnamedAgent"