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

Code before:
        elif self.memory_type == "external":
            params = {
                "user_id": user_id,
                "agent_id": agent_name,
                "metadata": {"type": "external", **metadata},
            }

        if params:
            if isinstance(self.memory, MemoryClient):
                params["output_format"] = "v2"

                if self.mem0_run_id:
                    params["run_id"] = self.mem0_run_id
            self.memory.add(value, **params)

    def search(
        self,
        query: str,
        limit: int = 3,
        score_threshold: float = 0.35,
    ) -> List[Any]:
        params = {"query": query, "limit": limit, "output_format": "v2", "run_id": self.mem0_run_id}
        
        if user_id := self._get_user_id():
            params["user_id"] = user_id

        agent_name = self._get_agent_name()
        if self.memory_type == "short_term":
            params["agent_id"] = agent_name
            params["metadata"] = {"type": "short_term"}
        elif self.memory_type == "long_term":
            params["agent_id"] = agent_name
            params["metadata"] = {"type": "long_term"}
        elif self.memory_type == "entities":
            params["agent_id"] = agent_name
            params["metadata"] = {"type": "entity"}
        elif self.memory_type == "external":
            params["agent_id"] = agent_name
            params["metadata"] = {"type": "external"}

        # Discard the filters for now since we create the filters
        # automatically when the crew is created.
        if isinstance(self.memory, Memory):
            del params["metadata"], params["output_format"], params["run_id"]
            
        results = self.memory.search(**params)
        return [r for r in results["results"] if r["score"] >= score_threshold]

    def _get_user_id(self) -> str:
        return self._get_config().get("user_id", "")


Review comment (suggestion):
Thirdly, it would be cool to have support for mem0 [new features](https://docs.mem0.ai/features/platform-overview) such as (I find the most relevant once) `memory inclusion`, `custom categories`.

You can add support for `memory inclusion` 
```py
# get includes from config, then with each save
params["includes"] = includes
```

Code after:
        # 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":
            params["user_id"] = user_id

        
        if params:
            # 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":
                    params["run_id"] = self.mem0_run_id

            self.memory.add(assistant_message, **params)

    def search(self,query: str,limit: int = 3,score_threshold: float = 0.35) -> List[Any]:
        params = {
            "query": query, 
            "limit": limit, 
            "version": "v2",
            "output_format": "v1.1"
            }
        
        if user_id := self.config.get("user_id", ""):
            params["user_id"] = user_id