enhance rag retrieval + summary

This commit is contained in:
Adrien
2026-04-07 22:39:28 +02:00
parent 0cf318f0a7
commit aee6a9dfba
34 changed files with 2306 additions and 279 deletions
+239
View File
@@ -0,0 +1,239 @@
<template>
<div class="book-panel">
<div class="book-panel-header">
<span class="book-panel-title">{{ bookTitle || 'Book' }} p.&nbsp;{{ page }}</span>
<div class="book-panel-nav">
<button class="nav-btn" :disabled="page <= 1" @click="emit('navigate', page - 1)">&#8592;</button>
<button class="nav-btn" @click="emit('navigate', page + 1)">&#8594;</button>
</div>
<button class="close-btn" @click="emit('close')" title="Close">&#x2715;</button>
</div>
<div class="book-panel-body">
<div v-if="loading" class="panel-loading">
<div class="spinner spinner-dark" style="width:24px;height:24px;margin:0 auto 0.5rem;"></div>
<p>Loading page {{ page }}</p>
</div>
<div v-else-if="error" class="panel-error">{{ error }}</div>
<div v-else class="markdown-body" v-html="renderedHtml"></div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, watch, onMounted, onUnmounted } from 'vue'
import { api } from '@/services/api'
const props = defineProps<{
bookId: string
page: number
bookTitle?: string
}>()
const emit = defineEmits<{
close: []
navigate: [page: number]
}>()
const loading = ref(false)
const error = ref<string | null>(null)
const renderedHtml = ref('')
let activeBlobUrls: string[] = []
onMounted(() => loadPage(props.page))
watch(() => [props.bookId, props.page], () => loadPage(props.page))
onUnmounted(() => {
activeBlobUrls.forEach(u => URL.revokeObjectURL(u))
})
async function loadPage(page: number) {
loading.value = true
error.value = null
renderedHtml.value = ''
activeBlobUrls.forEach(u => URL.revokeObjectURL(u))
activeBlobUrls = []
try {
const res = await api.get<string>(`/books/${props.bookId}/pages/${page}/html`, {
headers: { Accept: 'text/html' },
responseType: 'text'
})
renderedHtml.value = await resolveImages(res.data)
} catch (e: any) {
error.value = e.message ?? 'Failed to load page.'
} finally {
loading.value = false
}
}
async function resolveImages(html: string): Promise<string> {
const srcPattern = /src="(\/api\/v1\/figures\/[^"]+)"/g
const matches = [...html.matchAll(srcPattern)]
if (matches.length === 0) return html
const unique = [...new Set(matches.map(m => m[1]))]
const blobMap: Record<string, string> = {}
await Promise.all(
unique.map(async (src) => {
try {
const res = await api.get(src.replace(/^\/api\/v1/, ''), { responseType: 'blob' })
const blobUrl = URL.createObjectURL(res.data)
activeBlobUrls.push(blobUrl)
blobMap[src] = blobUrl
} catch {
// leave original src
}
})
)
return html.replace(/src="(\/api\/v1\/figures\/[^"]+)"/g, (_, src) =>
blobMap[src] ? `src="${blobMap[src]}"` : `src="${src}"`
)
}
</script>
<style scoped>
.book-panel {
display: flex;
flex-direction: column;
height: 100%;
background: white;
border-left: 1px solid #e2e8f0;
border-radius: 0 10px 10px 0;
overflow: hidden;
}
.book-panel-header {
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.6rem 0.75rem;
background: #f7fafc;
border-bottom: 1px solid #e2e8f0;
flex-shrink: 0;
}
.book-panel-title {
flex: 1;
font-size: 0.8rem;
font-weight: 600;
color: #2b6cb0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.book-panel-nav {
display: flex;
gap: 0.25rem;
}
.nav-btn {
width: 1.75rem;
height: 1.75rem;
border: 1px solid #cbd5e0;
border-radius: 5px;
background: white;
cursor: pointer;
font-size: 0.85rem;
display: flex;
align-items: center;
justify-content: center;
transition: background 0.15s;
}
.nav-btn:hover:not(:disabled) { background: #ebf8ff; border-color: #3182ce; }
.nav-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.close-btn {
width: 1.75rem;
height: 1.75rem;
border: none;
border-radius: 5px;
background: none;
cursor: pointer;
font-size: 1rem;
color: #718096;
display: flex;
align-items: center;
justify-content: center;
transition: background 0.15s, color 0.15s;
}
.close-btn:hover { background: #fed7d7; color: #742a2a; }
.book-panel-body {
flex: 1;
overflow-y: auto;
padding: 1rem 1.25rem;
}
.panel-loading {
text-align: center;
padding: 2rem;
color: #718096;
font-size: 0.875rem;
}
.panel-error {
padding: 1rem;
background: #fff5f5;
border: 1px solid #fed7d7;
color: #742a2a;
border-radius: 6px;
font-size: 0.875rem;
}
.markdown-body {
font-size: 0.9rem;
line-height: 1.75;
color: #2d3748;
}
.markdown-body :deep(h1),
.markdown-body :deep(h2),
.markdown-body :deep(h3) {
color: #1a365d;
font-weight: 600;
margin: 1.25rem 0 0.5rem;
}
.markdown-body :deep(h2) { font-size: 1.05rem; border-bottom: 1px solid #e2e8f0; padding-bottom: 0.3rem; }
.markdown-body :deep(h3) { font-size: 0.95rem; }
.markdown-body :deep(p) { margin: 0.6rem 0; }
.markdown-body :deep(img) {
max-width: 100%;
border-radius: 6px;
display: block;
margin: 0.75rem auto;
box-shadow: 0 1px 4px rgba(0,0,0,0.12);
}
.markdown-body :deep(ul),
.markdown-body :deep(ol) { padding-left: 1.4rem; margin: 0.5rem 0; }
.markdown-body :deep(code) {
background: #f7fafc;
border: 1px solid #e2e8f0;
border-radius: 3px;
padding: 0.1em 0.3em;
font-size: 0.85em;
}
.markdown-body :deep(blockquote) {
border-left: 3px solid #3182ce;
padding-left: 0.75rem;
color: #4a5568;
margin: 0.5rem 0;
}
.markdown-body :deep(table) {
width: 100%;
border-collapse: collapse;
font-size: 0.875em;
margin: 0.75rem 0;
}
.markdown-body :deep(th),
.markdown-body :deep(td) {
border: 1px solid #e2e8f0;
padding: 0.35rem 0.6rem;
text-align: left;
}
.markdown-body :deep(th) { background: #f7fafc; font-weight: 600; }
</style>
+110 -6
View File
@@ -3,22 +3,30 @@
<div class="message-bubble" :class="isUser ? 'bubble-user' : 'bubble-assistant'">
<div class="message-role">{{ isUser ? 'You' : 'AI Teacher' }}</div>
<div v-if="isUser" class="message-content">{{ message.content }}</div>
<div v-else class="message-content message-content--markdown" v-html="renderedContent"></div>
<div v-else class="message-content message-content--markdown" v-html="renderedWithBadges" @click="onContentClick"></div>
<!-- Sources for assistant messages -->
<div v-if="!isUser && message.sources && message.sources.length > 0" class="message-sources">
<div class="sources-label">Sources:</div>
<div class="source-list">
<div class="source-list" ref="sourceListEl">
<!-- TEXT sources -->
<div
v-for="(source, idx) in textSources"
:key="'text-' + idx"
class="source-item"
:class="{ 'source-item--active': activeRef === source.refLabel }"
:data-ref-label="source.refLabel"
>
<div class="source-chip source-chip--text">
<div
class="source-chip source-chip--text"
:class="{ 'source-chip--clickable': source.bookId && source.page }"
@click="source.bookId && source.page ? emit('open-source', source.bookId, source.page) : undefined"
>
<span class="source-icon">📖</span>
<span v-if="source.refLabel" class="source-ref-label">{{ source.refLabel }}</span>
<span class="source-book-title">{{ source.bookTitle }}</span>
<span v-if="source.page" class="source-page">p.&nbsp;{{ source.page }}</span>
<span v-if="source.bookId && source.page" class="source-open-hint"></span>
</div>
<div v-if="source.chunkText" class="source-chunk">{{ source.chunkText }}</div>
</div>
@@ -28,12 +36,20 @@
v-for="(source, idx) in figureSources"
:key="'fig-' + idx"
class="source-item source-item--figure"
:class="{ 'source-item--active': activeRef === source.refLabel }"
:data-ref-label="source.refLabel"
>
<div class="source-chip source-chip--figure">
<div
class="source-chip source-chip--figure"
:class="{ 'source-chip--clickable': source.bookId && source.page }"
@click="source.bookId && source.page ? emit('open-source', source.bookId, source.page) : undefined"
>
<span class="source-icon">🖼</span>
<span v-if="source.refLabel" class="source-ref-label source-ref-label--figure">{{ source.refLabel }}</span>
<span class="source-figure-label">{{ source.label || 'Figure' }}</span>
<span v-if="source.page" class="source-page">p.&nbsp;{{ source.page }}</span>
<span v-if="source.figureType" class="source-figure-type">{{ formatFigureType(source.figureType) }}</span>
<span v-if="source.bookId && source.page" class="source-open-hint"></span>
</div>
<div v-if="source.caption" class="source-caption">{{ source.caption }}</div>
<div class="source-figure-image">
@@ -55,7 +71,7 @@
</template>
<script setup lang="ts">
import { computed } from 'vue'
import { computed, ref } from 'vue'
import { marked } from 'marked'
import type { ChatMessage, ChatSource } from '@/stores/chatStore'
@@ -63,8 +79,43 @@ const props = defineProps<{
message: ChatMessage
}>()
const emit = defineEmits<{
'open-source': [bookId: string, page: number]
}>()
const isUser = computed(() => props.message.role === 'USER')
const renderedContent = computed(() => marked.parse(props.message.content) as string)
const activeRef = ref<string | null>(null)
const sourceListEl = ref<HTMLElement | null>(null)
/** Replaces [S1]/[F1]-style labels in the rendered HTML with clickable badges. */
const renderedWithBadges = computed(() => {
const html = marked.parse(props.message.content) as string
return html.replace(/\[(S|F)\d+\]/g, (match) => {
const inner = match.slice(1, -1) // e.g. "S1"
return `<span class="citation-badge" data-ref="${inner}" title="Jump to source ${inner}">${match}</span>`
})
})
function onContentClick(e: MouseEvent) {
const target = e.target as HTMLElement
if (!target.classList.contains('citation-badge')) return
const label = target.getAttribute('data-ref') // e.g. "S1" or "F1"
if (!label) return
activeRef.value = activeRef.value === label ? null : label
// Scroll to the matching source chip
const sourceEl = sourceListEl.value?.querySelector(`[data-ref-label="${label}"]`) as HTMLElement | null
sourceEl?.scrollIntoView({ behavior: 'smooth', block: 'start' })
// Open the book at the referenced page
const allSources = props.message.sources ?? []
const source = allSources.find((s: ChatSource) => s.refLabel === label)
if (source?.bookId && source.page) {
emit('open-source', source.bookId, source.page)
}
}
const textSources = computed(() =>
(props.message.sources ?? []).filter((s: ChatSource) => s.type === 'TEXT' || !s.type)
@@ -255,6 +306,22 @@ function formatTime(iso: string): string {
border: 1px solid #bee3f8;
}
.source-chip--clickable {
cursor: pointer;
transition: background 0.15s, border-color 0.15s;
}
.source-chip--clickable:hover {
background: #bee3f8;
border-color: #90cdf4;
}
.source-open-hint {
font-size: 0.75rem;
color: #3182ce;
margin-left: 0.1rem;
}
.source-chip--figure {
background: #f0fff4;
border: 1px solid #9ae6b4;
@@ -322,6 +389,43 @@ function formatTime(iso: string): string {
font-style: italic;
}
.message-content--markdown :deep(.citation-badge) {
display: inline-block;
background: #ebf8ff;
border: 1px solid #90cdf4;
border-radius: 3px;
padding: 0 0.3em;
font-size: 0.78em;
font-weight: 600;
color: #2b6cb0;
cursor: pointer;
user-select: none;
transition: background 0.15s;
}
.message-content--markdown :deep(.citation-badge:hover) {
background: #bee3f8;
}
.source-item--active {
outline: 2px solid #4299e1;
border-radius: 6px;
}
.source-ref-label {
font-size: 0.72rem;
font-weight: 700;
background: #bee3f8;
color: #2b6cb0;
border-radius: 3px;
padding: 0 0.3rem;
}
.source-ref-label--figure {
background: #9ae6b4;
color: #276749;
}
.message-timestamp {
font-size: 0.7rem;
opacity: 0.6;
+2
View File
@@ -4,8 +4,10 @@ import { api } from '@/services/api'
export interface ChatSource {
type: 'TEXT' | 'FIGURE'
bookId?: string
bookTitle: string
page: number | null
refLabel?: string
// TEXT-specific
chunkText?: string
// FIGURE-specific
+45
View File
@@ -10,11 +10,14 @@ export interface Topic {
}
export interface SourceReference {
bookId: string | null
bookTitle: string
page: number | null
}
export interface TopicSummary {
id: string
summaryNumber: number
topicId: string
topicName: string
summary: string
@@ -22,12 +25,20 @@ export interface TopicSummary {
generatedAt: string
}
export interface SavedSummaryItem {
id: string
summaryNumber: number
generatedAt: string
}
export const useTopicStore = defineStore('topics', () => {
const topics = ref<Topic[]>([])
const activeSummary = ref<TopicSummary | null>(null)
const activeSummaryTopicId = ref<string | null>(null)
const summaryList = ref<SavedSummaryItem[]>([])
const loading = ref(false)
const summaryLoading = ref(false)
const summaryListLoading = ref(false)
const error = ref<string | null>(null)
async function fetchTopics() {
@@ -43,6 +54,36 @@ export const useTopicStore = defineStore('topics', () => {
}
}
async function fetchSummaries(topicId: string) {
summaryListLoading.value = true
summaryList.value = []
error.value = null
try {
const response = await api.get<SavedSummaryItem[]>(`/topics/${topicId}/summaries`)
summaryList.value = response.data
} catch (err: any) {
error.value = err.message
} finally {
summaryListLoading.value = false
}
}
async function fetchSummaryDetail(topicId: string, summaryId: string): Promise<TopicSummary | null> {
summaryLoading.value = true
activeSummary.value = null
error.value = null
try {
const response = await api.get<TopicSummary>(`/topics/${topicId}/summaries/${summaryId}`)
activeSummary.value = response.data
return response.data
} catch (err: any) {
error.value = err.message
return null
} finally {
summaryLoading.value = false
}
}
async function generateSummary(topicId: string): Promise<TopicSummary | null> {
summaryLoading.value = true
activeSummaryTopicId.value = topicId
@@ -65,10 +106,14 @@ export const useTopicStore = defineStore('topics', () => {
topics,
activeSummary,
activeSummaryTopicId,
summaryList,
loading,
summaryLoading,
summaryListLoading,
error,
fetchTopics,
fetchSummaries,
fetchSummaryDetail,
generateSummary
}
})
+103 -123
View File
@@ -3,27 +3,10 @@
<h1 class="page-title">Knowledge Chat</h1>
<p class="page-subtitle">Ask questions grounded in your uploaded medical textbooks.</p>
<!-- Step 1: Topic Selection -->
<div v-if="!chatStore.session && !selectedTopic" class="topic-selection">
<h2 class="section-title">Select a Topic</h2>
<div class="topic-grid">
<button
v-for="topic in topicStore.topics"
:key="topic.id"
:class="['topic-tile', { 'topic-tile-freeform': topic.id === 'free-form' }]"
@click="handleTopicSelect(topic)"
>
<span class="topic-tile-name">{{ topic.name }}</span>
<span v-if="topic.id === 'free-form'" class="topic-tile-hint">Any neurosurgery question</span>
</button>
</div>
</div>
<!-- Step 2: Topic selected previous sessions + new chat -->
<div v-else-if="!chatStore.session && selectedTopic" class="session-setup card">
<!-- Session selection -->
<div v-if="!chatStore.session" class="session-setup card">
<div class="setup-header">
<button class="btn-back" @click="handleBack"> Topics</button>
<h2 class="section-title">{{ selectedTopic.name }}</h2>
<h2 class="section-title">Free-form Chat</h2>
</div>
<div v-if="chatStore.error" class="error-banner">{{ chatStore.error }}</div>
@@ -71,56 +54,74 @@
</div>
</div>
<!-- Messages Area -->
<div class="messages-container" ref="messagesContainer">
<div v-if="chatStore.loading && chatStore.messages.length === 0" class="empty-state">
<div class="spinner spinner-dark" style="width:32px;height:32px;margin:0 auto 1rem;"></div>
<p class="empty-state-text">Loading messages...</p>
</div>
<!-- Chat + Reader split -->
<div class="chat-reader-split">
<!-- Messages + Input -->
<div class="chat-column">
<!-- Messages Area -->
<div class="messages-container" ref="messagesContainer">
<div v-if="chatStore.loading && chatStore.messages.length === 0" class="empty-state">
<div class="spinner spinner-dark" style="width:32px;height:32px;margin:0 auto 1rem;"></div>
<p class="empty-state-text">Loading messages...</p>
</div>
<div v-else-if="chatStore.messages.length === 0" class="empty-state">
<div class="empty-state-icon">💬</div>
<p class="empty-state-text">No messages yet</p>
<p class="empty-state-hint">Ask a question about the uploaded books below.</p>
</div>
<div v-else-if="chatStore.messages.length === 0" class="empty-state">
<div class="empty-state-icon">💬</div>
<p class="empty-state-text">No messages yet</p>
<p class="empty-state-hint">Ask a question about the uploaded books below.</p>
</div>
<div v-else class="messages-list">
<ChatMessage
v-for="message in chatStore.messages"
:key="message.id"
:message="message"
/>
<div v-if="chatStore.sending" class="typing-indicator">
<div class="typing-bubble">
<span></span><span></span><span></span>
<div v-else class="messages-list">
<ChatMessage
v-for="message in chatStore.messages"
:key="message.id"
:message="message"
@open-source="handleOpenSource"
/>
<div v-if="chatStore.sending" class="typing-indicator">
<div class="typing-bubble">
<span></span><span></span><span></span>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Input Area -->
<div class="input-area card">
<div v-if="chatStore.error" class="error-banner">{{ chatStore.error }}</div>
<div class="input-row">
<textarea
v-model="inputText"
class="message-input"
placeholder="Ask a question about your uploaded books..."
rows="2"
:disabled="chatStore.sending"
@keydown.enter.exact.prevent="handleSend"
@keydown.enter.shift.exact="inputText += '\n'"
></textarea>
<button
class="btn btn-primary send-btn"
:disabled="!inputText.trim() || chatStore.sending"
@click="handleSend"
>
<span v-if="chatStore.sending" class="spinner"></span>
<span v-else>Send</span>
</button>
<!-- Input Area -->
<div class="input-area card">
<div v-if="chatStore.error" class="error-banner">{{ chatStore.error }}</div>
<div class="input-row">
<textarea
v-model="inputText"
class="message-input"
placeholder="Ask a question about your uploaded books..."
rows="2"
:disabled="chatStore.sending"
@keydown.enter.exact.prevent="handleSend"
@keydown.enter.shift.exact="inputText += '\n'"
></textarea>
<button
class="btn btn-primary send-btn"
:disabled="!inputText.trim() || chatStore.sending"
@click="handleSend"
>
<span v-if="chatStore.sending" class="spinner"></span>
<span v-else>Send</span>
</button>
</div>
<p class="input-hint">Press Enter to send, Shift+Enter for new line.</p>
</div>
</div>
<p class="input-hint">Press Enter to send, Shift+Enter for new line.</p>
<!-- Inline book reader panel -->
<BookPagePanel
v-if="readerPanel"
:book-id="readerPanel.bookId"
:page="readerPanel.page"
:book-title="readerPanel.bookTitle"
class="reader-panel"
@close="readerPanel = null"
@navigate="(p) => readerPanel && (readerPanel.page = p)"
/>
</div>
</div>
</div>
@@ -130,8 +131,10 @@
import { ref, nextTick, onMounted, watch, inject } from 'vue'
import { useChatStore } from '@/stores/chatStore'
import { useTopicStore } from '@/stores/topicStore'
import { useBookStore } from '@/stores/bookStore'
import type { ChatSession } from '@/stores/chatStore'
import ChatMessage from '@/components/ChatMessage.vue'
import BookPagePanel from '@/components/BookPagePanel.vue'
interface Topic {
id: string
@@ -142,6 +145,7 @@ interface Topic {
const chatStore = useChatStore()
const topicStore = useTopicStore()
const bookStore = useBookStore()
const showToast = inject<(msg: string, type?: 'error' | 'success') => void>('showToast')
const selectedTopic = ref<Topic | null>(null)
@@ -150,10 +154,22 @@ const loadingTopicSessions = ref(false)
const inputText = ref('')
const messagesContainer = ref<HTMLElement | null>(null)
interface ReaderPanel { bookId: string; page: number; bookTitle?: string }
const readerPanel = ref<ReaderPanel | null>(null)
function handleOpenSource(bookId: string, page: number) {
const book = bookStore.books.find(b => b.id === bookId)
readerPanel.value = { bookId, page, bookTitle: book?.title }
}
onMounted(async () => {
if (topicStore.topics.length === 0) {
await topicStore.fetchTopics()
}
const freeForm = topicStore.topics.find((t) => t.id === 'free-form')
if (freeForm) {
await handleTopicSelect(freeForm)
}
})
watch(
@@ -189,11 +205,6 @@ async function handleTopicSelect(topic: Topic) {
loadingTopicSessions.value = false
}
function handleBack() {
selectedTopic.value = null
topicSessions.value = []
}
async function handleNewChat() {
const ok = await chatStore.createSession(selectedTopic.value!.id)
if (!ok) {
@@ -207,9 +218,7 @@ async function handleResumeSession(session: ChatSession) {
}
function handleLeaveSession() {
// Leave without deleting — session stays in DB and will appear in "Previous Chats"
chatStore.leaveSession()
// Refresh the sessions list for the current topic
if (selectedTopic.value) {
loadingTopicSessions.value = true
chatStore.fetchSessionsByTopic(selectedTopic.value.id).then((sessions) => {
@@ -231,12 +240,6 @@ async function handleSend() {
</script>
<style scoped>
.topic-selection {
display: flex;
flex-direction: column;
gap: 1.25rem;
}
.section-title {
font-size: 1.1rem;
font-weight: 600;
@@ -244,52 +247,6 @@ async function handleSend() {
margin: 0;
}
.topic-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
gap: 0.75rem;
}
.topic-tile {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 0.25rem;
padding: 1rem 1.1rem;
background: white;
border: 1px solid #e2e8f0;
border-radius: 8px;
cursor: pointer;
text-align: left;
transition: border-color 0.15s, box-shadow 0.15s;
}
.topic-tile:hover {
border-color: #3182ce;
box-shadow: 0 2px 8px rgba(49, 130, 206, 0.15);
}
.topic-tile-freeform {
border-style: dashed;
border-color: #a0aec0;
}
.topic-tile-freeform:hover {
border-color: #718096;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}
.topic-tile-name {
font-size: 0.9rem;
font-weight: 600;
color: #2d3748;
}
.topic-tile-hint {
font-size: 0.78rem;
color: #a0aec0;
}
.session-setup {
max-width: 540px;
}
@@ -381,6 +338,29 @@ async function handleSend() {
min-height: 500px;
}
.chat-reader-split {
display: flex;
flex: 1;
min-height: 0;
gap: 0;
}
.chat-column {
display: flex;
flex-direction: column;
flex: 1;
min-width: 0;
gap: 1rem;
}
.reader-panel {
width: 420px;
flex-shrink: 0;
border-radius: 10px;
margin-left: 1rem;
box-shadow: -2px 0 8px rgba(0, 0, 0, 0.07);
}
.session-bar {
display: flex;
align-items: center;
+377 -54
View File
@@ -1,7 +1,7 @@
<template>
<div class="topics-view">
<h1 class="page-title">Topics</h1>
<p class="page-subtitle">Select a topic to generate an AI-powered summary from uploaded books.</p>
<p class="page-subtitle">Select a topic to view or generate an AI-powered summary from uploaded books.</p>
<!-- Loading state -->
<div v-if="topicStore.loading" class="empty-state">
@@ -18,83 +18,198 @@
</div>
<div v-else class="topics-layout">
<!-- Topic Grid -->
<div class="topic-grid">
<TopicCard
v-for="topic in topicStore.topics"
:key="topic.id"
:topic="topic"
:is-generating="topicStore.activeSummaryTopicId === topic.id"
@generate="handleGenerate"
/>
</div>
<div class="topics-main">
<!-- Summary Panel -->
<div v-if="topicStore.summaryLoading" class="summary-panel card">
<div class="summary-loading">
<div class="spinner spinner-dark" style="width:36px;height:36px;margin:0 auto 1rem;"></div>
<p class="summary-loading-text">Generating summary from uploaded books...</p>
<p class="summary-loading-hint">This may take up to 30 seconds.</p>
</div>
</div>
<!-- Summary history list -->
<div v-if="selectedTopicId" class="history-panel card">
<div class="history-header">
<span class="history-title">Saved summaries</span>
<button class="btn btn-primary btn-sm" :disabled="topicStore.summaryLoading" @click="handleGenerate(selectedTopicId!)">
<span v-if="topicStore.summaryLoading" class="spinner" style="width:14px;height:14px;display:inline-block;vertical-align:middle;margin-right:4px;"></span>
Generate New
</button>
</div>
<div v-else-if="summaryError" class="summary-panel card summary-error">
<h2 class="summary-topic-name">Summary Error</h2>
<p class="error-text">{{ summaryError }}</p>
<p v-if="isNoBooks" class="no-books-hint">
Please
<RouterLink to="/">upload and process at least one book</RouterLink>
first.
</p>
</div>
<div v-if="topicStore.summaryListLoading" class="history-loading">
<div class="spinner spinner-dark" style="width:20px;height:20px;margin-right:8px;display:inline-block;vertical-align:middle;"></div>
Loading...
</div>
<div v-else-if="topicStore.activeSummary" class="summary-panel card">
<div class="summary-header">
<h2 class="summary-topic-name">{{ topicStore.activeSummary.topicName }}</h2>
<span class="summary-timestamp">{{ formatDate(topicStore.activeSummary.generatedAt) }}</span>
</div>
<div v-else-if="topicStore.summaryList.length === 0" class="history-empty">
No summaries yet. Click "Generate New" to create one.
</div>
<div class="summary-text">{{ topicStore.activeSummary.summary }}</div>
<div v-if="topicStore.activeSummary.sources.length > 0" class="sources-section">
<button class="sources-toggle" @click="showSources = !showSources">
Sources ({{ topicStore.activeSummary.sources.length }})
<span>{{ showSources ? '▲' : '▼' }}</span>
</button>
<div v-if="showSources" class="sources-list">
<div
v-for="(source, idx) in topicStore.activeSummary.sources"
:key="idx"
class="source-chip"
<div v-else class="history-list">
<button
v-for="item in topicStore.summaryList"
:key="item.id"
class="history-chip"
:class="{ 'history-chip--active': topicStore.activeSummary?.id === item.id }"
@click="handleLoadSummary(item)"
>
<span class="source-book">{{ source.bookTitle }}</span>
<span v-if="source.page" class="source-page">p. {{ source.page }}</span>
</div>
Summary #{{ item.summaryNumber }}
<span class="history-chip-date">· {{ formatDateShort(item.generatedAt) }}</span>
</button>
</div>
</div>
<div v-else class="no-sources">
No source citations available for this summary.
<!-- Summary Panel -->
<div v-if="topicStore.summaryLoading" class="summary-panel card">
<div class="summary-loading">
<div class="spinner spinner-dark" style="width:36px;height:36px;margin:0 auto 1rem;"></div>
<p class="summary-loading-text">Generating summary from uploaded books...</p>
<p class="summary-loading-hint">This may take up to 30 seconds.</p>
</div>
</div>
</div>
<div v-else-if="summaryError" class="summary-panel card summary-error">
<h2 class="summary-topic-name">Summary Error</h2>
<p class="error-text">{{ summaryError }}</p>
<p v-if="isNoBooks" class="no-books-hint">
Please
<RouterLink to="/">upload and process at least one book</RouterLink>
first.
</p>
</div>
<div v-else-if="!topicStore.activeSummary" class="summary-panel card summary-placeholder">
<p class="summary-placeholder-text">
{{ selectedTopicId ? 'Select a saved summary or generate a new one.' : 'Select a topic to get started.' }}
</p>
</div>
<div v-else class="summary-panel card">
<div class="summary-header">
<h2 class="summary-topic-name">{{ topicStore.activeSummary.topicName }}</h2>
<div class="summary-meta">
<span v-if="topicStore.activeSummary.summaryNumber" class="summary-number">
Summary #{{ topicStore.activeSummary.summaryNumber }}
</span>
<span class="summary-timestamp">{{ formatDate(topicStore.activeSummary.generatedAt) }}</span>
</div>
</div>
<div class="summary-text summary-text--markdown" v-html="renderedSummary" @click="handleSummaryClick"></div>
<div ref="sourcesSection" v-if="topicStore.activeSummary.sources.length > 0" class="sources-section">
<button class="sources-toggle" @click="showSources = !showSources">
Sources ({{ topicStore.activeSummary.sources.length }})
<span>{{ showSources ? '▲' : '▼' }}</span>
</button>
<div v-if="showSources" class="sources-list">
<div
v-for="(source, idx) in topicStore.activeSummary.sources"
:key="idx"
class="source-chip"
:class="{ 'source-chip--clickable': source.bookId && source.page }"
@click="source.bookId && source.page ? handleOpenSource(source.bookId, source.page) : undefined"
>
<span class="source-icon">📖</span>
<span class="source-book">{{ source.bookTitle }}</span>
<span v-if="source.page" class="source-page">p.&nbsp;{{ source.page }}</span>
<span v-if="source.bookId && source.page" class="source-open-hint"></span>
</div>
</div>
<BookPagePanel
v-if="readerPanel"
:book-id="readerPanel.bookId"
:page="readerPanel.page"
:book-title="readerPanel.bookTitle"
class="reader-panel"
@close="readerPanel = null"
@navigate="(p) => readerPanel && (readerPanel.page = p)"
/>
</div>
<div v-else class="no-sources">
No source citations available for this summary.
</div>
</div>
<!-- Topic Grid -->
<div class="topic-grid">
<TopicCard
v-for="topic in summaryTopics"
:key="topic.id"
:topic="topic"
:is-generating="topicStore.activeSummaryTopicId === topic.id"
:is-selected="selectedTopicId === topic.id"
@generate="handleTopicClick"
/>
</div>
</div><!-- end topics-main -->
</div>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted, inject } from 'vue'
import { ref, computed, onMounted, inject } from 'vue'
import { marked } from 'marked'
import { RouterLink } from 'vue-router'
import { useTopicStore } from '@/stores/topicStore'
import { useTopicStore, type SavedSummaryItem } from '@/stores/topicStore'
import { useBookStore } from '@/stores/bookStore'
import TopicCard from '@/components/TopicCard.vue'
import BookPagePanel from '@/components/BookPagePanel.vue'
const topicStore = useTopicStore()
const bookStore = useBookStore()
const showToast = inject<(msg: string, type?: 'error' | 'success') => void>('showToast')
const showSources = ref(true)
const summaryError = ref<string | null>(null)
const isNoBooks = ref(false)
const sourcesSection = ref<HTMLElement | null>(null)
const selectedTopicId = ref<string | null>(null)
interface ReaderPanel { bookId: string; page: number; bookTitle?: string }
const readerPanel = ref<ReaderPanel | null>(null)
const summaryTopics = computed(() => topicStore.topics.filter(t => t.id !== 'free-form'))
const renderedSummary = computed(() => {
if (!topicStore.activeSummary) return ''
const html = marked.parse(topicStore.activeSummary.summary) as string
return html.replace(/\[S(\d+)\]/g, '<span class="source-ref">[S$1]</span>')
})
function handleSummaryClick(e: MouseEvent) {
if ((e.target as HTMLElement).classList.contains('source-ref')) {
showSources.value = true
sourcesSection.value?.scrollIntoView({ behavior: 'smooth', block: 'start' })
}
}
function handleOpenSource(bookId: string, page: number) {
const book = bookStore.books.find(b => b.id === bookId)
readerPanel.value = { bookId, page, bookTitle: book?.title }
showSources.value = true
}
async function handleTopicClick(topicId: string) {
if (selectedTopicId.value !== topicId) {
selectedTopicId.value = topicId
topicStore.activeSummary = null
summaryError.value = null
await topicStore.fetchSummaries(topicId)
// Auto-load the latest summary if any exist
const list = topicStore.summaryList
if (list.length > 0) {
await topicStore.fetchSummaryDetail(topicId, list[list.length - 1].id)
}
}
}
async function handleLoadSummary(item: SavedSummaryItem) {
if (!selectedTopicId.value) return
summaryError.value = null
await topicStore.fetchSummaryDetail(selectedTopicId.value, item.id)
}
onMounted(async () => {
await topicStore.fetchTopics()
if (bookStore.books.length === 0) {
await bookStore.fetchBooks()
}
})
async function handleGenerate(topicId: string) {
@@ -109,27 +224,122 @@ async function handleGenerate(topicId: string) {
summaryError.value.toLowerCase().includes('no books') ||
summaryError.value.toLowerCase().includes('knowledge source')
showToast?.(summaryError.value, 'error')
} else {
// Refresh the history list to include the newly saved summary
await topicStore.fetchSummaries(topicId)
}
}
function formatDate(iso: string): string {
return new Date(iso).toLocaleString()
}
function formatDateShort(iso: string): string {
return new Date(iso).toLocaleDateString(undefined, { month: 'short', day: 'numeric' })
}
</script>
<style scoped>
.topics-layout {
display: flex;
gap: 2rem;
}
.topics-main {
flex: 1;
min-width: 0;
display: flex;
flex-direction: column;
gap: 2rem;
}
.reader-panel {
margin-top: 1rem;
height: 600px;
min-height: 400px;
border-radius: 10px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.07);
}
.topic-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 1rem;
}
/* History panel */
.history-panel {
border-top: 3px solid #805ad5;
padding: 1rem;
}
.history-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 0.75rem;
}
.history-title {
font-size: 0.875rem;
font-weight: 600;
color: #553c9a;
}
.btn-sm {
font-size: 0.8rem;
padding: 0.3rem 0.75rem;
}
.history-loading {
font-size: 0.85rem;
color: #718096;
display: flex;
align-items: center;
}
.history-empty {
font-size: 0.85rem;
color: #a0aec0;
font-style: italic;
}
.history-list {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
.history-chip {
background: #faf5ff;
border: 1px solid #d6bcfa;
border-radius: 6px;
padding: 0.3rem 0.75rem;
font-size: 0.8rem;
font-weight: 500;
color: #553c9a;
cursor: pointer;
transition: background 0.15s, border-color 0.15s;
}
.history-chip:hover {
background: #e9d8fd;
border-color: #b794f4;
}
.history-chip--active {
background: #805ad5;
border-color: #805ad5;
color: #fff;
}
.history-chip-date {
font-weight: 400;
color: inherit;
opacity: 0.75;
}
/* Summary panel */
.summary-panel {
border-top: 3px solid #3182ce;
}
@@ -170,6 +380,22 @@ function formatDate(iso: string): string {
color: #1a365d;
}
.summary-meta {
display: flex;
align-items: baseline;
gap: 0.5rem;
}
.summary-number {
font-size: 0.8rem;
font-weight: 600;
color: #805ad5;
background: #faf5ff;
border: 1px solid #d6bcfa;
border-radius: 4px;
padding: 0.1rem 0.4rem;
}
.summary-timestamp {
font-size: 0.8rem;
color: #a0aec0;
@@ -179,10 +405,60 @@ function formatDate(iso: string): string {
font-size: 0.95rem;
line-height: 1.7;
color: #2d3748;
white-space: pre-wrap;
margin-bottom: 1rem;
}
.summary-text--markdown {
white-space: normal;
}
.summary-text--markdown :deep(h1),
.summary-text--markdown :deep(h2),
.summary-text--markdown :deep(h3),
.summary-text--markdown :deep(h4) {
font-weight: 700;
margin: 0.75rem 0 0.35rem;
line-height: 1.3;
color: #1a202c;
}
.summary-text--markdown :deep(h1) { font-size: 1.15rem; }
.summary-text--markdown :deep(h2) { font-size: 1.05rem; }
.summary-text--markdown :deep(h3) { font-size: 0.975rem; }
.summary-text--markdown :deep(h4) { font-size: 0.925rem; }
.summary-text--markdown :deep(p) { margin: 0.4rem 0; }
.summary-text--markdown :deep(ul),
.summary-text--markdown :deep(ol) {
padding-left: 1.4rem;
margin: 0.4rem 0;
}
.summary-text--markdown :deep(li) { margin: 0.2rem 0; }
.summary-text--markdown :deep(strong) {
font-weight: 700;
color: #1a202c;
}
.summary-text--markdown :deep(em) { font-style: italic; }
.summary-text--markdown :deep(code) {
background: #edf2f7;
border-radius: 3px;
padding: 0.1em 0.35em;
font-size: 0.87em;
font-family: monospace;
}
.summary-text--markdown :deep(blockquote) {
border-left: 3px solid #bee3f8;
margin: 0.5rem 0;
padding: 0.25rem 0.75rem;
color: #4a5568;
}
.sources-section {
border-top: 1px solid #e2e8f0;
padding-top: 0.75rem;
@@ -223,6 +499,20 @@ function formatDate(iso: string): string {
font-size: 0.8rem;
}
.source-chip--clickable {
cursor: pointer;
transition: background 0.15s, border-color 0.15s;
}
.source-chip--clickable:hover {
background: #bee3f8;
border-color: #90cdf4;
}
.source-icon {
font-size: 0.8rem;
}
.source-book {
color: #2b6cb0;
font-weight: 500;
@@ -232,6 +522,12 @@ function formatDate(iso: string): string {
color: #718096;
}
.source-open-hint {
font-size: 0.75rem;
color: #3182ce;
margin-left: 0.1rem;
}
.no-sources {
font-size: 0.85rem;
color: #a0aec0;
@@ -252,4 +548,31 @@ function formatDate(iso: string): string {
color: #3182ce;
text-decoration: underline;
}
.summary-placeholder {
display: flex;
align-items: center;
justify-content: center;
min-height: 6rem;
border-top-color: #cbd5e0;
}
.summary-placeholder-text {
font-size: 0.95rem;
color: #a0aec0;
font-style: italic;
}
.summary-text--markdown :deep(.source-ref) {
color: #3182ce;
font-weight: 600;
cursor: pointer;
border-radius: 3px;
padding: 0 0.15em;
}
.summary-text--markdown :deep(.source-ref:hover) {
background: #ebf8ff;
text-decoration: underline;
}
</style>