Files
alfred/agent/tools.py

77 lines
2.6 KiB
Python

# agent/tools.py
from dataclasses import dataclass
from typing import Callable, Any, Dict
import os
from .memory import Memory
@dataclass
class Tool:
name: str
description: str
func: Callable[..., Dict[str, Any]]
parameters: Dict[str, Any] # JSON Schema des paramètres
def make_tools(memory: Memory) -> dict[str, Tool]:
def set_project_root(project_root: str) -> Dict[str, Any]:
if not os.path.isdir(project_root):
return {"error": "invalid_path", "message": f"Le chemin {project_root} n'est pas un dossier valide."}
print(f"Setting project root to: {project_root}")
print("Memory before:", memory.data)
memory.set_project_root(project_root)
print("Memory after:", memory.data)
return {"status": "ok", "project_root": project_root}
def list_directory(path: str) -> Dict[str, Any]:
print("Proper tool used")
if not memory.data.get("project_root"):
return {"error": "no_project_root", "message": "Project root not set."}
root = memory.data.get("project_root")
full_path = os.path.abspath(os.path.join(root, path))
root_abs = os.path.abspath(root)
if not full_path.startswith(root_abs):
return {"error": "forbidden", "message": "Path outside project_root."}
try:
entries = os.listdir(full_path)
return {"path": path, "entries": entries}
except Exception as e:
return {"error": "os_error", "message": str(e)}
tools = [
Tool(
name="set_project_root",
description="Enregistre le path du dossier racine du projet.",
func=set_project_root,
parameters={
"type": "object",
"properties": {
"project_root": {
"type": "string",
"description": "Chemin absolu du dossier racine du projet (ex: /home/user/mon_projet)"
}
},
"required": ["project_root"]
}
),
Tool(
name="list_directory",
description="Liste le contenu d'un dossier relatif au project_root.",
func=list_directory,
parameters={
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Chemin relatif du dossier à lister (ex: 'src' ou '.' pour la racine)"
}
},
"required": ["path"]
}
),
]
return {t.name: t for t in tools}