Repository: crewAIInc/crewAI
File: src/crewai/memory/storage/mem0_storage.py (Python)

Code before:

        return filter

    def save(self, value: Any, metadata: dict[str, Any]) -> None:
        conversations = []

        if messages := metadata.get("messages"):
            user_content = ""
            assistant_content = ""

            for message in messages:
                if message.get("role") == "user": # Using the last appended user content
                    user_content = message.get("content","")

                if message.get("role") == "assistant": # Using the last appended assistant content
                    assistant_content = message.get("content","")

            if user_msg := self._get_user_message(user_content):
                conversations.append({"role": "user", "content": user_msg})
            if assistant_msg := self._get_assistant_message(assistant_content):
                conversations.append({"role": "assistant", "content": assistant_msg})

            metadata.pop('messages') # Dropping messages from the metadata dictionary
        else:
            conversations.append({"role": "assistant", "content": value})

        user_id = self.config.get("user_id", "")

        base_metadata = {
            "short_term": "short_term",
            "long_term": "long_term",
            "entities": "entity",
            "external": "external"
        }

        # Shared base params
        params: dict[str, Any] = {
            "metadata": {"type": base_metadata[self.memory_type], **metadata},
            "infer": self.infer
        }

        # MemoryClient-specific overrides
        if isinstance(self.memory, MemoryClient):
            params["includes"] = self.includes
            params["excludes"] = self.excludes
            params["output_format"] = "v1.1"
            params["version"] = "v2"

        if self.memory_type == "short_term" and self.mem0_run_id:
            params["run_id"] = self.mem0_run_id


Review comment (suggestion):
Here is a more clear suggestion
```suggestion
def save(self, value: Any, metadata: dict[str, Any]) -> None:
    def _last_content(messages: Iterable[dict[str, Any]], role: str) -> str:
        return next(
            (m.get("content", "") for m in reversed(list(messages)) if m.get("role") == role),
            ""
        )

    messages = metadata.pop("messages", None)

    if messages:
        last_user = _last_content(messages, "user")
        last_assistant = _last_content(messages, "assistant")

        if user_msg := self._get_user_message(last_user):
            conversations.append({"role": "user", "content": user_msg})

        if assistant_msg := self._get_assistant_message(last_assistant):
            conversations.append({"role": "assistant", "content": assistant_msg})
    else:
        conversations.append({"role": "assistant", "content": value})
```

Code after:

        return filter

    def save(self, value: Any, metadata: dict[str, Any]) -> None:
        def _last_content(messages: Iterable[dict[str, Any]], role: str) -> str:
            return next(
                (m.get("content", "") for m in reversed(list(messages)) if m.get("role") == role),
                ""
            )
        
        conversations = []
        messages = metadata.pop("messages", None)
        if messages:
            last_user = _last_content(messages, "user")
            last_assistant = _last_content(messages, "assistant")

            if user_msg := self._get_user_message(last_user):
                conversations.append({"role": "user", "content": user_msg})
                
            if assistant_msg := self._get_assistant_message(last_assistant):
                conversations.append({"role": "assistant", "content": assistant_msg})
        else:
            conversations.append({"role": "assistant", "content": value})

        user_id = self.config.get("user_id", "")

        base_metadata = {
            "short_term": "short_term",
            "long_term": "long_term",
            "entities": "entity",
            "external": "external"
        }

        # Shared base params
        params: dict[str, Any] = {
            "metadata": {"type": base_metadata[self.memory_type], **metadata},
            "infer": self.infer
        }

        # MemoryClient-specific overrides
        if isinstance(self.memory, MemoryClient):
            params["includes"] = self.includes
            params["excludes"] = self.excludes
            params["output_format"] = "v1.1"
            params["version"] = "v2"

        if self.memory_type == "short_term" and self.mem0_run_id:
            params["run_id"] = self.mem0_run_id

        if user_id:
            params["user_id"] = user_id