okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b82d1d456
main
1"""#4078: a string argument that quotes a whole tool call, or reopens its own parameter, stays exact. 2 3python -m unittest serve.test_quoted_call_values -v 4""" 5import json 6import unittest 7 8from serve .frontend import (CALL_END ,CALL_START ,FUNC_END ,FUNC_START ,PARAM_END ,PARAM_START ,OutputParser , 9parse_tool_call ) 10 11SCHEMA = {"name" :"write" ,"parameters" : {"properties" : {"path" : {"type" :"string" },"content" : {"type" :"string" }}}} 12QUOTED = (f" { CALL_START } \n { FUNC_START } write>\n { PARAM_START } path>\nx.txt\n { PARAM_END } \n { FUNC_END } \n { CALL_END } " ) 13VALUES = { 14"quoted call" :f"example transcript:\n { QUOTED } \nafter block" , 15"quoted call at the end" :f"example transcript:\n { QUOTED } " , 16"heredoc" :f"python3 - <<'PY'\nprint(''' { QUOTED } ''')\nPY" , 17"repeated parameter" :f"head part\n { PARAM_END } \n { PARAM_START } content>\ntail part" , 18"unclosed opener in prose" :f"A call starts with ` { FUNC_START } NAME>` and its parameters follow." , 19"unclosed opener in code" :f'opener = " { FUNC_START } "\n' , 20} 21 22 23def call_text (value ): 24return (f" { CALL_START } \n { FUNC_START } write>\n { PARAM_START } path>\nprobe.txt\n { PARAM_END } \n" 25f" { PARAM_START } content>\n { value } \n { PARAM_END } \n { FUNC_END } \n { CALL_END } " ) 26 27 28class QuotedCallValues (unittest .TestCase ): 29def test_values_are_exact_whole_streamed_and_chunked (self ): 30for label ,value in VALUES .items (): 31text = call_text (value ) 32expected = {"path" :"probe.txt" ,"content" :value } 33self .assertEqual (parse_tool_call (text [len (CALL_START ):- len (CALL_END )],SCHEMA ).arguments ,expected ) 34for stream in (False ,True ): 35for size in (1 ,7 ,len (text )): 36with self .subTest (label = label ,stream = stream ,size = size ): 37parser = OutputParser (thinking = False ,tools = [SCHEMA ],stream_tools = stream ) 38events = [] 39for i in range (0 ,len (text ),size ): 40events .extend (parser .feed (text [i :i + size ])) 41events .extend (parser .finish ()) 42calls = [e .call for e in events if e .kind == "tool_call" ] 43self .assertEqual ([c .arguments for c in calls ], [expected ]) 44self .assertEqual ("" .join (e .text for e in events if e .kind == "content" ).strip (),"" ) 45if stream : 46self .assertEqual (json .loads ("" .join (e .text for e in events if e .kind == "tool_args" )), 47expected ) 48 49def test_prose_between_two_calls_still_ends_the_first (self ): 50first ,second = call_text ("one" ),call_text ("two" ) 51parser = OutputParser (thinking = False ,tools = [SCHEMA ],stream_tools = True ) 52events = parser .feed (first + "\nnow the second:\n" + second )+ parser .finish () 53self .assertEqual ([e .call .arguments ["content" ]for e in events if e .kind == "tool_call" ], ["one" ,"two" ]) 54 55 56if __name__ == "__main__" : 57unittest .main ()