66import threading
77from typing import Any
88
9+ import httpx
910import pytest
1011
1112pytest .importorskip ("langchain_core" )
@@ -967,6 +968,61 @@ async def test_async_retriever_uses_native_client_for_search_and_read():
967968 assert set (client .call_thread_ids ) == {main_thread_id }
968969
969970
971+ @pytest .mark .asyncio
972+ @pytest .mark .parametrize ("asynchronous" , [False , True ])
973+ @pytest .mark .parametrize ("search_mode" , ["find" , "search" ])
974+ @pytest .mark .parametrize ("session_id" , [None , "review-session" ])
975+ async def test_retriever_passes_session_only_to_search (
976+ monkeypatch , asynchronous , search_mode , session_id
977+ ):
978+ from openviking_sdk import AsyncHTTPClient , SyncHTTPClient
979+
980+ requests = []
981+
982+ async def request (self , method , path , ** kwargs ):
983+ requests .append ((method , path , kwargs ["json" ]))
984+ return httpx .Response (
985+ 200 ,
986+ json = {
987+ "status" : "ok" ,
988+ "result" : {
989+ "resources" : [
990+ {"uri" : "viking://resources/release.md" , "abstract" : "Release checklist" }
991+ ]
992+ },
993+ },
994+ )
995+
996+ monkeypatch .setattr (AsyncHTTPClient , "_request" , request )
997+ client_type = AsyncHTTPClient if asynchronous else SyncHTTPClient
998+ client = client_type (url = "http://openviking.test" , api_key = "test" )
999+ retriever = OpenVikingRetriever (
1000+ ** ({"async_client" : client } if asynchronous else {"client" : client }),
1001+ search_mode = search_mode ,
1002+ session_id = session_id ,
1003+ content_mode = "abstract" ,
1004+ score_threshold = 0.5 ,
1005+ tags = ["env=test" ],
1006+ )
1007+ try :
1008+ documents = (
1009+ await retriever .ainvoke ("release" ) if asynchronous else retriever .invoke ("release" )
1010+ )
1011+ finally :
1012+ await retriever .aclose ()
1013+ if asynchronous :
1014+ await client .close ()
1015+ else :
1016+ client .close ()
1017+
1018+ assert [document .page_content for document in documents ] == ["Release checklist" ]
1019+ expected = {"query" : "release" , "target_uri" : "" , "limit" : 10 , "score_threshold" : 0.5 }
1020+ expected ["tags" ] = ["env=test" ]
1021+ if search_mode == "search" and session_id is not None :
1022+ expected ["session_id" ] = session_id
1023+ assert requests == [("POST" , f"/api/v1/search/{ search_mode } " , expected )]
1024+
1025+
9701026@pytest .mark .asyncio
9711027async def test_async_recorder_preserves_batch_and_commit_semantics ():
9721028 class BatchTrackingClient (InMemoryOpenVikingClient ):
@@ -1318,16 +1374,31 @@ async def test_async_chat_history_reads_writes_and_clears_natively():
13181374
13191375
13201376@pytest .mark .asyncio
1321- async def test_async_context_assembler_combines_session_and_recall ():
1322- backing = InMemoryOpenVikingClient (
1323- {"viking://resources/runbooks/async.md" : "Async context is azure." }
1324- )
1377+ @pytest .mark .parametrize ("metadata_prefix" , ["openviking" , "recall" , "" , "my_recall" ])
1378+ @pytest .mark .parametrize (
1379+ "uri,context_type" ,
1380+ [
1381+ ("viking://resources/runbooks/async.md" , "resource" ),
1382+ ("viking://user/default/memories/preferences/async.md" , "memory" ),
1383+ ("viking://skills/async.md" , "skill" ),
1384+ ],
1385+ )
1386+ async def test_async_context_assembler_combines_session_and_recall (
1387+ metadata_prefix , uri , context_type
1388+ ):
1389+ content = "Async context is azure. " * 30
1390+ backing = InMemoryOpenVikingClient ({uri : content })
13251391 backing .add_message ("async-assembler" , "user" , content = "Active async turn." )
13261392 client = AsyncInMemoryOpenVikingClient (backing )
13271393 assembler = OpenVikingSessionContextAssembler (
13281394 include_session_context = True ,
13291395 async_client = client ,
1330- target_uri = "viking://resources" ,
1396+ retriever = OpenVikingRetriever (
1397+ async_client = client ,
1398+ target_uri = uri ,
1399+ metadata_prefix = metadata_prefix ,
1400+ search_mode = "search" ,
1401+ ),
13311402 )
13321403
13331404 assembled = await assembler .aassemble (
@@ -1337,7 +1408,23 @@ async def test_async_context_assembler_combines_session_and_recall():
13371408
13381409 assert "Active async turn." in assembled .block
13391410 assert "Async context is azure." in assembled .block
1340- assert assembled .context_parts [0 ]["uri" ] == "viking://resources/runbooks/async.md"
1411+ assert assembled .context_parts == [
1412+ {"type" : "context" , "uri" : uri , "context_type" : context_type , "abstract" : content [:240 ]}
1413+ ]
1414+ recorder = OpenVikingSessionRecorder (async_client = client )
1415+ await recorder .arecord (
1416+ "async-assembler" ,
1417+ [HumanMessage (content = "Deployment question" ), AIMessage (content = "Deployment answer" )],
1418+ context_parts = assembled .context_parts ,
1419+ )
1420+ assert [
1421+ part
1422+ for message in backing .sessions ["async-assembler" ][- 2 :]
1423+ for part in message ["parts" ]
1424+ if part ["type" ] == "context"
1425+ ] == assembled .context_parts
1426+ await recorder .aclose ()
1427+ await assembler .aclose ()
13411428
13421429
13431430@pytest .mark .asyncio
0 commit comments