|
| 1 | +"""Auto-detect content type and route to the appropriate compressor.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from graph_tool_call.compressor.base import CompressConfig |
| 9 | +from graph_tool_call.compressor.error_comp import ( |
| 10 | + compress_error_dict, |
| 11 | + compress_error_text, |
| 12 | + is_error_dict, |
| 13 | + is_error_text, |
| 14 | +) |
| 15 | +from graph_tool_call.compressor.html_comp import compress_html, is_html |
| 16 | +from graph_tool_call.compressor.json_comp import compress_json_dict, compress_json_list |
| 17 | +from graph_tool_call.compressor.text_comp import compress_text |
| 18 | + |
| 19 | + |
| 20 | +def _detect_and_compress(content: Any, config: CompressConfig) -> str: |
| 21 | + """Detect content type and compress accordingly.""" |
| 22 | + # -- Already structured data -- |
| 23 | + if isinstance(content, list): |
| 24 | + return compress_json_list(content, config) |
| 25 | + |
| 26 | + if isinstance(content, dict): |
| 27 | + if is_error_dict(content): |
| 28 | + return compress_error_dict(content, config) |
| 29 | + return compress_json_dict(content, config) |
| 30 | + |
| 31 | + # -- String content: try to parse / classify -- |
| 32 | + if not isinstance(content, str): |
| 33 | + content = str(content) |
| 34 | + |
| 35 | + # Short enough — no compression needed. |
| 36 | + if len(content) <= config.max_chars: |
| 37 | + return content |
| 38 | + |
| 39 | + # Try JSON parse. |
| 40 | + try: |
| 41 | + parsed = json.loads(content) |
| 42 | + except (json.JSONDecodeError, ValueError): |
| 43 | + parsed = None |
| 44 | + |
| 45 | + if parsed is not None: |
| 46 | + if isinstance(parsed, list): |
| 47 | + return compress_json_list(parsed, config) |
| 48 | + if isinstance(parsed, dict): |
| 49 | + if is_error_dict(parsed): |
| 50 | + return compress_error_dict(parsed, config) |
| 51 | + return compress_json_dict(parsed, config) |
| 52 | + |
| 53 | + # HTML detection. |
| 54 | + if is_html(content): |
| 55 | + return compress_html(content, config) |
| 56 | + |
| 57 | + # Error text detection. |
| 58 | + if is_error_text(content): |
| 59 | + return compress_error_text(content, config) |
| 60 | + |
| 61 | + # Fallback: plain text. |
| 62 | + return compress_text(content, config) |
| 63 | + |
| 64 | + |
| 65 | +def compress_tool_result( |
| 66 | + content: str | dict | list | Any, |
| 67 | + *, |
| 68 | + config: CompressConfig | None = None, |
| 69 | + max_chars: int = 4000, |
| 70 | + content_type: str | None = None, |
| 71 | +) -> str: |
| 72 | + """Intelligently compress a tool result for LLM context. |
| 73 | +
|
| 74 | + Parameters: |
| 75 | + content: The tool result — str, dict, list, or anything with ``__str__``. |
| 76 | + config: Compression configuration. When *None*, a default |
| 77 | + ``CompressConfig(max_chars=max_chars)`` is created. |
| 78 | + max_chars: Shorthand for ``CompressConfig(max_chars=...)``. |
| 79 | + Ignored when *config* is provided. |
| 80 | + content_type: Force a specific compressor instead of auto-detecting. |
| 81 | + One of ``"json"``, ``"html"``, ``"error"``, ``"text"``. |
| 82 | +
|
| 83 | + Returns: |
| 84 | + The compressed string. If *content* is already short enough it is |
| 85 | + returned as-is (for strings) or serialised (for dicts/lists). |
| 86 | + """ |
| 87 | + if config is None: |
| 88 | + config = CompressConfig(max_chars=max_chars) |
| 89 | + |
| 90 | + # Forced content type — skip auto-detection. |
| 91 | + if content_type is not None: |
| 92 | + return _compress_by_type(content, content_type, config) |
| 93 | + |
| 94 | + return _detect_and_compress(content, config) |
| 95 | + |
| 96 | + |
| 97 | +def _compress_by_type(content: Any, content_type: str, config: CompressConfig) -> str: |
| 98 | + """Route to a specific compressor by name.""" |
| 99 | + if isinstance(content, str): |
| 100 | + text = content |
| 101 | + else: |
| 102 | + text = json.dumps(content, ensure_ascii=False, default=str) |
| 103 | + |
| 104 | + if content_type == "json": |
| 105 | + try: |
| 106 | + parsed = json.loads(text) if isinstance(content, str) else content |
| 107 | + except (json.JSONDecodeError, ValueError): |
| 108 | + return compress_text(text, config) |
| 109 | + if isinstance(parsed, list): |
| 110 | + return compress_json_list(parsed, config) |
| 111 | + if isinstance(parsed, dict): |
| 112 | + return compress_json_dict(parsed, config) |
| 113 | + return compress_text(text, config) |
| 114 | + |
| 115 | + if content_type == "html": |
| 116 | + return compress_html(text, config) |
| 117 | + |
| 118 | + if content_type == "error": |
| 119 | + if isinstance(content, dict): |
| 120 | + return compress_error_dict(content, config) |
| 121 | + return compress_error_text(text, config) |
| 122 | + |
| 123 | + # "text" or unknown |
| 124 | + return compress_text(text, config) |
0 commit comments