Skip to content

Self-improving AI Employees for everyone

7.5 KB
Raw Blame
"""Dependency injection container for the application."""


def _clog(msg):
    """Pre-logger import-boundary marker.

    Emits via ``print()``; the CLI wrapper prefixes the absolute
    timestamp, so this helper carries no inline elapsed-time anymore.
    Kept as a function rather than inlining ``print()`` so future
    routing changes (e.g. to a logger after init) hit one site.
    """
    print(f"           container: {msg}", flush=True)


from typing import Any

from dependency_injector import containers, providers

from core.config import Settings
from core.database import Database
from core.cache import CacheService
from core.encryption import EncryptionService
from core.credentials_database import CredentialsDatabase

_clog("core imports done")
from services.ai import AIService

_clog("AIService imported")
from services.workflow import WorkflowService

_clog("WorkflowService imported")
from services.auth import AuthService
from services.text import TextService
from services.user_auth import UserAuthService
from services.compaction import init_compaction_service

_clog("all service imports done")

# Side-effect import: populates ``services.llm.registry._REGISTRY`` with
# every shipped provider (4 dedicated + 6 OpenAI-compat). MUST run
# BEFORE the DI container constructs ``ChatUnifier`` so the unifier sees
# the full provider catalogue. ``ChatUnifier`` is the single facade
# ``AIService`` uses for chat-model dispatch + typed-exception translation.
from services.llm import ChatUnifier
from services.llm.config import LLM_DEFAULTS

_clog("LLM registry populated + unifier imported")

from services.temporal import TemporalClientWrapper


def _create_temporal_client(server_address: str, namespace: str):
    """Factory function for temporal client."""
    return TemporalClientWrapper(server_address=server_address, namespace=namespace)


def _build_gateway_router(settings, auth_service):
    """The OpenCompany Gateway router (services/gateway/routing.py).

    Imported here, not at module scope, so the container's import doesn't
    register the gateway's Connectors handlers as a side effect.
    """
    from services.gateway.routing import GatewayRouter

    return GatewayRouter(settings=settings, auth_service=auth_service, defaults=LLM_DEFAULTS)


def _build_chat_unifier(auth_service, gateway_router):
    """Construct the chat-model unifier with the shared JSON defaults.

    Wrapped in a factory function so the DI container doesn't have to
    inject ``LLM_DEFAULTS`` (a module-level dict). ``LLM_DEFAULTS`` is
    loaded once at import time by ``services.llm.config`` and reflects
    the on-disk ``server/config/llm_defaults.json``.
    """
    return ChatUnifier(defaults=LLM_DEFAULTS, auth_service=auth_service, gateway=gateway_router)


# Wave 12 C4 sub-piece C: plugin-owned service factories.
# ``MapsService`` (nodes/location) and ``AndroidService`` (nodes/android)
# used to be imported here at module scope so providers.Factory could
# reference the classes directly. That made the framework DI container
# know about specific plugins' implementation classes — direct
# violation of the "framework knows no plugin names" rule.
# Now the plugins self-register their service classes via
# ``services.plugin.service_factories.register_service_factory``;
# the container's provider declarations reference the helper functions
# below, which look up the registered factory at instantiation time
# (first ``container.foo()`` call). Plugin auto-discovery on startup
# populates the registry before any service is first resolved, so
# import-ordering is not a constraint.


def _build_registered_service(name: str, **kwargs: Any) -> Any:
    """Look up a plugin-registered service factory and instantiate it.

    Wrapped behind a thin function so the DI container can declare
    providers without importing the concrete service classes.
    """
    from services.plugin.service_factories import get_service_factory

    factory = get_service_factory(name)
    if factory is None:
        raise RuntimeError(
            f"DI container: service factory {name!r} is not registered. "
            "The owning plugin failed to import (or hasn't shipped a "
            "register_service_factory call from its __init__.py). Check "
            "the plugin folder's __init__.py for the registration."
        )
    return factory(**kwargs)


def _build_maps_service(auth_service, settings):
    return _build_registered_service("maps", auth_service=auth_service, settings=settings)


def _build_android_service():
    return _build_registered_service("android")


class Container(containers.DeclarativeContainer):
    """Application dependency injection container."""

    # Configuration
    config = providers.Configuration()

    # Settings
    settings = providers.Singleton(
        Settings,
    )

    # Database (needed by CacheService for SQLite fallback)
    database = providers.Singleton(Database, settings=settings)

    # Cache service (uses Redis when available, SQLite otherwise)
    cache = providers.Singleton(CacheService, settings=settings, database=database)

    # Encryption service for credentials (initialized on user login)
    encryption_service = providers.Singleton(EncryptionService)

    # Credentials database (separate encrypted database for API keys and OAuth tokens)
    credentials_database = providers.Singleton(
        CredentialsDatabase, db_path=settings.provided.credentials_db_resolved, encryption=encryption_service
    )

    # Temporal client
    temporal_client = providers.Singleton(
        _create_temporal_client,
        server_address=settings.provided.temporal_server_address,
        namespace=settings.provided.temporal_namespace,
    )

    # Services
    auth_service = providers.Singleton(AuthService, credentials_db=credentials_database, cache=cache, database=database, settings=settings)

    user_auth_service = providers.Factory(
        UserAuthService, database=database, settings=settings, encryption=encryption_service, credentials_db=credentials_database
    )

    # ChatUnifier — single SERVICE facade for chat-model dispatch.
    # Reads the provider registry populated at import time and applies
    # JSON-driven typed-exception translation + ``incompatible_models``
    # filter. ``AIService`` delegates to this rather than calling
    # provider classes directly.
    # Own keys first; a provider with no key of the install's own runs on
    # the OpenCompany Gateway when it is connected (services/gateway).
    gateway_router = providers.Singleton(_build_gateway_router, settings=settings, auth_service=auth_service)

    chat_unifier = providers.Singleton(_build_chat_unifier, auth_service=auth_service, gateway_router=gateway_router)

    ai_service = providers.Singleton(
        AIService,
        auth_service=auth_service,
        database=database,
        cache=cache,
        settings=settings,
        chat_unifier=chat_unifier,
        gateway_router=gateway_router,
    )

    maps_service = providers.Factory(_build_maps_service, auth_service=auth_service, settings=settings)

    text_service = providers.Factory(TextService)

    android_service = providers.Factory(_build_android_service)

    compaction_service = providers.Singleton(init_compaction_service, database=database, settings=settings)

    workflow_service = providers.Singleton(
        WorkflowService,
        database=database,
        ai_service=ai_service,
        maps_service=maps_service,
        text_service=text_service,
        android_service=android_service,
        cache=cache,
        settings=settings,
    )


# Global container instance
container = Container()