Dedupe input (right-side) messages in add_messages

This commit is contained in:
jacoblee93
2025-02-06 11:26:15 -08:00
parent 3daa4ae466
commit c3847fae6c
2 changed files with 12 additions and 0 deletions
@@ -176,9 +176,12 @@ def add_messages(
for m in left:
if m.id is None:
m.id = str(uuid.uuid4())
dedupe_map = {}
for m in right:
if m.id is None:
m.id = str(uuid.uuid4())
dedupe_map[m.id] = m
right = list(dedupe_map.values())
# merge
left_idx_by_id = {m.id: i for i, m in enumerate(left)}
merged = left.copy()
@@ -66,6 +66,15 @@ def test_missing_ids():
assert all(isinstance(m.id, str) and UUID(m.id, version=4) for m in result)
def test_duplicates_in_input():
left = []
right = [AIMessage(id="1", content="Hi there!"), AIMessage(id="1", content="Hi there again!")]
result = add_messages(left, right)
assert len(result) == 1
assert result[0].id == "1"
assert result[0].content == "Hi there again!"
def test_remove_message():
left = [
HumanMessage(content="Hello", id="1"),