|
| 1 | +import logging |
| 2 | +import os |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +logger = logging.getLogger(__name__) |
| 6 | + |
| 7 | +SECURITY_YML_PATH = Path( |
| 8 | + os.environ.get("PICOCLAW_SECURITY_YML", "/root/.picoclaw/.security.yml") |
| 9 | +) |
| 10 | + |
| 11 | +_cached_config: tuple[str, str] | None = None |
| 12 | + |
| 13 | + |
| 14 | +def load_asr_config(prefixes: list[str] | None = None, use_cache: bool = True) -> tuple[str, str]: |
| 15 | + global _cached_config |
| 16 | + if use_cache and _cached_config is not None: |
| 17 | + return _cached_config |
| 18 | + env_model = os.environ.get("ASR_MODEL", "").strip() |
| 19 | + env_key = os.environ.get("DASHSCOPE_API_KEY", "").strip() |
| 20 | + |
| 21 | + if env_model and env_key: |
| 22 | + _cached_config = (env_model, env_key) |
| 23 | + return _cached_config |
| 24 | + |
| 25 | + # Try .security.yml |
| 26 | + yml_result = _load_from_yml(prefixes) |
| 27 | + if yml_result is not None: |
| 28 | + yml_model, yml_key = yml_result |
| 29 | + model = env_model or yml_model |
| 30 | + key = env_key or yml_key |
| 31 | + if model and key: |
| 32 | + _cached_config = (model, key) |
| 33 | + return _cached_config |
| 34 | + |
| 35 | + # Fallback |
| 36 | + model = env_model or "" |
| 37 | + key = env_key or "" |
| 38 | + result = (model, key) |
| 39 | + if model and key: |
| 40 | + _cached_config = result |
| 41 | + return result |
| 42 | + |
| 43 | + |
| 44 | +def _load_from_yml(prefixes: list[str] | None = None) -> tuple[str, str] | None: |
| 45 | + try: |
| 46 | + if not SECURITY_YML_PATH.exists(): |
| 47 | + return None |
| 48 | + text = SECURITY_YML_PATH.read_text(encoding="utf-8") |
| 49 | + return _parse_yml(text, prefixes) |
| 50 | + except Exception as exc: |
| 51 | + logger.debug("Failed to read %s: %s", SECURITY_YML_PATH, exc) |
| 52 | + return None |
| 53 | + |
| 54 | + |
| 55 | +def _parse_yml(text: str, prefixes: list[str] | None = None) -> tuple[str, str] | None: |
| 56 | + """Extract first model block (matching prefixes) with an api_key. |
| 57 | +
|
| 58 | + Expected structure (indent = 2 spaces per level): |
| 59 | + <model-name>:0: |
| 60 | + api_keys: |
| 61 | + - <key> |
| 62 | + """ |
| 63 | + lines = text.splitlines() |
| 64 | + found_model = "" |
| 65 | + in_model = False |
| 66 | + in_api_keys = False |
| 67 | + |
| 68 | + for raw in lines: |
| 69 | + line = raw.rstrip() |
| 70 | + stripped = line.strip() |
| 71 | + if not stripped or stripped.startswith("#"): |
| 72 | + continue |
| 73 | + |
| 74 | + indent = len(line) - len(line.lstrip(" ")) |
| 75 | + |
| 76 | + if not in_model: |
| 77 | + if indent == 2 and stripped.endswith(":0:"): |
| 78 | + candidate = stripped[:-3] |
| 79 | + if prefixes is None or any(candidate.startswith(p) for p in prefixes): |
| 80 | + found_model = candidate |
| 81 | + in_model = True |
| 82 | + continue |
| 83 | + |
| 84 | + # Moved to another top-level model block |
| 85 | + if indent <= 2 and stripped.endswith(":0:"): |
| 86 | + # Check if this new block also matches |
| 87 | + candidate = stripped[:-3] |
| 88 | + in_model = False |
| 89 | + in_api_keys = False |
| 90 | + if prefixes is None or any(candidate.startswith(p) for p in prefixes): |
| 91 | + found_model = candidate |
| 92 | + in_model = True |
| 93 | + continue |
| 94 | + if indent == 0: |
| 95 | + break |
| 96 | + |
| 97 | + if indent == 4 and stripped == "api_keys:": |
| 98 | + in_api_keys = True |
| 99 | + continue |
| 100 | + |
| 101 | + if in_api_keys: |
| 102 | + if indent <= 4: |
| 103 | + in_api_keys = False |
| 104 | + continue |
| 105 | + if stripped.startswith("- "): |
| 106 | + key = stripped[2:].strip().strip('"').strip("'") |
| 107 | + if key: |
| 108 | + return found_model, key |
| 109 | + |
| 110 | + return None |
0 commit comments