Commit ·
64a9ca9
1
Parent(s): c747f40
loading date in system prompt, search code update (still not there)
Browse filesFiles changed (6)
1 unmodified line23456734 unmodified lines424344454647484950511 unmodified lineContext management for conversation history"""from pathlib import Pathfrom typing import Any34 unmodified linesprompt_data = yaml.safe_load(f)template_str = prompt_data.get("system_prompt", "")template = Template(template_str)return template.render(tools=tool_specs,num_tools=len(tool_specs),)def add_message(self, message: Message, token_count: int = None) -> None:1 unmodified line2345678934 unmodified lines44454647484950515253545556575859606162631 unmodified lineContext management for conversation history"""import zoneinfofrom datetime import datetimefrom pathlib import Pathfrom typing import Any34 unmodified linesprompt_data = yaml.safe_load(f)template_str = prompt_data.get("system_prompt", "")# Get current date and timetz = zoneinfo.ZoneInfo("Europe/Paris")now = datetime.now(tz)current_date = now.strftime("%d-%m-%Y")current_time = now.strftime("%H:%M:%S.%f")[:-3]current_timezone = f"{now.strftime('%Z')} (UTC{now.strftime('%z')[:3]}:{now.strftime('%z')[3:]})"template = Template(template_str)return template.render(tools=tool_specs,num_tools=len(tool_specs),current_date=current_date,current_time=current_time,current_timezone=current_timezone,)def add_message(self, message: Message, token_count: int = None) -> None:
30 unmodified lines3132333435363738394041424344454647192 unmodified lines24024124224324424524629 unmodified lines27627727827928028128228328428528628728828929029129229329430 unmodified linesGITHUB_READ_FILE_TOOL_SPEC,github_read_file_handler,)from agent.tools.github_search_code import (GITHUB_SEARCH_CODE_TOOL_SPEC,github_search_code_handler,)from agent.tools.jobs_tool import HF_JOBS_TOOL_SPEC, hf_jobs_handlerfrom agent.tools.plan_tool import PLAN_TOOL_SPEC, plan_tool_handlerfrom agent.tools.private_hf_repo_tools import (PRIVATE_HF_REPO_TOOL_SPEC,private_hf_repo_handler,)from agent.tools.utils_tools import UTILS_TOOL_SPEC, utils_handler# Suppress aiohttp deprecation warningwarnings.filterwarnings(192 unmodified linesdef create_builtin_tools() -> list[ToolSpec]:"""Create built-in tool specifications"""print(f"Creating built-in tools: {EXPLORE_HF_DOCS_TOOL_SPEC['name']}, {HF_DOCS_FETCH_TOOL_SPEC['name']}, {PLAN_TOOL_SPEC['name']}, {HF_JOBS_TOOL_SPEC['name']}, {PRIVATE_HF_REPO_TOOL_SPEC['name']}, {UTILS_TOOL_SPEC['name']}, {GITHUB_SEARCH_CODE_TOOL_SPEC['name']}, {GITHUB_FIND_EXAMPLES_TOOL_SPEC['name']}, {GITHUB_LIST_REPOS_TOOL_SPEC['name']}, {GITHUB_READ_FILE_TOOL_SPEC['name']}")# in order of importancereturn [29 unmodified linesparameters=PRIVATE_HF_REPO_TOOL_SPEC["parameters"],handler=private_hf_repo_handler,),ToolSpec(name=UTILS_TOOL_SPEC["name"],description=UTILS_TOOL_SPEC["description"],parameters=UTILS_TOOL_SPEC["parameters"],handler=utils_handler,),# GitHub toolsToolSpec(name=GITHUB_SEARCH_CODE_TOOL_SPEC["name"],description=GITHUB_SEARCH_CODE_TOOL_SPEC["description"],parameters=GITHUB_SEARCH_CODE_TOOL_SPEC["parameters"],handler=github_search_code_handler,),ToolSpec(name=GITHUB_FIND_EXAMPLES_TOOL_SPEC["name"],description=GITHUB_FIND_EXAMPLES_TOOL_SPEC["description"],30 unmodified lines313233343536373839404142434445192 unmodified lines23823924024124224324429 unmodified lines27427527627727827928028128228328428528628728828929029129229329430 unmodified linesGITHUB_READ_FILE_TOOL_SPEC,github_read_file_handler,)from agent.tools.jobs_tool import HF_JOBS_TOOL_SPEC, hf_jobs_handlerfrom agent.tools.plan_tool import PLAN_TOOL_SPEC, plan_tool_handlerfrom agent.tools.private_hf_repo_tools import (PRIVATE_HF_REPO_TOOL_SPEC,private_hf_repo_handler,)# NOTE: Utils tool disabled - date/time now loaded into system prompt at initialization# from agent.tools.utils_tools import UTILS_TOOL_SPEC, utils_handler# Suppress aiohttp deprecation warningwarnings.filterwarnings(192 unmodified linesdef create_builtin_tools() -> list[ToolSpec]:"""Create built-in tool specifications"""print(f"Creating built-in tools: {EXPLORE_HF_DOCS_TOOL_SPEC['name']}, {HF_DOCS_FETCH_TOOL_SPEC['name']}, {PLAN_TOOL_SPEC['name']}, {HF_JOBS_TOOL_SPEC['name']}, {PRIVATE_HF_REPO_TOOL_SPEC['name']}, {GITHUB_FIND_EXAMPLES_TOOL_SPEC['name']}, {GITHUB_LIST_REPOS_TOOL_SPEC['name']}, {GITHUB_READ_FILE_TOOL_SPEC['name']}")# in order of importancereturn [29 unmodified linesparameters=PRIVATE_HF_REPO_TOOL_SPEC["parameters"],handler=private_hf_repo_handler,),# NOTE: Utils tool disabled - date/time now loaded into system prompt at initialization (less tool calls=more reliablity)# ToolSpec(# name=UTILS_TOOL_SPEC["name"],# description=UTILS_TOOL_SPEC["description"],# parameters=UTILS_TOOL_SPEC["parameters"],# handler=utils_handler,# ),# GitHub tools# NOTE: Github search code tool disabled - a bit buggy# ToolSpec(# name=GITHUB_SEARCH_CODE_TOOL_SPEC["name"],# description=GITHUB_SEARCH_CODE_TOOL_SPEC["description"],# parameters=GITHUB_SEARCH_CODE_TOOL_SPEC["parameters"],# handler=github_search_code_handler,# ),ToolSpec(name=GITHUB_FIND_EXAMPLES_TOOL_SPEC["name"],description=GITHUB_FIND_EXAMPLES_TOOL_SPEC["description"],
12345625 unmodified lines323334353637383940414243444546system_prompt: |You are HF Agent, a powerful AI assistant for Machine Learning Engineering, particularly training Large Language Models. You have access to {{ num_tools }} tools for interacting with Hugging Face Hub and performing ML tasks.# Task Approach**CRITICAL: Research First, Then Implement**25 unmodified lines2. Don't be shy to ask questions if needed.3. Don't be overly talkative, explaining everything after a task ended.# Available ToolsYou have access to the following categories of tools:- Hugging Face Hub: Search and interact with models, datasets, papers, and documentation- Spaces: Use and discover ML applications- Jobs: Manage compute jobs for training and inference- Image Generation: Generate and transform images- Planning : a planning/to-do tool.# Conventions1234567825 unmodified lines343536373839system_prompt: |You are HF Agent, a powerful AI assistant for Machine Learning Engineering, particularly training Large Language Models. You have access to {{ num_tools }} tools for interacting with Hugging Face Hub and performing ML tasks._Current Time: **{{ current_date }} {{ current_time }} ({{ current_timezone }})**_# Task Approach**CRITICAL: Research First, Then Implement**25 unmodified lines2. Don't be shy to ask questions if needed.3. Don't be overly talkative, explaining everything after a task ended.# Conventions
123456716 unmodified lines24252627282930313233343536373839404142439 unmodified lines535455565758596061626364656667686970717273747576777879808182838485868757 unmodified lines14514614714814915015115215315486 unmodified lines2412422432442452462472482492502512522532542552562572582592602612622632643 unmodified lines26826927027127227327427527627727827928014 unmodified lines295296297298299300301302"""GitHub Code Search Tool - Search code across GitHub with advanced filteringFind code patterns using regex and glob filters for repositories and file paths."""import fnmatch16 unmodified linesreturn fnmatch.fnmatch(text, pattern)def search_code(query: str,repo_glob: Optional[str] = None,path_glob: Optional[str] = None,regex: bool = False,max_results: int = 20,) -> ToolResult:"""Search for code across GitHub with glob filtering.Args:query: Search term or pattern to find in coderepo_glob: Glob pattern to filter repositories (e.g., "github/*", "*/react")path_glob: Glob pattern to filter file paths (e.g., "*.py", "src/**/*.js")regex: If True, treat query as regular expressionmax_results: Maximum number of results to return (default 20)9 unmodified lines"isError": True,}# Build GitHub queryquery_parts = []if regex:query_parts.append(f"/{query}/")else:query_parts.append(f'"{query}"' if " " in query else query)# Add repo filterif repo_glob:if "/" in repo_glob:query_parts.append(f"repo:{repo_glob}")else:query_parts.append(f"user:{repo_glob}")# Add path filterif path_glob:if "*" not in path_glob and "?" not in path_glob:query_parts.append(f"path:{path_glob}")elif path_glob.startswith("*."):ext = path_glob[2:]query_parts.append(f"extension:{ext}")elif "/" not in path_glob and "*" in path_glob:query_parts.append(f"filename:{path_glob}")else:# Complex pattern, extract extension if possibleext_match = re.search(r"\*\.(\w+)", path_glob)if ext_match:query_parts.append(f"extension:{ext_match.group(1)}")github_query = " ".join(query_parts)57 unmodified linesfile_path = item.get("path", "")sha = item.get("sha", "")# Apply client-side glob filteringif repo_glob and not _glob_match(repo_name, repo_glob):continueif path_glob and not _glob_match(file_path, path_glob):continue# Extract text matches86 unmodified linesGITHUB_SEARCH_CODE_TOOL_SPEC = {"name": "search_code","description": ("Search for code patterns across GitHub with advanced glob filtering.\n\n""Features:\n""- Text or regex search\n""- Repository glob patterns (e.g., 'github/*', '*/react')\n""- File path glob patterns (e.g., '*.py', 'src/**/*.js')\n""- Returns code snippets with line numbers\n""- Direct URLs to matches\n\n""## Examples:\n\n""**Search for Python function definitions:**\n""{'query': 'def search', 'path_glob': '*.py', 'max_results': 10}\n\n""**Search for TODO comments in specific org:**\n""{'query': 'TODO', 'repo_glob': 'github/*', 'max_results': 5}\n\n""**Regex search for test functions:**\n""{'query': r'func Test\\w+', 'path_glob': '*.go', 'regex': True}\n\n""**Search in specific repo with path filter:**\n""{'query': 'SearchCode', 'repo_glob': 'github/github-mcp-server', 'path_glob': '*.go'}\n\n""**Find imports in TypeScript files:**\n""{'query': 'import', 'path_glob': 'src/**/*.ts', 'repo_glob': 'facebook/*'}\n\n""Perfect for finding code patterns, learning from examples, or exploring implementations."),"parameters": {3 unmodified lines"type": "string","description": "Search term or pattern to find in code. Required.",},"repo_glob": {"type": "string","description": "Glob pattern to filter repositories (e.g., 'github/*', '*/react'). Optional.",},"path_glob": {"type": "string","description": "Glob pattern to filter file paths (e.g., '*.py', 'src/**/*.js'). Optional.",},"regex": {"type": "boolean",14 unmodified linestry:result = search_code(query=arguments["query"],repo_glob=arguments.get("repo_glob"),path_glob=arguments.get("path_glob"),regex=arguments.get("regex", False),max_results=arguments.get("max_results", 20),)123456716 unmodified lines242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461479 unmodified lines15715815916016116216316416516616716816917017117217317417517617717817918057 unmodified lines23823924024124224324424524624786 unmodified lines3343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763 unmodified lines38038138238338438538638738838939039139214 unmodified lines407408409410411412413414"""GitHub Code Search Tool - Search code across GitHub with intelligent filteringMaps user-friendly patterns to GitHub's Code Search API capabilities."""import fnmatch16 unmodified linesreturn fnmatch.fnmatch(text, pattern)def _parse_repo_filter(repo_pattern: str) -> tuple[Optional[str], Optional[str]]:"""Parse repository pattern into GitHub API filter and client-side glob pattern.Returns: (api_filter, client_glob)- api_filter: GitHub API filter string (e.g., "org:huggingface")- client_glob: Pattern for client-side filtering (e.g., "huggingface/trl*")Examples:"huggingface/trl" → ("repo:huggingface/trl", None)"huggingface/*" → ("org:huggingface", "huggingface/*")"huggingface/trl*" → ("org:huggingface", "huggingface/trl*")"huggingface" → ("org:huggingface", None)"*/*" → (None, "*/*")"""if not repo_pattern:return None, None# Pattern: owner/repo (exact match)if "/" in repo_pattern and "*" not in repo_pattern and "?" not in repo_pattern:return f"repo:{repo_pattern}", None# Pattern: owner/* or owner/prefix* (org + client filter)if "/" in repo_pattern and ("*" in repo_pattern or "?" in repo_pattern):org_name = repo_pattern.split("/")[0]if "*" not in org_name and "?" not in org_name:return f"org:{org_name}", repo_pattern# Org name has wildcards - can't filter server-sidereturn None, repo_pattern# Pattern: owner (just org name, no wildcards)if "*" not in repo_pattern and "?" not in repo_pattern:return f"org:{repo_pattern}", None# Pattern: */* or other complex patterns (client-side only)return None, repo_patterndef _parse_path_filter(path_pattern: str) -> tuple[Optional[str], Optional[str]]:"""Parse path pattern into GitHub API filter and client-side glob pattern.Returns: (api_filter, client_glob)Examples:"*.py" → ("extension:py", None)"**/*.py" → ("extension:py", None)"src/**/*.py" → ("extension:py", "src/**/*.py")"test_*.py" → ("extension:py", "test_*.py")"src/main.py" → ("path:src/main.py", None)"""if not path_pattern:return None, None# Exact path (no wildcards)if "*" not in path_pattern and "?" not in path_pattern:return f"path:{path_pattern}", None# Extract extension if presentext_match = re.search(r"\*\.(\w+)$", path_pattern)if ext_match:extension = ext_match.group(1)api_filter = f"extension:{extension}"# Check if there's a directory prefix that needs client-side filtering# e.g., "src/**/*.py" needs client filter, "**/*.py" doesn'tif path_pattern in [f"*.{extension}", f"**/*.{extension}"]:# Simple patterns - API filter is enoughreturn api_filter, Noneelse:# Complex pattern - need client-side filter tooreturn api_filter, path_pattern# Pattern like "test_*.py" or "README*" - use filename with client filter# GitHub's filename: doesn't support wildcards, so we rely on client-sideif "/" not in path_pattern:# Try to extract extension for API filteringif "." in path_pattern:parts = path_pattern.rsplit(".", 1)if "*" not in parts[-1] and "?" not in parts[-1]:# Extension is cleanreturn f"extension:{parts[-1]}", path_pattern# No extension or complex - client-side onlyreturn None, path_pattern# Complex path pattern - client-side onlyreturn None, path_patterndef search_code(query: str,repo_pattern: Optional[str] = None,path_pattern: Optional[str] = None,regex: bool = False,max_results: int = 20,) -> ToolResult:"""Search for code across GitHub with intelligent pattern matching.This tool intelligently maps user patterns to GitHub's Code Search API capabilities:Repository Patterns:- "owner/repo" → Searches exact repository- "owner/*" or "owner" → Searches all repos in organization- "*/*" → Searches all GitHub (no repo filter)- Wildcards trigger client-side filtering when neededPath Patterns:- "*.py" → Searches all Python files- "**/*.js" → Searches all JavaScript files (any directory)- "src/**/*.py" → Python files in src/ (uses client-side filtering)- "test_*.py" → Files matching pattern (client-side filtering)- "path/to/file.py" → Exact file pathArgs:query: Search term or pattern to find in coderepo_pattern: Repository pattern (e.g., "huggingface/trl", "huggingface/*", "huggingface")path_pattern: File path pattern (e.g., "*.py", "src/**/*.js")regex: If True, treat query as regular expressionmax_results: Maximum number of results to return (default 20)9 unmodified lines"isError": True,}# Build GitHub API queryquery_parts = []# Add search termif regex:query_parts.append(f"/{query}/")else:query_parts.append(f'"{query}"' if " " in query else query)# Parse repository filterrepo_api_filter, repo_client_glob = _parse_repo_filter(repo_pattern)if repo_api_filter:query_parts.append(repo_api_filter)# Parse path filterpath_api_filter, path_client_glob = _parse_path_filter(path_pattern)if path_api_filter:query_parts.append(path_api_filter)github_query = " ".join(query_parts)57 unmodified linesfile_path = item.get("path", "")sha = item.get("sha", "")# Apply client-side filteringif repo_client_glob and not _glob_match(repo_name, repo_client_glob):continueif path_client_glob and not _glob_match(file_path, path_client_glob):continue# Extract text matches86 unmodified linesGITHUB_SEARCH_CODE_TOOL_SPEC = {"name": "search_code","description": ("Search for code patterns across GitHub with intelligent pattern matching.\n\n""This tool automatically maps your patterns to GitHub's Code Search API:\n\n""## Repository Patterns:\n""- **Exact repo**: `'huggingface/trl'` → Searches only that repo\n""- **Organization**: `'huggingface'` or `'huggingface/*'` → All repos in org\n""- **All repos**: `'*/*'` or omit → Searches all GitHub\n""- Wildcards like `'huggingface/trl*'` automatically use client-side filtering\n\n""## Path Patterns:\n""- **Extension**: `'*.py'` or `'**/*.py'` → All Python files\n""- **Directory**: `'src/**/*.js'` → JavaScript files in src/ (client-filtered)\n""- **Pattern**: `'test_*.py'` → Files matching pattern (client-filtered)\n""- **Exact path**: `'README.md'` → Specific file\n\n""## How It Works:\n""1. Converts patterns to GitHub API filters (server-side, fast)\n""2. Falls back to client-side filtering for complex patterns\n""3. Returns code snippets with line numbers and URLs\n\n""## Examples:\n\n""**Search for function in specific repo:**\n""```python\n""{'query': 'def train', 'repo_pattern': 'huggingface/trl', 'path_pattern': '*.py'}\n""```\n\n""**Search across entire organization:**\n""```python\n""{'query': 'GRPOTrainer', 'repo_pattern': 'huggingface', 'path_pattern': '*.py'}\n""```\n\n""**Search specific directory pattern:**\n""```python\n""{'query': 'TODO', 'repo_pattern': 'facebook/react', 'path_pattern': 'src/**/*.js'}\n""```\n\n""**Regex search across GitHub:**\n""```python\n""{'query': r'class \\w+Trainer', 'path_pattern': '*.py', 'regex': True}\n""```\n\n""**Search all repos (no filter):**\n""```python\n""{'query': 'import transformers', 'path_pattern': '*.py', 'max_results': 50}\n""```\n\n""Perfect for finding code patterns, learning from examples, or exploring implementations."),"parameters": {3 unmodified lines"type": "string","description": "Search term or pattern to find in code. Required.",},"repo_pattern": {"type": "string","description": "Repository pattern: 'owner/repo' (exact), 'owner' (org), 'owner/*' (org with filter), '*/*' (all). Optional.",},"path_pattern": {"type": "string","description": "File path pattern: '*.ext' (extension), 'dir/**/*.ext' (directory), 'pattern*.ext' (name pattern). Optional.",},"regex": {"type": "boolean",14 unmodified linestry:result = search_code(query=arguments["query"],repo_pattern=arguments.get("repo_pattern"),path_pattern=arguments.get("path_pattern"),regex=arguments.get("regex", False),max_results=arguments.get("max_results", 20),)
1 unmodified line23456789116 unmodified lines1261271281291301311322 unmodified lines1351361371381391401411 unmodified lineUtility functions for Hugging Face toolsPorted from: hf-mcp-server/packages/mcp/src/jobs/formatters.ts"""from datetime import datetimefrom typing import Any, Dict, List, Optional116 unmodified linesdef format_job_details(jobs: Any) -> str:"""Format job details as JSON in a markdown code block"""import jsonjob_array = jobs if isinstance(jobs, list) else [jobs]json_str = json.dumps(job_array, indent=2)2 unmodified linesdef format_scheduled_job_details(jobs: Any) -> str:"""Format scheduled job details as JSON in a markdown code block"""import jsonjob_array = jobs if isinstance(jobs, list) else [jobs]json_str = json.dumps(job_array, indent=2)1 unmodified line234567891011116 unmodified lines1281291301311321332 unmodified lines1361371381391401411 unmodified lineUtility functions for Hugging Face toolsPorted from: hf-mcp-server/packages/mcp/src/jobs/formatters.tsIncludes GPU memory validation for job submissions"""import jsonfrom datetime import datetimefrom typing import Any, Dict, List, Optional116 unmodified linesdef format_job_details(jobs: Any) -> str:"""Format job details as JSON in a markdown code block"""job_array = jobs if isinstance(jobs, list) else [jobs]json_str = json.dumps(job_array, indent=2)2 unmodified linesdef format_scheduled_job_details(jobs: Any) -> str:"""Format scheduled job details as JSON in a markdown code block"""job_array = jobs if isinstance(jobs, list) else [jobs]json_str = json.dumps(job_array, indent=2)
3 unmodified lines4567891011121314151617105 unmodified lines1231241251261271281293 unmodified linesProvides system information like current date/time with timezone support."""import asynciofrom datetime import datetimefrom typing import Any, Dict, Literal, Optionaltry:import zoneinfoexcept ImportError:from backports import zoneinfofrom agent.tools.types import ToolResult105 unmodified linesdate_str = now.strftime("%d-%m-%Y")# Format time as HH:MM:SS.mmmtime_str = now.strftime("%H:%M:%S.%f")[:-3] # Remove last 3 digits to keep only milliseconds# Get timezone abbreviation/offsettz_offset = now.strftime("%z")3 unmodified lines456789101112105 unmodified lines1181191201211221231241251263 unmodified linesProvides system information like current date/time with timezone support."""import zoneinfofrom datetime import datetimefrom typing import Any, Dict, Literalfrom agent.tools.types import ToolResult105 unmodified linesdate_str = now.strftime("%d-%m-%Y")# Format time as HH:MM:SS.mmmtime_str = now.strftime("%H:%M:%S.%f")[:-3] # Remove last 3 digits to keep only milliseconds# Get timezone abbreviation/offsettz_offset = now.strftime("%z")
agent/context_manager/manager.py+12
agent/core/tools.py+18-18
agent/prompts/system_prompt.yaml+3-10
agent/tools/github_search_code.py+167-55
agent/tools/utilities.py+2-2
agent/tools/utils_tools.py+5-8