| 1 | import pytest
|
| 2 |
|
| 3 | from minisweagent.models.utils.anthropic_utils import _reorder_anthropic_thinking_blocks
|
| 4 |
|
| 5 |
|
| 6 | @pytest.mark.parametrize(
|
| 7 | ("messages", "expected"),
|
| 8 | [
|
| 9 | # No thinking blocks - unchanged
|
| 10 | (
|
| 11 | [{"role": "assistant", "content": [{"type": "text", "text": "hello"}]}],
|
| 12 | [{"role": "assistant", "content": [{"type": "text", "text": "hello"}]}],
|
| 13 | ),
|
| 14 | # Thinking block already first - unchanged
|
| 15 | (
|
| 16 | [
|
| 17 | {
|
| 18 | "role": "assistant",
|
| 19 | "content": [
|
| 20 | {"type": "thinking", "thinking": "..."},
|
| 21 | {"type": "text", "text": "hello"},
|
| 22 | ],
|
| 23 | }
|
| 24 | ],
|
| 25 | [
|
| 26 | {
|
| 27 | "role": "assistant",
|
| 28 | "content": [
|
| 29 | {"type": "thinking", "thinking": "..."},
|
| 30 | {"type": "text", "text": "hello"},
|
| 31 | ],
|
| 32 | }
|
| 33 | ],
|
| 34 | ),
|
| 35 | # Thinking block after text - reorder
|
| 36 | (
|
| 37 | [
|
| 38 | {
|
| 39 | "role": "assistant",
|
| 40 | "content": [
|
| 41 | {"type": "text", "text": "hello"},
|
| 42 | {"type": "thinking", "thinking": "..."},
|
| 43 | ],
|
| 44 | }
|
| 45 | ],
|
| 46 | [
|
| 47 | {
|
| 48 | "role": "assistant",
|
| 49 | "content": [
|
| 50 | {"type": "thinking", "thinking": "..."},
|
| 51 | {"type": "text", "text": "hello"},
|
| 52 | ],
|
| 53 | }
|
| 54 | ],
|
| 55 | ),
|
| 56 | # Multiple thinking blocks mixed
|
| 57 | (
|
| 58 | [
|
| 59 | {
|
| 60 | "role": "assistant",
|
| 61 | "content": [
|
| 62 | {"type": "text", "text": "a"},
|
| 63 | {"type": "thinking", "thinking": "1"},
|
| 64 | {"type": "text", "text": "b"},
|
| 65 | {"type": "redacted_thinking"},
|
| 66 | ],
|
| 67 | }
|
| 68 | ],
|
| 69 | [
|
| 70 | {
|
| 71 | "role": "assistant",
|
| 72 | "content": [
|
| 73 | {"type": "thinking", "thinking": "1"},
|
| 74 | {"type": "redacted_thinking"},
|
| 75 | {"type": "text", "text": "a"},
|
| 76 | {"type": "text", "text": "b"},
|
| 77 | ],
|
| 78 | }
|
| 79 | ],
|
| 80 | ),
|
| 81 | # Only thinking blocks - adds empty text block
|
| 82 | (
|
| 83 | [{"role": "assistant", "content": [{"type": "thinking", "thinking": "..."}]}],
|
| 84 | [
|
| 85 | {
|
| 86 | "role": "assistant",
|
| 87 | "content": [{"type": "thinking", "thinking": "..."}, {"type": "text", "text": ""}],
|
| 88 | }
|
| 89 | ],
|
| 90 | ),
|
| 91 | # Non-assistant message - unchanged
|
| 92 | (
|
| 93 | [{"role": "user", "content": [{"type": "text", "text": "hello"}]}],
|
| 94 | [{"role": "user", "content": [{"type": "text", "text": "hello"}]}],
|
| 95 | ),
|
| 96 | # String content - unchanged
|
| 97 | (
|
| 98 | [{"role": "assistant", "content": "hello"}],
|
| 99 | [{"role": "assistant", "content": "hello"}],
|
| 100 | ),
|
| 101 | # Preserves other message fields
|
| 102 | (
|
| 103 | [
|
| 104 | {
|
| 105 | "role": "assistant",
|
| 106 | "content": [{"type": "text", "text": "hi"}, {"type": "thinking", "thinking": "..."}],
|
| 107 | "extra_field": "preserved",
|
| 108 | }
|
| 109 | ],
|
| 110 | [
|
| 111 | {
|
| 112 | "role": "assistant",
|
| 113 | "content": [{"type": "thinking", "thinking": "..."}, {"type": "text", "text": "hi"}],
|
| 114 | "extra_field": "preserved",
|
| 115 | }
|
| 116 | ],
|
| 117 | ),
|
| 118 | ],
|
| 119 | )
|
| 120 | def test_reorder_thinking_blocks(messages, expected):
|
| 121 | assert _reorder_anthropic_thinking_blocks(messages) == expected
|
| 122 |
|