okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
99866a47a
main
1"""#4702 and #4703: a call quoted in a Markdown code form is text, not a call. A four-backtick fence is closed only by a fence of 2at least four backticks (an inner ``` block stays inside it), and an indented (4-space) code block after a blank line is a quote. 3A call the model writes plainly, or indents inside a paragraph, is still a call. 4 5python -m unittest serve.test_parser_markdown_code 6""" 7import unittest 8 9from serve .frontend import OutputParser 10 11 12def tool (name ,** props ): 13return {"name" :name ,"parameters" : {"type" :"object" ,"properties" :props }} 14 15 16S = {"type" :"string" } 17TOOLS = [tool ("read" ,path = S ),tool ("write_file" ,path = S ,content = S )] 18 19 20def qwen (name ,** params ): 21body = "" .join (f"<parameter= { k } >\n { v } \n</parameter>\n" for k ,v in params .items ()) 22return f"<tool_call>\n<function= { name } >\n { body } </function>\n</tool_call>" 23 24 25def parse (text ,piece = 0 ,recover = False ): 26p = OutputParser (thinking = False ,tools = TOOLS ,stream_tools = False ,recover = recover ,declared_only = True ) 27if piece : 28evs = [] 29for i in range (0 ,len (text ),piece ): 30evs += p .feed (text [i :i + piece ]) 31evs += p .finish ("stop" ) 32else : 33evs = p .feed (text )+ p .finish ("stop" ) 34return ([(e .call .name ,e .call .arguments )for e in evs if e .kind == "tool_call" ], 35"" .join (e .text for e in evs if e .kind == "content" )) 36 37 38class MarkdownCode (unittest .TestCase ): 39def each (self ,text ): 40for recover in (False ,True ): 41for piece in (0 ,1 ,7 ): 42yield parse (text ,piece ,recover ) 43 44def test_an_inner_fence_does_not_close_a_longer_one (self ): 45text = ("Save this as docs/TOOLS.md:\n\n````markdown\n# Tool calls\n\nA call:\n\n```xml\n" 46+ qwen ("read" ,path = "EXAMPLE.txt" )+ "\n```\n````\n\nThat is the file." ) 47for calls ,answer in self .each (text ): 48self .assertEqual (calls , []) 49self .assertEqual (answer ,text ) 50 51def test_a_call_after_a_closed_longer_fence_is_a_call (self ): 52text = "Here:\n\n````md\nexample\n````\n\n" + qwen ("read" ,path = "a.txt" ) 53for calls ,_ in self .each (text ): 54self .assertEqual (calls , [("read" , {"path" :"a.txt" })]) 55 56def test_a_tilde_fence_is_not_closed_by_backticks (self ): 57text = "Quote:\n\n~~~\n```\n" + qwen ("read" ,path = "EXAMPLE.txt" )+ "\n```\n~~~\n" 58for calls ,_ in self .each (text ): 59self .assertEqual (calls , []) 60 61def test_an_indented_code_block_is_a_quote (self ): 62block = "\n" .join (" " + line for line in qwen ("read" ,path = "EXAMPLE.txt" ).split ("\n" )) 63text = "The format, indented as code:\n\n" + block + "\n\nIndentation makes it a code block in Markdown." 64for calls ,answer in self .each (text ): 65self .assertEqual (calls , []) 66self .assertEqual (answer ,text ) 67 68def test_a_plain_call_after_a_paragraph_is_still_a_call (self ): 69text = "Let me read it.\n" + qwen ("read" ,path = "a.txt" ) 70for calls ,_ in self .each (text ): 71self .assertEqual (calls , [("read" , {"path" :"a.txt" })]) 72 73def test_a_call_after_an_indented_block_ends_it (self ): 74text = "Example:\n\n x = 1\n\nNow the real one:\n\n" + qwen ("read" ,path = "a.txt" ) 75for calls ,_ in self .each (text ): 76self .assertEqual (calls , [("read" , {"path" :"a.txt" })]) 77 78 79if __name__ == "__main__" : 80unittest .main ()