okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-029-urgent (bc694233b)99866a47a

main
3.3 KiB80 linesraw
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
5    python -m unittest serve.test_parser_markdown_code
6"""
7import unittest
8
9from serve.frontend import OutputParser
10
11
12def tool(name, **props):
13    return {"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):
21    body = "".join(f"<parameter={k}>\n{v}\n</parameter>\n" for k, v in params.items())
22    return f"<tool_call>\n<function={name}>\n{body}</function>\n</tool_call>"
23
24
25def parse(text, piece=0, recover=False):
26    p = OutputParser(thinking=False, tools=TOOLS, stream_tools=False, recover=recover, declared_only=True)
27    if piece:
28        evs = []
29        for i in range(0, len(text), piece):
30            evs += p.feed(text[i:i + piece])
31        evs += p.finish("stop")
32    else:
33        evs = p.feed(text) + p.finish("stop")
34    return ([(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):
39    def each(self, text):
40        for recover in (False, True):
41            for piece in (0, 1, 7):
42                yield parse(text, piece, recover)
43
44    def test_an_inner_fence_does_not_close_a_longer_one(self):
45        text = ("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.")
47        for calls, answer in self.each(text):
48            self.assertEqual(calls, [])
49            self.assertEqual(answer, text)
50
51    def test_a_call_after_a_closed_longer_fence_is_a_call(self):
52        text = "Here:\n\n````md\nexample\n````\n\n" + qwen("read", path="a.txt")
53        for calls, _ in self.each(text):
54            self.assertEqual(calls, [("read", {"path": "a.txt"})])
55
56    def test_a_tilde_fence_is_not_closed_by_backticks(self):
57        text = "Quote:\n\n~~~\n```\n" + qwen("read", path="EXAMPLE.txt") + "\n```\n~~~\n"
58        for calls, _ in self.each(text):
59            self.assertEqual(calls, [])
60
61    def test_an_indented_code_block_is_a_quote(self):
62        block = "\n".join("    " + line for line in qwen("read", path="EXAMPLE.txt").split("\n"))
63        text = "The format, indented as code:\n\n" + block + "\n\nIndentation makes it a code block in Markdown."
64        for calls, answer in self.each(text):
65            self.assertEqual(calls, [])
66            self.assertEqual(answer, text)
67
68    def test_a_plain_call_after_a_paragraph_is_still_a_call(self):
69        text = "Let me read it.\n" + qwen("read", path="a.txt")
70        for calls, _ in self.each(text):
71            self.assertEqual(calls, [("read", {"path": "a.txt"})])
72
73    def test_a_call_after_an_indented_block_ends_it(self):
74        text = "Example:\n\n    x = 1\n\nNow the real one:\n\n" + qwen("read", path="a.txt")
75        for calls, _ in self.each(text):
76            self.assertEqual(calls, [("read", {"path": "a.txt"})])
77
78
79if __name__ == "__main__":
80    unittest.main()