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

Code before:
                self.memory.update_project(custom_categories=self.custom_categories)
        else:
            self.memory = (
                Memory.from_config(local_config)
                if local_config and len(local_config)
                else Memory()
            )

    def _create_filter_for_search(self):
        """
        Returns:
            dict: A filter dictionary containing AND conditions for querying data.
                - Includes user_id if memory_type is 'external'.
                - Includes run_id if memory_type is 'short_term' and mem0_run_id is present.
        """
        filter = {
            "AND": []
        }

        # Add user_id condition if the memory type is external
        if self.memory_type == "external":
            if user_id := self.config.get("user_id", ""):
                filter["AND"].append({"user_id": user_id})

            if agent_id := self.config.get("agent_id", ""):
                filter["AND"].append({"agent_id": agent_id})

        # Add run_id condition if the memory type is short_term and a run ID is set
        if self.memory_type == "short_term" and self.mem0_run_id:
            filter["AND"].append({"run_id": self.mem0_run_id})

        return filter

    def save(self, value: Any, metadata: Dict[str, Any]) -> None:
        user_id = self.config.get("user_id", "")
        assistant_message = [{"role" : "assistant","content" : value}]

        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
        }

        if self.memory_type == "external":

Review comment (suggestion):
@lucasgomide, I think 

this particular line of code 
```python
if agent_id := self.config.get("agent_id", ""):
                filter["AND"].append({"agent_id": agent_id})
```

should be out of the if condition. Consider the case when mem0 is being used by some other memory_type other than external.



Code after:
            if self.custom_categories:
                self.memory.update_project(custom_categories=self.custom_categories)
        else:
            self.memory = (
                Memory.from_config(local_config)
                if local_config and len(local_config)
                else Memory()
            )

    def _create_filter_for_search(self):
        """
        Returns:
            dict: A filter dictionary containing AND conditions for querying data.
                - Includes user_id and agent_id if both are present.
                - Includes user_id if only user_id is present.
                - Includes agent_id if only agent_id is present.
                - Includes run_id if memory_type is 'short_term' and mem0_run_id is present.
        """
        filter = defaultdict(list)

        if self.memory_type == "short_term" and self.mem0_run_id:
            filter["AND"].append({"run_id": self.mem0_run_id})
        else:
            user_id = self.config.get("user_id", "")
            agent_id = self.config.get("agent_id", "")

            if user_id and agent_id:
                filter["OR"].append({"user_id": user_id})
                filter["OR"].append({"agent_id": agent_id})
            elif user_id:
                filter["AND"].append({"user_id": user_id})
            elif agent_id:
                filter["AND"].append({"agent_id": agent_id})

        return filter

    def save(self, value: Any, metadata: Dict[str, Any]) -> None:
        user_id = self.config.get("user_id", "")
        assistant_message = [{"role" : "assistant","content" : value}]

        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