You are given an existing API:
python def get_chat_messages(message_id: int) -> list[Message]:
For a given message_id, it returns the surrounding chat context:
The Message class has the following properties:
id: the unique identifier of the messagecontent: the text content of the messagetimestamp: the time at which the message was sentYour task is to implement a function that merges chat message windows for a given list of message IDs. The merged chat message window should contain:
get_chat_messages API will always return a list of 11 messages (5 before, the message itself, and 5 after).Message class will have the properties id, content, and timestamp.Input:
python message_ids = [101, 102, 103, 104, 105]
Output:
A merged chat message window containing all messages between the earliest and latest message IDs (inclusive), maintaining their original order.
Input:
python message_ids = [205, 207, 203, 202]
Output:
A merged chat message window containing all messages between the earliest and latest message IDs (inclusive), maintaining their original order.
get_chat_messages API to fetch each message window.`python class Message: def init(self, id, content, timestamp): self.id = id self.content = content self.timestamp = timestamp
def get_chat_messages(message_id: int) -> list[Message]: # Simulated API call # Replace with actual API call in production code pass
def merge_chat_message_windows(message_ids: list[int]) -> list[Message]: if not message_ids: return []
# Find the earliest and latest message IDs
earliest_id = min(message_ids)
latest_id = max(message_ids)
# Initialize the merged chat message window
merged_window = []
# Iterate through the range of message IDs
for mid in range(earliest_id, latest_id + 1):
messages = get_chat_messages(mid)
# Find the index of the current message in the message window
current_message_index = next((i for i, msg in enumerate(messages) if msg.id == mid), None)
if current_message_index is not None:
# Add messages before and after the current message to the merged window
merged_window.extend(messages[max(0, current_message_index - 5):current_message_index + 6])
# Remove duplicates and sort by timestamp
merged_window = list(dict.fromkeys(merged_window, key=lambda x: x.id))
merged_window.sort(key=lambda x: x.timestamp)
return merged_window
`
Note: The actual implementation of the get_chat_messages API is not provided, as it is assumed to be an existing API call. Replace the simulated API call with the actual API call in production code.