from typing import Optional from langchain.tools import tool, ToolRuntime from langgraph.config import get_stream_writer @tool def check_project_id(runtime: ToolRuntime) -> Optional[str]: """Retourne le project_id présent dans l'état d'exécution, sinon None.""" writer = get_stream_writer() state = getattr(runtime, "state", None) writer({ "kind": "tool", "tool": "check_project_id", "message": "Vérification de la présence d'un projet", }) if isinstance(state, dict): raw_project_id = state.get("project_id") else: raw_project_id = getattr(state, "project_id", None) if raw_project_id is None: writer({"kind": "tool", "tool": "check_project_id", "message": "Aucun projet identifié"}) return None project_id = raw_project_id if isinstance(raw_project_id, str) else str(raw_project_id) if project_id.strip(): writer({"kind": "tool", "tool": "check_project_id", "message": "Projet identifié"}) return project_id writer({"kind": "tool", "tool": "check_project_id", "message": "Aucun projet identifié"}) return None