-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
271 lines (220 loc) · 10.8 KB
/
Program.cs
File metadata and controls
271 lines (220 loc) · 10.8 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
262
263
264
265
266
267
268
269
270
271
using System.Text.Json.Serialization;
using Three2025.Components;
using FoundryRulesAndUnits.Units;
using Radzen;
using FoundryRulesAndUnits.Extensions;
using FoundryMentorModeler;
using FoundryMentorModeler.Model;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.AspNetCore.Components.Server.Circuits;
using Microsoft.Extensions.FileProviders;
using Three2025.Apprentice;
using Three2025.Services.Visualization;
using Three2025.Services.Logging;
using FoundryWorldsAndDrawings;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
// Configure Blazor Server circuit options to support multiple tabs
builder.Services.AddServerSideBlazor(options =>
{
// CRITICAL: Prevent hub from timing out inactive tabs
options.DetailedErrors = true;
//options.MaximumReceiveMessageSize = 32 * 1024 * 1024; // 32MB for large geometry batches
})
.AddCircuitOptions(options =>
{
options.DetailedErrors = true; // Enable detailed error messages
options.DisconnectedCircuitMaxRetained = 100;
options.DisconnectedCircuitRetentionPeriod = TimeSpan.FromMinutes(3);
options.JSInteropDefaultCallTimeout = TimeSpan.FromMinutes(1);
options.MaxBufferedUnacknowledgedRenderBatches = 20;
// Note: MaximumReceiveMessageSize moved to HubOptions below
})
.AddHubOptions(options =>
{
// CRITICAL: Keep both tabs alive with generous timeouts
options.ClientTimeoutInterval = TimeSpan.FromMinutes(5); // How long server waits for client pings
options.HandshakeTimeout = TimeSpan.FromSeconds(30);
options.KeepAliveInterval = TimeSpan.FromSeconds(15); // Server pings client every 15s
options.MaximumReceiveMessageSize = 32 * 1024 * 1024; // 32MB
options.StreamBufferCapacity = 10;
});
builder.Services.AddRadzenComponents();
// Add services to the container.
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(builder =>
{
builder
// .WithOrigins(new[] { "http://localhost:8080", "http://localhost:8081" })
//.AllowCredentials()
.AllowAnyHeader()
.SetIsOriginAllowed(_ => true)
.AllowAnyOrigin()
.AllowAnyMethod();
});
});
builder.Services.AddCascadingAuthenticationState();
// Enable circuit monitoring (logs when tabs connect/disconnect)
builder.Services.AddScoped<CircuitHandler, CustomCircuitHandler>();
var provider = new FileExtensionContentTypeProvider();
builder.Services.Configure<StaticFileOptions>(options =>
{
// TODO: FileExtensionHelpers.MIMETypeData() no longer exists
// foreach (var item in FileExtensionHelpers.MIMETypeData())
// if ( !provider.Mappings.ContainsKey(item.Name))
// provider.Mappings.Add(item.Name,item.Value);
options.ContentTypeProvider = provider;
});
var envConfig = new EnvConfig("./.env");
builder.Services.AddFoundryWorldsAndDrawingsServices(envConfig);
builder.Services.AddFoundryMentorModelerServices();
// TODO: IRackTech/RackTech and ICageTech/CageTech no longer exist
// builder.Services.AddScoped<IRackTech, RackTech>();
// builder.Services.AddScoped<ICageTech, CageTech>();
builder.Services.AddScoped<IClockTech, ClockTech>();
builder.Services.AddScoped<ICuckooClockTech, CuckooClockTech>();
builder.Services.AddScoped<ITrisocTech, TrisocTech>();
builder.Services.AddScoped<IShape3DTech, Shape3DTech>();
builder.Services.AddScoped<IShape3DEditor, Shape3DEditor>();
builder.Services.AddScoped<IShape2DTech, Shape2DTech>();
builder.Services.AddScoped<IMentor2DTech, Mentor2DTech>();
// Model management - MentorServices holds CurrentModel/CurrentComponent state
builder.Services.AddScoped<IMentorServices, MentorServices>();
builder.Services.AddScoped<IModelTech, ModelTech>();
// Note: Editors (ModelEditor, Shape2DEditor, Shape3DEditor) are NOT in DI
// Each technician creates and manages its own editor instances dynamically
// Register tool provider for agent system (Phase 0) - MUST BE SCOPED to access scoped technicians
builder.Services.AddScoped<Three2025.Services.Agents.ITechnicianToolProvider, Three2025.Services.Agents.TechnicianToolProvider>();
// Register testing services for manual tool verification
builder.Services.AddScoped<Three2025.Services.Testing.ToolMetadataExtractor>();
builder.Services.AddScoped<Three2025.Services.Testing.TechnicianTestExecutor>();
builder.Services.AddScoped<Three2025.Services.Testing.ITestValueProvider, Three2025.Services.Testing.DefaultTestValueProvider>();
builder.Services.AddSingleton<Three2025.Services.Testing.TestResultsService>();
// Register multi-provider chat service
builder.Services.AddScoped<Three2025.Services.Chat.IMultiProviderChatService, Three2025.Services.Chat.MultiProviderChatService>();
// Register multi-agent orchestration services (Phase 2)
builder.Services.AddScoped<Three2025.Services.Chat.IChatOrchestrator, Three2025.Services.Chat.ChatOrchestrator>();
builder.Services.AddScoped<Three2025.Services.Chat.IAgentFactory, Three2025.Services.Chat.AgentFactory>();
// Register geometry visualization service
builder.Services.AddScoped<IGeometryVisualizationService, GeometryVisualizationService>();
builder.Services.AddControllers().AddJsonOptions(options =>
{
var ser = options.JsonSerializerOptions;
ser.IgnoreReadOnlyFields = true;
ser.IncludeFields = true;
ser.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
ser.WriteIndented = true;
});
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
builder.Services.AddDirectoryBrowser();
builder.Services.AddControllers().AddJsonOptions(options =>
{
var ser = options.JsonSerializerOptions;
ser.IgnoreReadOnlyFields = true;
ser.IncludeFields = true;
ser.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
ser.WriteIndented = true;
});
builder.Services.AddHttpClient();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Configure UniversalLogger for custom console output
builder.Logging.ClearProviders();
builder.Logging.AddProvider(new UniversalLoggerProvider());
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.UseStaticFiles();
app.UseAntiforgery();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
var serviceScope = ((IApplicationBuilder)app).ApplicationServices
.GetRequiredService<IServiceScopeFactory>()
.CreateScope();
var unitsystem = serviceScope.ServiceProvider.GetService<IUnitSystem>();
unitsystem?.Apply(UnitSystemType.MKS);
// ═══════════════════════════════════════════════════════════════
// AI PROVIDER HEALTH CHECK
// ═══════════════════════════════════════════════════════════════
var logger = app.Services.GetRequiredService<ILogger<Program>>();
var config = app.Services.GetRequiredService<IConfiguration>();
logger.LogInformation("═══════════════════════════════════════════════════════════════");
logger.LogInformation("🏥 Running AI Provider Health Check...");
// Check GitHub rate limits first
var githubToken = config["GitHubPatToken"] ??
Environment.GetEnvironmentVariable("GitHubPatToken", EnvironmentVariableTarget.User);
if (!string.IsNullOrEmpty(githubToken))
{
try
{
logger.LogInformation("🔍 Checking GitHub Models rate limits...");
var rateLimitChecker = new Three2025.Services.Agents.RateLimitChecker(githubToken);
await rateLimitChecker.TestSimpleCallAsync();
}
catch (Exception ex)
{
logger.LogWarning($"⚠️ GitHub rate limit check failed: {ex.Message}");
}
}
var (isHealthy, healthMessage, providerName) = await Three2025.Services.Chat.AIProviderHealthCheck.CheckHealthAsync(config, logger);
if (isHealthy)
{
logger.LogInformation($"{healthMessage} (Provider: {providerName})");
}
else
{
logger.LogWarning("═══════════════════════════════════════════════════════════════");
// Split multi-line messages for better formatting
var lines = healthMessage.Split('\n');
foreach (var line in lines)
{
if (!string.IsNullOrWhiteSpace(line))
{
logger.LogWarning($"⚠️ {line.TrimStart()}");
}
}
logger.LogWarning($"⚠️ Provider: {providerName}");
logger.LogWarning("⚠️ AI chat features may not work properly.");
logger.LogWarning("⚠️ Consider switching to a different provider or waiting.");
logger.LogWarning("═══════════════════════════════════════════════════════════════");
}
logger.LogInformation("═══════════════════════════════════════════════════════════════");
// ═══════════════════════════════════════════════════════════════
var storagePath = Path.Combine(Directory.GetCurrentDirectory(), "storage");
// Enable directory browsing for the storage folder
app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
FileProvider = new PhysicalFileProvider(storagePath),
RequestPath = "/storage"
});
//include the static files at wwwwroot
app.UseStaticFiles();
// Serve files from storage directory
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(storagePath),
RequestPath = "/storage",
ContentTypeProvider = provider,
OnPrepareResponse = ctx =>
{
var headers = ctx.Context.Response.Headers;
headers.Append("Access-Control-Allow-Origin", "*");
headers.Append("Cache-Control", $"public, max-age=3600");
}
});
// Run matrix rotation tests at startup
//Three2025.Matrix3RotationSimpleTest.Run();
//this pull the 3d model files and others to the storage folder
//envConfig.RefreshStaticFiles();
app.Run();