Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions evoagentx/memory/context_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ def add_user_prompt(self, user_prompt: Union[str, list]):
else:
self.context[-1]["content"] = last_msg + user_prompt

elif isinstance(self.context[-1]["content"], list):
self.context[-1]["content"] = [
*self.context[-1]["content"],
{"type": "text", "text": user_prompt},
]
else:
self.context[-1]["content"] += "\n\n" + user_prompt

Expand Down
39 changes: 39 additions & 0 deletions tests/src/memory/test_context_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Regression coverage for consecutive text and multimodal user prompts."""

from copy import deepcopy
from unittest.mock import Mock

import pytest

from evoagentx.memory.context_manager import ContextManager
from evoagentx.models import BaseLLM

IMAGE = {"type": "image_url", "image_url": {"url": "https://example.com/image.png"}}
TEXT = {"type": "text", "text": "Describe this image."}


@pytest.mark.parametrize("native_tool_calling", [False, True])
@pytest.mark.parametrize(
"first,second,expected",
[
("First", "Second", "First\n\nSecond"),
("First", [IMAGE], [{"type": "text", "text": "First"}, IMAGE]),
([IMAGE], [TEXT], [IMAGE, TEXT]),
([IMAGE], TEXT["text"], [IMAGE, TEXT]),
],
)
def test_consecutive_user_prompts_preserve_content_parts(
native_tool_calling, first, second, expected
):
"""Text following an image remains one text part, never individual characters."""
llm = Mock(spec=BaseLLM)
llm.supports_native_tool_calling.return_value = native_tool_calling
context = ContextManager(llm=llm)
first, second = deepcopy(first), deepcopy(second)
original_inputs = deepcopy((first, second))

context.add_user_prompt(first)
context.add_user_prompt(second)

assert context.context == [{"role": "user", "content": expected}]
assert (first, second) == original_inputs