File size: 2,156 Bytes
c641d5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Resolve the official submission's agent_code value for Space or local runs."""

from __future__ import annotations

from pathlib import Path
from urllib.parse import urlparse

SOURCE_FILES = (
    "agent.py",
    "answer_formatter.py",
    "cache.py",
    "config.py",
    "evaluation.py",
    "gaia_client.py",
    "router.py",
    "tools/__init__.py",
    "tools/agent_tools.py",
    "tools/audio.py",
    "tools/image_chess.py",
    "tools/markdown_logic.py",
    "tools/python_exec.py",
    "tools/retry.py",
    "tools/spreadsheet.py",
    "tools/text_transform.py",
    "tools/video.py",
    "tools/web.py",
    "tools/youtube.py",
)


def _public_url(value: str) -> str:
    url = value.strip()
    parsed = urlparse(url)
    if parsed.scheme not in {"http", "https"} or not parsed.netloc:
        raise ValueError("GAIA_AGENT_CODE_URL must be an absolute HTTP(S) URL")
    return url


def build_inline_agent_code(root: Path) -> str:
    """Bundle implementation sources without reading secrets, caches, or results."""
    sections: list[str] = []
    for relative in SOURCE_FILES:
        path = (root / relative).resolve()
        if not path.is_relative_to(root.resolve()) or not path.is_file():
            raise FileNotFoundError(f"Required source file is missing: {relative}")
        sections.append(
            f"# ===== {relative} =====\n{path.read_text(encoding='utf-8').rstrip()}"
        )
    return "\n\n".join(sections)


def resolve_agent_code(
    *,
    space_id: str | None,
    configured_url: str | None,
    allow_inline: bool,
    root: Path,
) -> str:
    """Prefer a public code URL; permit explicit inline source for local-only use."""
    if configured_url and configured_url.strip():
        return _public_url(configured_url)
    if space_id and space_id.strip():
        return f"https://huggingface.co/spaces/{space_id.strip()}/tree/main"
    if allow_inline:
        return build_inline_agent_code(root)
    raise RuntimeError(
        "Local submission needs GAIA_AGENT_CODE_URL pointing to a public code repository, "
        "or explicit GAIA_ALLOW_INLINE_AGENT_CODE=1 to send the local source bundle."
    )