@@ -338,7 +338,7 @@ async def test_model_invoking_single_message(self, mock_mem0_client: AsyncMock)
338338 mock_mem0_client .search .assert_called_once ()
339339 call_args = mock_mem0_client .search .call_args
340340 assert call_args .kwargs ["query" ] == "What's the weather?"
341- assert call_args .kwargs ["user_id " ] == " user123"
341+ assert call_args .kwargs ["filters " ] == { "user_id" : " user123"}
342342
343343 assert isinstance (context , Context )
344344 expected_instructions = (
@@ -373,8 +373,7 @@ async def test_model_invoking_with_agent_id(self, mock_mem0_client: AsyncMock) -
373373 await provider .invoking (message )
374374
375375 call_args = mock_mem0_client .search .call_args
376- assert call_args .kwargs ["agent_id" ] == "agent123"
377- assert call_args .kwargs ["user_id" ] is None
376+ assert call_args .kwargs ["filters" ] == {"agent_id" : "agent123" }
378377
379378 async def test_model_invoking_with_scope_to_per_operation_thread_id (self , mock_mem0_client : AsyncMock ) -> None :
380379 """Test invoking with scope_to_per_operation_thread_id enabled."""
@@ -392,7 +391,7 @@ async def test_model_invoking_with_scope_to_per_operation_thread_id(self, mock_m
392391 await provider .invoking (message )
393392
394393 call_args = mock_mem0_client .search .call_args
395- assert call_args .kwargs ["run_id " ] == " operation_thread"
394+ assert call_args .kwargs ["filters " ] == { "user_id" : "user123" , "run_id" : " operation_thread"}
396395
397396 async def test_model_invoking_no_memories_returns_none_instructions (self , mock_mem0_client : AsyncMock ) -> None :
398397 """Test that no memories returns context with None instructions."""
@@ -510,3 +509,87 @@ def test_validate_per_operation_thread_id_disabled_scope(self, mock_mem0_client:
510509
511510 # Should not raise exception even with different thread ID
512511 provider ._validate_per_operation_thread_id ("different_thread" )
512+
513+
514+ class TestMem0ProviderBuildFilters :
515+ """Test the _build_filters method."""
516+
517+ def test_build_filters_with_user_id_only (self , mock_mem0_client : AsyncMock ) -> None :
518+ """Test building filters with only user_id."""
519+ provider = Mem0Provider (user_id = "user123" , mem0_client = mock_mem0_client )
520+
521+ filters = provider ._build_filters ()
522+ assert filters == {"user_id" : "user123" }
523+
524+ def test_build_filters_with_all_parameters (self , mock_mem0_client : AsyncMock ) -> None :
525+ """Test building filters with all initialization parameters."""
526+ provider = Mem0Provider (
527+ user_id = "user123" ,
528+ agent_id = "agent456" ,
529+ thread_id = "thread789" ,
530+ application_id = "app999" ,
531+ mem0_client = mock_mem0_client ,
532+ )
533+
534+ filters = provider ._build_filters ()
535+ assert filters == {
536+ "user_id" : "user123" ,
537+ "agent_id" : "agent456" ,
538+ "run_id" : "thread789" ,
539+ "app_id" : "app999" ,
540+ }
541+
542+ def test_build_filters_excludes_none_values (self , mock_mem0_client : AsyncMock ) -> None :
543+ """Test that None values are excluded from filters."""
544+ provider = Mem0Provider (
545+ user_id = "user123" ,
546+ agent_id = None ,
547+ thread_id = None ,
548+ application_id = None ,
549+ mem0_client = mock_mem0_client ,
550+ )
551+
552+ filters = provider ._build_filters ()
553+ assert filters == {"user_id" : "user123" }
554+ assert "agent_id" not in filters
555+ assert "run_id" not in filters
556+ assert "app_id" not in filters
557+
558+ def test_build_filters_with_per_operation_thread_id (self , mock_mem0_client : AsyncMock ) -> None :
559+ """Test that per-operation thread ID takes precedence over base thread_id."""
560+ provider = Mem0Provider (
561+ user_id = "user123" ,
562+ thread_id = "base_thread" ,
563+ scope_to_per_operation_thread_id = True ,
564+ mem0_client = mock_mem0_client ,
565+ )
566+ provider ._per_operation_thread_id = "operation_thread"
567+
568+ filters = provider ._build_filters ()
569+ assert filters == {
570+ "user_id" : "user123" ,
571+ "run_id" : "operation_thread" , # Per-operation thread, not base_thread
572+ }
573+
574+ def test_build_filters_uses_base_thread_when_no_per_operation (self , mock_mem0_client : AsyncMock ) -> None :
575+ """Test that base thread_id is used when per-operation thread is not set."""
576+ provider = Mem0Provider (
577+ user_id = "user123" ,
578+ thread_id = "base_thread" ,
579+ scope_to_per_operation_thread_id = True ,
580+ mem0_client = mock_mem0_client ,
581+ )
582+ # _per_operation_thread_id is None
583+
584+ filters = provider ._build_filters ()
585+ assert filters == {
586+ "user_id" : "user123" ,
587+ "run_id" : "base_thread" , # Falls back to base thread_id
588+ }
589+
590+ def test_build_filters_returns_empty_dict_when_no_parameters (self , mock_mem0_client : AsyncMock ) -> None :
591+ """Test that _build_filters returns an empty dict when no parameters are set."""
592+ provider = Mem0Provider (mem0_client = mock_mem0_client )
593+
594+ filters = provider ._build_filters ()
595+ assert filters == {}
0 commit comments