-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDependencyInjection.cs
More file actions
192 lines (173 loc) · 10.2 KB
/
DependencyInjection.cs
File metadata and controls
192 lines (173 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Taskdeck.Application.Connectors;
using Taskdeck.Application.Interfaces;
using Taskdeck.Application.Services;
using Taskdeck.Domain.Connectors;
using Taskdeck.Infrastructure.Connectors;
using Taskdeck.Infrastructure.Persistence;
using Taskdeck.Infrastructure.Repositories;
using Taskdeck.Infrastructure.Services;
namespace Taskdeck.Infrastructure;
public static class DependencyInjection
{
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
{
var connectionString = configuration.GetConnectionString("DefaultConnection")
?? "Data Source=taskdeck.db";
var databaseSettings = configuration.GetSection("Database").Get<DatabaseSettings>()
?? new DatabaseSettings();
// Enforce validation for all host modes (API, CLI, MCP).
// ValidateOnStart causes an exception at startup if CommandTimeoutSeconds
// is out of the [1, 300] range, regardless of which host runs AddInfrastructure.
services.AddOptions<DatabaseSettings>()
.Bind(configuration.GetSection("Database"))
.ValidateDataAnnotations()
.ValidateOnStart();
services.AddDbContext<TaskdeckDbContext>(options =>
options.UseSqlite(connectionString, sqliteOptions =>
{
// Apply command timeout from configuration (default: 30s).
// This applies to all EF Core commands including Database.Migrate().
sqliteOptions.CommandTimeout(databaseSettings.CommandTimeoutSeconds);
}));
services.AddScoped<IBoardRepository, BoardRepository>();
services.AddScoped<IColumnRepository, ColumnRepository>();
services.AddScoped<ICardRepository, CardRepository>();
services.AddScoped<ICardCommentRepository, CardCommentRepository>();
services.AddScoped<ILabelRepository, LabelRepository>();
services.AddScoped<IUserRepository, UserRepository>();
services.AddScoped<IBoardAccessRepository, BoardAccessRepository>();
services.AddScoped<IAuditLogRepository, AuditLogRepository>();
services.AddScoped<ILlmQueueRepository, LlmQueueRepository>();
services.AddScoped<IAutomationProposalRepository, AutomationProposalRepository>();
services.AddScoped<IArchiveItemRepository, ArchiveItemRepository>();
services.AddScoped<IChatSessionRepository, ChatSessionRepository>();
services.AddScoped<IChatMessageRepository, ChatMessageRepository>();
services.AddScoped<ICommandRunRepository, CommandRunRepository>();
services.AddScoped<INotificationRepository, NotificationRepository>();
services.AddScoped<INotificationPreferenceRepository, NotificationPreferenceRepository>();
services.AddScoped<IUserPreferenceRepository, UserPreferenceRepository>();
services.AddScoped<IOutboundWebhookSubscriptionRepository, OutboundWebhookSubscriptionRepository>();
services.AddScoped<IOutboundWebhookDeliveryRepository, OutboundWebhookDeliveryRepository>();
services.AddScoped<ILlmUsageRecordRepository, LlmUsageRecordRepository>();
services.AddScoped<IAgentProfileRepository, AgentProfileRepository>();
services.AddScoped<IAgentRunRepository, AgentRunRepository>();
services.AddScoped<IKnowledgeDocumentRepository, KnowledgeDocumentRepository>();
services.AddScoped<IKnowledgeChunkRepository, KnowledgeChunkRepository>();
services.AddScoped<IExternalLoginRepository, ExternalLoginRepository>();
services.AddScoped<IOAuthAuthCodeRepository, OAuthAuthCodeRepository>();
services.AddScoped<IApiKeyRepository, ApiKeyRepository>();
services.AddScoped<IMfaCredentialRepository, MfaCredentialRepository>();
services.AddScoped<IIntegrationConnectorRepository, IntegrationConnectorRepository>();
services.AddScoped<IConnectorEventRepository, ConnectorEventRepository>();
services.AddScoped<IConnectorCredentialRepository, ConnectorCredentialRepository>();
services.AddScoped<IProposalOutcomeRepository, ProposalOutcomeRepository>();
services.AddScoped<Taskdeck.Infrastructure.Services.KnowledgeFtsSearchService>();
services.AddScoped<IFtsKnowledgeSearchService>(sp =>
sp.GetRequiredService<Taskdeck.Infrastructure.Services.KnowledgeFtsSearchService>());
services.AddScoped<IProposalRevisionRepository, ProposalRevisionRepository>();
services.AddScoped<IDailySnapshotRepository, DailySnapshotRepository>();
services.AddScoped<ITomorrowNoteRepository, TomorrowNoteRepository>();
// Vector index is local; hash-based in-memory embeddings are development/test
// oriented and stay disabled unless explicitly opted in.
var enableInMemoryEmbeddings = configuration.GetValue<bool>("Knowledge:EnableInMemoryEmbeddings");
services.AddSingleton<IVectorIndex, Taskdeck.Infrastructure.Services.InMemoryVectorIndex>();
if (enableInMemoryEmbeddings)
{
services.AddSingleton<IEmbeddingGenerator, Taskdeck.Infrastructure.Services.InMemoryEmbeddingGenerator>();
}
else
{
services.AddSingleton<IEmbeddingGenerator, Taskdeck.Infrastructure.Services.DisabledEmbeddingGenerator>();
}
services.AddScoped<IEmbeddingBackfillService, Taskdeck.Infrastructure.Services.EmbeddingBackfillService>();
services.AddScoped<Taskdeck.Infrastructure.Services.FallbackSemanticSearchService>();
services.AddScoped<ISemanticSearchService>(sp =>
sp.GetRequiredService<Taskdeck.Infrastructure.Services.FallbackSemanticSearchService>());
services.AddScoped<IKnowledgeSearchService>(sp =>
sp.GetRequiredService<Taskdeck.Infrastructure.Services.FallbackSemanticSearchService>());
// Provenance services
services.AddSingleton<IFuzzyTextMatcher, Taskdeck.Application.Services.FuzzyTextMatcher>();
services.AddSingleton<IDeterministicPreExtractor, Taskdeck.Infrastructure.Services.DeterministicPreExtractor>();
// Credential encryption — requires a configured AES-256 key.
// Fail-fast: the service refuses to start without a valid encryption key.
var credentialEncryptionKey = configuration["Connectors:EncryptionKey"];
if (string.IsNullOrWhiteSpace(credentialEncryptionKey))
{
throw new InvalidOperationException(
"Connectors:EncryptionKey is not configured. " +
"Set a base64-encoded 256-bit key via configuration or the " +
"TASKDECK_CONNECTORS__ENCRYPTIONKEY environment variable. " +
"Generate one with: openssl rand -base64 32");
}
services.AddSingleton<ICredentialEncryptionService>(
new AesCredentialEncryptionService(credentialEncryptionKey));
// Connector provider framework (concrete providers registered in Infrastructure).
// Providers and registry are scoped to align with HttpClient lifetime from
// AddHttpClient (which registers a transient typed client). Singleton registration
// would capture a transient HttpClient, causing socket exhaustion.
services.AddHttpClient<GitHubConnectorProvider>(client =>
{
client.DefaultRequestHeaders.Add("User-Agent", "Taskdeck-Connector/1.0");
client.Timeout = TimeSpan.FromSeconds(10);
});
services.AddScoped<IConnectorProvider>(sp =>
sp.GetRequiredService<GitHubConnectorProvider>());
services.AddScoped<IConnectorProviderRegistry>(sp =>
new ConnectorProviderRegistry(sp.GetServices<IConnectorProvider>()));
services.AddScoped<IUnitOfWork, UnitOfWork>();
// Cache service registration
services.AddCacheService(configuration);
return services;
}
private static void AddCacheService(this IServiceCollection services, IConfiguration configuration)
{
var cacheSettings = configuration.GetSection("Cache").Get<CacheSettings>() ?? new CacheSettings();
switch (cacheSettings.Provider.ToLowerInvariant())
{
case "redis":
if (string.IsNullOrWhiteSpace(cacheSettings.RedisConnectionString))
{
// Fallback to in-memory if Redis is configured but no connection string
services.AddSingleton<ICacheService>(sp =>
new InMemoryCacheService(
sp.GetRequiredService<ILogger<InMemoryCacheService>>(),
cacheSettings.KeyPrefix));
}
else
{
services.AddSingleton<ICacheService>(sp =>
new RedisCacheService(
cacheSettings.RedisConnectionString,
sp.GetRequiredService<ILogger<RedisCacheService>>(),
cacheSettings.KeyPrefix));
}
break;
case "none":
services.AddSingleton<ICacheService>(NoOpCacheService.Instance);
break;
case "inmemory":
services.AddSingleton<ICacheService>(sp =>
new InMemoryCacheService(
sp.GetRequiredService<ILogger<InMemoryCacheService>>(),
cacheSettings.KeyPrefix));
break;
default:
// Log a warning so operators notice configuration typos (e.g., "Rediss" or "inmem")
// instead of silently falling back to InMemory.
services.AddSingleton<ICacheService>(sp =>
{
var logger = sp.GetRequiredService<ILogger<InMemoryCacheService>>();
logger.LogWarning(
"Unknown cache provider '{Provider}', falling back to InMemory. Valid values: Redis, InMemory, None",
cacheSettings.Provider);
return new InMemoryCacheService(logger, cacheSettings.KeyPrefix);
});
break;
}
services.AddSingleton(cacheSettings);
}
}