Extract the text content of the most recent user message (concatenating multiple text segments).
(messages: List[Message])
| 22 | |
| 23 | |
| 24 | def get_latest_user_text(messages: List[Message]) -> str: |
| 25 | """Extract the text content of the most recent user message (concatenating multiple text segments).""" |
| 26 | for msg in reversed(messages): |
| 27 | if msg.role == "user": |
| 28 | content = msg.content |
| 29 | if isinstance(content, str): |
| 30 | return content |
| 31 | elif isinstance(content, list): |
| 32 | parts: List[str] = [] |
| 33 | for it in content: |
| 34 | if isinstance(it, dict): |
| 35 | if it.get("type") == "text": |
| 36 | text_raw = it.get("text", "") |
| 37 | if isinstance(text_raw, str): |
| 38 | parts.append(text_raw) |
| 39 | else: |
| 40 | parts.append(str(text_raw)) |
| 41 | elif hasattr(it, "type") and it.type == "text": |
| 42 | text_attr = getattr(it, "text", "") |
| 43 | if isinstance(text_attr, str): |
| 44 | parts.append(text_attr) |
| 45 | else: |
| 46 | parts.append(str(text_attr)) |
| 47 | return "\n".join(p for p in parts if p) |
| 48 | else: |
| 49 | return "" |
| 50 | return "" |
no test coverage detected