okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1"""#4701 decision data: a file whose text lists </parameter>, </function>, </tool_call> on lines of their own, written through a real call. 2 3Option C (decided 10-30, phaseA-tests/urgent029/OPTIONS-4701-4704.md): the closing sequence ends the value unless prose follows it AND a later 4closing sequence follows that prose, with no new <tool_call> opener in between. While the turn is still streaming a call that might go on is 5held, bounded by the end of the turn, a new <tool_call>, or CLOSE_LOOKAHEAD_CHARS of text. 6 7python -m unittest serve.test_param_end_options 8""" 9import unittest 10 11from serve .frontend import OutputParser 12 13 14def tool (name ,** props ): 15return {"name" :name ,"parameters" : {"type" :"object" ,"properties" :props }} 16 17 18S = {"type" :"string" } 19TOOLS = [tool ("read" ,path = S ),tool ("write_file" ,path = S ,content = S )] 20DOC = "# Tool calls\n\nThe closing tags, in order:\n\n</parameter>\n</function>\n</tool_call>\n\nNothing may follow them." 21READ = "<tool_call>\n<function=read>\n<parameter=path>\na.txt\n</parameter>\n</function>\n</tool_call>" 22WRITE = ("<tool_call>\n<function=write_file>\n<parameter=path>\nd.md\n</parameter>\n<parameter=content>\n" + DOC 23+ "\n</parameter>\n</function>\n</tool_call>" ) 24 25 26def parse (text ): 27p = OutputParser (thinking = False ,tools = TOOLS ,stream_tools = False ,recover = False ,declared_only = True ) 28evs = p .feed (text )+ p .finish ("stop" ) 29return [(e .call .name ,e .call .arguments )for e in evs if e .kind == "tool_call" ],"" .join (e .text for e in evs if e .kind == "content" ) 30 31 32class ParamEndOptions (unittest .TestCase ): 33def test_a_file_that_lists_the_closing_tags_keeps_all_its_text (self ): 34calls ,answer = parse (WRITE ) 35self .assertEqual (calls , [("write_file" , {"path" :"d.md" ,"content" :DOC })]) 36self .assertEqual (answer ,"" ) 37 38def test_a_call_followed_by_a_sentence_is_still_a_call (self ): 39calls ,answer = parse (READ + "\n\nI will read that file now." ) 40self .assertEqual (calls , [("read" , {"path" :"a.txt" })]) 41self .assertIn ("I will read" ,answer ) 42 43def test_two_calls_with_a_sentence_between_are_two_calls (self ): 44calls ,_ = parse (READ + "\nOkay.\n" + READ ) 45self .assertEqual (len (calls ),2 ) 46 47 48def stream (text ,piece ,finish = True ): 49p = OutputParser (thinking = False ,tools = TOOLS ,stream_tools = False ,recover = False ,declared_only = True ) 50seen = []# (characters fed so far, calls delivered so far) 51n = 0 52for i in range (0 ,len (text ),piece ): 53n += len (text [i :i + piece ]) 54got = [e for e in p .feed (text [i :i + piece ])if e .kind == "tool_call" ] 55seen .append ((n ,len (got ))) 56evs = p .finish ("stop" )if finish else [] 57calls = [(e .call .name ,e .call .arguments )for e in evs if e .kind == "tool_call" ] 58return seen ,calls ,"" .join (e .text for e in evs if e .kind == "content" ) 59 60 61class Streaming (unittest .TestCase ): 62def test_the_file_comes_whole_however_it_is_cut_into_pieces (self ): 63for piece in (1 ,3 ,7 ,50 ,len (WRITE )): 64p = OutputParser (thinking = False ,tools = TOOLS ,stream_tools = False ,recover = False ,declared_only = True ) 65evs = [] 66for i in range (0 ,len (WRITE ),piece ): 67evs += p .feed (WRITE [i :i + piece ]) 68evs += p .finish ("stop" ) 69calls = [(e .call .name ,e .call .arguments )for e in evs if e .kind == "tool_call" ] 70self .assertEqual (calls , [("write_file" , {"path" :"d.md" ,"content" :DOC })],piece ) 71self .assertEqual ("" .join (e .text for e in evs if e .kind == "content" ),"" ,piece ) 72 73def test_an_ordinary_call_is_delivered_when_the_turn_ends_and_not_before_a_second_call_is_needed (self ): 74for piece in (1 ,7 ): 75p = OutputParser (thinking = False ,tools = TOOLS ,stream_tools = False ,recover = False ,declared_only = True ) 76got = [] 77for i in range (0 ,len (READ ),piece ): 78got += [e for e in p .feed (READ [i :i + piece ])if e .kind == "tool_call" ] 79got += [e for e in p .finish ("stop" )if e .kind == "tool_call" ] 80self .assertEqual (len (got ),1 ) 81 82def test_a_second_call_releases_the_first_at_once (self ): 83p = OutputParser (thinking = False ,tools = TOOLS ,stream_tools = False ,recover = False ,declared_only = True ) 84first = [e for e in p .feed (READ + "\n<tool_call>" )if e .kind == "tool_call" ] 85self .assertEqual (len (first ),1 ) 86 87def test_a_long_sentence_after_a_call_cannot_hold_it_for_ever (self ): 88from serve .frontend import CLOSE_LOOKAHEAD_CHARS 89p = OutputParser (thinking = False ,tools = TOOLS ,stream_tools = False ,recover = False ,declared_only = True ) 90got = [e for e in p .feed (READ + "\n\n" + "word " * 200 )if e .kind == "tool_call" ] 91self .assertEqual (len (got ),0 )# still inside the bound 92got = [e for e in p .feed ("more " * 200 )if e .kind == "tool_call" ] 93self .assertEqual (len (got ),1 )# past it: the call goes out while the model is still talking 94 95 96if __name__ == "__main__" : 97unittest .main ()