refactor: centralize settings and application services

This commit is contained in:
kandrusyak
2026-07-27 17:47:39 +03:00
parent 248e90faf5
commit ab029a4c67
10 changed files with 296 additions and 78 deletions

View File

@@ -12,22 +12,7 @@ from telegram.ext import (
from .ai import AIClient
from .auth import authentication_guard
from .config import (
get_assistant_mode,
get_assistant_password,
get_bot_token,
get_db_path,
get_local_timezone,
get_ollama_base_url,
get_voice_max_duration_seconds,
get_whisper_compute_type,
get_whisper_device,
get_whisper_language,
get_whisper_model,
get_yandex_cloud_folder,
get_yandex_stt_language,
get_yandex_stt_model,
)
from .config import AppSettings, load_app_settings
from .handlers import (
ask_command,
help_command,
@@ -42,6 +27,7 @@ from .handlers import (
)
from .jobs import post_init, post_shutdown
from .ollama import OllamaClient
from .services import SERVICES_KEY, ApplicationServices
from .storage import AssistantStorage
from .speech import SpeechRecognizer, SpeechTranscriber
from .yandex_ai import YandexAIClient, YandexSpeechRecognizer
@@ -55,42 +41,51 @@ def configure_logging() -> None:
logging.getLogger("httpx").setLevel(logging.WARNING)
def create_application() -> Application:
assistant_password = get_assistant_password()
storage = AssistantStorage(get_db_path())
mode = get_assistant_mode()
def create_services(settings: AppSettings) -> ApplicationServices:
storage = AssistantStorage(settings.db_path)
ai_client: AIClient
speech_recognizer: SpeechTranscriber
if settings.mode == "local":
ai_client = OllamaClient(settings.ollama_base_url)
speech_recognizer = SpeechRecognizer(
model_name=settings.whisper_model,
device=settings.whisper_device,
compute_type=settings.whisper_compute_type,
language=settings.whisper_language,
)
else:
folder_id = settings.yandex_cloud_folder
if folder_id is None:
raise RuntimeError(
"YANDEX_CLOUD_FOLDER is required in yandex mode."
)
ai_client = YandexAIClient(folder_id=folder_id)
speech_recognizer = YandexSpeechRecognizer(
folder_id=folder_id,
language=settings.yandex_stt_language,
model=settings.yandex_stt_model,
)
return ApplicationServices(
storage=storage,
ai_client=ai_client,
timezone=settings.timezone,
speech_recognizer=speech_recognizer,
voice_max_duration_seconds=settings.voice_max_duration_seconds,
assistant_password=settings.assistant_password,
)
def create_application(settings: AppSettings | None = None) -> Application:
settings = settings or load_app_settings()
application = (
Application.builder()
.token(get_bot_token())
.token(settings.bot_token)
.post_init(post_init)
.post_shutdown(post_shutdown)
.build()
)
application.bot_data["storage"] = storage
application.bot_data["assistant_password"] = assistant_password
ai_client: AIClient
speech_recognizer: SpeechTranscriber
if mode == "local":
ai_client = OllamaClient(get_ollama_base_url())
speech_recognizer = SpeechRecognizer(
model_name=get_whisper_model(),
device=get_whisper_device(),
compute_type=get_whisper_compute_type(),
language=get_whisper_language(),
)
else:
folder_id = get_yandex_cloud_folder()
ai_client = YandexAIClient(folder_id=folder_id)
speech_recognizer = YandexSpeechRecognizer(
folder_id=folder_id,
language=get_yandex_stt_language(),
model=get_yandex_stt_model(),
)
application.bot_data["ai_client"] = ai_client
application.bot_data["timezone"] = get_local_timezone()
application.bot_data["speech_recognizer"] = speech_recognizer
application.bot_data["voice_max_duration"] = get_voice_max_duration_seconds()
application.bot_data[SERVICES_KEY] = create_services(settings)
application.add_handler(TypeHandler(Update, authentication_guard), group=-1)
application.add_handler(CommandHandler("start", start))