-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
261 lines (218 loc) · 10.6 KB
/
Program.cs
File metadata and controls
261 lines (218 loc) · 10.6 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Connector.Authentication;
using InterviewSchedulingBot.Services;
using InterviewSchedulingBot.Interfaces;
using InterviewSchedulingBot.Interfaces.Integration;
using InterviewSchedulingBot.Interfaces.Business;
using InterviewSchedulingBot.Services.Integration;
using InterviewSchedulingBot.Services.Business;
using Microsoft.OpenApi.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
// Clean Architecture imports
using MediatR;
using InterviewBot.Domain.Interfaces;
using InterviewBot.Persistence;
using InterviewBot.Persistence.Repositories;
using InterviewBot.Infrastructure.Calendar;
using InterviewBot.Infrastructure.Scheduling;
using InterviewBot.Infrastructure.Caching;
using InterviewBot.Infrastructure.Telemetry;
using InterviewBot.Bot.State;
using InterviewBot.Bot;
using InterviewBot.Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers().AddNewtonsoftJson();
// Add Memory Cache for caching
builder.Services.AddMemoryCache();
// === CLEAN ARCHITECTURE SETUP ===
// Add MediatR for CQRS
builder.Services.AddMediatR(cfg => {
cfg.RegisterServicesFromAssembly(typeof(Program).Assembly);
});
// Add Entity Framework with SQLite
builder.Services.AddDbContext<InterviewBotDbContext>(options =>
{
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection")
?? "Data Source=InterviewBot.db";
options.UseSqlite(connectionString);
});
// Register Unit of Work and Repositories
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
builder.Services.AddScoped<IInterviewRepository, InterviewRepository>();
builder.Services.AddScoped<IParticipantRepository, ParticipantRepository>();
builder.Services.AddScoped<IAvailabilityRepository, AvailabilityRepository>();
// Register Domain Services with caching
builder.Services.AddScoped<ICalendarService, GraphCalendarService>();
builder.Services.AddScoped<AvailabilityService>(); // Base service
builder.Services.AddScoped<IAvailabilityService>(provider =>
{
var baseService = provider.GetRequiredService<AvailabilityService>();
var cache = provider.GetRequiredService<IMemoryCache>();
var logger = provider.GetRequiredService<ILogger<CachedAvailabilityService>>();
return new CachedAvailabilityService(baseService, cache, logger);
});
builder.Services.AddScoped<ISchedulingService, SchedulingService>();
builder.Services.AddScoped<ITelemetryService, TelemetryService>();
// Register Infrastructure Services
builder.Services.AddScoped<IGraphClientFactory, GraphClientFactory>();
builder.Services.AddScoped<OptimalSlotFinder>();
// Register Natural Language Processing Services
builder.Services.AddHttpClient<InterviewSchedulingBot.Services.Integration.IOpenWebUIClient, InterviewSchedulingBot.Services.Integration.OpenWebUIClient>();
builder.Services.AddHttpClient();
builder.Services.AddSingleton<InterviewSchedulingBot.Services.Integration.ICleanOpenWebUIClient>(sp =>
{
var config = sp.GetRequiredService<IConfiguration>();
var logger = sp.GetRequiredService<ILogger<InterviewSchedulingBot.Services.Integration.CleanOpenWebUIClient>>();
var factory = sp.GetRequiredService<IHttpClientFactory>();
var client = factory.CreateClient("openwebui");
return new InterviewSchedulingBot.Services.Integration.CleanOpenWebUIClient(client, config, logger);
});
builder.Services.AddScoped<InterviewSchedulingBot.Services.Business.SlotQueryParser>();
builder.Services.AddScoped<InterviewSchedulingBot.Services.Business.ConversationalResponseGenerator>();
builder.Services.AddScoped<InterviewSchedulingBot.Services.Business.ISlotRecommendationService, InterviewSchedulingBot.Services.Business.SlotRecommendationService>();
builder.Services.AddScoped<InterviewSchedulingBot.Services.Business.IResponseFormatter, InterviewSchedulingBot.Services.Business.ResponseFormatter>();
builder.Services.AddScoped<InterviewSchedulingBot.Services.Business.IAIResponseService, InterviewSchedulingBot.Services.Business.AIResponseService>();
// Register Conversation Store
builder.Services.AddSingleton<InterviewSchedulingBot.Interfaces.IConversationStore, InterviewSchedulingBot.Services.InMemoryConversationStore>();
// Register ConversationStateManager
builder.Services.AddSingleton<InterviewSchedulingBot.Services.ConversationStateManager>();
// Register Bot State Accessors
builder.Services.AddSingleton<BotStateAccessors>();
// Register Clean Services
builder.Services.AddHttpClient<InterviewSchedulingBot.Services.Integration.ISimpleOpenWebUIParameterExtractor, InterviewSchedulingBot.Services.Integration.SimpleOpenWebUIParameterExtractor>();
builder.Services.AddScoped<InterviewSchedulingBot.Services.Business.ICleanMockDataGenerator, InterviewSchedulingBot.Services.Business.CleanMockDataGenerator>();
// Register new enhanced slot services
builder.Services.AddSingleton<InterviewBot.Services.SlotRecommendationService>();
builder.Services.AddSingleton<InterviewBot.Services.SlotResponseFormatter>();
// Register new enhanced AI-powered scheduling service
builder.Services.AddScoped<InterviewSchedulingBot.Services.Business.IInterviewSchedulingService, InterviewSchedulingBot.Services.Business.InterviewSchedulingService>();
// === EXISTING SERVICES ===
// Add services to the container.
builder.Services.AddControllers().AddNewtonsoftJson();
// Add Swagger services
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Interview Scheduling Bot API",
Version = "v1",
Description = "RESTful API for interview scheduling operations with clear separation between business and integration layers",
Contact = new OpenApiContact
{
Name = "Interview Scheduling Bot",
Email = "support@interviewbot.com"
}
});
// Include XML comments for better documentation
var xmlFile = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
if (File.Exists(xmlPath))
{
c.IncludeXmlComments(xmlPath);
}
});
// Create the Bot Framework Authentication to be used with the Bot Adapter.
builder.Services.AddSingleton<BotFrameworkAuthentication, ConfigurationBotFrameworkAuthentication>();
// Create the Bot Adapter with error handling enabled.
builder.Services.AddSingleton<IBotFrameworkHttpAdapter, InterviewSchedulingBot.AdapterWithErrorHandler>();
// Create the storage we'll be using for user and conversation state.
builder.Services.AddSingleton<IStorage, MemoryStorage>();
// Create the user state (used for dialog state).
builder.Services.AddSingleton<UserState>();
// Create the conversation state (used for dialog state).
builder.Services.AddSingleton<ConversationState>();
// Register authentication service
builder.Services.AddSingleton<IAuthenticationService, AuthenticationService>();
// === INTEGRATION LAYER SERVICES ===
// Register Teams integration service (includes calendar access through Teams API)
// Use mock service if configured for testing, otherwise use real service
var useMockTeamsService = builder.Configuration.GetValue<bool>("TeamsIntegration:UseMockService", false);
if (useMockTeamsService)
{
builder.Services.AddSingleton<ITeamsIntegrationService, InterviewSchedulingBot.Services.Mock.MockTeamsIntegrationService>();
Console.WriteLine("✓ Using MockTeamsIntegrationService for testing (no Teams deployment required)");
}
else
{
builder.Services.AddSingleton<ITeamsIntegrationService, TeamsIntegrationService>();
Console.WriteLine("✓ Using TeamsIntegrationService (requires Teams deployment)");
}
// === BUSINESS LAYER SERVICES ===
// Register pure business logic service
builder.Services.AddSingleton<ISchedulingBusinessService, SchedulingBusinessService>();
// Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
// Use the enhanced bot with Clean Architecture integration
builder.Services.AddTransient<IBot, InterviewSchedulingBotEnhanced>();
var app = builder.Build();
// === CONFIGURATION VERIFICATION ===
// Verify and log API configuration
var configuration = app.Services.GetRequiredService<IConfiguration>();
var logger = app.Services.GetRequiredService<ILogger<Program>>();
var apiKey = configuration["OpenWebUI:ApiKey"];
var useMockData = configuration.GetValue<bool>("OpenWebUI:UseMockData", false);
if (string.IsNullOrEmpty(apiKey))
{
logger.LogWarning("OpenWebUI API key not found - using mock data for responses");
}
else if (useMockData)
{
logger.LogWarning("OpenWebUI mock data enabled by configuration - API calls will be simulated");
}
else
{
logger.LogInformation("OpenWebUI configured with API key - will make real API calls");
}
// === DATABASE INITIALIZATION ===
// Ensure database is created and apply migrations
using (var scope = app.Services.CreateScope())
{
try
{
var dbContext = scope.ServiceProvider.GetRequiredService<InterviewBotDbContext>();
dbContext.Database.EnsureCreated();
app.Logger.LogInformation("✓ Database initialized successfully");
}
catch (Exception ex)
{
app.Logger.LogError(ex, "❌ Failed to initialize database");
}
}
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
else
{
// Enable Swagger in development
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Interview Scheduling Bot API v1");
c.RoutePrefix = "swagger"; // Make Swagger available at /swagger
c.DocumentTitle = "Interview Scheduling Bot API Documentation";
});
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllers();
// Set chat interface as default route instead of old dashboard
app.MapGet("/", context => {
context.Response.Redirect("/api/chat");
return Task.CompletedTask;
});
// Log architectural information
var architectureLogger = app.Services.GetRequiredService<ILogger<Program>>();
architectureLogger.LogInformation("🏗️ Interview Scheduling Bot - Layered Architecture");
architectureLogger.LogInformation("📋 Integration Layer: Teams, Calendar, External AI services");
architectureLogger.LogInformation("💼 Business Layer: Pure scheduling logic and business rules");
architectureLogger.LogInformation("🌐 API Layer: RESTful endpoints with Swagger documentation");
architectureLogger.LogInformation("📚 Swagger Documentation available at: /swagger");
app.Run();