okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1"""#4663: a call to a tool that this turn's `tools` no longer lists, but that the conversation already called, is a tool call (not raw text). 2 3python -m unittest serve.test_history_tools 4""" 5import unittest 6 7import serve .server as server 8from serve .frontend import OutputParser 9 10TODO = {"name" :"TodoWrite" ,"parameters" : {"type" :"object" ,"properties" : {"todos" : {"type" :"array" }}}} 11CALL = "<tool_call>\n<function=Bash>\n<parameter=command>\nls -la\n</parameter>\n</function>\n</tool_call>" 12 13 14def history (* names ): 15return {"messages" : [{"role" :"user" ,"content" :"go" }, 16* [{"role" :"assistant" ,"content" :"" ,"tool_calls" : [{"id" :f"c { i } " ,"type" :"function" , 17"function" : {"name" :n ,"arguments" :"{}" }}]}for i ,n in enumerate (names )]]} 18 19 20def run (tools ,text = CALL ): 21p = OutputParser (thinking = False ,tools = tools ,stream_tools = True ,recover = True ,declared_only = True ) 22evs = p .feed (text )+ p .finish ("stop" ) 23return [e .call .name for e in evs if e .kind == "tool_call" ],"" .join (e .text for e in evs if e .kind == "content" ) 24 25 26class HistoryTools (unittest .TestCase ): 27def test_a_tool_the_conversation_called_is_a_tool_call_again (self ): 28names ,answer = run (server .with_history_tools ([TODO ],history ("Bash" ))) 29self .assertEqual (names , ["Bash" ]) 30self .assertEqual (answer ,"" ) 31 32def test_without_the_history_it_stays_text (self ): 33names ,answer = run (server .with_history_tools ([TODO ],history ("Read" ))) 34self .assertEqual (names , []) 35self .assertIn ("<tool_call>" ,answer ) 36 37def test_declared_names_are_not_doubled_and_no_tools_stays_no_tools (self ): 38tools = [TODO , {"name" :"Bash" ,"parameters" : {"type" :"object" ,"properties" : {"command" : {"type" :"string" }}}}] 39self .assertIs (server .with_history_tools (tools ,history ("Bash" ,"TodoWrite" )),tools ) 40self .assertEqual (server .with_history_tools ([],history ("Bash" )), []) 41self .assertIsNone (server .with_history_tools (None ,history ("Bash" ))) 42 43def test_anthropic_tool_use_blocks_count (self ): 44req = {"messages" : [{"role" :"user" ,"content" :"go" }, 45 {"role" :"assistant" ,"content" : [{"type" :"text" ,"text" :"ok" }, 46 {"type" :"tool_use" ,"id" :"t1" ,"name" :"Bash" ,"input" : {}}]}]} 47names ,answer = run (server .with_history_tools ([TODO ],req )) 48self .assertEqual (names , ["Bash" ]) 49self .assertEqual (answer ,"" ) 50 51def test_responses_function_call_items_count (self ): 52req = {"input" : [{"type" :"message" ,"role" :"user" ,"content" :"go" }, 53 {"type" :"function_call" ,"name" :"Bash" ,"call_id" :"c1" ,"arguments" :"{}" }]} 54names ,answer = run (server .with_history_tools ([TODO ],req )) 55self .assertEqual (names , ["Bash" ]) 56self .assertEqual (answer ,"" ) 57 58def test_responses_namespace_is_part_of_the_name (self ): 59req = {"input" : [{"type" :"function_call" ,"namespace" :"mcp" ,"name" :"Bash" ,"call_id" :"c1" ,"arguments" :"{}" }]} 60self .assertEqual ([t ["name" ]for t in server .with_history_tools ([TODO ],req )], ["TodoWrite" ,"mcp.Bash" ]) 61 62def test_the_other_routes_without_history_stay_text (self ): 63names ,answer = run (server .with_history_tools ([TODO ], {"input" : [{"type" :"message" ,"role" :"user" ,"content" :"go" }]})) 64self .assertEqual (names , []) 65self .assertIn ("<tool_call>" ,answer ) 66 67def test_odd_requests_are_left_alone (self ): 68self .assertEqual (server .with_history_tools ([TODO ],None ), [TODO ]) 69self .assertEqual (server .with_history_tools ([TODO ], {"messages" : [None , {"role" :"assistant" ,"tool_calls" : [None , {}]}]}), [TODO ]) 70 71 72 73class PrefixedNames (unittest .TestCase ): 74"""#4751: `tools.write` for a declared `write` (a programmatic-tool-calling harness shows the model `tools.write(...)`).""" 75 76WRITE = {"name" :"write" ,"parameters" : {"type" :"object" ,"properties" : {"file_path" : {"type" :"string" },"content" : {"type" :"string" }}}} 77TEXT = ("<tool_call>\n<function=tools.write>\n<parameter=file_path>/a/b.py</parameter>\n" 78"<parameter=content>x = 1\n</parameter>\n</function>\n</tool_call>" ) 79 80def calls (self ,tools ,text = None ): 81p = OutputParser (thinking = False ,tools = tools ,stream_tools = True ,recover = True ,declared_only = True ) 82evs = p .feed (text or self .TEXT )+ p .finish ("stop" ) 83return [e .call for e in evs if e .kind == "tool_call" ],"" .join (e .text for e in evs if e .kind == "content" ) 84 85def test_the_prefix_is_read_as_the_declared_tool (self ): 86calls ,answer = self .calls ([self .WRITE ]) 87self .assertEqual ([(c .name ,c .arguments ["file_path" ])for c in calls ], [("write" ,"/a/b.py" )]) 88self .assertEqual (answer ,"" ) 89 90def test_functions_prefix_too (self ): 91calls ,_ = self .calls ([self .WRITE ],self .TEXT .replace ("tools.write" ,"functions.write" )) 92self .assertEqual ([c .name for c in calls ], ["write" ]) 93 94def test_an_undeclared_name_stays_text (self ): 95calls ,answer = self .calls ([self .WRITE ],self .TEXT .replace ("tools.write" ,"tools.delete" )) 96self .assertEqual (calls , []) 97self .assertIn ("<tool_call>" ,answer ) 98 99def test_a_declared_dotted_name_wins (self ): 100dotted = {"name" :"tools.write" ,"parameters" :self .WRITE ["parameters" ]} 101calls ,_ = self .calls ([self .WRITE ,dotted ]) 102self .assertEqual ([c .name for c in calls ], ["tools.write" ]) 103 104 105if __name__ == "__main__" : 106unittest .main ()