This commit is contained in:
h z
2025-05-04 15:21:01 +01:00
commit 619ddc6ddf
70 changed files with 60624 additions and 0 deletions

21
Components/App.razor Normal file
View File

@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<base href="/"/>
<link rel="stylesheet" href="@Assets["lib/bootstrap/dist/css/bootstrap.min.css"]"/>
<link rel="stylesheet" href="@Assets["app.css"]"/>
<link rel="stylesheet" href="@Assets["Alchegos.HCI.styles.css"]"/>
<ImportMap/>
<link rel="icon" type="image/png" href="favicon.png"/>
<HeadOutlet/>
</head>
<body>
<Routes/>
<script src="_framework/blazor.web.js"></script>
</body>
</html>

View File

@@ -0,0 +1,19 @@
@inherits LayoutComponentBase
<div class="page">
<div class="sidebar">
<NavMenu/>
</div>
<main>
<article class="content px-4">
@Body
</article>
</main>
</div>
<div id="blazor-error-ui" data-nosnippet>
An unhandled error has occurred.
<a href="." class="reload">Reload</a>
<span class="dismiss">🗙</span>
</div>

View File

View File

@@ -0,0 +1,17 @@
<div class="top-row ps-3 navbar navbar-dark">
<div class="container-fluid">
<a class="navbar-brand" href="">Alchegos.HCI</a>
</div>
</div>
<input type="checkbox" title="Navigation menu" class="navbar-toggler"/>
<div class="nav-scrollable" onclick="document.querySelector('.navbar-toggler').click()">
<nav class="nav flex-column">
<div class="nav-item px-3">
<NavLink class="nav-link" href="chat">
<span class="bi bi-chat-dots-fill-nav-menu" aria-hidden="true"></span> Chat
</NavLink>
</div>
</nav>
</div>

View File

@@ -0,0 +1,105 @@
.navbar-toggler {
appearance: none;
cursor: pointer;
width: 3.5rem;
height: 2.5rem;
color: white;
position: absolute;
top: 0.5rem;
right: 1rem;
border: 1px solid rgba(255, 255, 255, 0.1);
background: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'%3e%3cpath stroke='rgba%28255, 255, 255, 0.55%29' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/%3e%3c/svg%3e") no-repeat center/1.75rem rgba(255, 255, 255, 0.1);
}
.navbar-toggler:checked {
background-color: rgba(255, 255, 255, 0.5);
}
.top-row {
min-height: 3.5rem;
background-color: rgba(0,0,0,0.4);
}
.navbar-brand {
font-size: 1.1rem;
}
.bi {
display: inline-block;
position: relative;
width: 1.25rem;
height: 1.25rem;
margin-right: 0.75rem;
top: -1px;
background-size: cover;
}
.bi-house-door-fill-nav-menu {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='white' class='bi bi-house-door-fill' viewBox='0 0 16 16'%3E%3Cpath d='M6.5 14.5v-3.505c0-.245.25-.495.5-.495h2c.25 0 .5.25.5.5v3.5a.5.5 0 0 0 .5.5h4a.5.5 0 0 0 .5-.5v-7a.5.5 0 0 0-.146-.354L13 5.793V2.5a.5.5 0 0 0-.5-.5h-1a.5.5 0 0 0-.5.5v1.293L8.354 1.146a.5.5 0 0 0-.708 0l-6 6A.5.5 0 0 0 1.5 7.5v7a.5.5 0 0 0 .5.5h4a.5.5 0 0 0 .5-.5Z'/%3E%3C/svg%3E");
}
.bi-plus-square-fill-nav-menu {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='white' class='bi bi-plus-square-fill' viewBox='0 0 16 16'%3E%3Cpath d='M2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2H2zm6.5 4.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3a.5.5 0 0 1 1 0z'/%3E%3C/svg%3E");
}
.bi-list-nested-nav-menu {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='white' class='bi bi-list-nested' viewBox='0 0 16 16'%3E%3Cpath fill-rule='evenodd' d='M4.5 11.5A.5.5 0 0 1 5 11h10a.5.5 0 0 1 0 1H5a.5.5 0 0 1-.5-.5zm-2-4A.5.5 0 0 1 3 7h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm-2-4A.5.5 0 0 1 1 3h10a.5.5 0 0 1 0 1H1a.5.5 0 0 1-.5-.5z'/%3E%3C/svg%3E");
}
.nav-item {
font-size: 0.9rem;
padding-bottom: 0.5rem;
}
.nav-item:first-of-type {
padding-top: 1rem;
}
.nav-item:last-of-type {
padding-bottom: 1rem;
}
.nav-item ::deep .nav-link {
color: #d7d7d7;
background: none;
border: none;
border-radius: 4px;
height: 3rem;
display: flex;
align-items: center;
line-height: 3rem;
width: 100%;
}
.nav-item ::deep a.active {
background-color: rgba(255,255,255,0.37);
color: white;
}
.nav-item ::deep .nav-link:hover {
background-color: rgba(255,255,255,0.1);
color: white;
}
.nav-scrollable {
display: none;
}
.navbar-toggler:checked ~ .nav-scrollable {
display: block;
}
@media (min-width: 641px) {
.navbar-toggler {
display: none;
}
.nav-scrollable {
/* Never collapse the sidebar for wide screens */
display: block;
/* Allow sidebar to scroll for tall menus */
height: calc(100vh - 3.5rem);
overflow-y: auto;
}
}

250
Components/Pages/Chat.razor Normal file
View File

@@ -0,0 +1,250 @@
@page "/chat"
@page "/chat/{InitialSessionId}"
@rendermode InteractiveServer
@inject IChatSessionService ChatSessionService
@inject NavigationManager NavigationManager
@using Alchegos.HCI.Models
@using Alchegos.HCI.Services
@implements IAsyncDisposable
<PageTitle>Chat</PageTitle>
<h1>Chat Session</h1>
<div class="session-selector">
<input @bind="currentSessionIdInput" placeholder="Enter or Generate Session ID" />
<button @onclick="LoadSession" disabled="@isLoading">Load / Start Session</button>
<button @onclick="GenerateSessionId" disabled="@isLoading">Generate New ID</button>
@if (!string.IsNullOrEmpty(activeSessionId))
{
<span>Active Session: @activeSessionId</span>
}
</div>
@if (isLoading)
{
<p><em>Loading session...</em></p>
}
else if (currentSession != null)
{
<div class="chat-history" style="height: 400px; overflow-y: scroll; border: 1px solid #ccc; margin-bottom: 10px; padding: 5px;">
@foreach (var message in currentSession.Messages)
{
<div class="@(message.IsUser ? "user-message" : "server-message")">
<strong>@(message.IsUser ? "You" : "Server"):</strong>
<span>@message.Content</span>
<small style="font-size: 0.7em; color: grey; display: block;">@message.Timestamp.ToLocalTime()</small>
</div>
}
@if (currentSession.IsWaitingForResponse)
{
<p><em>Waiting for response...</em></p>
}
</div>
<div class="chat-input">
<input @bind="newMessage" placeholder="Type your message..." @onkeydown="HandleKeyDown" disabled="@currentSession.IsWaitingForResponse" />
<button @onclick="SendMessage" disabled="@(currentSession.IsWaitingForResponse || string.IsNullOrWhiteSpace(newMessage))">Send</button>
</div>
}
else if (sessionLoadAttempted)
{
<p><em>Enter a Session ID and click "Load / Start Session" or generate a new one.</em></p>
}
@code {
[Parameter] public string? InitialSessionId { get; set; }
private string currentSessionIdInput { get; set; } = string.Empty;
private string activeSessionId { get; set; } = string.Empty;
private ChatSession? currentSession;
private string newMessage { get; set; } = string.Empty;
private bool isLoading = false;
private bool sessionLoadAttempted = false;
private string? _previousSessionId = null;
protected override async Task OnParametersSetAsync()
{
Console.WriteLine($"--- OnParametersSetAsync START. InitialSessionId: {InitialSessionId} ---");
if (InitialSessionId != _previousSessionId && !string.IsNullOrEmpty(InitialSessionId))
{
isLoading = true;
sessionLoadAttempted = true;
if (!string.IsNullOrEmpty(_previousSessionId) && _previousSessionId != InitialSessionId)
{
Console.WriteLine($"Parameter changed from {_previousSessionId} to {InitialSessionId}. Unregistering old callbacks for {_previousSessionId}.");
var oldId = _previousSessionId;
_ = Task.Run(() => ChatSessionService.UnregisterSessionCallbacks(oldId));
}
activeSessionId = InitialSessionId;
_previousSessionId = InitialSessionId;
Console.WriteLine($"Loading session from parameter: {activeSessionId}");
currentSession = ChatSessionService.GetOrCreateSession(activeSessionId);
if (currentSession != null)
{
Console.WriteLine($"Session obtained: {currentSession.SessionId}");
await RegisterCallbacksAsync();
}
else
Console.WriteLine("!!! Failed to get/create session in OnParametersSetAsync!");
isLoading = false;
Console.WriteLine("--- OnParametersSetAsync END. isLoading set to false ---");
}
else if (string.IsNullOrEmpty(InitialSessionId) && _previousSessionId != null)
{
Console.WriteLine("--- OnParametersSetAsync: InitialSessionId became null/empty. Clearing session. ---");
await UnregisterCallbacksAsync();
currentSession = null;
activeSessionId = string.Empty;
_previousSessionId = null;
currentSessionIdInput = string.Empty;
sessionLoadAttempted = false;
}
else if (InitialSessionId == _previousSessionId)
{
Console.WriteLine($"--- OnParametersSetAsync: InitialSessionId '{InitialSessionId}' did not change. Skipping load. ---");
}
else
{
Console.WriteLine($"--- OnParametersSetAsync: InitialSessionId '{InitialSessionId}' is null/empty and no previous ID. Initial state. ---");
}
}
protected override void OnInitialized()
{
if (!string.IsNullOrEmpty(InitialSessionId))
currentSessionIdInput = InitialSessionId;
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender && !string.IsNullOrEmpty(activeSessionId))
await RegisterCallbacksAsync();
}
private void GenerateSessionId()
{
var newId = Guid.NewGuid().ToString("N");
currentSessionIdInput = newId;
Console.WriteLine($"--- GenerateSessionId: Generated {newId}, Navigating... ---");
NavigationManager.NavigateTo($"/chat/{newId}");
}
private void LoadSession()
{
if (string.IsNullOrWhiteSpace(currentSessionIdInput))
{
Console.WriteLine("--- LoadSession Clicked: Input empty, doing nothing. ---");
return;
}
var sessionIdToLoad = currentSessionIdInput;
var targetUrl = $"/chat/{sessionIdToLoad}";
string currentPathAndQuery = new Uri(NavigationManager.Uri).PathAndQuery;
if (!currentPathAndQuery.Equals(targetUrl, StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine($"--- LoadSession Clicked: Navigating to {targetUrl}... ---");
NavigationManager.NavigateTo(targetUrl);
}
else
{
Console.WriteLine($"--- LoadSession Clicked: Already at {targetUrl}. No navigation needed. ---");
}
}
private async Task SendMessage()
{
if (currentSession == null || string.IsNullOrWhiteSpace(newMessage) || currentSession.IsWaitingForResponse)
return;
var userMessage = new ChatMessage { IsUser = true, Content = newMessage };
ChatSessionService.AddMessage(activeSessionId, userMessage);
ChatSessionService.SetWaitingStatus(activeSessionId, true);
var messageToSend = newMessage;
newMessage = string.Empty;
StateHasChanged();
await ChatSessionService.SendMessageToExternalWebhook(activeSessionId, messageToSend);
}
private async Task HandleKeyDown(KeyboardEventArgs e)
{
if (e.Key == "Enter" && !currentSession.IsWaitingForResponse && !string.IsNullOrWhiteSpace(newMessage))
await SendMessage();
}
[JSInvokable]
public async Task HandleMessageReceived(ChatMessage message)
{
await InvokeAsync(StateHasChanged);
}
[JSInvokable]
public async Task HandleSessionClosed()
{
await InvokeAsync(() =>
{
if (currentSession != null && currentSession.SessionId == activeSessionId)
{
Console.WriteLine($"UI notified session closed: {activeSessionId}");
currentSession = null;
activeSessionId = string.Empty;
currentSessionIdInput = string.Empty;
NavigationManager.NavigateTo("/chat", forceLoad: false);
StateHasChanged();
}
});
}
private async Task RegisterCallbacksAsync()
{
if (currentSession != null)
{
_ = Task.Run(() =>
{
ChatSessionService.RegisterSessionCallbacks(
currentSession.SessionId,
HandleMessageReceived,
HandleSessionClosed
);
});
await Task.CompletedTask;
}
}
private async Task UnregisterCallbacksAsync()
{
if (!string.IsNullOrEmpty(activeSessionId))
{
var capturedSessionId = activeSessionId;
_ = Task.Run(() => ChatSessionService.UnregisterSessionCallbacks(capturedSessionId));
await Task.CompletedTask;
}
}
public async ValueTask DisposeAsync()
{
await UnregisterCallbacksAsync();
}
}

View File

@@ -0,0 +1,22 @@
.user-message {
text-align: right;
margin-bottom: 5px;
padding: 8px;
background-color: #e1f5fe;
border-radius: 8px;
margin-left: 20%;
}
.server-message {
text-align: left;
margin-bottom: 5px;
padding: 8px;
background-color: #f1f1f1;
border-radius: 8px;
margin-right: 20%;
}
.session-selector { margin-bottom: 15px; }
.chat-history {}
.chat-input { display: flex; }
.chat-input input { flex-grow: 1; margin-right: 5px; }

6
Components/Routes.razor Normal file
View File

@@ -0,0 +1,6 @@
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)"/>
<FocusOnNavigate RouteData="routeData" Selector="h1"/>
</Found>
</Router>

10
Components/_Imports.razor Normal file
View File

@@ -0,0 +1,10 @@
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using Alchegos.HCI
@using Alchegos.HCI.Components