chore: optimize the messagef content processing

This commit is contained in:
anchen 2025-01-27 09:37:29 +11:00
parent 74ae24b883
commit 07f3d7d6bb

View File

@ -133,15 +133,15 @@ def process_message_content(messages):
processed_messages = [] processed_messages = []
for message in messages: for message in messages:
message_copy = message.copy() message_copy = message.copy()
if "content" in message_copy: if "content" in message_copy and isinstance(message_copy["content"], list):
flattened_text = "" text_fragments = [
if isinstance(message_copy["content"], list): fragment["text"]
for content_fragment in message_copy["content"]: for fragment in message_copy["content"]
if content_fragment["type"] == "text" and "text" in content_fragment: if fragment.get("type") == "text"
flattened_text += content_fragment["text"] ]
else: if len(text_fragments) != len(message_copy["content"]):
raise ValueError("Only 'text' content type is supported.") raise ValueError("Only 'text' content type is supported.")
message_copy["content"] = flattened_text message_copy["content"] = "".join(text_fragments)
processed_messages.append(message_copy) processed_messages.append(message_copy)
return processed_messages return processed_messages