505 lines
15 KiB
Plaintext
505 lines
15 KiB
Plaintext
@page "/intelligence"
|
|
@attribute [Authorize]
|
|
@using NexusReader.Application.DTOs.AI
|
|
@using NexusReader.Application.Abstractions.Services
|
|
@using NexusReader.Application.DTOs.User
|
|
@using System.Net.Http.Json
|
|
@inject HttpClient Http
|
|
@inject IKnowledgeService KnowledgeService
|
|
@inject AuthenticationStateProvider AuthStateProvider
|
|
|
|
|
|
<div class="intelligence-page">
|
|
<header class="intelligence-header">
|
|
<div class="header-title-section">
|
|
<h1>Global AI Q&A</h1>
|
|
<p class="subtitle">Search, interrogate, and extract grounded facts from your library using Polyglot KM-RAG</p>
|
|
</div>
|
|
</header>
|
|
|
|
<div class="intelligence-layout glass-panel">
|
|
<div class="search-scope-bar">
|
|
<div class="input-group search-input-group">
|
|
<input class="nexus-input"
|
|
placeholder="Ask a question about your books..."
|
|
@bind="_question"
|
|
@bind:event="oninput"
|
|
@onkeyup="HandleKeyUp" />
|
|
<button class="btn-nexus primary search-btn"
|
|
disabled="@(string.IsNullOrWhiteSpace(_question) || _isLoading)"
|
|
@onclick="AskQuestionAsync">
|
|
@if (_isLoading)
|
|
{
|
|
<div class="spinner-glow small btn-spinner"></div>
|
|
}
|
|
else
|
|
{
|
|
<span>Ask AI</span>
|
|
}
|
|
</button>
|
|
</div>
|
|
|
|
<div class="scope-selector">
|
|
<label for="book-select">Scope:</label>
|
|
<select id="book-select" class="nexus-select" @bind="_selectedBookId">
|
|
<option value="">All Books (Global Search)</option>
|
|
@if (_books != null)
|
|
{
|
|
@foreach (var book in _books)
|
|
{
|
|
<option value="@book.Id">@book.Title</option>
|
|
}
|
|
}
|
|
</select>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="results-area">
|
|
@if (_isLoading)
|
|
{
|
|
<div class="loading-state">
|
|
<div class="nexus-spinner"></div>
|
|
<span>Analyzing conceptual graph and synthesizing response...</span>
|
|
</div>
|
|
}
|
|
else if (_response != null)
|
|
{
|
|
<div class="response-container">
|
|
<div class="response-section">
|
|
<h4><i class="bi bi-robot"></i> Answer</h4>
|
|
<div class="answer-text">
|
|
@_response.Answer
|
|
</div>
|
|
</div>
|
|
|
|
@if (_response.Citations != null && _response.Citations.Any())
|
|
{
|
|
<div class="citations-section">
|
|
<h4><i class="bi bi-journal-check"></i> Grounded Citations</h4>
|
|
<div class="citations-grid">
|
|
@foreach (var citation in _response.Citations)
|
|
{
|
|
<div class="citation-card">
|
|
<div class="citation-header">
|
|
<span class="source-badge">@citation.SourceBook</span>
|
|
@if (!string.IsNullOrEmpty(citation.CitationId) && citation.CitationId.Length > 8)
|
|
{
|
|
<span class="id-badge">ID: @citation.CitationId.Substring(0, Math.Min(8, citation.CitationId.Length))</span>
|
|
}
|
|
</div>
|
|
<div class="citation-body">
|
|
"@citation.Snippet"
|
|
</div>
|
|
</div>
|
|
}
|
|
</div>
|
|
</div>
|
|
}
|
|
</div>
|
|
}
|
|
else if (_hasSearched)
|
|
{
|
|
<div class="empty-state">
|
|
<i class="bi bi-info-circle"></i>
|
|
<p>No answers generated. Try adjusting your question.</p>
|
|
</div>
|
|
}
|
|
else
|
|
{
|
|
<div class="welcome-state">
|
|
<div class="welcome-icon">
|
|
<svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">
|
|
<path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"></path>
|
|
</svg>
|
|
</div>
|
|
<h3>Start Interrogating Your Library</h3>
|
|
<p>Ask complex questions across all your books. The system will search vectors, pull concept graph relations, and formulate a grounded answer with precise citations.</p>
|
|
</div>
|
|
}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<style>
|
|
.intelligence-page {
|
|
padding: 3rem 2rem;
|
|
max-width: 1100px;
|
|
margin: 0 auto;
|
|
animation: fadeIn 0.5s ease-out;
|
|
}
|
|
|
|
.intelligence-header {
|
|
margin-bottom: 2rem;
|
|
}
|
|
|
|
.header-title-section h1 {
|
|
font-family: var(--nexus-font-serif);
|
|
font-size: 2.8rem;
|
|
font-weight: 700;
|
|
margin: 0 0 0.5rem 0;
|
|
background: linear-gradient(135deg, var(--nexus-text, #ffffff) 0%, rgba(255, 255, 255, 0.7) 100%);
|
|
-webkit-background-clip: text;
|
|
-webkit-text-fill-color: transparent;
|
|
}
|
|
|
|
.subtitle {
|
|
font-size: 1rem;
|
|
color: rgba(255, 255, 255, 0.6);
|
|
margin: 0;
|
|
}
|
|
|
|
.intelligence-layout {
|
|
padding: 2.5rem;
|
|
border-radius: 20px;
|
|
background: rgba(255, 255, 255, 0.03);
|
|
border: 1px solid rgba(255, 255, 255, 0.05);
|
|
backdrop-filter: blur(10px);
|
|
-webkit-backdrop-filter: blur(10px);
|
|
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
|
|
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
|
|
}
|
|
|
|
.search-scope-bar {
|
|
display: flex;
|
|
gap: 1.5rem;
|
|
align-items: center;
|
|
margin-bottom: 2.5rem;
|
|
flex-wrap: wrap;
|
|
}
|
|
|
|
.search-input-group {
|
|
flex-grow: 1;
|
|
display: flex;
|
|
background: rgba(255, 255, 255, 0.02);
|
|
border: 1px solid rgba(255, 255, 255, 0.05);
|
|
border-radius: 10px;
|
|
padding: 0.25rem 0.25rem 0.25rem 1.25rem;
|
|
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
|
|
}
|
|
|
|
.search-input-group:focus-within {
|
|
border-color: var(--nexus-neon);
|
|
background: rgba(0, 255, 153, 0.02);
|
|
box-shadow: 0 0 15px rgba(0, 255, 153, 0.15);
|
|
}
|
|
|
|
.nexus-input {
|
|
flex-grow: 1;
|
|
background: transparent;
|
|
border: none;
|
|
color: #ffffff;
|
|
font-size: 1rem;
|
|
font-family: var(--nexus-font-sans);
|
|
outline: none;
|
|
padding: 0.5rem 0;
|
|
}
|
|
|
|
.nexus-input::placeholder {
|
|
color: rgba(255, 255, 255, 0.4);
|
|
}
|
|
|
|
.btn-nexus {
|
|
padding: 0.75rem 1.25rem;
|
|
border-radius: 10px;
|
|
font-size: 0.9rem;
|
|
font-weight: 600;
|
|
cursor: pointer;
|
|
transition: all 0.2s;
|
|
border: none;
|
|
font-family: var(--nexus-font-sans);
|
|
}
|
|
|
|
.btn-nexus.primary {
|
|
background: var(--nexus-neon);
|
|
color: #000000;
|
|
}
|
|
|
|
.btn-nexus:hover:not(:disabled) {
|
|
transform: translateY(-2px);
|
|
filter: brightness(1.1);
|
|
box-shadow: 0 4px 12px rgba(0, 255, 153, 0.3);
|
|
}
|
|
|
|
.btn-nexus:disabled {
|
|
background: rgba(255, 255, 255, 0.05);
|
|
color: rgba(255, 255, 255, 0.3);
|
|
cursor: not-allowed;
|
|
}
|
|
|
|
.search-btn {
|
|
padding: 0.5rem 1.5rem !important;
|
|
font-size: 0.95rem !important;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
}
|
|
|
|
.scope-selector {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 0.75rem;
|
|
color: #A0A0A0;
|
|
font-family: var(--nexus-font-sans);
|
|
}
|
|
|
|
.nexus-select {
|
|
background: rgba(255, 255, 255, 0.02);
|
|
border: 1px solid rgba(255, 255, 255, 0.05);
|
|
color: #ffffff;
|
|
padding: 0.5rem 2.5rem 0.5rem 1rem;
|
|
border-radius: 10px;
|
|
outline: none;
|
|
cursor: pointer;
|
|
font-family: var(--nexus-font-sans);
|
|
font-size: 0.9rem;
|
|
transition: all 0.3s ease;
|
|
appearance: none;
|
|
-webkit-appearance: none;
|
|
background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
|
|
background-repeat: no-repeat;
|
|
background-position: right 1rem center;
|
|
background-size: 1em;
|
|
}
|
|
|
|
.nexus-select:focus {
|
|
border-color: var(--nexus-neon);
|
|
background-color: rgba(0, 255, 153, 0.02);
|
|
box-shadow: 0 0 10px rgba(0, 255, 153, 0.15);
|
|
}
|
|
|
|
.results-area {
|
|
min-height: 250px;
|
|
display: flex;
|
|
flex-direction: column;
|
|
justify-content: center;
|
|
}
|
|
|
|
.loading-state {
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
gap: 1.5rem;
|
|
color: rgba(255, 255, 255, 0.7);
|
|
}
|
|
|
|
.nexus-spinner {
|
|
width: 50px;
|
|
height: 50px;
|
|
border: 3px solid rgba(0, 255, 153, 0.1);
|
|
border-radius: 50%;
|
|
border-top-color: var(--nexus-neon);
|
|
animation: spin 1s linear infinite;
|
|
filter: drop-shadow(0 0 8px var(--nexus-neon));
|
|
}
|
|
|
|
.welcome-state, .empty-state {
|
|
text-align: center;
|
|
color: rgba(255, 255, 255, 0.5);
|
|
padding: 3rem 1rem;
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
}
|
|
|
|
.welcome-icon {
|
|
color: rgba(255, 255, 255, 0.25);
|
|
margin-bottom: 1.5rem;
|
|
animation: pulse 2s infinite alternate;
|
|
}
|
|
|
|
.welcome-state h3 {
|
|
color: #ffffff;
|
|
font-family: var(--nexus-font-sans);
|
|
margin: 0 0 0.5rem 0;
|
|
}
|
|
|
|
.welcome-state p, .empty-state p {
|
|
max-width: 500px;
|
|
margin: 0;
|
|
font-size: 0.95rem;
|
|
line-height: 1.6;
|
|
}
|
|
|
|
.response-container {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 2.5rem;
|
|
animation: slideUp 0.4s ease-out;
|
|
}
|
|
|
|
.response-section h4, .citations-section h4 {
|
|
font-size: 1.1rem;
|
|
text-transform: uppercase;
|
|
letter-spacing: 1px;
|
|
color: rgba(255, 255, 255, 0.5);
|
|
margin: 0 0 1rem 0;
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 0.5rem;
|
|
}
|
|
|
|
.answer-text {
|
|
font-size: 1.15rem;
|
|
line-height: 1.7;
|
|
color: #ffffff;
|
|
background: rgba(255, 255, 255, 0.02);
|
|
padding: 1.5rem;
|
|
border-radius: 12px;
|
|
border: 1px solid rgba(255, 255, 255, 0.05);
|
|
}
|
|
|
|
.citations-grid {
|
|
display: grid;
|
|
grid-template-columns: 1fr;
|
|
gap: 1.25rem;
|
|
}
|
|
|
|
.citation-card {
|
|
background: rgba(255, 255, 255, 0.01);
|
|
border: 1px solid rgba(255, 255, 255, 0.05);
|
|
border-radius: 12px;
|
|
padding: 1.25rem;
|
|
transition: all 0.3s ease;
|
|
}
|
|
|
|
.citation-card:hover {
|
|
border-color: rgba(0, 255, 153, 0.3);
|
|
transform: translateY(-2px);
|
|
background: rgba(255, 255, 255, 0.05);
|
|
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
|
|
}
|
|
|
|
.citation-header {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
margin-bottom: 0.75rem;
|
|
}
|
|
|
|
.source-badge {
|
|
font-size: 0.8rem;
|
|
font-weight: 600;
|
|
color: var(--nexus-neon);
|
|
background: rgba(0, 255, 153, 0.05);
|
|
border: 1px solid rgba(0, 255, 153, 0.2);
|
|
padding: 0.25rem 0.75rem;
|
|
border-radius: 20px;
|
|
}
|
|
|
|
.id-badge {
|
|
font-size: 0.75rem;
|
|
color: rgba(255, 255, 255, 0.4);
|
|
font-family: monospace;
|
|
}
|
|
|
|
.citation-body {
|
|
font-size: 0.95rem;
|
|
line-height: 1.5;
|
|
color: rgba(255, 255, 255, 0.8);
|
|
font-style: italic;
|
|
}
|
|
|
|
.btn-spinner {
|
|
margin: 0;
|
|
}
|
|
|
|
/* Animations */
|
|
@@keyframes fadeIn {
|
|
from { opacity: 0; transform: translateY(15px); }
|
|
to { opacity: 1; transform: translateY(0); }
|
|
}
|
|
|
|
@@keyframes slideUp {
|
|
from { opacity: 0; transform: translateY(10px); }
|
|
to { opacity: 1; transform: translateY(0); }
|
|
}
|
|
|
|
@@keyframes spin {
|
|
0% { transform: rotate(0deg); }
|
|
100% { transform: rotate(360deg); }
|
|
}
|
|
|
|
@@keyframes pulse {
|
|
0% { transform: scale(0.95); opacity: 0.7; }
|
|
100% { transform: scale(1.05); opacity: 1; }
|
|
}
|
|
</style>
|
|
|
|
@code {
|
|
private string _question = string.Empty;
|
|
private string _selectedBookId = string.Empty;
|
|
private bool _isLoading;
|
|
private bool _hasSearched;
|
|
private GroundedResponseDto? _response;
|
|
private List<LastReadBookDto>? _books;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
try
|
|
{
|
|
_books = await Http.GetFromJsonAsync<List<LastReadBookDto>>("api/library/books");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"[Intelligence] Failed to load books for scope selector: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private async Task HandleKeyUp(KeyboardEventArgs e)
|
|
{
|
|
if (e.Key == "Enter" && !string.IsNullOrWhiteSpace(_question) && !_isLoading)
|
|
{
|
|
await AskQuestionAsync();
|
|
}
|
|
}
|
|
|
|
private async Task AskQuestionAsync()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(_question) || _isLoading) return;
|
|
|
|
_isLoading = true;
|
|
_hasSearched = true;
|
|
_response = null;
|
|
StateHasChanged();
|
|
|
|
try
|
|
{
|
|
Guid? ebookId = null;
|
|
if (!string.IsNullOrEmpty(_selectedBookId) && Guid.TryParse(_selectedBookId, out var parsedId))
|
|
{
|
|
ebookId = parsedId;
|
|
}
|
|
|
|
var authState = await AuthStateProvider.GetAuthenticationStateAsync();
|
|
var tenantId = authState.User.FindFirst("TenantId")?.Value ?? "global";
|
|
|
|
var result = await KnowledgeService.AskQuestionAsync(_question, tenantId, ebookId);
|
|
if (result.IsSuccess)
|
|
{
|
|
_response = result.Value;
|
|
}
|
|
else
|
|
{
|
|
_response = new GroundedResponseDto
|
|
{
|
|
Answer = $"Error: {result.Errors.FirstOrDefault()?.Message ?? "An error occurred."}",
|
|
Citations = new List<CitationDto>()
|
|
};
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_response = new GroundedResponseDto
|
|
{
|
|
Answer = $"Network/API Error: {ex.Message}",
|
|
Citations = new List<CitationDto>()
|
|
};
|
|
}
|
|
finally
|
|
{
|
|
_isLoading = false;
|
|
StateHasChanged();
|
|
}
|
|
}
|
|
}
|