File size: 7,428 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"""
Main Settings Configuration Module
Contains runtime settings such as environment variable configuration, path configuration, proxy configuration, etc.
"""

import os
from pathlib import Path

from dotenv import load_dotenv

# Load .env file
load_dotenv()

# --- Global Log Control Configuration ---
DEBUG_LOGS_ENABLED = os.environ.get("DEBUG_LOGS_ENABLED", "false").lower() in (
    "true",
    "1",
    "yes",
)
TRACE_LOGS_ENABLED = os.environ.get("TRACE_LOGS_ENABLED", "false").lower() in (
    "true",
    "1",
    "yes",
)
JSON_LOGS_ENABLED = os.environ.get("JSON_LOGS", "false").lower() in (
    "true",
    "1",
    "yes",
)

# --- Log Rotation Configuration ---
LOG_FILE_MAX_BYTES = int(
    os.environ.get("LOG_FILE_MAX_BYTES", str(10 * 1024 * 1024))
)  # 10MB default
LOG_FILE_BACKUP_COUNT = int(os.environ.get("LOG_FILE_BACKUP_COUNT", "5"))

# --- Auth Related Configuration ---
AUTO_SAVE_AUTH = os.environ.get("AUTO_SAVE_AUTH", "").lower() in ("1", "true", "yes")
AUTH_SAVE_TIMEOUT = int(os.environ.get("AUTH_SAVE_TIMEOUT", "30"))
AUTO_CONFIRM_LOGIN = os.environ.get("AUTO_CONFIRM_LOGIN", "true").lower() in (
    "1",
    "true",
    "yes",
)

# --- Path Configuration (Using pathlib) ---
_CONFIG_DIR = Path(__file__).parent
_PROJECT_ROOT = _CONFIG_DIR.parent

AUTH_PROFILES_DIR = str(_PROJECT_ROOT / "auth_profiles")
ACTIVE_AUTH_DIR = str(_PROJECT_ROOT / "auth_profiles" / "active")
SAVED_AUTH_DIR = str(_PROJECT_ROOT / "auth_profiles" / "saved")
LOG_DIR = str(_PROJECT_ROOT / "logs")
APP_LOG_FILE_PATH = str(_PROJECT_ROOT / "logs" / "app.log")
UPLOAD_FILES_DIR = str(_PROJECT_ROOT / "upload_files")


def get_environment_variable(key: str, default: str = "") -> str:
    """Get environment variable value"""
    return os.environ.get(key, default)


def get_boolean_env(key: str, default: bool = False) -> bool:
    """Get boolean environment variable"""
    value = os.environ.get(key, "").lower()
    if default:
        return value not in ("false", "0", "no", "off")
    else:
        return value in ("true", "1", "yes", "on")


def get_int_env(key: str, default: int = 0) -> int:
    """Get integer environment variable"""
    try:
        return int(os.environ.get(key, str(default)))
    except (ValueError, TypeError):
        return default


# --- Proxy Configuration ---
NO_PROXY_ENV = os.environ.get("NO_PROXY")

# --- Script Injection Configuration ---
ENABLE_SCRIPT_INJECTION = get_boolean_env("ENABLE_SCRIPT_INJECTION", True)
NETWORK_INTERCEPTION_ENABLED = get_boolean_env("NETWORK_INTERCEPTION_ENABLED", True)
USERSCRIPT_PATH = os.environ.get(
    "USERSCRIPT_PATH", str(_PROJECT_ROOT / "browser_utils" / "more_models.js")
)
ONLY_COLLECT_CURRENT_USER_ATTACHMENTS = get_boolean_env(
    "ONLY_COLLECT_CURRENT_USER_ATTACHMENTS", False
)

# --- Response Integrity Verification Configuration ---
EMERGENCY_WAIT_SECONDS = get_int_env("EMERGENCY_WAIT_SECONDS", 3)

# --- Thinking Budget Configuration ---
DISABLE_THINKING_BUDGET_ON_STREAMING_DISABLE = get_boolean_env(
    "DISABLE_THINKING_BUDGET_ON_STREAMING_DISABLE", default=True
)

# --- Proactive Rotation Configuration ---
QUOTA_SOFT_LIMIT = get_int_env("QUOTA_SOFT_LIMIT", 650000)
QUOTA_HARD_LIMIT = get_int_env("QUOTA_HARD_LIMIT", 800000)

MODEL_QUOTA_LIMITS = {}
for key, value in os.environ.items():
    if key.upper().startswith("QUOTA_LIMIT_"):
        try:
            model_id = key[12:].lower()
            MODEL_QUOTA_LIMITS[model_id] = int(value)
        except ValueError:
            continue

PROACTIVE_ROTATION_TOKEN_LIMIT = QUOTA_HARD_LIMIT

# --- Dynamic Rotation Guard Configuration ---
HIGH_TRAFFIC_QUEUE_THRESHOLD = get_int_env("HIGH_TRAFFIC_QUEUE_THRESHOLD", 5)
ROTATION_DEPLETION_GUARD_HIGH_TRAFFIC = get_int_env(
    "ROTATION_DEPLETION_GUARD_HIGH_TRAFFIC", 10
)

# --- Granular Configuration for Auth and Budget ---
AUTO_ROTATE_AUTH_PROFILE = get_boolean_env("AUTO_ROTATE_AUTH_PROFILE", True)


def _get_thinking_budget_value(env_key: str, default: int, level_name: str) -> int:
    try:
        value = get_int_env(env_key, default)
        if value <= 0:
            return default
        return value
    except (ValueError, TypeError):
        return default


THINKING_BUDGET_LOW = _get_thinking_budget_value("THINKING_BUDGET_LOW", 8000, "LOW")
THINKING_BUDGET_MEDIUM = _get_thinking_budget_value(
    "THINKING_BUDGET_MEDIUM", 16000, "MEDIUM"
)
THINKING_BUDGET_HIGH = _get_thinking_budget_value("THINKING_BUDGET_HIGH", 32000, "HIGH")

DEFAULT_THINKING_LEVEL = os.environ.get("DEFAULT_THINKING_LEVEL", "low")

# --- Function Calling Configuration ---
# Mode: "emulated" | "native" | "auto"
# - "emulated": Current text-based approach (default, backwards compatible)
# - "native": AI Studio UI-driven function calling
# - "auto": Native with automatic fallback to emulated on failure
FUNCTION_CALLING_MODE = os.environ.get("FUNCTION_CALLING_MODE", "emulated").lower()

# Enable automatic fallback to emulated mode when native mode fails
FUNCTION_CALLING_NATIVE_FALLBACK = get_boolean_env(
    "FUNCTION_CALLING_NATIVE_FALLBACK", True
)

# Timeout for function calling UI operations (milliseconds)
FUNCTION_CALLING_UI_TIMEOUT = get_int_env("FUNCTION_CALLING_UI_TIMEOUT", 5000)

# Native mode retry attempts before fallback
FUNCTION_CALLING_NATIVE_RETRY_COUNT = get_int_env(
    "FUNCTION_CALLING_NATIVE_RETRY_COUNT", 2
)

# Clear function definitions between requests (stateless behavior)
FUNCTION_CALLING_CLEAR_BETWEEN_REQUESTS = get_boolean_env(
    "FUNCTION_CALLING_CLEAR_BETWEEN_REQUESTS", True
)

# Enable detailed function calling debug logs
FUNCTION_CALLING_DEBUG = get_boolean_env("FUNCTION_CALLING_DEBUG", False)

# --- Function Calling Cache Configuration ---
# Enable caching of function calling toggle state and tool declarations
# Skips redundant UI operations when same tools are used in subsequent requests
FUNCTION_CALLING_CACHE_ENABLED = get_boolean_env("FUNCTION_CALLING_CACHE_ENABLED", True)

# Cache TTL in seconds (0 = no expiration within session)
# Cache is automatically invalidated on model switch or new chat
FUNCTION_CALLING_CACHE_TTL = get_int_env("FUNCTION_CALLING_CACHE_TTL", 0)

# --- New Function Calling Configuration (FC Improvements) ---
# Add thoughtSignature to functionCall parts for Gemini 3 compatibility
# Reference: ADR-002-thoughtSignature-support.md
FUNCTION_CALLING_THOUGHT_SIGNATURE = get_boolean_env(
    "FUNCTION_CALLING_THOUGHT_SIGNATURE", True
)

# Convert JSON Schema types to UPPERCASE (e.g., "string" -> "STRING")
# Default is false for backwards compatibility - enable only after UI verification
# Reference: ADR-005-type-case-normalization.md
FUNCTION_CALLING_UPPERCASE_TYPES = get_boolean_env(
    "FUNCTION_CALLING_UPPERCASE_TYPES", False
)

# --- Cookie Refresh Configuration ---
# Enable automatic cookie refresh to keep auth profiles fresh
COOKIE_REFRESH_ENABLED = get_boolean_env("COOKIE_REFRESH_ENABLED", True)

# Periodic refresh interval in seconds (default: 30 minutes)
COOKIE_REFRESH_INTERVAL_SECONDS = get_int_env("COOKIE_REFRESH_INTERVAL_SECONDS", 1800)

# Enable cookie save on successful API requests (saves after every N requests)
COOKIE_REFRESH_ON_REQUEST_ENABLED = get_boolean_env(
    "COOKIE_REFRESH_ON_REQUEST_ENABLED", True
)
COOKIE_REFRESH_REQUEST_INTERVAL = get_int_env("COOKIE_REFRESH_REQUEST_INTERVAL", 10)

# Enable cookie save on graceful shutdown
COOKIE_REFRESH_ON_SHUTDOWN = get_boolean_env("COOKIE_REFRESH_ON_SHUTDOWN", True)