okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
538acd098
main
1"""Chat Completions tool results must follow call IDs before the template drops them.""" 2import copy 3import http .client 4import json 5from pathlib import Path 6import unittest 7 8from serve .frontend import ChatTemplate ,openai_to_messages 9from serve .server import ByteTokenizer ,MockEngine ,Service ,serve 10 11 12TEMPLATE = ChatTemplate (Path (__file__ ).with_name ("chat_template.jinja" )) 13 14 15def history (): 16return [ 17 {"role" :"user" ,"content" :"Read a.txt and b.txt." }, 18 {"role" :"assistant" ,"content" :"" ,"tool_calls" : [ 19 {"id" :"call_a" ,"type" :"function" ,"function" : { 20"name" :"read_file" ,"arguments" :'{"path":"a.txt"}' }}, 21 {"id" :"call_b" ,"type" :"function" ,"function" : { 22"name" :"read_file" ,"arguments" :'{"path":"b.txt"}' }}]}, 23 {"role" :"tool" ,"tool_call_id" :"call_b" ,"content" :"RESULT_FROM_B" }, 24 {"role" :"tool" ,"tool_call_id" :"call_a" ,"content" :"RESULT_FROM_A" }, 25 ] 26 27 28def normalized (messages ): 29return openai_to_messages ({"messages" :messages })[0 ] 30 31 32def rendered (messages ): 33return TEMPLATE .render (normalized (messages ),enable_thinking = False ) 34 35 36class ChatToolResults (unittest .TestCase ): 37def test_reversed_results_of_the_same_function_follow_call_ids (self ): 38raw = history () 39before = copy .deepcopy (raw ) 40ordered = raw [:2 ]+ list (reversed (raw [2 :])) 41self .assertEqual (rendered (raw ),rendered (ordered )) 42self .assertLess (rendered (raw ).index ("RESULT_FROM_A" ),rendered (raw ).index ("RESULT_FROM_B" )) 43self .assertEqual (raw ,before ) 44messages = normalized (raw ) 45self .assertEqual ([m ["content" ]for m in messages [2 :]], ["RESULT_FROM_A" ,"RESULT_FROM_B" ]) 46self .assertTrue (all (set (m )== {"role" ,"content" }for m in messages [2 :])) 47self .assertTrue (all (set (c )== {"function" }for c in messages [1 ]["tool_calls" ])) 48 49def test_already_ordered_and_legacy_prompts_are_identical (self ): 50ordered = history () 51ordered [2 :]= reversed (ordered [2 :]) 52legacy = copy .deepcopy (ordered ) 53for call in legacy [1 ]["tool_calls" ]: 54call .pop ("id" ) 55for result in legacy [2 :]: 56result .pop ("tool_call_id" ) 57self .assertEqual (normalized (ordered ),normalized (legacy )) 58self .assertEqual (rendered (ordered ),rendered (legacy )) 59 60def test_ambiguous_or_incomplete_ids_preserve_received_order (self ): 61for case in ("missing_call" ,"missing_result" ,"duplicate_call" ,"duplicate_result" ,"unknown" , 62"list_call" ,"list_result" ,"empty_call" ,"empty_result" ,"partial" ): 63raw = history () 64if case == "missing_call" : 65raw [1 ]["tool_calls" ][0 ].pop ("id" ) 66elif case == "missing_result" : 67raw [2 ].pop ("tool_call_id" ) 68elif case == "duplicate_call" : 69raw [1 ]["tool_calls" ][1 ]["id" ]= "call_a" 70elif case == "duplicate_result" : 71raw [3 ]["tool_call_id" ]= "call_b" 72elif case == "unknown" : 73raw [2 ]["tool_call_id" ]= "unknown" 74elif case == "list_call" : 75raw [1 ]["tool_calls" ][0 ]["id" ]= [] 76elif case == "list_result" : 77raw [2 ]["tool_call_id" ]= [] 78elif case == "empty_call" : 79raw [1 ]["tool_calls" ][0 ]["id" ]= "" 80elif case == "empty_result" : 81raw [2 ]["tool_call_id" ]= "" 82else : 83raw .pop () 84with self .subTest (case = case ): 85self .assertEqual ([m ["content" ]for m in normalized (raw )[2 :]], 86 [m ["content" ]for m in raw [2 :]]) 87 88def test_each_tool_run_is_scoped_to_its_assistant_turn (self ): 89raw = history ()+ history () 90self .assertEqual ([m ["content" ]for m in normalized (raw )if m ["role" ]== "tool" ], 91 ["RESULT_FROM_A" ,"RESULT_FROM_B" ]* 2 ) 92raw = history () 93raw .insert (2 , {"role" :"user" ,"content" :"intervening message" }) 94self .assertEqual ([m ["content" ]for m in normalized (raw )if m ["role" ]== "tool" ], 95 ["RESULT_FROM_B" ,"RESULT_FROM_A" ]) 96 97def test_json_encoded_history_and_calls (self ): 98raw = history () 99raw [1 ]["tool_calls" ]= json .dumps (raw [1 ]["tool_calls" ]) 100self .assertEqual (normalized (json .dumps (raw )),normalized (history ())) 101 102def test_multimodal_result_moves_with_its_call (self ): 103raw = history () 104raw [2 ]["content" ]= [{"type" :"text" ,"text" :"RESULT_FROM_B" }, 105 {"type" :"image_url" ,"image_url" : {"url" :"data:image/png;base64,AA==" }}] 106messages = normalized (raw ) 107self .assertEqual (messages [2 ]["content" ],"RESULT_FROM_A" ) 108self .assertEqual (messages [3 ]["content" ], [{"type" :"text" ,"text" :"RESULT_FROM_B" }, 109 {"type" :"image" ,"source" :"data:image/png;base64,AA==" }]) 110 111def test_http_engine_receives_the_ordered_prompt (self ): 112tok = ByteTokenizer () 113engine = MockEngine (tok ,"ok" ,max_context = 4096 ) 114service = Service (engine ,tok ,TEMPLATE ) 115httpd = serve (service ,port = 0 ) 116connection = http .client .HTTPConnection ("127.0.0.1" ,httpd .server_address [1 ],timeout = 10 ) 117try : 118connection .request ("POST" ,"/v1/chat/completions" ,json .dumps ({ 119"messages" :history (),"max_tokens" :16 ,"reasoning_effort" :"none" }), 120 {"Content-Type" :"application/json" }) 121response = connection .getresponse () 122body = response .read () 123self .assertEqual (response .status ,200 ,body ) 124prompt = tok .decode (engine .last_prompt ) 125self .assertLess (prompt .index ("RESULT_FROM_A" ),prompt .index ("RESULT_FROM_B" )) 126finally : 127connection .close () 128httpd .shutdown () 129httpd .server_close () 130 131 132if __name__ == "__main__" : 133unittest .main ()