|
| 1 | +from typing import Any, cast |
| 2 | + |
| 3 | +from posthog.test.base import BaseTest |
| 4 | + |
| 5 | +from langchain_core.messages import ( |
| 6 | + AIMessage as LangchainAIMessage, |
| 7 | + HumanMessage as LangchainHumanMessage, |
| 8 | +) |
| 9 | +from parameterized import parameterized |
| 10 | + |
| 11 | +from ee.hogai.graph.conversation_summarizer.nodes import AnthropicConversationSummarizer |
| 12 | + |
| 13 | + |
| 14 | +class TestAnthropicConversationSummarizer(BaseTest): |
| 15 | + def setUp(self): |
| 16 | + super().setUp() |
| 17 | + self.summarizer = AnthropicConversationSummarizer(team=self.team, user=self.user) |
| 18 | + |
| 19 | + @parameterized.expand( |
| 20 | + [ |
| 21 | + ( |
| 22 | + "single_message_with_cache_control", |
| 23 | + [ |
| 24 | + LangchainHumanMessage( |
| 25 | + content=[ |
| 26 | + {"type": "text", "text": "Hello", "cache_control": {"type": "ephemeral"}}, |
| 27 | + ] |
| 28 | + ) |
| 29 | + ], |
| 30 | + [[{"type": "text", "text": "Hello"}]], |
| 31 | + ), |
| 32 | + ( |
| 33 | + "multiple_items_with_cache_control", |
| 34 | + [ |
| 35 | + LangchainAIMessage( |
| 36 | + content=[ |
| 37 | + {"type": "text", "text": "First", "cache_control": {"type": "ephemeral"}}, |
| 38 | + {"type": "text", "text": "Second", "cache_control": {"type": "ephemeral"}}, |
| 39 | + ] |
| 40 | + ) |
| 41 | + ], |
| 42 | + [[{"type": "text", "text": "First"}, {"type": "text", "text": "Second"}]], |
| 43 | + ), |
| 44 | + ( |
| 45 | + "mixed_items_some_with_cache_control", |
| 46 | + [ |
| 47 | + LangchainHumanMessage( |
| 48 | + content=[ |
| 49 | + {"type": "text", "text": "With cache", "cache_control": {"type": "ephemeral"}}, |
| 50 | + {"type": "text", "text": "Without cache"}, |
| 51 | + ] |
| 52 | + ) |
| 53 | + ], |
| 54 | + [[{"type": "text", "text": "With cache"}, {"type": "text", "text": "Without cache"}]], |
| 55 | + ), |
| 56 | + ( |
| 57 | + "multiple_messages_with_cache_control", |
| 58 | + [ |
| 59 | + LangchainHumanMessage( |
| 60 | + content=[ |
| 61 | + {"type": "text", "text": "Message 1", "cache_control": {"type": "ephemeral"}}, |
| 62 | + ] |
| 63 | + ), |
| 64 | + LangchainAIMessage( |
| 65 | + content=[ |
| 66 | + {"type": "text", "text": "Message 2", "cache_control": {"type": "ephemeral"}}, |
| 67 | + ] |
| 68 | + ), |
| 69 | + ], |
| 70 | + [ |
| 71 | + [{"type": "text", "text": "Message 1"}], |
| 72 | + [{"type": "text", "text": "Message 2"}], |
| 73 | + ], |
| 74 | + ), |
| 75 | + ] |
| 76 | + ) |
| 77 | + def test_removes_cache_control(self, name, input_messages, expected_contents): |
| 78 | + result = self.summarizer._construct_messages(input_messages) |
| 79 | + |
| 80 | + # Extract the actual messages from the prompt template |
| 81 | + messages = result.messages[1:-1] # Skip system prompt and user prompt |
| 82 | + |
| 83 | + self.assertEqual(len(messages), len(expected_contents), f"Wrong number of messages in test case: {name}") |
| 84 | + |
| 85 | + for i, (message, expected_content) in enumerate(zip(messages, expected_contents)): |
| 86 | + self.assertEqual( |
| 87 | + message.content, |
| 88 | + expected_content, |
| 89 | + f"Message {i} content mismatch in test case: {name}", |
| 90 | + ) |
| 91 | + |
| 92 | + @parameterized.expand( |
| 93 | + [ |
| 94 | + ( |
| 95 | + "string_content", |
| 96 | + [LangchainHumanMessage(content="Simple string")], |
| 97 | + ), |
| 98 | + ( |
| 99 | + "empty_list_content", |
| 100 | + [LangchainHumanMessage(content=[])], |
| 101 | + ), |
| 102 | + ( |
| 103 | + "non_dict_items_in_list", |
| 104 | + [LangchainHumanMessage(content=["string_item", {"type": "text", "text": "dict_item"}])], |
| 105 | + ), |
| 106 | + ] |
| 107 | + ) |
| 108 | + def test_handles_non_dict_content_without_errors(self, name, input_messages): |
| 109 | + result = self.summarizer._construct_messages(input_messages) |
| 110 | + self.assertIsNotNone(result) |
| 111 | + |
| 112 | + def test_original_message_not_modified(self): |
| 113 | + original_content: list[str | dict[Any, Any]] = [ |
| 114 | + {"type": "text", "text": "Hello", "cache_control": {"type": "ephemeral"}}, |
| 115 | + ] |
| 116 | + message = LangchainHumanMessage(content=original_content) |
| 117 | + |
| 118 | + # Store the original cache_control to verify it's not modified |
| 119 | + content_list = cast(list[dict[str, Any]], message.content) |
| 120 | + self.assertIn("cache_control", content_list[0]) |
| 121 | + |
| 122 | + self.summarizer._construct_messages([message]) |
| 123 | + |
| 124 | + # Verify original message still has cache_control |
| 125 | + content_list = cast(list[dict[str, Any]], message.content) |
| 126 | + self.assertIn("cache_control", content_list[0]) |
| 127 | + self.assertEqual(content_list[0]["cache_control"], {"type": "ephemeral"}) |
| 128 | + |
| 129 | + def test_deep_copy_prevents_modification(self): |
| 130 | + original_content: list[str | dict[Any, Any]] = [ |
| 131 | + { |
| 132 | + "type": "text", |
| 133 | + "text": "Test", |
| 134 | + "cache_control": {"type": "ephemeral"}, |
| 135 | + "other_key": "value", |
| 136 | + }, |
| 137 | + ] |
| 138 | + message = LangchainHumanMessage(content=original_content) |
| 139 | + |
| 140 | + content_list = cast(list[dict[str, Any]], message.content) |
| 141 | + original_keys = set(content_list[0].keys()) |
| 142 | + |
| 143 | + self.summarizer._construct_messages([message]) |
| 144 | + |
| 145 | + # Verify original message structure unchanged |
| 146 | + content_list = cast(list[dict[str, Any]], message.content) |
| 147 | + self.assertEqual(set(content_list[0].keys()), original_keys) |
| 148 | + self.assertIn("cache_control", content_list[0]) |
| 149 | + |
| 150 | + def test_preserves_other_content_properties(self): |
| 151 | + input_messages = [ |
| 152 | + LangchainHumanMessage( |
| 153 | + content=[ |
| 154 | + { |
| 155 | + "type": "text", |
| 156 | + "text": "Hello", |
| 157 | + "cache_control": {"type": "ephemeral"}, |
| 158 | + "custom_field": "custom_value", |
| 159 | + "another_field": 123, |
| 160 | + }, |
| 161 | + ] |
| 162 | + ) |
| 163 | + ] |
| 164 | + |
| 165 | + result = self.summarizer._construct_messages(input_messages) |
| 166 | + messages = result.messages[1:-1] |
| 167 | + |
| 168 | + # Verify other fields are preserved |
| 169 | + content = messages[0].content[0] |
| 170 | + self.assertEqual(content["custom_field"], "custom_value") |
| 171 | + self.assertEqual(content["another_field"], 123) |
| 172 | + self.assertNotIn("cache_control", content) |
| 173 | + |
| 174 | + def test_empty_messages_list(self): |
| 175 | + result = self.summarizer._construct_messages([]) |
| 176 | + # Should return prompt template with just system and user prompts |
| 177 | + self.assertEqual(len(result.messages), 2) |
0 commit comments