File size: 1,765 Bytes
0bb4dfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from app.utils.sanitize import strip_thinking, response_has_thinking


def test_strip_simple_think_tag():
    assert strip_thinking("<think>plan</think>final") == "final"


def test_strip_multiline_think_tag():
    raw = "<think>line1\nline2\n</think>actual reply"
    assert strip_thinking(raw) == "actual reply"


def test_strip_nested_reasoning_blocks():
    raw = (
        "<reasoning>step 1\nstep 2</reasoning>"
        "<analysis>more thinking</analysis>"
        "real text"
    )
    assert strip_thinking(raw) == "real text"


def test_strip_multiple_think_blocks():
    raw = "<think>a</think>middle<think>b</think>end"
    assert strip_thinking(raw) == "middleend"


def test_strip_uppercase_tag():
    assert strip_thinking("<THINK>plan</THINK>final") == "final"


def test_idempotent():
    cleaned = strip_thinking("<think>foo</think>bar")
    assert strip_thinking(cleaned) == cleaned


def test_empty_inputs_safe():
    assert strip_thinking(None) == ""
    assert strip_thinking("") == ""
    assert strip_thinking("   \n\t  ") == ""


def test_thought_prologue():
    raw = "Thought: I should probably mention X.\n\nReal response here."
    assert strip_thinking(raw) == "Real response here."


def test_response_has_thinking_via_text():
    assert response_has_thinking("<think>plan</think>x")
    assert not response_has_thinking("just plain text")


def test_response_has_thinking_via_msg_field():
    assert response_has_thinking("plain text", {"reasoning_content": "stuff"})
    assert response_has_thinking("plain text", {"reasoning": "stuff"})
    assert not response_has_thinking("plain text", {"content": "x"})


def test_strip_framing_tokens():
    raw = "<|reasoning|>plan<|/reasoning|>final"
    assert strip_thinking(raw) == "final"