File size: 1,311 Bytes
f34d9e8
54f6dae
f34d9e8
 
 
 
 
54f6dae
f34d9e8
 
76ded85
f34d9e8
 
 
76ded85
f34d9e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76ded85
f34d9e8
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def get_clean_text(content):
    """
    Извлекает чистый текст из любого формата (str, dict, list).
    Поддерживает:
    - Простые строки
    - Gradio формат: {'text': '...'}
    - OpenAI/Zed формат: [{'type': 'text', 'text': '...'}]
    """
    if content is None:
        return ""

    # 1. Если это уже строка — возвращаем как есть
    if isinstance(content, str):
        return content

    # 2. Если это словарь (Gradio 5 или часть OpenAI message)
    if isinstance(content, dict):
        return content.get("text", "")

    # 3. Если это список (Zed, VS Code, OpenAI Vision)
    if isinstance(content, list):
        text_parts = []
        for item in content:
            if isinstance(item, str):
                text_parts.append(item)
            elif isinstance(item, dict):
                # Извлекаем 'text' если он есть, иначе игнорируем (например, картинки)
                text_parts.append(item.get("text", ""))
            else:
                text_parts.append(str(item))
        return "".join(text_parts)

    # 4. Фолбэк для всего остального
    return str(content)