577 lines
20 KiB
Plaintext
577 lines
20 KiB
Plaintext
@page "/"
|
|
@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.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
|
|
@attribute [Sitemap]
|
|
|
|
<div class="page" style="z-index: 1">
|
|
<NavMenu MenuString="@Menu" OnMenuClicked=@MenuClick></NavMenu>
|
|
<main>
|
|
|
|
<article class="content container text-center" style="position: relative; z-index: 4;">
|
|
<PageTitle>Home</PageTitle>
|
|
<VideoComponent SelectedBrandName="@selectedBrandName" />
|
|
@* <HeadContent>
|
|
|
|
<style id="seemgen-style">@dynamicallyLoadedCss</style>
|
|
|
|
</HeadContent> *@
|
|
|
|
<div id="maincontrol">
|
|
|
|
|
|
@* <div class="hoverslide"> *@
|
|
|
|
<div class="displaysearch">
|
|
<div class="searchBox">
|
|
|
|
<input @oninput="(e) => UserInput = e.Value.ToString()"
|
|
@onkeydown="@Enter" class="searchInput" type="text" name="" value="@UserInput" placeholder="Ask any question">
|
|
<button class="searchButton border-0" @onclick="SendUserQuery" href="#">
|
|
<i class="fa-solid fa-magnifying-glass"></i>
|
|
</button>
|
|
</div>
|
|
|
|
|
|
<button id="recButton" class="btn btn-primary voicebutton" onclick="startRecording()"><i class="fa-solid fa-microphone"></i></button>
|
|
|
|
<button id="stopButton" class="btn btn-primary voicebutton" onclick="stopRecording()" hidden><i class="fa-solid fa-microphone-slash"></i></button>
|
|
|
|
</div>
|
|
|
|
@* Type anything *@
|
|
|
|
@* </div> *@
|
|
</div>
|
|
|
|
<p id="recordingText"></p>
|
|
<div class="row" id="currentContent">
|
|
@{
|
|
if (!string.IsNullOrEmpty(HtmlContent.ToString()))
|
|
{
|
|
<div class="pt-5 @FirstColumnClass">
|
|
@((MarkupString)HtmlContent.ToString())
|
|
</div>
|
|
if (isEmailFormVisible)
|
|
{
|
|
<div class="pt-5 col-12 col-md-6">
|
|
<ContactFormComponent ContactFormModel="@ContactFormModel" DocumentEmailAddress="@DocumentEmailAddress" OnDataChanged="@ContentChangedInForm"></ContactFormComponent>
|
|
<button @onclick="CancelEmail" class="btn btn-primary">Cancel</button>
|
|
</div>
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
<div class="text-center row" style="height: 70vh;">
|
|
<p>@StatusContent</p>
|
|
<div class="mydiv"></div>
|
|
<div class="mydiv"></div>
|
|
<div class="mydiv"></div>
|
|
<div class="mydiv"></div>
|
|
<div class="mydiv"></div>
|
|
|
|
|
|
</div>
|
|
}
|
|
|
|
|
|
}
|
|
</div>
|
|
|
|
<button class="btn btn-primary" @onclick="HomeClick"><i class="fa-solid fa-rotate"></i></button>
|
|
@* <FormWizardComponent SessionId="@sessionId"></FormWizardComponent> *@
|
|
|
|
</article>
|
|
</main>
|
|
</div>
|
|
|
|
<script>
|
|
|
|
var sessionId = null;
|
|
|
|
function setSessionId(id) {
|
|
sessionId = id;
|
|
console.log("Session ID set:", sessionId);
|
|
}
|
|
|
|
function callAI(inputString) {
|
|
console.log(sessionId);
|
|
DotNet.invokeMethodAsync('BLAIzor', 'CallCSharpMethod2', inputString, sessionId, false)
|
|
.then(response => console.log(response))
|
|
.catch(error => console.error(error));
|
|
}
|
|
|
|
var mediaRecorder;
|
|
var audioChunks = [];
|
|
var recButton = document.getElementById("recButton");
|
|
var stopButton = document.getElementById("stopButton");
|
|
var recText = document.getElementById("recordingText");
|
|
|
|
navigator.permissions.query({name: 'microphone'}).then(function (result) {
|
|
if (result.state == 'granted') {
|
|
} else if (result.state == 'prompt') {
|
|
} else if (result.state == 'denied') {
|
|
}
|
|
result.onchange = function () {};
|
|
});
|
|
|
|
async function startRecording() {
|
|
if (recButton) {
|
|
recButton.hidden = true;
|
|
stopButton.hidden = false;
|
|
recText.textContent = "recording...";
|
|
}
|
|
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
mediaRecorder = new MediaRecorder(stream);
|
|
|
|
mediaRecorder.ondataavailable = event => audioChunks.push(event.data);
|
|
mediaRecorder.onstop = () => {
|
|
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
|
|
audioChunks = [];
|
|
console.log("media stopped");
|
|
const reader = new FileReader();
|
|
reader.onload = () => {
|
|
const base64Audio = reader.result.split(',')[1];
|
|
DotNet.invokeMethodAsync('BLAIzor', 'ProcessAudio2', base64Audio, sessionId);
|
|
};
|
|
reader.readAsDataURL(audioBlob);
|
|
};
|
|
|
|
mediaRecorder.start();
|
|
}
|
|
|
|
function stopRecording() {
|
|
mediaRecorder.stop();
|
|
var recButton = document.getElementById("recButton");
|
|
var stopButton = document.getElementById("stopButton");
|
|
var recText = document.getElementById("recordingText");
|
|
mediaRecorder.stream.getTracks().forEach(track => track.stop()); // Free memory
|
|
if (stopButton) {
|
|
stopButton.hidden = true;
|
|
recButton.hidden = false;
|
|
recText.textContent = "";
|
|
}
|
|
}
|
|
</script>
|
|
<script>
|
|
function openCalendar(calendlyUserName) {
|
|
console.log(calendlyUserName);
|
|
Calendly.initPopupWidget({
|
|
url: 'https://calendly.com/' + calendlyUserName + '?name=JohnDoe&email=john.doe@example.com'
|
|
});
|
|
}
|
|
</script>
|
|
|
|
<script>
|
|
function openContactForm(emailAddress) {
|
|
console.log(emailAddress);
|
|
if (emailAddress) {
|
|
DotNet.invokeMethodAsync('BLAIzor', 'OpenEmailForm2', emailAddress)
|
|
}
|
|
}
|
|
</script>
|
|
|
|
|
|
|
|
@code {
|
|
public static Index myHome;
|
|
private string? Subdomain;
|
|
public int SiteId;
|
|
public SiteInfo SiteInfo;
|
|
private StringBuilder HtmlContent = new StringBuilder("");
|
|
private string StatusContent = "";
|
|
private string UserInput = string.Empty;
|
|
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 selectedBrandName = "default";
|
|
private string dynamicallyLoadedCss = string.Empty;
|
|
private string collectionName = "html_snippets";
|
|
private string sessionId;
|
|
private static readonly Dictionary<string, Index> _instances = new();
|
|
private string Menu;
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
{
|
|
if (firstRender)
|
|
{
|
|
await jsRuntime.InvokeVoidAsync("setSessionId", sessionId);
|
|
|
|
|
|
// 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;
|
|
}
|
|
else
|
|
{
|
|
SiteId = 1;
|
|
_scopedContentService.SelectedBrandName = "default";
|
|
|
|
}
|
|
_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();
|
|
if (string.IsNullOrEmpty(HtmlContent.ToString()))
|
|
{
|
|
// HtmlContent = await ChatGptService.GetChatGptWelcomeMessage();
|
|
await ChatGptService.GetChatGptWelcomeMessage(sessionId, SiteId, Menu);
|
|
// 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);
|
|
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
[JSInvokable("OpenEmailForm2")]
|
|
public static async void OpenEmailForm2(string emailAddress)
|
|
{
|
|
if (myHome != null)
|
|
{
|
|
await myHome.DisplayEmailForm(emailAddress);
|
|
}
|
|
Console.Write("openEmail with: " + emailAddress);
|
|
}
|
|
|
|
public async Task DisplayEmailForm(string emailAddress)
|
|
{
|
|
FirstColumnClass = "col-12 col-md-6";
|
|
DocumentEmailAddress = emailAddress;
|
|
isEmailFormVisible = true;
|
|
StateHasChanged();
|
|
var result = await jsRuntime.InvokeAsync<object>("getDivContent", "currentContent");
|
|
_scopedContentService.CurrentDOM = JsonSerializer.Serialize(result);
|
|
// Console.Write($"{_scopedContentService.CurrentDOM}");
|
|
}
|
|
|
|
|
|
[JSInvokable("CallCSharpMethod2")]
|
|
public static async Task CallCSharpMethod2(string input, string sessionId, bool forceUnModified = false)
|
|
{
|
|
// if (myHome != null)
|
|
// {
|
|
// await myHome.HandleJsCall(input, );
|
|
// }
|
|
|
|
if (_instances.TryGetValue(sessionId, out var instance))
|
|
{
|
|
await instance.HandleJsCall(input, sessionId, forceUnModified);
|
|
}
|
|
Console.Write("Button clicked:" + input);
|
|
}
|
|
|
|
[JSInvokable("ProcessAudio2")]
|
|
public static async Task ProcessAudio2(string base64Audio, string sessionId)
|
|
{
|
|
|
|
Console.Write("audio incoming");
|
|
if (myHome != null)
|
|
{
|
|
var languageCode = "hu-HU";
|
|
if (myHome._scopedContentService.SelectedLanguage == "Hungarian")
|
|
{
|
|
languageCode = "hu-HU";
|
|
}
|
|
else if (myHome._scopedContentService.SelectedLanguage == "English")
|
|
{
|
|
languageCode = "en-US";
|
|
}
|
|
else if (myHome._scopedContentService.SelectedLanguage == "German")
|
|
{
|
|
languageCode = "de-DE";
|
|
}
|
|
var credentialsPath = myHome.configuration.GetSection("GoogleAPI").GetValue<string>("CredentialsPath");
|
|
Console.Write(credentialsPath);
|
|
var builder = new SpeechClientBuilder
|
|
{
|
|
CredentialsPath = credentialsPath
|
|
};
|
|
var speech = builder.Build();
|
|
|
|
byte[] audioBytes = Convert.FromBase64String(base64Audio);
|
|
myHome.HtmlContent.Clear();
|
|
var response = await speech.RecognizeAsync(new RecognitionConfig
|
|
{
|
|
Encoding = RecognitionConfig.Types.AudioEncoding.Mp3,
|
|
SampleRateHertz = 48000, // Match the actual sample rate
|
|
LanguageCode = languageCode
|
|
}, RecognitionAudio.FromBytes(audioBytes));
|
|
Console.Write("BILLED: " + response.TotalBilledTime);
|
|
foreach (var result in response.Results)
|
|
{
|
|
//Console.Write("RESULT: " + result.Alternatives.Count);
|
|
foreach (var alternative in result.Alternatives)
|
|
{
|
|
//Console.WriteLine($"Transcription: {alternative.Transcript}");
|
|
await myHome.HandleVoiceCommand(alternative.Transcript, sessionId);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private async Task SendMessage()
|
|
{
|
|
Console.WriteLine("Button clicked!");
|
|
var menu = await GetMenuList();
|
|
HtmlContent.Clear();
|
|
await ChatGptService.ProcessUserIntent(sessionId, UserInput, SiteId, (int)SiteInfo.TemplateId!, collectionName, menu);
|
|
UserInput = string.Empty;
|
|
}
|
|
|
|
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
_scopedContentService.OnBrandNameChanged += HandleBrandNameChanged;
|
|
// ChatGptService.OnContentReceived += UpdateContent;
|
|
AIService.OnContentReceived += UpdateContent;
|
|
AIService.OnContentReceiveFinished += UpdateFinished;
|
|
// ChatGptService.OnStatusChangeReceived += UpdateStatus;
|
|
AIService.OnStatusChangeReceived += UpdateStatus;
|
|
|
|
sessionId = Guid.NewGuid().ToString();
|
|
_instances[sessionId] = this;
|
|
}
|
|
|
|
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<string>("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<object>("getDivContent", "currentContent");
|
|
_scopedContentService.CurrentDOM = JsonSerializer.Serialize(result);
|
|
Console.Write(_scopedContentService.CurrentDOM);
|
|
}
|
|
}
|
|
|
|
private async Task ContentChangedInForm()
|
|
{
|
|
var result = await jsRuntime.InvokeAsync<object>("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();
|
|
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 async Task HandleVoiceCommand(string input, string sessionId)
|
|
{
|
|
// HtmlContent = string.Empty;
|
|
UserInput = input;
|
|
await InvokeAsync(StateHasChanged);
|
|
|
|
await SendUserQuery();
|
|
//UserInput = string.Empty;
|
|
}
|
|
|
|
public async Task HandleJsCall(string input, string sessionId, bool forceUnmodified)
|
|
{
|
|
HtmlContent.Clear();
|
|
await InvokeAsync(StateHasChanged);
|
|
UserInput = input;
|
|
await DisplayMenuContent(input, forceUnmodified);
|
|
UserInput = string.Empty;
|
|
await InvokeAsync(StateHasChanged);
|
|
}
|
|
|
|
private async Task SendUserQuery()
|
|
{
|
|
if (!string.IsNullOrEmpty(UserInput))
|
|
{
|
|
HtmlContent.Clear();
|
|
var menu = await GetMenuList();
|
|
await ChatGptService.ProcessUserIntent(sessionId, UserInput, SiteId, (int)SiteInfo.TemplateId!, collectionName, menu);
|
|
UserInput = string.Empty;
|
|
}
|
|
}
|
|
|
|
private async Task DisplayMenuContent(string input, bool forceUnmodified)
|
|
{
|
|
if (!string.IsNullOrEmpty(UserInput))
|
|
{
|
|
HtmlContent.Clear();
|
|
var menu = await GetMenuList();
|
|
var menuItem = (await GetMenuItems()).Where(m => m.Name == input).FirstOrDefault();
|
|
if(menuItem == null)
|
|
{
|
|
await ChatGptService.ProcessContentRequest(sessionId, input, SiteId, (int)SiteInfo.TemplateId!, collectionName, menu, forceUnmodified);
|
|
}
|
|
else
|
|
{
|
|
await ChatGptService.ProcessContentRequest(sessionId, menuItem, SiteId, (int)SiteInfo.TemplateId!, collectionName, menu, forceUnmodified);
|
|
}
|
|
UserInput = string.Empty;
|
|
}
|
|
}
|
|
|
|
private async Task<string> GetMenuList()
|
|
{
|
|
List<MenuItem> menuItems = (await _contentEditorService.GetMenuItemsBySiteIdAsync(SiteId)).Where(m => m.ShowInMainMenu == true).OrderBy(m => m.SortOrder).ToList();
|
|
string menuList = "";
|
|
foreach (MenuItem item in menuItems)
|
|
{
|
|
menuList += item.Name + ",";
|
|
}
|
|
return menuList;
|
|
}
|
|
|
|
private async Task<List<MenuItem>> GetMenuItems()
|
|
{
|
|
List<MenuItem> menuItems = (await _contentEditorService.GetMenuItemsBySiteIdAsync(SiteId)).Where(m => m.ShowInMainMenu == true).OrderBy(m => m.SortOrder).ToList();
|
|
return menuItems;
|
|
}
|
|
|
|
private async void HandleBrandNameChanged()
|
|
{
|
|
selectedBrandName = _scopedContentService.SelectedBrandName;
|
|
//StateHasChanged();
|
|
await InvokeAsync(() =>
|
|
{
|
|
StateHasChanged();
|
|
});
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
dynamicallyLoadedCss = "";
|
|
HtmlContent.Clear();
|
|
_scopedContentService.OnBrandNameChanged -= HandleBrandNameChanged;
|
|
AIService.OnContentReceived -= UpdateContent;
|
|
AIService.OnContentReceiveFinished -= UpdateFinished;
|
|
AIService.OnStatusChangeReceived -= UpdateStatus;
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
await CssTemplateService.DeleteSessionCssFile(sessionId);
|
|
}
|
|
|
|
|
|
}
|