File size: 3,104 Bytes
c3e9fb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f55fed4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3e9fb6
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import re
from typing import Optional


IMAGE_SUFFIXES = {".png", ".jpg", ".jpeg", ".webp", ".bmp", ".gif"}
SPREADSHEET_SUFFIXES = {".xlsx", ".xls", ".csv"}
PYTHON_SUFFIXES = {".py"}
AUDIO_SUFFIXES = {".mp3", ".wav", ".m4a", ".aac", ".flac", ".ogg", ".opus", ".webm"}
TEXT_SUFFIXES = {".txt", ".md", ".json", ".csv", ".tsv", ".html", ".htm"}


def build_user_content(question: str, task_id: Optional[str]) -> str:
    if not task_id:
        return question

    return (
        f"{question}\n\n"
        f"Task ID: {task_id}\n"
        "If this question has an attachment, call download_task_file with this task_id first. "
        "Then use the most specific follow-up tool for the downloaded file type."
    )


def classify_attachment(question: str, suffix: str) -> Optional[str]:
    suffix = (suffix or "").lower()
    q = (question or "").lower()

    if suffix in IMAGE_SUFFIXES:
        return "image"
    if suffix in AUDIO_SUFFIXES:
        return "audio"
    if suffix in PYTHON_SUFFIXES:
        return "python"
    if suffix in SPREADSHEET_SUFFIXES:
        return "spreadsheet"
    if suffix in TEXT_SUFFIXES:
        return "text"

    if any(x in q for x in ["image", "picture", "screenshot", "chess position", "visual", "diagram", "shown in"]):
        return "image"
    if any(x in q for x in ["audio", "recording", "mp3", "wav", "says", "say in response", "lecture"]):
        return "audio"
    if any(x in q for x in ["python code", "attached python", "numeric output", "run the attached python"]):
        return "python"
    if any(x in q for x in ["excel", "spreadsheet", "csv", "sales", "table contains"]):
        return "spreadsheet"
    if any(x in q for x in ["attached text", "text file", "read the attached", "document"]):
        return "text"

    return None


def is_youtube_question(question: str) -> bool:
    return bool(re.search(r"https?://(?:www\.)?(?:youtube\.com/watch\?v=|youtu\.be/)", question or ""))


def is_youtube_visual_question(question: str) -> bool:
    q = (question or "").lower()
    if not is_youtube_question(question):
        return False

    visual_markers = [
        "on camera",
        "visible",
        "shown",
        "see in the video",
        "highest number",
        "how many",
        "appears",
        "frame",
    ]
    speech_markers = [
        "what does",
        "say",
        "says",
        "spoken",
        "response",
        "transcript",
    ]

    return any(marker in q for marker in visual_markers) and not any(marker in q for marker in speech_markers)


def cleanup_exact_answer(raw_answer: str) -> str:
    answer = str(raw_answer or "").strip()
    answer = re.sub(r"^```(?:\w+)?\s*", "", answer)
    answer = re.sub(r"\s*```$", "", answer)
    answer = answer.strip().strip("`").strip()
    answer = re.sub(r"^(?:final answer|answer)\s*:\s*", "", answer, flags=re.IGNORECASE)
    answer = re.sub(r"^the answer is\s*:?\s*", "", answer, flags=re.IGNORECASE)

    if len(answer) > 1 and answer.endswith(".") and not re.search(r"\d\.\d$", answer):
        answer = answer[:-1]

    return answer.strip()