|
| 1 | +from typing import Any |
| 2 | + |
| 3 | + |
| 4 | +def find_items_with_images(items: list[dict[str, Any]]) -> list[int]: |
| 5 | + """ |
| 6 | + Finds all items in the conversation history that contain images |
| 7 | +
|
| 8 | + Args: |
| 9 | + items: Array of conversation items to check |
| 10 | +
|
| 11 | + Returns: |
| 12 | + Array of indices where images were found |
| 13 | + """ |
| 14 | + items_with_images = [] |
| 15 | + |
| 16 | + for index, item in enumerate(items): |
| 17 | + has_image = False |
| 18 | + |
| 19 | + if isinstance(item.get("content"), list): |
| 20 | + has_image = any( |
| 21 | + content_item.get("type") == "tool_result" |
| 22 | + and "content" in content_item |
| 23 | + and isinstance(content_item["content"], list) |
| 24 | + and any( |
| 25 | + nested_item.get("type") == "image" |
| 26 | + for nested_item in content_item["content"] |
| 27 | + if isinstance(nested_item, dict) |
| 28 | + ) |
| 29 | + for content_item in item["content"] |
| 30 | + if isinstance(content_item, dict) |
| 31 | + ) |
| 32 | + |
| 33 | + if has_image: |
| 34 | + items_with_images.append(index) |
| 35 | + |
| 36 | + return items_with_images |
| 37 | + |
| 38 | + |
| 39 | +def compress_conversation_images( |
| 40 | + items: list[dict[str, Any]], keep_most_recent_count: int = 2 |
| 41 | +) -> dict[str, list[dict[str, Any]]]: |
| 42 | + """ |
| 43 | + Compresses conversation history by removing images from older items |
| 44 | + while keeping the most recent images intact |
| 45 | +
|
| 46 | + Args: |
| 47 | + items: Array of conversation items to process |
| 48 | + keep_most_recent_count: Number of most recent image-containing items to preserve (default: 2) |
| 49 | +
|
| 50 | + Returns: |
| 51 | + Dictionary with processed items |
| 52 | + """ |
| 53 | + items_with_images = find_items_with_images(items) |
| 54 | + |
| 55 | + for index, item in enumerate(items): |
| 56 | + image_index = -1 |
| 57 | + if index in items_with_images: |
| 58 | + image_index = items_with_images.index(index) |
| 59 | + |
| 60 | + should_compress = ( |
| 61 | + image_index >= 0 |
| 62 | + and image_index < len(items_with_images) - keep_most_recent_count |
| 63 | + ) |
| 64 | + |
| 65 | + if should_compress: |
| 66 | + if isinstance(item.get("content"), list): |
| 67 | + new_content = [] |
| 68 | + for content_item in item["content"]: |
| 69 | + if isinstance(content_item, dict): |
| 70 | + if ( |
| 71 | + content_item.get("type") == "tool_result" |
| 72 | + and "content" in content_item |
| 73 | + and isinstance(content_item["content"], list) |
| 74 | + and any( |
| 75 | + nested_item.get("type") == "image" |
| 76 | + for nested_item in content_item["content"] |
| 77 | + if isinstance(nested_item, dict) |
| 78 | + ) |
| 79 | + ): |
| 80 | + # Replace the content with a text placeholder |
| 81 | + new_content.append( |
| 82 | + {**content_item, "content": "screenshot taken"} |
| 83 | + ) |
| 84 | + else: |
| 85 | + new_content.append(content_item) |
| 86 | + else: |
| 87 | + new_content.append(content_item) |
| 88 | + |
| 89 | + item["content"] = new_content |
| 90 | + |
| 91 | + return {"items": items} |
0 commit comments