-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.py
More file actions
605 lines (555 loc) · 20.3 KB
/
Copy pathconfig.py
File metadata and controls
605 lines (555 loc) · 20.3 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
from __future__ import annotations
from typing import List, Optional
import json
import os
from urllib.parse import urlparse
from pydantic import Field, ValidationError, field_validator
from pydantic_settings import (
BaseSettings,
DotEnvSettingsSource,
PydanticBaseSettingsSource,
SettingsConfigDict,
)
class BotConfig(BaseSettings):
"""
קונפיגורציה עיקרית של הבוט המבוססת על Pydantic Settings.
- קורא אוטומטית משתני סביבה ו-`.env`.
- מבצע המרות טיפוסים ו-Validation ברור.
"""
# שדות חובה
BOT_TOKEN: str = Field(..., description="Telegram bot token")
MONGODB_URL: str = Field(..., description="MongoDB connection string")
# בסיסי DB
DATABASE_NAME: str = Field(
default="code_keeper_bot", description="MongoDB database name"
)
# MongoDB Pooling/Timeouts via ENV (stage 2)
MONGODB_MAX_POOL_SIZE: int = Field(
default=50,
ge=1,
le=100_000,
description="MongoDB connection pool max size (maxPoolSize)",
)
MONGODB_MIN_POOL_SIZE: int = Field(
default=5,
ge=0,
le=100_000,
description="MongoDB connection pool min size (minPoolSize)",
)
MONGODB_MAX_IDLE_TIME_MS: int = Field(
default=30_000,
ge=0,
le=86_400_000,
description="MongoDB max idle time in ms (maxIdleTimeMS)",
)
MONGODB_WAIT_QUEUE_TIMEOUT_MS: int = Field(
default=8_000,
ge=0,
le=86_400_000,
description="MongoDB wait queue timeout in ms (waitQueueTimeoutMS)",
)
MONGODB_SERVER_SELECTION_TIMEOUT_MS: int = Field(
default=5_000,
ge=100,
le=600_000,
description="MongoDB server selection timeout in ms (serverSelectionTimeoutMS)",
)
MONGODB_SOCKET_TIMEOUT_MS: int = Field(
default=45_000,
ge=0,
le=3_600_000,
description="MongoDB socket timeout in ms (socketTimeoutMS)",
)
MONGODB_CONNECT_TIMEOUT_MS: int = Field(
default=5_000,
ge=0,
le=3_600_000,
description="MongoDB connect timeout in ms (connectTimeoutMS)",
)
MONGODB_RETRY_WRITES: bool = Field(
default=True, description="Enable MongoDB retryWrites"
)
MONGODB_RETRY_READS: bool = Field(
default=True, description="Enable MongoDB retryReads"
)
MONGODB_APPNAME: Optional[str] = Field(
default=None, description="MongoDB appName client metadata"
)
MONGODB_COMPRESSORS: Optional[str] = Field(
default=None,
description="Comma-separated compressor names supported by server (e.g., zstd,snappy,zlib)",
)
MONGODB_HEARTBEAT_FREQUENCY_MS: int = Field(
default=10_000,
ge=1000,
le=600_000,
description="Server monitor heartbeat frequency in milliseconds",
)
# Cache/Redis
REDIS_URL: Optional[str] = Field(default=None, description="Redis URL")
CACHE_ENABLED: bool = Field(
default=False, description="Enable in-memory/Redis caching where applicable"
)
REDIS_MAX_CONNECTIONS: int = Field(
default=50,
ge=1,
le=100_000,
description="Redis connection pool max size",
)
# הערכים הוגדלו בדצמבר 2025 בעקבות עומס על בסיס הנתונים שגרם ל-Timeouts שגויים בקאש
REDIS_CONNECT_TIMEOUT: float = Field(
default=float(os.getenv("REDIS_CONNECT_TIMEOUT", "3")),
description="Redis socket_connect_timeout (seconds); if None, uses SAFE_MODE defaults",
)
# הערכים הוגדלו בדצמבר 2025 בעקבות עומס על בסיס הנתונים שגרם ל-Timeouts שגויים בקאש
REDIS_SOCKET_TIMEOUT: float = Field(
default=float(os.getenv("REDIS_SOCKET_TIMEOUT", "5")),
description="Redis socket_timeout (seconds); if None, uses SAFE_MODE defaults",
)
# הערכים הוגדלו בדצמבר 2025 בעקבות עומס על בסיס הנתונים שגרם ל-Timeouts שגויים בקאש
CACHE_CLEAR_BUDGET_SECONDS: float = Field(
default=float(os.getenv("CACHE_CLEAR_BUDGET_SECONDS", "5")),
ge=0.0,
le=30.0,
description="Time budget in seconds for cache maintenance (SCAN+DEL) to avoid blocking workers",
)
# HTTP client pooling/timeouts (aiohttp)
AIOHTTP_POOL_LIMIT: int = Field(
default=50,
ge=1,
le=10_000,
description="Default TCPConnector limit for aiohttp client sessions",
)
AIOHTTP_TIMEOUT_TOTAL: int = Field(
default=10,
ge=1,
le=300,
description="Default total timeout (seconds) for aiohttp client sessions",
)
AIOHTTP_LIMIT_PER_HOST: int = Field(
default=25,
ge=0,
le=10_000,
description="Per-host connection limit for aiohttp TCPConnector (0 = unlimited)",
)
# HTTP client pooling/timeouts (requests)
REQUESTS_POOL_CONNECTIONS: int = Field(
default=20,
ge=1,
le=10_000,
description="HTTPAdapter pool_connections for requests.Session",
)
REQUESTS_POOL_MAXSIZE: int = Field(
default=100,
ge=1,
le=100_000,
description="HTTPAdapter pool_maxsize for requests.Session",
)
REQUESTS_TIMEOUT: float = Field(
default=8.0,
ge=0.1,
le=600.0,
description="Default timeout (seconds) for requests.Session operations",
)
REQUESTS_RETRIES: int = Field(
default=2,
ge=0,
le=10,
description="Number of retry attempts for transient HTTP errors (if supported)",
)
REQUESTS_RETRY_BACKOFF: float = Field(
default=0.2,
ge=0.0,
le=60.0,
description="Exponential backoff factor between retries for requests",
)
# Rate limiting
RATE_LIMIT_ENABLED: bool = Field(default=True, description="Enable rate limiting globally")
RATE_LIMIT_SHADOW_MODE: bool = Field(
default=False,
description="Count-only mode, no blocking (for testing/deploy canary)",
)
RATE_LIMIT_STRATEGY: str = Field(
default="moving-window",
description="fixed-window or moving-window (if supported by backend)",
)
ADMIN_USER_IDS: List[int] = Field(
default_factory=list,
description="Admin user IDs who may bypass some limits",
)
# אינטגרציות
GITHUB_TOKEN: Optional[str] = Field(default=None, description="GitHub token")
PASTEBIN_API_KEY: Optional[str] = Field(default=None, description="Pastebin API key")
# Semantic Search / Embeddings
GEMINI_API_KEY: Optional[str] = Field(
default=None, description="Gemini API key for embeddings"
)
EMBEDDING_DIMENSIONS: int = Field(
default=768,
ge=256,
le=3072,
description="Embedding vector dimensions (768 or 1536)",
)
SEMANTIC_SEARCH_ENABLED: bool = Field(
default=True, description="Enable/disable semantic search feature"
)
CHUNK_SIZE_LINES: int = Field(
default=220,
ge=10,
description="Number of lines per code chunk",
)
CHUNK_OVERLAP_LINES: int = Field(
default=40, description="Overlap between consecutive chunks"
)
# מגבלות ושדות כלליים
MAX_CODE_SIZE: int = Field(
default=100_000,
ge=1_000,
le=10_000_000,
description="Maximum code size in bytes",
)
MAX_FILES_PER_USER: int = Field(
default=1_000, ge=1, le=100_000, description="Maximum files per user"
)
SUPPORTED_LANGUAGES: List[str] = Field(
default_factory=lambda: [
"python",
"javascript",
"html",
"css",
"java",
"cpp",
"c",
"php",
"ruby",
"go",
"rust",
"typescript",
"sql",
"bash",
"json",
"xml",
"yaml",
"markdown",
"dockerfile",
"nginx",
],
description="Supported languages for code highlighting and features",
)
# סל מיחזור
RECYCLE_TTL_DAYS: int = Field(
default=7, ge=1, description="Days to keep items in recycle bin"
)
# URL-ים
PUBLIC_BASE_URL: Optional[str] = Field(
default=None, description="Public base URL for sharing links"
)
WEBAPP_URL: Optional[str] = Field(
default=None, description="WebApp base URL (if different from public)"
)
# Remote Push Delivery (Worker)
PUSH_REMOTE_DELIVERY_ENABLED: bool = Field(
default=False,
description="Enable remote push delivery via external Worker",
)
PUSH_DELIVERY_URL: Optional[str] = Field(
default=None,
description="Base URL for push-delivery worker (without /send at the end)",
)
PUSH_DELIVERY_TOKEN: Optional[str] = Field(
default=None,
description="Shared bearer token for calls to the push-delivery worker",
)
PUSH_DELIVERY_TIMEOUT_SECONDS: float = Field(
default=3.0,
ge=0.1,
le=120.0,
description="Timeout (seconds) for worker health checks and delivery calls",
)
# תחזוקה
MAINTENANCE_MODE: bool = Field(default=False, description="Maintenance gate")
MAINTENANCE_MESSAGE: str = Field(
default="🚀 אנחנו מעלים עדכון חדש!\nהבוט יחזור לפעול ממש בקרוב (1 - 3 דקות)",
description="User-facing maintenance message",
)
MAINTENANCE_AUTO_WARMUP_SECS: int = Field(
default=30, ge=1, le=600, description="Warmup seconds after maintenance"
)
MAINTENANCE_WARMUP_GRACE_SECS: float = Field(
default=0.75,
ge=0.0,
le=30.0,
description="Grace window added to the maintenance warmup timer (seconds)",
)
# קצב
RATE_LIMIT_PER_MINUTE: int = Field(
default=30, ge=1, description="Default rate limit per minute"
)
# עיצוב
HIGHLIGHT_THEME: str = Field(default="github-dark", description="Pygments theme")
# Git
GIT_CHECKPOINT_PREFIX: str = Field(
default="checkpoint", description="Prefix for git checkpoints"
)
# Google Drive OAuth
GOOGLE_CLIENT_ID: Optional[str] = Field(default=None, description="OAuth client id")
GOOGLE_CLIENT_SECRET: Optional[str] = Field(
default=None, description="OAuth client secret"
)
GOOGLE_OAUTH_SCOPES: str = Field(
default="https://www.googleapis.com/auth/drive.file",
description="OAuth scopes for Google Drive",
)
GOOGLE_TOKEN_REFRESH_MARGIN_SECS: int = Field(
default=120, ge=30, le=3600, description="Refresh token margin (seconds)"
)
# דגלים ותיעוד
DRIVE_MENU_V2: bool = Field(default=True, description="Enable Drive menu v2")
DOCUMENTATION_URL: str = Field(
default="https://amirbiron.github.io/CodeBot/", description="Docs URL"
)
BOT_LABEL: str = Field(default="CodeBot", description="Bot label for UI")
# אימוג'י מותאם (טלגרם פרימיום) לאייקון ZIP בהודעות הבוט; ריק/None => אימוג'י רגיל.
# ה-ID הוא משאב חיצוני (חבילת צד ג') ולכן חי רק ב-ENV — לא מוטבע בקוד.
CUSTOM_EMOJI_ZIP_ID: Optional[str] = Field(
default=None, description="Custom emoji ID for ZIP icon (Telegram premium)"
)
DRIVE_ADD_HASH: bool = Field(
default=False, description="Append hash to filenames to avoid collisions"
)
NORMALIZE_CODE_ON_SAVE: bool = Field(
default=True, description="Normalize hidden characters before save"
)
# Feature flags
FEATURE_MY_COLLECTIONS: bool = Field(
default=True,
description="Enable 'My Collections' feature (API/UI)"
)
FEATURE_COLLECTIONS_TAGS: bool = Field(
default=True,
description="Enable tags for items in 'My Collections' (API/UI)"
)
# Community Library (bots/apps) feature flag
COMMUNITY_LIBRARY_ENABLED: bool = Field(
default=True,
description="Enable Community Library (public catalog, bot submit/approve, API/UI)"
)
# Pagination defaults
SEARCH_PAGE_SIZE: int = Field(
default=200,
ge=1,
le=10_000,
description="Default page size for DB-backed search pagination",
)
UI_PAGE_SIZE: int = Field(
default=10,
ge=1,
le=200,
description="Default UI pagination size for chat menus (e.g., lists)",
)
# Observability / Sentry
SENTRY_DSN: Optional[str] = Field(
default=None, description="Sentry DSN for error reporting"
)
# Enable admin-only test button in notifications menu
SENTRY_TEST_BUTTON_ENABLED: bool = Field(
default=False, description="Enable 'Send Sentry test event' admin button"
)
# Anomaly detection tuning
ANOMALY_IGNORE_ENDPOINTS: List[str] = Field(
default_factory=list,
description=(
"Comma-separated or JSON list of URL paths/endpoints to exclude from EWMA "
"latency calculations and slow-endpoint sampling (metrics are still recorded)."
),
)
# Metrics DB
METRICS_DB_ENABLED: bool = Field(
default=False, description="Enable metrics dual-write to DB"
)
METRICS_COLLECTION: str = Field(
default="service_metrics", description="Metrics collection name"
)
METRICS_BATCH_SIZE: int = Field(
default=50, ge=1, le=10_000, description="Metrics batch size"
)
METRICS_FLUSH_INTERVAL_SEC: int = Field(
default=5, ge=1, le=300, description="Metrics flush interval in seconds"
)
# הגדרות קריאה מ-.env ומשתני סביבה
model_config = SettingsConfigDict(
env_file=".env",
env_file_encoding="utf-8",
case_sensitive=True,
extra="ignore",
)
@field_validator("ADMIN_USER_IDS", mode="before")
@classmethod
def _parse_admin_user_ids(cls, v):
"""Parse ADMIN_USER_IDS from int, CSV string, JSON string, or list.
Security note: invalid tokens now raise ValueError instead of being silently dropped.
Accepted formats:
- int -> [int]
- "1,2,3" (CSV) -> [1, 2, 3]
- "[1, 2, 3]" (JSON) -> [1, 2, 3]
- [1, "2", 3] -> [1, 2, 3]
- empty/None -> []
"""
if v is None or v == "":
return []
# Handle iterable inputs (lists/tuples/sets)
if isinstance(v, (list, tuple, set)):
normalized: list[int] = []
invalid_items: list[str] = []
for item in v:
try:
normalized.append(int(item))
except Exception:
invalid_items.append(repr(item))
if invalid_items:
raise ValueError(
f"ADMIN_USER_IDS contains non-integer values: {', '.join(invalid_items)}"
)
return normalized
# Single integer
if isinstance(v, int):
return [v]
# Strings: try JSON first, then CSV
if isinstance(v, str):
s = v.strip()
if s == "":
return []
# Attempt JSON decoding (preserves previous behavior when JSON was used)
try:
parsed = json.loads(s)
except Exception:
parsed = None
if parsed is not None:
if isinstance(parsed, list):
return cls._parse_admin_user_ids(parsed)
if isinstance(parsed, int):
return [parsed]
raise ValueError(
"ADMIN_USER_IDS JSON must be a list of integers or a single integer"
)
# Fallback to strict CSV parsing
parts = [p.strip() for p in s.split(",")]
normalized: list[int] = []
invalid_tokens: list[str] = []
for part in parts:
if part == "": # allow empty tokens from trailing commas
continue
try:
normalized.append(int(part))
except Exception:
invalid_tokens.append(part)
if invalid_tokens:
raise ValueError(
f"ADMIN_USER_IDS contains non-integer tokens: {', '.join(invalid_tokens)}"
)
return normalized
raise ValueError(
"ADMIN_USER_IDS must be list[int], int, CSV string, or JSON list/int"
)
@field_validator("ANOMALY_IGNORE_ENDPOINTS", mode="before")
@classmethod
def _parse_anomaly_ignore_endpoints(cls, v):
"""Parse ANOMALY_IGNORE_ENDPOINTS from CSV/JSON/list and normalize paths."""
if v is None or v == "":
return []
tokens: list[object]
if isinstance(v, (list, tuple, set)):
tokens = list(v)
elif isinstance(v, str):
s = v.strip()
if s == "":
return []
if s.startswith("["):
try:
parsed = json.loads(s)
except Exception:
parsed = None
if parsed is not None:
if isinstance(parsed, list):
tokens = list(parsed)
else:
tokens = [parsed]
else:
tokens = [p.strip() for p in s.split(",")]
else:
tokens = [p.strip() for p in s.split(",")]
else:
tokens = [v]
out: list[str] = []
seen: set[str] = set()
for token in tokens:
try:
item = str(token or "").strip()
except Exception:
continue
if not item:
continue
if "://" in item:
try:
parsed_url = urlparse(item)
if parsed_url and parsed_url.path:
item = parsed_url.path
except Exception:
pass
# Drop query/hash, normalize trailing slash for paths
item = item.split("?", 1)[0].split("#", 1)[0].strip()
if item.startswith("/") and len(item) > 1:
item = item.rstrip("/")
if item and item not in seen:
out.append(item)
seen.add(item)
return out
@field_validator("PUSH_DELIVERY_URL", mode="before")
@classmethod
def _normalize_push_delivery_url(cls, v):
if v is None:
return None
s = str(v).strip()
if not s:
return None
# Keep as provided, but normalize trailing slash.
return s.rstrip("/")
@classmethod
def settings_customise_sources(
cls,
settings_cls: type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
"""אפשר שרשרת קבצי .env: קודם .env.local ואז .env, בנוסף למשתני סביבה."""
return (
init_settings,
env_settings,
# local overrides
DotEnvSettingsSource(settings_cls, env_file=".env.local", case_sensitive=True),
# default .env
DotEnvSettingsSource(settings_cls, env_file=".env", case_sensitive=True),
file_secret_settings,
)
@field_validator("MONGODB_URL")
@classmethod
def _validate_mongodb_url(cls, v: str) -> str:
if not v or not v.startswith(("mongodb://", "mongodb+srv://")):
raise ValueError(
"MONGODB_URL must start with mongodb:// or mongodb+srv://"
)
return v
def load_config() -> BotConfig:
"""
טוען את הקונפיגורציה ומחזיר מופע של BotConfig.
נשמרת תאימות לאחור עבור טסטים/קריאות קיימות.
"""
return BotConfig()
# יצירת אינסטנס גלובלי של הקונפיגורציה בזמן import — נשמרת תאימות לטסטים
try:
config = load_config()
except ValidationError as exc: # תאימות לטסט שמצפה ValueError בזמן import
# המרה ל-ValueError כדי לשמר התנהגות היסטורית
raise ValueError(str(exc)) from exc