@page "/" @page "/menu/{topic?}" @inherits SharedDisplayLogic @using BLAIzor.Models @using BLAIzor.Services @using BLAIzor.Components.Partials @using Google.Cloud.Speech.V1 @using Microsoft.AspNetCore.Identity.UI.Services @using System.Text @using System.Net @using System.Text.Json @using Sidio.Sitemap.Blazor @* @inject AIService ChatGptService *@ @rendermode InteractiveServer @* @inject IJSRuntime jsRuntime; *@ @* @inject IConfiguration configuration *@ @inject ContentService _contentService @* @inject ContentEditorService _contentEditorService *@ @* @inject ScopedContentService _scopedContentService *@ @* @inject IEmailSender _emailService *@ @inject NavigationManager _navigationManager @inject IHttpContextAccessor HttpContextAccessor @inject DesignTemplateService DesignTemplateService @inject CssTemplateService CssTemplateService @inject CssInjectorService CssService @* @inject HttpClient Http *@ @attribute [Sitemap]
Home @* *@
@*
*@
@{ @if(VoiceEnabled) { if(STTEnabled) { } if(TTSEnabled) { if (!AiVoicePermitted) { } else { } } } }
@* Type anything *@ @*
*@

@{ if (!string.IsNullOrEmpty(HtmlContent.ToString())) { if (isEmailFormVisible) {
@((MarkupString)HtmlContent.ToString())
} else {
@((MarkupString)HtmlContent.ToString())
} } else {

@StatusContent

} }
@* *@

An error occurred: @ex.Message

Please try again later.

@* You can log the exception here if you want *@ @{ Console.WriteLine($"Error caught by ErrorBoundary: {ex.Message}"); }
@code { [Parameter] public string? topic { get; set; } // public static Index myHome; private string? Subdomain; private string ChatGptResponse = string.Empty; private bool isRecording = false; // private string FirstColumnClass = ""; // private bool isEmailFormVisible = false; // private ContactFormModel ContactFormModel = new(); // // private string? SuccessMessage; // // private string? ErrorMessage; // private string? DocumentEmailAddress = ""; private string dynamicallyLoadedCss = string.Empty; private string Menu; private void AllowAIVoice() { AiVoicePermitted = true; } private void MuteAI() { AiVoicePermitted = false; } protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { await jsRuntime.InvokeVoidAsync("setSessionId", SessionId); await jsRuntime.InvokeVoidAsync("initHints"); // sessionId = Guid.NewGuid().ToString(); // _instances[sessionId] = this; Console.Write($"\n\n SessionId: {SessionId}\n\n"); // _scopedContentService.OnBrandNameChanged += HandleBrandNameChanged; if (!string.IsNullOrEmpty(_scopedContentService.SelectedBrandName)) { SelectedBrandName = _scopedContentService.SelectedBrandName; } else { _scopedContentService.SelectedBrandName = "default"; SelectedBrandName = "default"; } Subdomain = HttpContextAccessor.HttpContext?.Items["Subdomain"]?.ToString(); SiteInfo = await _scopedContentService.GetSiteInfoByNameAsync(Subdomain); if (SiteInfo != null && SiteInfo.IsPublished) { SiteId = SiteInfo.Id; _scopedContentService.SelectedBrandName = SiteInfo.SiteName; TTSEnabled = SiteInfo.TTSActive; STTEnabled = SiteInfo.STTActive; } else { SiteId = 1; _scopedContentService.SelectedBrandName = "default"; TTSEnabled = false; STTEnabled = false; } _scopedContentService.SelectedSiteId = SiteId; Console.Write("------------------------"); // Load the CSS template for the selected brand from the database var designTemplate = await DesignTemplateService.GetByIdAsync((int)SiteInfo.TemplateId!); var cssTemplate = await CssTemplateService.GetByDesignTemplateIdAsync((int)SiteInfo.TemplateId); CollectionName = designTemplate.QDrandCollectionName; if (cssTemplate != null) { dynamicallyLoadedCss = cssTemplate.CssContent; // Assuming Content holds the CSS string var cssPath = await CssTemplateService.SaveTempCssFileAsync(dynamicallyLoadedCss, SessionId); await jsRuntime.InvokeVoidAsync("seemgen.injectCssFile", cssPath); //await CssService.ApplyCssAsync(dynamicallyLoadedCss); } Console.Write($"------------------------ {SiteInfo.MenuItems}, {SiteId}, {SiteInfo.TemplateId}, {SiteInfo.SiteName}"); Menu = await GetMenuList(SiteId); if (string.IsNullOrEmpty(HtmlContent.ToString())) { if(!string.IsNullOrWhiteSpace(topic)) { UserInput = topic; await ChatGptService.ProcessContentRequest(SessionId, UserInput, SiteId, (int)SiteInfo.TemplateId!, CollectionName, Menu, true); } else { await ChatGptService.GetChatGptWelcomeMessage(SessionId, SiteId, Menu); } // HtmlContent = await ChatGptService.GetChatGptWelcomeMessage(); // UserInput = "Sumerize for me, what is this website about, and what can I do on this website?"; // await ChatGptService.ProcessUserIntent(SessionId, UserInput, SiteId, (int)SiteInfo.TemplateId!, CollectionName, Menu); } UserInput = string.Empty; // await InvokeAsync(StateHasChanged); _initVoicePending = true; } if (_initVoicePending) { Console.WriteLine("PENDING VOICE--------------------------------------------------"); _initVoicePending = false; await jsRuntime.InvokeVoidAsync("initVoiceRecorder", "ProcessAudio2"); } } public async void MenuClick(string menuName) { await CallCSharpMethod2(menuName, SessionId, true); } public void HomeClick() { //ChatGptService.OnContentReceived -= UpdateContent; AIService.OnContentReceived -= UpdateContent; _navigationManager.Refresh(true); } private void CancelEmail() { FirstColumnClass = ""; isEmailFormVisible = false; StateHasChanged(); } public Index() { myHome = this; // Set the static reference to the current instance } protected override async Task OnInitializedAsync() { _scopedContentService.OnBrandNameChanged += HandleBrandNameChanged; // ChatGptService.OnContentReceived += UpdateContent; AIService.OnContentReceived += UpdateContent; AIService.OnContentReceiveFinished += UpdateFinished; // ChatGptService.OnStatusChangeReceived += UpdateStatus; AIService.OnStatusChangeReceived += UpdateStatus; AIService.OnTextContentAvailable += UpdateTextContentForVoice; SessionId = Guid.NewGuid().ToString(); _instances[SessionId] = this; VoiceEnabled = configuration?.GetSection("AiSettings")?.GetValue("VoiceActivated") ?? false; } private async void UpdateContent(string receivedSessionId, string content) { if (receivedSessionId == SessionId) // Only accept messages meant for this tab { HtmlContent.Clear(); HtmlContent.Append(content); //InvokeAsync(StateHasChanged); // Ensures UI updates dynamically await InvokeAsync(() => { StateHasChanged(); }); //_scopedContentService.CurrentDOM = await jsRuntime.InvokeAsync("getDivContent", "currentContent"); } } private async void UpdateTextContentForVoice(string receivedSessionId, string content) { Console.WriteLine("UPDATETEXTCONTENT called"); if (receivedSessionId == SessionId) // Only accept messages meant for this tab { TextContent = content; await ConvertTextToSpeech(content); //_scopedContentService.CurrentDOM = await jsRuntime.InvokeAsync("getDivContent", "currentContent"); } } private async void UpdateFinished(string receivedSessionId) { if (receivedSessionId == SessionId) // Only accept messages meant for this tab { Console.WriteLine("Content update finished"); var result = await jsRuntime.InvokeAsync("getDivContent", "currentContent"); //await ConvertTextToSpeech(); _scopedContentService.CurrentDOM = JsonSerializer.Serialize(result); Console.Write(_scopedContentService.CurrentDOM); } } private async Task ContentChangedInForm() { var result = await jsRuntime.InvokeAsync("getDivContent", "currentContent"); _scopedContentService.CurrentDOM = JsonSerializer.Serialize(result); Console.Write(_scopedContentService.CurrentDOM); } private async void UpdateStatus(string receivedSessionId, string content) { if (receivedSessionId == SessionId) // Only accept messages meant for this tab { StatusContent = content; //InvokeAsync(StateHasChanged); // Ensures UI updates dynamically await InvokeAsync(() => { StateHasChanged(); }); } } public async Task Enter(KeyboardEventArgs e) { if (e.Code == "Enter" || e.Code == "NumpadEnter") { var menu = await GetMenuList(SiteId); HtmlContent.Clear(); string input = "Please tell me more about: " + UserInput; await ChatGptService.ProcessUserIntent(SessionId, input, SiteId, (int)SiteInfo.TemplateId!, CollectionName, menu); UserInput = string.Empty; } } public void Dispose() { dynamicallyLoadedCss = ""; HtmlContent.Clear(); _scopedContentService.OnBrandNameChanged -= HandleBrandNameChanged; AIService.OnContentReceived -= UpdateContent; AIService.OnContentReceiveFinished -= UpdateFinished; AIService.OnStatusChangeReceived -= UpdateStatus; AIService.OnTextContentAvailable -= UpdateTextContentForVoice; } public async ValueTask DisposeAsync() { await CssTemplateService.DeleteSessionCssFile(SessionId); } }