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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
"""

Settings Manager Module



This module provides secure environment variable management with UI integration,

supporting both cache and .env file storage options.



Features:

- πŸ” Secure API key handling with masking

- ⚑ Real-time validation and testing

- πŸ’Ύ Dual storage backends (cache + .env file)

- πŸ›‘οΈ Input sanitization and validation

- πŸ”„ Live system updates

"""

import os
import re
import logging
import json
import time
from typing import Dict, Any, Optional, Tuple, List
from pathlib import Path
from datetime import datetime
import tempfile


class SettingsManager:
    """

    Manages environment variables with secure storage and validation.



    Features:

    - Secure API key masking and validation

    - Real-time connection testing

    - Cache and .env file storage options

    - Integration with existing ConfigManager

    """

    def __init__(self, config_manager=None):
        """

        Initialize the SettingsManager.



        Args:

            config_manager: Optional ConfigManager instance for integration

        """
        self.logger = logging.getLogger(__name__)
        self.config_manager = config_manager

        # πŸ”§ Cache storage for temporary settings
        self._cache_storage = {}

        # πŸ“ Project root for .env file
        self.project_root = Path(__file__).parent.parent.parent
        self.env_file_path = self.project_root / ".env"

        # πŸ›‘οΈ Supported environment variables with validation rules
        self.supported_env_vars = {
            "GEMINI_API_KEY": {
                "required": True,
                "description": "Google Gemini API Key for embeddings and LLM",
                "format": r"^AIzaSy[A-Za-z0-9_-]{33}$",
                "mask": True,
                "test_function": self._test_gemini_connection,
                "placeholder": "AIzaSy...",
                "help_url": "https://aistudio.google.com/",
            },
            "PINECONE_API_KEY": {
                "required": False,
                "description": "Pinecone API Key for vector database",
                "format": r"^pc-[A-Za-z0-9]{32}$",
                "mask": True,
                "test_function": self._test_pinecone_connection,
                "placeholder": "pc-...",
                "help_url": "https://www.pinecone.io/",
            },
            "OPENAI_API_KEY": {
                "required": False,
                "description": "OpenAI API Key for alternative LLM",
                "format": r"^sk-[A-Za-z0-9]{48}$",
                "mask": True,
                "test_function": self._test_openai_connection,
                "placeholder": "sk-...",
                "help_url": "https://platform.openai.com/api-keys",
            },
            "TAVILY_API_KEY": {
                "required": False,
                "description": "Tavily API Key for live web search",
                "format": r"^tvly-[A-Za-z0-9-]{20,50}$",
                "mask": True,
                "test_function": self._test_tavily_connection,
                "placeholder": "tvly-dev-...",
                "help_url": "https://app.tavily.com/sign-in",
            },
            "PINECONE_ENVIRONMENT": {
                "required": False,
                "description": "Pinecone environment region",
                "format": r"^[a-z0-9-]+$",
                "mask": False,
                "default": "us-east-1",
                "placeholder": "us-east-1",
                "options": [
                    "us-east-1",
                    "us-west1-gcp",
                    "eu-west1-gcp",
                    "asia-southeast1-gcp",
                ],
            },
            "PINECONE_INDEX_NAME": {
                "required": False,
                "description": "Pinecone index name",
                "format": r"^[a-z0-9-]+$",
                "mask": False,
                "default": "rag-ai-index",
                "placeholder": "rag-ai-index",
            },
            "GRADIO_SHARE": {
                "required": False,
                "description": "Enable Gradio public sharing",
                "format": r"^(true|false)$",
                "mask": False,
                "default": "false",
                "options": ["true", "false"],
            },
            "PORT": {
                "required": False,
                "description": "Server port number",
                "format": r"^[1-9][0-9]{3,4}$",
                "mask": False,
                "default": "7860",
                "placeholder": "7860",
            },
        }

        self.logger.info("SettingsManager initialized successfully")

    def get_current_settings(self) -> Dict[str, Any]:
        """

        Get current environment variable settings with status.



        Returns:

            Dictionary with current settings and their status

        """
        settings = {}

        for var_name, config in self.supported_env_vars.items():
            # πŸ” Get value from cache, environment, or default
            value = self._get_env_value(var_name)

            settings[var_name] = {
                "value": (
                    self._mask_value(value, config.get("mask", False)) if value else ""
                ),
                "raw_value": value or "",
                "is_set": bool(value),
                "is_valid": (
                    self._validate_format(value, config.get("format"))
                    if value
                    else False
                ),
                "is_required": config.get("required", False),
                "description": config.get("description", ""),
                "placeholder": config.get("placeholder", ""),
                "help_url": config.get("help_url", ""),
                "options": config.get("options", []),
                "source": self._get_value_source(var_name),
                "last_tested": self._cache_storage.get(f"{var_name}_last_tested"),
                "test_status": self._cache_storage.get(
                    f"{var_name}_test_status", "untested"
                ),
            }

        return settings

    def update_setting(

        self, var_name: str, value: str, storage_type: str = "cache"

    ) -> Dict[str, Any]:
        """

        Update an environment variable setting.



        Args:

            var_name: Environment variable name

            value: New value

            storage_type: "cache" or "env_file"



        Returns:

            Dictionary with operation result

        """
        try:
            if var_name not in self.supported_env_vars:
                return {
                    "success": False,
                    "error": f"Unsupported environment variable: {var_name}",
                    "status": "❌ Invalid variable",
                }

            config = self.supported_env_vars[var_name]

            # πŸ›‘οΈ Validate format
            if value and not self._validate_format(value, config.get("format")):
                return {
                    "success": False,
                    "error": f"Invalid format for {var_name}",
                    "status": "❌ Invalid format",
                    "expected_format": config.get("placeholder", ""),
                }

            # πŸ’Ύ Store based on storage type
            if storage_type == "cache":
                self._cache_storage[var_name] = value
                os.environ[var_name] = value  # ⚑ Update current session
                status_msg = "πŸ’Ύ Saved to cache"
            elif storage_type == "env_file":
                self._save_to_env_file(var_name, value)
                os.environ[var_name] = value  # ⚑ Update current session
                status_msg = "πŸ“ Saved to .env file"
            else:
                return {
                    "success": False,
                    "error": f"Invalid storage type: {storage_type}",
                    "status": "❌ Invalid storage type",
                }

            # πŸ”„ Update config manager if available
            if self.config_manager:
                try:
                    self.config_manager.reload()
                except Exception as e:
                    self.logger.warning(f"Could not reload config manager: {e}")

            self.logger.info(f"Updated {var_name} via {storage_type}")

            return {
                "success": True,
                "status": f" {status_msg}",
                "value": self._mask_value(value, config.get("mask", False)),
                "storage_type": storage_type,
                "timestamp": datetime.now().isoformat(),
            }

        except Exception as e:
            self.logger.error(f"Error updating {var_name}: {e}")
            return {"success": False, "error": str(e), "status": " Update failed"}

    def test_connection(self, var_name: str) -> Dict[str, Any]:
        """

        Test API connection for a given environment variable.



        Args:

            var_name: Environment variable name



        Returns:

            Dictionary with test results

        """
        try:
            if var_name not in self.supported_env_vars:
                return {
                    "success": False,
                    "error": f"Cannot test {var_name}: not supported",
                    "status": "❌ Not testable",
                }

            config = self.supported_env_vars[var_name]
            test_function = config.get("test_function")

            if not test_function:
                return {
                    "success": False,
                    "error": f"No test function available for {var_name}",
                    "status": "⚠️ No test available",
                }

            value = self._get_env_value(var_name)
            if not value:
                return {
                    "success": False,
                    "error": f"{var_name} is not set",
                    "status": "❌ Not configured",
                }

            # πŸ§ͺ Run the test
            self.logger.info(f"Testing connection for {var_name}")
            test_result = test_function(value)

            # πŸ“Š Cache test results
            timestamp = datetime.now().isoformat()
            self._cache_storage[f"{var_name}_last_tested"] = timestamp
            self._cache_storage[f"{var_name}_test_status"] = (
                "success" if test_result["success"] else "failed"
            )

            return {**test_result, "timestamp": timestamp, "variable": var_name}

        except Exception as e:
            self.logger.error(f"Error testing {var_name}: {e}")
            error_result = {
                "success": False,
                "error": str(e),
                "status": "❌ Test failed",
                "timestamp": datetime.now().isoformat(),
            }

            # πŸ“Š Cache failed test
            self._cache_storage[f"{var_name}_last_tested"] = error_result["timestamp"]
            self._cache_storage[f"{var_name}_test_status"] = "failed"

            return error_result

    def load_from_env_file(self) -> Dict[str, Any]:
        """

        Load settings from .env file.



        Returns:

            Dictionary with load results

        """
        try:
            if not self.env_file_path.exists():
                return {
                    "success": False,
                    "error": ".env file not found",
                    "status": "πŸ“ No .env file found",
                    "loaded_count": 0,
                }

            loaded_vars = []

            with open(self.env_file_path, "r", encoding="utf-8") as f:
                for line_num, line in enumerate(f, 1):
                    line = line.strip()
                    if line and not line.startswith("#") and "=" in line:
                        try:
                            key, value = line.split("=", 1)
                            key = key.strip()
                            value = value.strip().strip("\"'")  # Remove quotes

                            if key in self.supported_env_vars:
                                os.environ[key] = value
                                loaded_vars.append(key)
                        except Exception as e:
                            self.logger.warning(
                                f"Error parsing line {line_num} in .env: {e}"
                            )

            # πŸ”„ Reload config manager
            if self.config_manager:
                try:
                    self.config_manager.reload()
                except Exception as e:
                    self.logger.warning(f"Could not reload config manager: {e}")

            return {
                "success": True,
                "status": f" Loaded {len(loaded_vars)} variables from .env",
                "loaded_count": len(loaded_vars),
                "loaded_variables": loaded_vars,
            }

        except Exception as e:
            self.logger.error(f"Error loading from .env file: {e}")
            return {
                "success": False,
                "error": str(e),
                "status": " Failed to load .env file",
                "loaded_count": 0,
            }

    def clear_cache(self) -> Dict[str, Any]:
        """

        Clear cached settings.



        Returns:

            Dictionary with operation result

        """
        try:
            # πŸ—‘οΈ Clear cache but preserve test results
            cached_vars = [
                key
                for key in self._cache_storage.keys()
                if key in self.supported_env_vars
            ]

            for var in cached_vars:
                if var in self._cache_storage:
                    del self._cache_storage[var]
                    # Remove from current environment if it was cached
                    if var in os.environ:
                        del os.environ[var]

            return {
                "success": True,
                "status": f"πŸ—‘οΈ Cleared {len(cached_vars)} cached variables",
                "cleared_count": len(cached_vars),
            }

        except Exception as e:
            self.logger.error(f"Error clearing cache: {e}")
            return {
                "success": False,
                "error": str(e),
                "status": " Failed to clear cache",
            }

    def export_settings(self, include_sensitive: bool = False) -> Dict[str, Any]:
        """

        Export current settings for backup/sharing.



        Args:

            include_sensitive: Whether to include API keys (masked)



        Returns:

            Dictionary with exported settings

        """
        try:
            settings = self.get_current_settings()
            exported = {}

            for var_name, config in settings.items():
                var_config = self.supported_env_vars[var_name]

                # πŸ” Skip sensitive data if not requested
                if var_config.get("mask", False) and not include_sensitive:
                    continue

                exported[var_name] = {
                    "value": (
                        config["value"] if include_sensitive else config["raw_value"]
                    ),
                    "is_set": config["is_set"],
                    "source": config["source"],
                    "description": config["description"],
                }

            return {
                "success": True,
                "settings": exported,
                "export_timestamp": datetime.now().isoformat(),
                "include_sensitive": include_sensitive,
            }

        except Exception as e:
            self.logger.error(f"Error exporting settings: {e}")
            return {"success": False, "error": str(e)}

    # πŸ”§ Private helper methods

    def _get_env_value(self, var_name: str) -> Optional[str]:
        """Get environment variable value from cache or environment."""
        # Priority: cache > environment > default
        if var_name in self._cache_storage:
            return self._cache_storage[var_name]

        env_value = os.environ.get(var_name)
        if env_value:
            return env_value

        return self.supported_env_vars[var_name].get("default")

    def _get_value_source(self, var_name: str) -> str:
        """Determine the source of an environment variable value."""
        if var_name in self._cache_storage:
            return "cache"
        elif os.environ.get(var_name):
            return "environment"
        elif self.supported_env_vars[var_name].get("default"):
            return "default"
        else:
            return "unset"

    def _mask_value(self, value: str, should_mask: bool) -> str:
        """Mask sensitive values for display."""
        if not value or not should_mask:
            return value

        if len(value) <= 8:
            return "*" * len(value)

        return value[:4] + "*" * (len(value) - 8) + value[-4:]

    def _validate_format(self, value: str, format_pattern: Optional[str]) -> bool:
        """Validate value against format pattern."""
        if not format_pattern or not value:
            return True

        try:
            return bool(re.match(format_pattern, value))
        except Exception:
            return False

    def _save_to_env_file(self, var_name: str, value: str):
        """Save environment variable to .env file."""
        env_vars = {}

        # πŸ“– Read existing .env file
        if self.env_file_path.exists():
            with open(self.env_file_path, "r", encoding="utf-8") as f:
                for line in f:
                    line = line.strip()
                    if line and not line.startswith("#") and "=" in line:
                        try:
                            key, val = line.split("=", 1)
                            env_vars[key.strip()] = val.strip().strip("\"'")
                        except Exception as e:
                            self.logger.warning(f"Error parsing line in .env: {e}")

        # ✏️ Update the variable
        env_vars[var_name] = value

        # πŸ’Ύ Write back to file
        with open(self.env_file_path, "w", encoding="utf-8") as f:
            f.write("# Environment Variables for RAG AI System\n")
            f.write(f"# Generated on {datetime.now().isoformat()}\n\n")

            for key, val in env_vars.items():
                # πŸ” Quote values that contain spaces or special characters
                if " " in val or any(char in val for char in ["$", '"', "'"]):
                    f.write(f'{key}="{val}"\n')
                else:
                    f.write(f"{key}={val}\n")

    # πŸ§ͺ API Testing Functions

    # Cache for Gemini client to avoid recreating it
    _gemini_client_cache = None
    _gemini_client_key = None
    _gemini_last_test_time = None
    _gemini_test_cooldown = 10  # seconds between tests

    def _test_gemini_connection(self, api_key: str) -> Dict[str, Any]:
        """Test Gemini API connection with caching and optimization."""
        try:
            # Check if we've tested this key recently
            current_time = time.time()
            if (
                self._gemini_last_test_time
                and api_key == self._gemini_client_key
                and current_time - self._gemini_last_test_time
                < self._gemini_test_cooldown
            ):

                self.logger.info(
                    "Using cached Gemini test result (within cooldown period)"
                )
                return {
                    "success": True,
                    "status": "βœ… Gemini API connected (cached)",
                    "details": "Using cached test result",
                }

            import google.generativeai as genai

            # Use cached client if the API key is the same
            if api_key == self._gemini_client_key and self._gemini_client_cache:
                self.logger.info("Using cached Gemini client")
                client = self._gemini_client_cache
            else:
                # Configure new client
                genai.configure(api_key=api_key)
                self._gemini_client_cache = genai
                self._gemini_client_key = api_key
                client = genai

            # πŸ§ͺ Simple test call - use embedding API instead of GenerativeModel
            # This is faster and more efficient for testing connection
            test_result = client.embed_content(
                model="gemini-embedding-exp-03-07",
                content="test connection",
                task_type="retrieval_document",
            )

            # Update last test time
            self._gemini_last_test_time = current_time

            if test_result and "embedding" in test_result:
                return {
                    "success": True,
                    "status": "βœ… Gemini API connected",
                    "details": "API key is valid and working",
                }
            else:
                return {
                    "success": False,
                    "status": "❌ Gemini API failed",
                    "error": "No embedding in response",
                }

        except Exception as e:
            return {
                "success": False,
                "status": "❌ Gemini connection failed",
                "error": str(e),
            }

    def _test_pinecone_connection(self, api_key: str) -> Dict[str, Any]:
        """Test Pinecone API connection."""
        try:
            from pinecone import Pinecone

            pc = Pinecone(api_key=api_key)

            # πŸ§ͺ Test by listing indexes
            indexes = pc.list_indexes()

            return {
                "success": True,
                "status": "βœ… Pinecone API connected",
                "details": f"Found {len(indexes)} indexes",
            }

        except Exception as e:
            return {
                "success": False,
                "status": "❌ Pinecone connection failed",
                "error": str(e),
            }

    def _test_openai_connection(self, api_key: str) -> Dict[str, Any]:
        """Test OpenAI API connection."""
        try:
            import openai

            client = openai.OpenAI(api_key=api_key)

            # πŸ§ͺ Test with a simple completion
            response = client.chat.completions.create(
                model="gpt-3.5-turbo",
                messages=[{"role": "user", "content": "Hello"}],
                max_tokens=5,
            )

            if response and response.choices:
                return {
                    "success": True,
                    "status": "βœ… OpenAI API connected",
                    "details": "API key is valid and working",
                }
            else:
                return {
                    "success": False,
                    "status": "❌ OpenAI API failed",
                    "error": "No response from API",
                }

        except Exception as e:
            return {
                "success": False,
                "status": " OpenAI connection failed",
                "error": str(e),
            }

    def _test_tavily_connection(self, api_key: str) -> Dict[str, Any]:
        """Test Tavily API connection."""
        try:
            from tavily import TavilyClient

            # πŸ§ͺ Initialize client and test with a simple search
            client = TavilyClient(api_key=api_key)

            # Test with a minimal search query
            response = client.search(query="test", max_results=1, search_depth="basic")

            if response and isinstance(response, dict):
                return {
                    "success": True,
                    "status": "βœ… Tavily API connected",
                    "details": "API key is valid and working",
                }
            else:
                return {
                    "success": False,
                    "status": "❌ Tavily API failed",
                    "error": "No valid response from API",
                }

        except Exception as e:
            return {
                "success": False,
                "status": "❌ Tavily connection failed",
                "error": str(e),
            }