"""Fix JSON brackets.""" from __future__ import annotations import contextlib import json from typing import Optional from autogpt.config import Config CFG = Config() def balance_braces(json_string: str) -> Optional[str]: """ Balance the braces in a JSON string. Args: json_string (str): The JSON string. Returns: str: The JSON string with braces balanced. """ open_braces_count = json_string.count("{") close_braces_count = json_string.count("}") while open_braces_count > close_braces_count: json_string += "}" close_braces_count += 1 while close_braces_count > open_braces_count: json_string = json_string.rstrip("}") close_braces_count -= 1 with contextlib.suppress(json.JSONDecodeError): json.loads(json_string) return json_string