File size: 3,806 Bytes
a5784e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import json
import logging
import os
import threading
from datetime import datetime

COOLDOWN_FILE = os.path.join(
    os.path.dirname(__file__), "..", "..", "config", "cooldown_status.json"
)
_lock = threading.Lock()


def load_cooldown_profiles():
    """
    Loads the cooldown profiles from the persistent JSON file.

    Returns:
        dict: A dictionary of cooldown profiles.
    """
    with _lock:
        if not os.path.exists(COOLDOWN_FILE):
            return {}
        try:
            with open(COOLDOWN_FILE, "r") as f:
                data = json.load(f)

            profiles = {}
            for profile, val in data.items():
                if isinstance(val, dict):
                    # Handle nested model-specific cooldowns
                    model_cooldowns = {}
                    for model_id, ts in val.items():
                        try:
                            model_cooldowns[model_id] = datetime.fromisoformat(ts)
                        except (ValueError, TypeError):
                            continue
                    if model_cooldowns:
                        # Clean up redundant "default" entries when specific models exist
                        has_specific_models = any(
                            model_id != "default" for model_id in model_cooldowns.keys()
                        )
                        if has_specific_models and "default" in model_cooldowns:
                            logger = logging.getLogger("CooldownManager")
                            logger.info(
                                f"🧹 Cleaning up redundant 'default' entry for profile {os.path.basename(profile)}"
                            )
                            del model_cooldowns["default"]
                        profiles[profile] = model_cooldowns
                else:
                    # Handle legacy/global cooldowns
                    try:
                        profiles[profile] = datetime.fromisoformat(val)
                    except (ValueError, TypeError):
                        continue
            return profiles
        except (json.JSONDecodeError, IOError):
            return {}


def save_cooldown_profiles(profiles):
    """
    Saves the cooldown profiles to the persistent JSON file.

    Args:
        profiles (dict): A dictionary of cooldown profiles to save.
    """
    with _lock:
        try:
            serializable_profiles = {}
            for profile, data in profiles.items():
                if isinstance(data, dict):
                    # Handle nested model-specific cooldowns
                    model_cooldowns = {}
                    for model_id, ts in data.items():
                        if isinstance(ts, datetime):
                            model_cooldowns[model_id] = ts.isoformat()
                        elif isinstance(ts, (int, float)):
                            try:
                                model_cooldowns[model_id] = datetime.fromtimestamp(
                                    ts
                                ).isoformat()
                            except (ValueError, OSError):
                                pass
                    serializable_profiles[profile] = model_cooldowns

                elif isinstance(data, datetime):
                    serializable_profiles[profile] = data.isoformat()
                elif isinstance(data, (int, float)):
                    try:
                        serializable_profiles[profile] = datetime.fromtimestamp(
                            data
                        ).isoformat()
                    except (ValueError, OSError):
                        pass

            with open(COOLDOWN_FILE, "w") as f:
                json.dump(serializable_profiles, f, indent=4)
        except IOError:
            pass