-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
222 lines (186 loc) · 8.54 KB
/
Program.cs
File metadata and controls
222 lines (186 loc) · 8.54 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
using AnswerCode.Models;
using AnswerCode.Services;
using AnswerCode.Services.Analysis;
using AnswerCode.Services.Lsp;
using AnswerCode.Services.Providers;
using AnswerCode.Services.Tools;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.Google;
using Serilog;
var builder = WebApplication.CreateBuilder(args);
// Configure Serilog
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).WriteTo
.Console().WriteTo
.File($"logs/log-{DateTime.Now:yyyy-MM-dd_HHmmss}.txt")
.CreateLogger();
builder.Host.UseSerilog();
// Load appsettings.Local.json for local overrides (gitignored - copy from appsettings.Example.json)
builder.Configuration.AddJsonFile("appsettings.Local.json", optional: true);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddMemoryCache();
// Configure LLM options from appsettings
builder.Services.Configure<LLMSettings>(builder.Configuration.GetSection(LLMSettings.SectionName));
// Register LLM provider creators (OCP-compliant: add new creators to support new providers)
builder.Services.AddSingleton<ILLMProviderCreator, AzureOpenAIProviderCreator>();
builder.Services.AddSingleton<ILLMProviderCreator, OpenAIProviderCreator>(); // fallback for all OpenAI-compatible
// Register LLM Service Factory (Singleton - creates providers once)
builder.Services.AddSingleton<ILLMServiceFactory, LLMServiceFactory>();
// Register LLM Service (uses Factory to get providers)
builder.Services.AddSingleton<ILLMService, LLMService>();
// Register Code Explorer Service
builder.Services.AddScoped<ICodeExplorerService, CodeExplorerService>();
// Register analysis services for symbol-aware tools
builder.Services.AddSingleton<IWorkspaceFileService, WorkspaceFileService>();
builder.Services.AddSingleton<ICSharpCompilationService, CSharpCompilationService>();
// LSP-enhanced language analysis (Decorator pattern: LSP → fallback to regex)
builder.Services.Configure<LspSettings>(builder.Configuration.GetSection(LspSettings.SectionName));
builder.Services.AddSingleton<LanguageHeuristicService>();
builder.Services.AddSingleton<ILspServerManager, LspServerManager>();
builder.Services.AddSingleton<ILanguageHeuristicService>(sp =>
new LspLanguageAnalysisService(
sp.GetRequiredService<LanguageHeuristicService>(),
sp.GetRequiredService<ILspServerManager>(),
sp.GetRequiredService<IWorkspaceFileService>(),
sp.GetRequiredService<ILogger<LspLanguageAnalysisService>>()));
builder.Services.AddSingleton<ISymbolAnalysisService, SymbolAnalysisService>();
builder.Services.AddSingleton<IReferenceAnalysisService, ReferenceAnalysisService>();
builder.Services.AddSingleton<ITestDiscoveryService, TestDiscoveryService>();
// Register repo map service
builder.Services.AddSingleton<IRepoMapService, RepoMapService>();
// Register call graph service
builder.Services.AddSingleton<ICallGraphService, CallGraphService>();
// Register config lookup service
builder.Services.AddSingleton<IConfigLookupService, ConfigLookupService>();
// Register tools via DI (add new tools here)
builder.Services.AddSingleton<ITool, GrepTool>();
builder.Services.AddSingleton<ITool, ReadFileTool>();
builder.Services.AddSingleton<ITool, ReadSymbolTool>();
builder.Services.AddSingleton<ITool, ListDirectoryTool>();
builder.Services.AddSingleton<ITool, GlobTool>();
builder.Services.AddSingleton<ITool, FileOutlineTool>();
builder.Services.AddSingleton<ITool, FindDefinitionTool>();
builder.Services.AddSingleton<ITool, FindReferencesTool>();
builder.Services.AddSingleton<ITool, FindTestsTool>();
builder.Services.AddSingleton<ITool, RelatedFilesTool>();
builder.Services.AddSingleton<ITool, RepoMapTool>();
builder.Services.AddSingleton<ITool, CallGraphTool>();
builder.Services.AddSingleton<ITool, WebSearchTool>();
builder.Services.AddSingleton<ITool, ConfigLookupTool>();
builder.Services.AddSingleton<ToolRegistry>();
// Register Agent Service (agentic tool-calling loop)
builder.Services.AddScoped<IAgentService, AgentService>();
// Register Conversation History Service (in-memory chat history per session)
builder.Services.AddSingleton<IConversationHistoryService, ConversationHistoryService>();
// Register upload cleanup background service (auto-delete expired uploads)
builder.Services.AddHostedService<UploadCleanupService>();
// Register user storage service
builder.Services.AddSingleton<IUserStorageService, UserStorageService>();
// Configure Authentication (Google OAuth + Cookie)
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.Cookie.Name = "AnswerCode.Auth";
options.Cookie.HttpOnly = true;
options.Cookie.SameSite = SameSiteMode.Lax;
options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
options.ExpireTimeSpan = TimeSpan.FromDays(7);
options.SlidingExpiration = true;
options.Events.OnRedirectToLogin = context =>
{
if (context.Request.Path.StartsWithSegments("/api"))
{
context.Response.StatusCode = 401;
return Task.CompletedTask;
}
context.Response.Redirect(context.RedirectUri);
return Task.CompletedTask;
};
})
.AddGoogle(options =>
{
options.ClientId = builder.Configuration["Authentication:Google:ClientId"] ?? "";
options.ClientSecret = builder.Configuration["Authentication:Google:ClientSecret"] ?? "";
options.CallbackPath = "/signin-google";
options.Scope.Add("profile");
options.Scope.Add("email");
options.SaveTokens = false;
options.ClaimActions.MapJsonKey("picture", "picture");
});
builder.Services.AddAuthorization();
// Add CORS — restrict to configured origins (defaults to same-origin only)
var allowedOrigins = builder.Configuration.GetSection("AllowedOrigins").Get<string[]>() ?? [];
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policy =>
{
if (allowedOrigins.Length > 0)
{
policy.WithOrigins(allowedOrigins)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
}
else
{
// No origins configured — allow same-origin only (no CORS headers emitted)
policy.AllowAnyMethod()
.AllowAnyHeader();
}
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseHttpsRedirection();
}
// Serve static files (for the frontend)
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
// Serve dashboard.html for the /dashboard route
app.MapGet("/dashboard", async context =>
{
context.Response.ContentType = "text/html";
var filePath = Path.Combine(app.Environment.WebRootPath, "dashboard.html");
if (File.Exists(filePath))
{
await context.Response.SendFileAsync(filePath);
}
else
{
context.Response.StatusCode = 404;
}
});
// Fallback to index.html for SPA-like behavior
app.MapFallbackToFile("index.html");
// Log available providers on startup
var factory = app.Services.GetRequiredService<ILLMServiceFactory>();
var providers = factory.GetAvailableProviders().ToList();
Console.WriteLine(@"
╔═══════════════════════════════════════════════════════════════╗
║ ║
║ AnswerCode - Source Code Q&A System ║
║ ║
║ Web UI: http://localhost:5000 ║
║ ║
║ Available LLM Providers: ║");
foreach (var provider in providers)
{
Console.WriteLine(
$"║ - {provider} ║");
}
Console.WriteLine(
@"║ ║
╚═══════════════════════════════════════════════════════════════╝
");
app.Run();