|
1 | 1 | @page "/" |
2 | 2 | @rendermode @(new InteractiveServerRenderMode(prerender: true)) |
3 | 3 | @inject IJSRuntime JS |
| 4 | +@inject SettingsService SettingsStorage |
| 5 | +@inject SettingsStateService SettingsState |
| 6 | +@inject MaIN.Domain.Configuration.MaINSettings MaINSettings |
4 | 7 | @implements IDisposable |
5 | 8 | @using MaIN.Core.Hub |
6 | 9 | @using MaIN.Core.Hub.Contexts.Interfaces.ChatContext |
|
18 | 21 |
|
19 | 22 | <ErrorNotification @bind-ErrorMessage="_errorMessage" /> |
20 | 23 |
|
| 24 | +@if (_showSettings) |
| 25 | +{ |
| 26 | + <Settings ShowCloseButton="@_hasExistingConfig" |
| 27 | + OnSettingsApplied="@HandleSettingsApplied" |
| 28 | + OnClose="@HideSettings" /> |
| 29 | +} |
| 30 | + |
21 | 31 | <div class="chat-container" id="chat-container"> |
22 | 32 | @if (_isDragging) |
23 | 33 | { |
|
204 | 214 | </div> |
205 | 215 |
|
206 | 216 | @code { |
| 217 | + private bool _showSettings; |
| 218 | + private bool _hasExistingConfig; |
207 | 219 | private bool _isLoading; |
208 | 220 | private bool _isThinking; |
209 | 221 | private bool _isDragging; |
|
214 | 226 | private string? _incomingReasoning; |
215 | 227 | private IChatMessageBuilder? ctx; |
216 | 228 | private CancellationTokenSource? _cancellationTokenSource; |
217 | | - private Chat Chat { get; } = new() { Name = "MaIN Infer", ModelId = Utils.Model! }; |
| 229 | + private Chat Chat { get; set; } = new() { Name = "MaIN Infer", ModelId = Utils.Model ?? "unknown" }; |
218 | 230 | private List<MessageExt> Messages { get; set; } = new(); |
219 | 231 | private ElementReference? _bottomElement; |
220 | 232 | private ElementReference _editorRef; |
|
243 | 255 | await JS.InvokeVoidAsync("editorManager.attachPasteHandler", _editorRef, _dotNetRef); |
244 | 256 | await JS.InvokeVoidAsync("editorManager.attachDropZone", "chat-container", _dotNetRef); |
245 | 257 |
|
| 258 | + // Settings initialization (requires JS interop, so must be in OnAfterRenderAsync) |
| 259 | + SettingsState.OnSettingsRequested += ShowSettingsFromGear; |
| 260 | + |
| 261 | + if (Utils.NeedsConfiguration) |
| 262 | + { |
| 263 | + var hasBrowserSettings = await SettingsStorage.HasSettingsAsync(); |
| 264 | + if (hasBrowserSettings) |
| 265 | + { |
| 266 | + await LoadAndApplyBrowserSettings(); |
| 267 | + _hasExistingConfig = true; |
| 268 | + InitializeChatContext(); |
| 269 | + SettingsState.NotifySettingsApplied(); |
| 270 | + } |
| 271 | + else |
| 272 | + { |
| 273 | + _showSettings = true; |
| 274 | + _hasExistingConfig = false; |
| 275 | + } |
| 276 | + } |
| 277 | + else |
| 278 | + { |
| 279 | + _hasExistingConfig = true; |
| 280 | + } |
| 281 | + |
246 | 282 | StateHasChanged(); |
247 | 283 | } |
248 | 284 | else if (_preserveScroll) |
|
254 | 290 |
|
255 | 291 | protected override Task OnInitializedAsync() |
256 | 292 | { |
257 | | - AIModel? model = null; |
| 293 | + // Only init chat if CLI args provided (no browser settings needed) |
| 294 | + if (!Utils.NeedsConfiguration && !string.IsNullOrEmpty(Utils.Model)) |
| 295 | + { |
| 296 | + InitializeChatContext(); |
| 297 | + } |
258 | 298 |
|
| 299 | + return base.OnInitializedAsync(); |
| 300 | + } |
| 301 | + |
| 302 | + private void InitializeChatContext() |
| 303 | + { |
259 | 304 | try |
260 | 305 | { |
261 | | - if (Utils.BackendType == BackendType.Self && Utils.Path != null) |
| 306 | + var model = ResolveModel(); |
| 307 | + if (model != null) |
262 | 308 | { |
263 | | - model = ModelRegistry.TryGetById(Utils.Model!, out var foundModel1) |
264 | | - ? foundModel1! |
265 | | - : new GenericLocalModel($"{Utils.Model}.gguf"); |
| 309 | + var newCtx = AIHub.Chat().WithModel(model, imageGen: Utils.ImageGen); |
| 310 | + // Preserve history on model switch; cast is safe — ChatContext implements both interfaces. |
| 311 | + ctx = Chat.Messages.Count > 0 |
| 312 | + ? (IChatMessageBuilder)newCtx.WithMessages(Chat.Messages) |
| 313 | + : newCtx; |
266 | 314 | } |
267 | | - else |
268 | | - { |
269 | | - model = ModelRegistry.TryGetById(Utils.Model!, out var foundModel1) |
270 | | - ? foundModel1! |
271 | | - : new GenericCloudModel(Id: Utils.Model!, Backend: Utils.BackendType); |
272 | | - } |
273 | | - |
274 | | - ctx = AIHub.Chat().WithModel(model, imageGen: Utils.ImageGen); |
275 | 315 | } |
276 | 316 | catch (MaINCustomException ex) |
277 | 317 | { |
|
281 | 321 | { |
282 | 322 | _errorMessage = ex.Message; |
283 | 323 | } |
| 324 | + } |
284 | 325 |
|
285 | | - return base.OnInitializedAsync(); |
| 326 | + private AIModel? ResolveModel() |
| 327 | + { |
| 328 | + if (string.IsNullOrEmpty(Utils.Model)) return null; |
| 329 | + |
| 330 | + if (ModelRegistry.TryGetById(Utils.Model, out var registeredModel)) |
| 331 | + return registeredModel; |
| 332 | + |
| 333 | + // Self = local GGUF file; Ollama Local is an HTTP server, not a file path |
| 334 | + if (Utils.BackendType == BackendType.Self) |
| 335 | + { |
| 336 | + string modelFileName; |
| 337 | + if (!string.IsNullOrEmpty(Utils.Path)) |
| 338 | + { |
| 339 | + modelFileName = Utils.Path.EndsWith(".gguf", StringComparison.OrdinalIgnoreCase) |
| 340 | + ? Utils.Path |
| 341 | + : System.IO.Path.Combine(Utils.Path, $"{Utils.Model.Replace(':', '-')}.gguf"); |
| 342 | + } |
| 343 | + else |
| 344 | + { |
| 345 | + modelFileName = $"{Utils.Model.Replace(':', '-')}.gguf"; |
| 346 | + } |
| 347 | + return new GenericLocalModel(FileName: modelFileName); |
| 348 | + } |
| 349 | + |
| 350 | + // Cloud model — pick the right generic type based on capabilities |
| 351 | + bool vision = Utils.Vision; |
| 352 | + bool reasoning = Utils.Reason; |
| 353 | + |
| 354 | + if (vision && reasoning) |
| 355 | + return new GenericCloudVisionReasoningModel(Id: Utils.Model, Backend: Utils.BackendType); |
| 356 | + if (vision) |
| 357 | + return new GenericCloudVisionModel(Id: Utils.Model, Backend: Utils.BackendType); |
| 358 | + if (reasoning) |
| 359 | + return new GenericCloudReasoningModel(Id: Utils.Model, Backend: Utils.BackendType); |
| 360 | + |
| 361 | + return new GenericCloudModel(Id: Utils.Model, Backend: Utils.BackendType); |
| 362 | + } |
| 363 | + |
| 364 | + private void ReinitializeChat() |
| 365 | + { |
| 366 | + if (string.IsNullOrEmpty(Utils.Model)) return; |
| 367 | + |
| 368 | + _errorMessage = null; |
| 369 | + Chat.ModelId = Utils.Model; |
| 370 | + Chat.ImageGen = Utils.ImageGen; |
| 371 | + InitializeChatContext(); |
| 372 | + StateHasChanged(); |
| 373 | + } |
| 374 | + |
| 375 | + private async Task LoadAndApplyBrowserSettings() |
| 376 | + { |
| 377 | + var settings = await SettingsStorage.LoadSettingsAsync(); |
| 378 | + if (settings == null) return; |
| 379 | + |
| 380 | + var backendType = (BackendType)settings.BackendType; |
| 381 | + |
| 382 | + string? apiKey = null; |
| 383 | + if (backendType != BackendType.Self) |
| 384 | + { |
| 385 | + var backendKey = backendType == BackendType.Ollama |
| 386 | + ? (settings.IsOllamaCloud ? "OllamaCloud" : "OllamaLocal") |
| 387 | + : backendType.ToString(); |
| 388 | + |
| 389 | + apiKey = await SettingsStorage.GetApiKeyForBackendAsync(backendKey); |
| 390 | + } |
| 391 | + |
| 392 | + Utils.ApplySettings( |
| 393 | + backendType, |
| 394 | + settings.Model!, |
| 395 | + settings.ModelPath, |
| 396 | + settings.HasVision, |
| 397 | + settings.HasReasoning, |
| 398 | + settings.HasImageGen, |
| 399 | + MaINSettings, |
| 400 | + apiKey); |
| 401 | + } |
| 402 | + |
| 403 | + private void ShowSettingsFromGear() |
| 404 | + { |
| 405 | + _showSettings = true; |
| 406 | + InvokeAsync(StateHasChanged); |
| 407 | + } |
| 408 | + |
| 409 | + private void HandleSettingsApplied() |
| 410 | + { |
| 411 | + _hasExistingConfig = true; |
| 412 | + _showSettings = false; |
| 413 | + ReinitializeChat(); |
| 414 | + SettingsState.NotifySettingsApplied(); |
| 415 | + } |
| 416 | + |
| 417 | + private void HideSettings() |
| 418 | + { |
| 419 | + _showSettings = false; |
| 420 | + StateHasChanged(); |
286 | 421 | } |
287 | 422 |
|
288 | 423 | private async Task HandleKeyDown(KeyboardEventArgs e) |
|
635 | 770 |
|
636 | 771 | public void Dispose() |
637 | 772 | { |
| 773 | + SettingsState.OnSettingsRequested -= ShowSettingsFromGear; |
638 | 774 | _cancellationTokenSource?.Dispose(); |
639 | 775 | _dotNetRef?.Dispose(); |
640 | 776 | } |
|
0 commit comments