@@ -1912,6 +1912,115 @@ async def test_find_turns_by_content():
19121912 session .close ()
19131913
19141914
1915+ @pytest .mark .parametrize (
1916+ ("earlier_content" , "matching_content" , "search_term" ),
1917+ [
1918+ ("Dogs" , "Tell me about cats" , "CATS" ),
1919+ ("Tokyo" , "大阪の天気" , "大阪" ),
1920+ ("Hello" , "你好世界" , "你好" ),
1921+ (r"Literal \u00e9" , "café" , "é" ),
1922+ ("café" , r"Literal \u00e9" , r"\u00e9" ),
1923+ ("CAFÉ" , "café" , "é" ),
1924+ (r"Literal \n" , "First\n second" , "\n " ),
1925+ ("First\n second" , r"Literal \n" , r"\n" ),
1926+ ("No quotes" , 'Say "hello"' , '"hello"' ),
1927+ ("Plain path" , r"C:\notes" , r"C:\notes" ),
1928+ ("100 dollars 東京" , "100% 東京" , "100% 東京" ),
1929+ ("axb 東京" , "a_b 東京" , "a_b 東京" ),
1930+ ("No percent" , "50%" , "%" ),
1931+ ("No underscore" , "a_b" , "_" ),
1932+ ],
1933+ )
1934+ async def test_find_turns_by_content_matches_literal_text (
1935+ earlier_content : str , matching_content : str , search_term : str
1936+ ):
1937+ """Search decoded content, without interpreting JSON escapes or SQL wildcards."""
1938+ session = AdvancedSQLiteSession (session_id = "literal_search" , create_tables = True )
1939+ try :
1940+ await session .add_items ([{"role" : "user" , "content" : earlier_content }])
1941+ await session .add_items ([{"role" : "user" , "content" : matching_content }])
1942+
1943+ matches = await session .find_turns_by_content (search_term )
1944+ assert [turn ["turn" ] for turn in matches ] == [2 ]
1945+ assert matches [0 ]["full_content" ] == matching_content
1946+ assert matches [0 ]["content" ] == matching_content
1947+ finally :
1948+ session .close ()
1949+
1950+
1951+ @pytest .mark .parametrize ("structured" , [False , True ], ids = ["string" , "text_parts" ])
1952+ async def test_create_branch_from_content_uses_decoded_match (structured : bool ):
1953+ """A literal escape in an earlier turn must not become the branch point."""
1954+ session = AdvancedSQLiteSession (session_id = "decoded_branch" , create_tables = True )
1955+ earlier_item : TResponseInputItem = {"role" : "user" , "content" : r"Literal \u00e9" }
1956+ matching_item : TResponseInputItem = {"role" : "user" , "content" : "café" }
1957+ if structured :
1958+ earlier_item ["content" ] = [{"type" : "input_text" , "text" : r"Literal \u00e9" }]
1959+ matching_item ["content" ] = [{"type" : "input_text" , "text" : "café" }]
1960+ try :
1961+ await session .add_items ([earlier_item ])
1962+ await session .add_items ([matching_item ])
1963+
1964+ matches = await session .find_turns_by_content ("é" )
1965+ assert [turn ["turn" ] for turn in matches ] == [2 ]
1966+ assert matches [0 ]["full_content" ] == matching_item ["content" ]
1967+
1968+ assert await session .create_branch_from_content ("é" , "cafe_branch" ) == "cafe_branch"
1969+ assert await session .get_items () == [earlier_item ]
1970+ finally :
1971+ session .close ()
1972+
1973+
1974+ async def test_find_turns_by_content_with_escaped_text_parts ():
1975+ """Structured text is searched after decoding, without matching the message envelope."""
1976+ session = AdvancedSQLiteSession (session_id = "escaped_text_parts" , create_tables = True )
1977+ try :
1978+ await session .add_items (
1979+ [{"role" : "user" , "content" : [{"type" : "input_text" , "text" : r"Literal \n" }]}]
1980+ )
1981+ await session .add_items (
1982+ [
1983+ {
1984+ "role" : "user" ,
1985+ "content" : [
1986+ {"type" : "input_text" , "text" : '東京 "café"\n 50% a_b' },
1987+ {"type" : "input_image" , "image_url" : "https://example.com/image.png" },
1988+ ],
1989+ }
1990+ ]
1991+ )
1992+
1993+ for search_term in ('東京 "café"' , "\n " , "50% a_b" ):
1994+ assert [turn ["turn" ] for turn in await session .find_turns_by_content (search_term )] == [
1995+ 2
1996+ ]
1997+ assert [turn ["turn" ] for turn in await session .find_turns_by_content (r"\n" )] == [1 ]
1998+ assert await session .find_turns_by_content ("user" ) == []
1999+ finally :
2000+ session .close ()
2001+
2002+
2003+ async def test_find_turns_by_content_filters_session_and_branch (tmp_path : Path ):
2004+ """Decoded matching retains the query's session and branch boundaries."""
2005+ db_path = tmp_path / "content_search.db"
2006+ session = AdvancedSQLiteSession (session_id = "search" , db_path = db_path , create_tables = True )
2007+ other = AdvancedSQLiteSession (session_id = "other" , db_path = db_path , create_tables = True )
2008+ try :
2009+ await other .add_items ([{"role" : "user" , "content" : "東京 other session" }])
2010+ await session .add_items ([{"role" : "user" , "content" : "First turn" }])
2011+ await session .add_items ([{"role" : "user" , "content" : "東京 main" }])
2012+ await session .create_branch_from_turn (2 , "alternate" )
2013+ await session .add_items ([{"role" : "user" , "content" : "東京 alternate" }])
2014+
2015+ matches = await session .find_turns_by_content ("東京" )
2016+ assert [turn ["full_content" ] for turn in matches ] == ["東京 alternate" ]
2017+ matches = await session .find_turns_by_content ("東京" , branch_id = "main" )
2018+ assert [turn ["full_content" ] for turn in matches ] == ["東京 main" ]
2019+ finally :
2020+ session .close ()
2021+ other .close ()
2022+
2023+
19152024async def test_get_conversation_turns_with_list_content ():
19162025 """List (multimodal) content is previewed as a string instead of crashing or leaking a list."""
19172026 session_id = "conversation_turns_list_content_test"
0 commit comments