fix: accept CSV telegram ID config (#27)
Co-authored-by: openhands <openhands@all-hands.dev>
This commit is contained in:
+40
-3
@@ -8,16 +8,50 @@ Supports configuration via:
|
||||
Environment variables take precedence over config file values.
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from omegaconf import OmegaConf
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _parse_int_list(value: Any) -> Any:
|
||||
if isinstance(value, int):
|
||||
return [value]
|
||||
if not isinstance(value, str):
|
||||
return value
|
||||
|
||||
text = value.strip()
|
||||
if not text:
|
||||
return []
|
||||
|
||||
if len(text) >= 2 and text[0] == text[-1] and text[0] in {"'", '"'}:
|
||||
text = text[1:-1].strip()
|
||||
if not text:
|
||||
return []
|
||||
|
||||
if text.startswith("["):
|
||||
try:
|
||||
parsed = json.loads(text)
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
else:
|
||||
if isinstance(parsed, list):
|
||||
return parsed
|
||||
|
||||
try:
|
||||
return [int(item.strip().strip("'\"")) for item in text.split(",") if item.strip()]
|
||||
except ValueError as exc:
|
||||
raise ValueError(
|
||||
"expected a comma-separated list of integers or a JSON integer list"
|
||||
) from exc
|
||||
|
||||
|
||||
class TelegramConfig(BaseModel):
|
||||
"""Telegram bot configuration."""
|
||||
|
||||
@@ -25,6 +59,11 @@ class TelegramConfig(BaseModel):
|
||||
allowed_user_ids: list[int] = Field(default_factory=list, description="Allowed user IDs")
|
||||
group_ids: list[int] = Field(default_factory=list, description="Allowed group/channel IDs")
|
||||
|
||||
@field_validator("allowed_user_ids", "group_ids", mode="before")
|
||||
@classmethod
|
||||
def parse_int_list(cls, value: Any) -> Any:
|
||||
return _parse_int_list(value)
|
||||
|
||||
|
||||
class OpenAIConfig(BaseModel):
|
||||
"""OpenAI/LLM configuration."""
|
||||
@@ -186,8 +225,6 @@ def _load_config_from_env() -> dict:
|
||||
# Try to parse value as JSON first (for lists, etc.)
|
||||
if isinstance(cfg[section], dict):
|
||||
try:
|
||||
import json
|
||||
|
||||
cfg[section][setting_key] = json.loads(value)
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
cfg[section][setting_key] = value
|
||||
|
||||
Reference in New Issue
Block a user