"""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." )