Add sub-question branching + document summary modals
- Source modal now shows LLM-generated document summary (lazy-gen + cached in documents.summary) instead of raw chunk text; toggle reveals matched chunk; "View all chunks" button fetches every chunk of the document via new api/document-chunks.php endpoint - Each sub-question card gets a "Branch ↓" button that pre-fills the query with that sub-question and shows a context panel with the prior brief summary; prior_context + branch_notes are injected into interpretSeed() and synthesise() so the LLM knows where the research is coming from - Upload document summaries generated at synthesis time and attached to upload sources alongside corpus summaries - DB: documents.summary TEXT column added to bnl_corpus on chloe Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+101
-1
@@ -6,6 +6,7 @@
|
||||
let lang = 'en';
|
||||
let uploadFiles = [];
|
||||
let lastResult = null;
|
||||
let branchContext = null;
|
||||
|
||||
const SLICE_DEFS = [
|
||||
{ id: 'family_core', label: 'Family Law Core' },
|
||||
@@ -65,6 +66,11 @@
|
||||
modalEyebrow: document.getElementById('advSourceModalEyebrow'),
|
||||
modalMeta: document.getElementById('advSourceModalMeta'),
|
||||
modalText: document.getElementById('advSourceModalText'),
|
||||
branchPanel: document.getElementById('advBranchPanel'),
|
||||
branchClear: document.getElementById('advBranchClear'),
|
||||
branchOrigin: document.getElementById('advBranchOrigin'),
|
||||
branchSummary: document.getElementById('advBranchSummary'),
|
||||
branchNotes: document.getElementById('advBranchNotes'),
|
||||
});
|
||||
|
||||
if (!els.form) return;
|
||||
@@ -75,7 +81,12 @@
|
||||
bindRanges();
|
||||
bindUpload();
|
||||
bindModal();
|
||||
bindBranch();
|
||||
els.form.addEventListener('submit', onSubmit);
|
||||
els.results.addEventListener('click', (e) => {
|
||||
const btn = e.target.closest('.dr-branch-btn');
|
||||
if (btn) branchFromSubQ(btn.dataset.question || '');
|
||||
});
|
||||
|
||||
renderTrace(STEP_LABELS.map((label) => ({ label, detail: 'Waiting…', status: 'idle' })));
|
||||
});
|
||||
@@ -214,7 +225,62 @@
|
||||
source.matched_sub_questions?.length ? ['Matched sub-Q', source.matched_sub_questions.join(', ')] : null,
|
||||
].filter(Boolean);
|
||||
els.modalMeta.innerHTML = '<dl>' + metaRows.map(([k, v]) => `<dt>${escapeHtml(k)}</dt><dd>${escapeHtml(String(v))}</dd>`).join('') + '</dl>';
|
||||
els.modalText.textContent = source.chunk_text || source.excerpt || '';
|
||||
|
||||
const summary = source.summary || '';
|
||||
const chunkText = source.chunk_text || source.excerpt || '';
|
||||
const isUpload = source.source_origin === 'upload';
|
||||
const hasDocId = source.document_id != null;
|
||||
|
||||
let html = summary
|
||||
? `<div class="dr-modal-summary">${escapeHtml(summary)}</div>`
|
||||
: `<div class="dr-modal-summary dr-modal-summary--empty"><em>Summary not yet generated — showing raw chunk below.</em></div>`;
|
||||
|
||||
if (chunkText) {
|
||||
html += `<button class="dr-modal-chunk-toggle" type="button">Show matching chunk ▼</button>`;
|
||||
html += `<div class="dr-modal-chunk-text is-hidden">${escapeHtml(chunkText)}</div>`;
|
||||
}
|
||||
if (!isUpload && hasDocId) {
|
||||
html += `<button class="dr-modal-all-chunks" type="button" data-document-id="${source.document_id}">View all document chunks →</button>`;
|
||||
html += `<div class="dr-modal-chunks-list"></div>`;
|
||||
}
|
||||
|
||||
els.modalText.innerHTML = html;
|
||||
|
||||
const chunkToggle = els.modalText.querySelector('.dr-modal-chunk-toggle');
|
||||
const chunkDiv = els.modalText.querySelector('.dr-modal-chunk-text');
|
||||
chunkToggle?.addEventListener('click', () => {
|
||||
const isHidden = chunkDiv.classList.toggle('is-hidden');
|
||||
chunkToggle.textContent = isHidden ? 'Show matching chunk ▼' : 'Hide matching chunk ▲';
|
||||
});
|
||||
|
||||
const allChunksBtn = els.modalText.querySelector('.dr-modal-all-chunks');
|
||||
const chunksListDiv = els.modalText.querySelector('.dr-modal-chunks-list');
|
||||
if (allChunksBtn && chunksListDiv) {
|
||||
allChunksBtn.addEventListener('click', async () => {
|
||||
allChunksBtn.disabled = true;
|
||||
allChunksBtn.textContent = 'Loading…';
|
||||
try {
|
||||
const res = await fetch(`api/document-chunks.php?document_id=${source.document_id}`, { credentials: 'same-origin' });
|
||||
const data = await res.json();
|
||||
if (data.ok && data.chunks) {
|
||||
chunksListDiv.innerHTML =
|
||||
`<div class="dr-modal-chunks-head">${escapeHtml(data.document?.title || '')} · ${data.chunks.length} chunks</div>` +
|
||||
data.chunks.map((c) => `<div class="dr-modal-chunk-item">
|
||||
<span class="dr-modal-chunk-idx">#${c.chunk_index + 1}${c.section_title ? ' · ' + escapeHtml(c.section_title) : ''}</span>
|
||||
<p class="dr-modal-chunk-preview">${escapeHtml(truncate(c.content, 300))}</p>
|
||||
</div>`).join('');
|
||||
allChunksBtn.remove();
|
||||
} else {
|
||||
allChunksBtn.textContent = 'Could not load chunks.';
|
||||
allChunksBtn.disabled = false;
|
||||
}
|
||||
} catch (_) {
|
||||
allChunksBtn.textContent = 'Error loading chunks.';
|
||||
allChunksBtn.disabled = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
els.modal.classList.remove('is-hidden');
|
||||
}
|
||||
|
||||
@@ -282,6 +348,10 @@
|
||||
controls: getControls(),
|
||||
advocate_role: advocateRole,
|
||||
};
|
||||
if (branchContext) {
|
||||
payload.prior_context = branchContext;
|
||||
payload.branch_notes = (els.branchNotes ? els.branchNotes.value : '').trim();
|
||||
}
|
||||
|
||||
const stepKeyToIndex = {
|
||||
interpretation: 0,
|
||||
@@ -370,6 +440,7 @@
|
||||
return;
|
||||
}
|
||||
|
||||
finalResult.query = query;
|
||||
lastResult = finalResult;
|
||||
const meta = finalResult.trace_metadata || {};
|
||||
const rc = meta.retrieval_counts || {};
|
||||
@@ -596,6 +667,7 @@
|
||||
<div class="dr-subq-report__question">${escapeHtml(sq.question || '')}</div>
|
||||
${sq.rationale ? `<div class="dr-subq-report__rationale">${escapeHtml(sq.rationale)}</div>` : ''}
|
||||
</div>
|
||||
<button class="dr-branch-btn" type="button" data-question="${escapeHtml(sq.question || '')}" aria-label="Branch research from this sub-question">Branch ↓</button>
|
||||
</div>
|
||||
<ul class="dr-mini-source-list">${sourceItems}</ul>
|
||||
</div>`;
|
||||
@@ -611,6 +683,34 @@
|
||||
}
|
||||
}
|
||||
|
||||
function bindBranch() {
|
||||
if (!els.branchClear) return;
|
||||
els.branchClear.addEventListener('click', clearBranch);
|
||||
}
|
||||
|
||||
function clearBranch() {
|
||||
branchContext = null;
|
||||
if (els.branchPanel) els.branchPanel.classList.add('is-hidden');
|
||||
if (els.branchNotes) els.branchNotes.value = '';
|
||||
}
|
||||
|
||||
function branchFromSubQ(question) {
|
||||
if (!lastResult || !question) return;
|
||||
branchContext = {
|
||||
original_query: lastResult.query || '',
|
||||
brief_summary: (lastResult.brief_markdown || '').slice(0, 600),
|
||||
what_we_found: lastResult.what_we_found || '',
|
||||
top_sources: (lastResult.sources || []).slice(0, 5).map((s) => ({
|
||||
n: s.n, title: s.title, excerpt: (s.excerpt || '').slice(0, 200),
|
||||
})),
|
||||
};
|
||||
els.input.value = question;
|
||||
if (els.branchOrigin) els.branchOrigin.textContent = 'Original query: ' + branchContext.original_query;
|
||||
if (els.branchSummary) els.branchSummary.textContent = branchContext.brief_summary;
|
||||
if (els.branchPanel) els.branchPanel.classList.remove('is-hidden');
|
||||
els.form.scrollIntoView({ behavior: 'smooth', block: 'start' });
|
||||
}
|
||||
|
||||
function renderSourceCard(s) {
|
||||
const score = s.reranker_score != null ? s.reranker_score : s.similarity;
|
||||
const originTagClass = s.source_origin === 'upload' ? 'dr-source-tag dr-source-tag--upload' : 'dr-source-tag';
|
||||
|
||||
Reference in New Issue
Block a user