-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
225 lines (201 loc) · 9.24 KB
/
Copy pathProgram.cs
File metadata and controls
225 lines (201 loc) · 9.24 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
using System.Globalization;
using Microsoft.EntityFrameworkCore;
using MoneyMirror.Ai;
using MoneyMirror.Ai.Configuration;
using MoneyMirror.Components;
using MoneyMirror.Data;
using MoneyMirror.Features.Financial;
using MoneyMirror.HumanCapital;
using MoneyMirror.HumanCapital.Configuration;
using MoneyMirror.PhysicalAssets;
using MoneyMirror.PhysicalAssets.Configuration;
// Containers start with no LANG/LC_ALL, so .NET falls back to the invariant culture
// and renders currency as "¤" instead of "$". Pin the formatting culture so money
// looks the same in Docker as it does on a dev machine.
var appCulture = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentCulture = appCulture;
CultureInfo.DefaultThreadCurrentUICulture = appCulture;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
builder.Services.Configure<Microsoft.AspNetCore.SignalR.HubOptions>(options =>
{
options.MaximumReceiveMessageSize = 512 * 1024;
});
builder.Services.Configure<NemotronOptions>(
builder.Configuration.GetSection(NemotronOptions.SectionName)
);
builder.Services.Configure<ClaudeOptions>(
builder.Configuration.GetSection(ClaudeOptions.SectionName)
);
builder.Services.Configure<BlsOptions>(builder.Configuration.GetSection(BlsOptions.SectionName));
builder.Services.Configure<EbayOptions>(
builder.Configuration.GetSection(EbayOptions.SectionName)
);
builder.Services.Configure<VisionModelOptions>(
builder.Configuration.GetSection(VisionModelOptions.SectionName)
);
builder.Services.Configure<ResumeUploadOptions>(
builder.Configuration.GetSection(ResumeUploadOptions.SectionName)
);
builder.Services.Configure<ImageUploadOptions>(
builder.Configuration.GetSection(ImageUploadOptions.SectionName)
);
builder.Services.AddTransient<TransientFaultRetryHandler>();
builder.Services.AddMemoryCache();
builder.Services.AddSingleton<EbayTokenCache>();
builder.Services.AddSingleton(sp =>
{
var options = sp.GetRequiredService<Microsoft.Extensions.Options.IOptions<EbayOptions>>().Value;
var cacheDirectory = Path.IsPathRooted(options.CacheDirectory)
? options.CacheDirectory
: Path.Combine(builder.Environment.ContentRootPath, options.CacheDirectory);
return new MarketDataSearchCache(cacheDirectory);
});
// The vision client shares NVIDIA's endpoint with Nemotron, which sheds load with a
// 503 when its workers are saturated - see TransientFaultRetryHandler. HttpClient.Timeout
// wraps the whole SendAsync pipeline, including TransientFaultRetryHandler's
// retries, so this is a hard ceiling on total time spent per call, not just
// the first attempt. #261: previously unset (100s .NET default), so a stuck
// request had no clear bound and no chance to surface the app's own
// "could not be loaded" error UI. 90s for the LLM because Nemotron and Claude are
// both reasoning models that have been observed taking upwards of 60s for a
// legitimate (non-error) response; the vision model and the plain BLS REST
// API are comparatively fast, so they get tighter budgets.
//
// Registered as themselves, not as ILlmService, so FallbackLlmService can depend on
// both concretely and be the sole ILlmService registration - see its remarks for why
// Claude exists at all.
builder
.Services.AddHttpClient<NemotronLlmService>(client =>
client.Timeout = TimeSpan.FromSeconds(90)
)
.AddHttpMessageHandler<TransientFaultRetryHandler>();
builder
.Services.AddHttpClient<ClaudeLlmService>(client =>
client.Timeout = TimeSpan.FromSeconds(90)
)
.AddHttpMessageHandler<TransientFaultRetryHandler>();
builder.Services.AddScoped<ILlmService, FallbackLlmService>();
builder
.Services.AddHttpClient<IVisionService, NvidiaVisionService>(client =>
client.Timeout = TimeSpan.FromSeconds(60)
)
.AddHttpMessageHandler<TransientFaultRetryHandler>();
builder.Services.AddHttpClient<IBlsWageDataService, BlsWageDataService>(client =>
client.Timeout = TimeSpan.FromSeconds(15)
);
builder.Services.AddHttpClient<IMarketDataService, EbayMarketDataService>(client =>
client.Timeout = TimeSpan.FromSeconds(15)
);
// eBay's OAuth token is a Bearer header, not a query-string secret, but keep routine
// HTTP logs at Warning so a future log-level bump cannot echo it either.
builder.Logging.AddFilter("System.Net.Http.HttpClient.IMarketDataService", LogLevel.Warning);
builder.Services.AddScoped<
IMarketPotentialExplanationService,
NemotronMarketPotentialExplanationService
>();
builder.Services.AddScoped<IMarketPotentialPipeline, MarketPotentialPipeline>();
builder.Services.AddScoped<IResumeTextExtractionService, ResumeTextExtractionService>();
builder.Services.AddSingleton<IResumeUploadValidator, ResumeUploadValidator>();
builder.Services.AddScoped<
IProfessionalProfileExtractionService,
NemotronProfileExtractionService
>();
builder.Services.AddScoped<IPhysicalAssetDetectionService, NvidiaAssetDetectionService>();
builder.Services.AddScoped<ISamSegmentationEngine, BlazorSamSegmentationEngine>();
// Temporary in-memory store; #8 (FC1) swaps this for the EF Core/PostgreSQL implementation.
builder.Services.AddSingleton<IFinancialEntryStore, InMemoryFinancialEntryStore>();
builder.Services.AddSingleton<IImageUploadValidator, ImageUploadValidator>();
builder.Services.AddSingleton<IPossessionImageStorage, FilesystemPossessionImageStorage>();
// Registered concretely (not just via IAssetValuationService) so
// EvidenceBasedAssetValuationService can take it as its fallback without a
// circular resolution through the interface both of them implement.
builder.Services.AddScoped<AiEstimatedValuationService>();
builder.Services.AddScoped<IAssetValuationService, EvidenceBasedAssetValuationService>();
builder.Services.AddScoped<IOccupationMatchingService, AiSuggestedOccupationMatchingService>();
builder.Services.AddScoped<ICompensationEstimationService, AiEstimatedCompensationService>();
builder.Services.AddScoped<IMarketPotentialSummaryService, CachedMarketPotentialSummaryService>();
builder.Services.AddScoped<IProfessionalProfileRepository, EfProfessionalProfileRepository>();
builder.Services.AddScoped<IPhysicalAssetRepository, EfPhysicalAssetRepository>();
// setup connection to postgresql database
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
if (string.IsNullOrWhiteSpace(connectionString))
{
throw new InvalidOperationException("ConnectionStrings:DefaultConnection is not configured.");
}
else
{
Console.WriteLine("Default Connection Configured.");
}
builder.Services.AddDbContext<MoneyMirrorDbContext>(options => options.UseNpgsql(connectionString));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseAntiforgery();
app.MapStaticAssets();
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();
app.MapGet(
"/api/possession-images/{reference}",
async (string reference, IPossessionImageStorage storage) =>
{
var stream = await storage.OpenReadAsync(reference);
return stream is null
? Results.NotFound()
: Results.File(stream, PossessionImageContentType.FromFileName(reference));
}
);
var shouldApplyMigrations =
app.Environment.IsDevelopment()
|| builder.Configuration.GetValue<bool>("Database:ApplyMigrations");
if (shouldApplyMigrations)
{
// automatically apply migrations
await using var scope = app.Services.CreateAsyncScope();
var db = scope.ServiceProvider.GetRequiredService<MoneyMirrorDbContext>();
await db.Database.MigrateAsync();
if (app.Environment.IsDevelopment())
{
// Dev-only smoke test for the F3 AI seam (#44) - proves ILlmService and
// IVisionService round-trip against real providers. No feature logic.
app.MapGet(
"/dev/ai-smoke-test",
async (ILlmService llm, IVisionService vision) =>
{
var results = new Dictionary<string, string>();
try
{
results["llm"] = await llm.CompleteAsync("Reply with exactly the word: pong");
}
catch (LlmServiceException ex)
{
results["llmError"] = ex.Message;
}
try
{
// 1x1 white pixel PNG - just enough to prove the call round-trips.
var pixel = Convert.FromBase64String(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="
);
results["vision"] = await vision.DetectAsync(
pixel,
"image/png",
"Describe this image in one short sentence."
);
}
catch (VisionServiceException ex)
{
results["visionError"] = ex.Message;
}
return Results.Ok(results);
}
);
}
}
app.Run();