|
| 1 | +import copy |
| 2 | + |
| 3 | +import pytest |
<
8000
/code> | 4 | + |
| 5 | +from strands.event_loop import message_processor |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.parametrize( |
| 9 | + "messages,expected,expected_messages", |
| 10 | + [ |
| 11 | + # Orphaned toolUse with empty input, no toolResult |
| 12 | + ( |
| 13 | + [ |
| 14 | + {"role": "assistant", "content": [{"toolUse": {"toolUseId": "1", "input": {}, "name": "foo"}}]}, |
| 15 | + {"role": "user", "content": [{"toolResult": {"toolUseId": "2"}}]}, |
| 16 | + ], |
| 17 | + True, |
| 18 | + [ |
| 19 | + {"role": "assistant", "content": [{"text": "[Attempted to use foo, but operation was canceled]"}]}, |
| 20 | + {"role": "user", "content": [{"toolResult": {"toolUseId": "2"}}]}, |
| 21 | + ], |
| 22 | + ), |
| 23 | + # toolUse with input, has matching toolResult |
| 24 | + ( |
| 25 | + [ |
| 26 | + {"role": "assistant", "content": [{"toolUse": {"toolUseId": "1", "input": {"a": 1}, "name": "foo"}}]}, |
| 27 | + {"role": "user", "content": [{"toolResult": {"toolUseId": "1"}}]}, |
| 28 | + ], |
| 29 | + False, |
| 30 | + [ |
| 31 | + {"role": "assistant", "content": [{"toolUse": {"toolUseId": "1", "input": {"a": 1}, "name": "foo"}}]}, |
| 32 | + {"role": "user", "content": [{"toolResult": {"toolUseId": "1"}}]}, |
| 33 | + ], |
| 34 | + ), |
| 35 | + # No messages |
| 36 | + ( |
| 37 | + [], |
| 38 | + False, |
| 39 | + [], |
| 40 | + ), |
| 41 | + ], |
| 42 | +) |
| 43 | +def test_clean_orphaned_empty_tool_uses(messages, expected, expected_messages): |
| 44 | + test_messages = copy.deepcopy(messages) |
| 45 | + result = message_processor.clean_orphaned_empty_tool_uses(test_messages) |
| 46 | + assert result == expected |
| 47 | + assert test_messages == expected_messages |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize( |
| 51 | + "messages,expected_idx", |
| 52 | + [ |
| 53 | + ( |
| 54 | + [ |
| 55 | + {"role": "user", "content": [{"text": "hi"}]}, |
| 56 | + {"role": "user", "content": [{"toolResult": {"toolUseId": "1"}}]}, |
| 57 | + {"role": "assistant", "content": [{"text": "ok"}]}, |
| 58 | + ], |
| 59 | + 1, |
| 60 | + ), |
| 61 | + ( |
| 62 | + [ |
| 63 | + {"role": "user", "content": [{"text": "hi"}]}, |
| 64 | + {"role": "assistant", "content": [{"text": "ok"}]}, |
| 65 | + ], |
| 66 | + None, |
| 67 | + ), |
| 68 | + ( |
| 69 | + [], |
| 70 | + None, |
| 71 | + ), |
| 72 | + ], |
| 73 | +) |
| 74 | +def test_find_last_message_with_tool_results(messages, expected_idx): |
| 75 | + idx = message_processor.find_last_message_with_tool_results(messages) |
| 76 | + assert idx == expected_idx |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.parametrize( |
| 80 | + "messages,msg_idx,expected_changed,expected_content", |
| 81 | + [ |
| 82 | + ( |
| 83 | + [ |
| 84 | + { |
| 85 | + "role": "user", |
| 86 | + "content": [{"toolResult": {"toolUseId": "1", "status": "ok", "content": [{"text": "big"}]}}], |
| 87 | + } |
| 88 | + ], |
| 89 | + 0, |
| 90 | + True, |
| 91 | + [ |
| 92 | + { |
| 93 | + "toolResult": { |
| 94 | + "toolUseId": "1", |
| 95 | + "status": "error", |
| 96 | + "content": [{"text": "The tool result was too large!"}], |
| 97 | + } |
| 98 | + } |
| 99 | + ], |
| 100 | + ), |
| 101 | + ( |
| 102 | + [{"role": "user", "content": [{"text": "no tool result"}]}], |
| 103 | + 0, |
| 104 | + False, |
| 105 | + [{"text": "no tool result"}], |
| 106 | + ), |
| 107 | + ( |
| 108 | + [], |
| 109 | + 0, |
| 110 | + False, |
| 111 | + [], |
| 112 | + ), |
| 113 | + ( |
| 114 | + [{"role": "user", "content": [{"toolResult": {"toolUseId": "1"}}]}], |
| 115 | + 2, |
| 116 | + False, |
| 117 | + [{"toolResult": {"toolUseId": "1"}}], |
| 118 | + ), |
| 119 | + ], |
| 120 | +) |
| 121 | +def test_truncate_tool_results(messages, msg_idx, expected_changed, expected_content): |
| 122 | + test_messages = copy.deepcopy(messages) |
| 123 | + changed = message_processor.truncate_tool_results(test_messages, msg_idx) |
| 124 | + assert changed == expected_changed |
| 125 | + if 0 <= msg_idx < len(test_messages): |
| 126 | + assert test_messages[msg_idx]["content"] == expected_content |
| 127 | + else: |
| 128 | + assert test_messages == messages |
0 commit comments