import time from typing import Callable from .agent_runs import AgentRunRepository, AgentService, configured_secrets from .alerts import AlertRepository from .chat import BACKEND_MATRIX, BACKEND_TELEGRAM from .checkmk import CheckmkClient from .commands import (CallbackHandler, CommandDeps, CommandHandler, SettingsReader, command_audit) from .constants import MATRIX_ALLOWLIST, TELEGRAM_ALLOWLIST from .db import Database from .downtime import DowntimeService, PendingStore from .listeners import ListenerState, MatrixListener, TelegramListener from .mutes import MuteService from .poller import StateStore ALLOWLIST_KEYS = {BACKEND_TELEGRAM: TELEGRAM_ALLOWLIST, BACKEND_MATRIX: MATRIX_ALLOWLIST} class ChatOps: def __init__(self, db: Database, store, state: StateStore, alerts: AlertRepository, mutes: MuteService, agent_prompt: Callable[[], str | None] | None = None): def allowlist(backend: str) -> str | None: return store.get(ALLOWLIST_KEYS[backend]) if backend in ALLOWLIST_KEYS else None bot_names: dict[str, str] = {} self.agent_runs = AgentRunRepository(db) self.agent = AgentService(store, db, self.agent_runs, state.recent_changes, lambda: configured_secrets(store), prompt_provider=agent_prompt) self.downtime = DowntimeService(store, db, PendingStore(time.monotonic), allowlist, reader_factory=lambda: CheckmkClient.from_settings(store)) deps = CommandDeps(SettingsReader(store), self.agent, mutes=mutes, lookup=alerts, downtime=self.downtime) audit_log = command_audit(db) self.handler = CommandHandler(deps, allowlist, audit_log, bot_name=bot_names.get) self.callbacks = CallbackHandler(deps, allowlist, audit_log) listener_state = ListenerState(db) self.telegram = TelegramListener(store, self.handler, self.callbacks, listener_state, bot_names) self.matrix = MatrixListener(store, self.handler, listener_state) def listener_runners(self): return [self.telegram.run, self.matrix.run]