<?php
declare(strict_types=1);
// @responsibility: Register Chat bounded context services in DI container
namespace ServiceProvider;
use Domain\Repository\ChatMessageRepositoryInterface;
use Domain\Repository\ChatSessionRepositoryInterface;
use Domain\Repository\ContentConfigRepositoryInterface;
use Domain\Repository\PromptsRepositoryInterface;
use Domain\Service\ModelRegistryInterface;
use Framework\Container;
use Infrastructure\AI\AIConfig;
use Infrastructure\AI\ChatService;
use Infrastructure\AI\ContentQualityValidator;
use Infrastructure\AI\ModelRegistry;
use Infrastructure\AI\OllamaService;
use Infrastructure\AI\QdrantClient;
use Infrastructure\AI\QdrantService;
use Infrastructure\AI\VectorSearchService;
use Infrastructure\Formatting\ChatMessageFormatter;
use Infrastructure\Persistence\ChatMessageRepository;
use Infrastructure\Persistence\ChatSessionRepository;
use Infrastructure\Persistence\ContentConfigRepository;
use Infrastructure\Persistence\CriticsRepository;
use Infrastructure\Persistence\PromptsRepository;
use UseCases\Chat\CreateChatSessionUseCase;
use UseCases\Chat\CreateChatSessionUseCaseInterface;
use UseCases\Chat\DeleteChatSessionUseCase;
use UseCases\Chat\DeleteChatSessionUseCaseInterface;
use UseCases\Chat\GetChatSessionUseCase;
use UseCases\Chat\GetChatSessionUseCaseInterface;
use UseCases\Chat\UpdateChatSessionUseCase;
use UseCases\Chat\UpdateChatSessionUseCaseInterface;
/**
* ChatServiceProvider registers all chat and AI-related services.
*/
final class ChatServiceProvider implements ServiceProviderInterface
{
public function register(Container $container): void
{
// Repositories
$container->set(ChatSessionRepository::class, fn (Container $c) => new ChatSessionRepository($c->get('pdo.content')));
$container->set(ChatMessageRepository::class, fn (Container $c) => new ChatMessageRepository($c->get('pdo.content')));
$container->set(PromptsRepository::class, fn (Container $c) => new PromptsRepository($c->get('pdo.content')));
$container->set(CriticsRepository::class, fn (Container $c) => new CriticsRepository($c->get('pdo.content')));
$container->set(ContentConfigRepository::class, fn (Container $c) => new ContentConfigRepository($c->get('pdo.content')));
// Interface aliases
$container->set(ChatSessionRepositoryInterface::class, fn (Container $c) => $c->get(ChatSessionRepository::class));
$container->set(ChatMessageRepositoryInterface::class, fn (Container $c) => $c->get(ChatMessageRepository::class));
$container->set(PromptsRepositoryInterface::class, fn (Container $c) => $c->get(PromptsRepository::class));
$container->set(ContentConfigRepositoryInterface::class, fn (Container $c) => $c->get(ContentConfigRepository::class));
// AI Services
$container->set(AIConfig::class, fn () => AIConfig::fromCredentialsFile());
$container->set(ChatService::class, fn (Container $c) => $c->get(AIConfig::class)->createChatService());
$container->set(OllamaService::class, fn (Container $c) => $c->get(AIConfig::class)->createOllamaService());
$container->set(QdrantService::class, fn (Container $c) => $c->get(AIConfig::class)->createQdrantService());
$container->set(VectorSearchService::class, fn () => new VectorSearchService());
$container->set(QdrantClient::class, fn () => new QdrantClient());
$container->set(ContentQualityValidator::class, function (Container $c) {
return new ContentQualityValidator($c->get(OllamaService::class));
});
$container->set(ModelRegistry::class, function (Container $c) {
$registry = new ModelRegistry($c->get('pdo.dev'));
ModelRegistry::setInstance($registry);
return $registry;
});
$container->set(ModelRegistryInterface::class, fn (Container $c) => $c->get(ModelRegistry::class));
// Formatters
$container->set(ChatMessageFormatter::class, fn () => new ChatMessageFormatter());
// Use Cases
$container->set(ManageChatSessionsUseCase::class, function (Container $c) {
return new ManageChatSessionsUseCase(
$c->get(ChatSessionRepositoryInterface::class),
$c->get(ChatMessageRepositoryInterface::class),
$c->get(ContentConfigRepository::class),
$c->get(\Domain\Repository\CollectionRepositoryInterface::class),
$c->get(\Infrastructure\Validation\CollectionValidator::class),
$c->get('pdo.dev'),
$c->get(ModelRegistry::class)
);
});
}
}