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:
@@ -150,6 +150,20 @@ require_once __DIR__ . '/includes/layout.php';
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="advBranchPanel" class="branch-panel is-hidden" aria-label="Branch context">
|
||||
<div class="branch-panel__head">
|
||||
<span class="branch-panel__label">Branching from sub-question</span>
|
||||
<button type="button" id="advBranchClear" class="upload-clear">× Clear branch</button>
|
||||
</div>
|
||||
<p id="advBranchOrigin" class="branch-panel__origin"></p>
|
||||
<details class="branch-panel__prior">
|
||||
<summary>Prior brief summary</summary>
|
||||
<div id="advBranchSummary" class="branch-panel__brief"></div>
|
||||
</details>
|
||||
<label class="input-label" for="advBranchNotes">Add your notes / context</label>
|
||||
<textarea id="advBranchNotes" rows="3" placeholder="Optional: add observations, corrections, or additional context for this branch…"></textarea>
|
||||
</div>
|
||||
|
||||
<label class="input-label" for="advInput">Describe the dispute or case situation</label>
|
||||
<textarea id="advInput" name="advInput" rows="8" placeholder="Describe the facts of the case, the dispute, and what matters most to your client. The agent will generate adversarial sub-questions, retrieve from the legal corpus and your uploaded files, and synthesise a partisan brief with citations."></textarea>
|
||||
|
||||
|
||||
@@ -62,6 +62,8 @@ try {
|
||||
if (mb_strlen($advocateRole, 'UTF-8') > 200) {
|
||||
throw new DbnToolsHttpException('advocate_role is too long.', 422, 'advocate_role_too_long');
|
||||
}
|
||||
$priorContext = is_array($input['prior_context'] ?? null) ? $input['prior_context'] : null;
|
||||
$branchNotes = mb_substr(trim((string)($input['branch_notes'] ?? '')), 0, 1000, 'UTF-8');
|
||||
|
||||
if (mb_strlen($seedQuery, 'UTF-8') > 4000) {
|
||||
throw new DbnToolsHttpException('Query is too long.', 422, 'query_too_long');
|
||||
@@ -118,7 +120,9 @@ try {
|
||||
$language,
|
||||
$controls,
|
||||
$emit,
|
||||
$advocateRole
|
||||
$advocateRole,
|
||||
$priorContext,
|
||||
$branchNotes
|
||||
);
|
||||
|
||||
$result['ok'] = true;
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../includes/bootstrap.php';
|
||||
|
||||
dbnToolsRequireMethod('GET');
|
||||
dbnToolsRequireAuth();
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Cache-Control: no-store');
|
||||
|
||||
try {
|
||||
$documentId = isset($_GET['document_id']) ? (int)$_GET['document_id'] : 0;
|
||||
if ($documentId <= 0) {
|
||||
echo json_encode(['ok' => false, 'error' => 'document_id is required']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$ragDb = dbnToolsRagDb();
|
||||
|
||||
$docStmt = $ragDb->prepare("SELECT id, title FROM documents WHERE id = ? LIMIT 1");
|
||||
$docStmt->execute([$documentId]);
|
||||
$doc = $docStmt->fetch(PDO::FETCH_ASSOC);
|
||||
if (!$doc) {
|
||||
echo json_encode(['ok' => false, 'error' => 'Document not found']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$chunkStmt = $ragDb->prepare("
|
||||
SELECT chunk_index, section_title, content
|
||||
FROM chunks
|
||||
WHERE document_id = ?
|
||||
ORDER BY chunk_index ASC
|
||||
");
|
||||
$chunkStmt->execute([$documentId]);
|
||||
$chunks = [];
|
||||
foreach ($chunkStmt as $row) {
|
||||
$chunks[] = [
|
||||
'chunk_index' => (int)$row['chunk_index'],
|
||||
'section_title' => $row['section_title'] ?? null,
|
||||
'content' => (string)$row['content'],
|
||||
];
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'ok' => true,
|
||||
'document' => ['id' => (int)$doc['id'], 'title' => (string)$doc['title']],
|
||||
'chunks' => $chunks,
|
||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
|
||||
} catch (Throwable $e) {
|
||||
error_log('DBN document-chunks error: ' . $e->getMessage());
|
||||
echo json_encode(['ok' => false, 'error' => 'Internal error']);
|
||||
}
|
||||
+165
-2
@@ -2573,7 +2573,7 @@ p {
|
||||
.dr-source-modal__meta dt:first-of-type { margin-top: 0; }
|
||||
|
||||
.dr-source-modal__text {
|
||||
white-space: pre-wrap;
|
||||
white-space: normal;
|
||||
line-height: 1.7;
|
||||
color: var(--ink);
|
||||
}
|
||||
@@ -2600,7 +2600,7 @@ p {
|
||||
|
||||
.dr-subq-report__head {
|
||||
display: grid;
|
||||
grid-template-columns: auto 1fr;
|
||||
grid-template-columns: auto 1fr auto;
|
||||
gap: 10px;
|
||||
align-items: start;
|
||||
margin-bottom: 10px;
|
||||
@@ -3213,3 +3213,166 @@ a.dr-source-title-link:hover {
|
||||
.adv-role-select, .adv-role-custom { max-width: 100%; }
|
||||
.adv-banner { flex-direction: column; align-items: flex-start; }
|
||||
}
|
||||
|
||||
/* ── Branch-from-sub-question panel ──────────────────────────────────────── */
|
||||
.branch-panel {
|
||||
background: #f0faf8;
|
||||
border: 1px solid var(--teal);
|
||||
border-left: 4px solid var(--teal);
|
||||
border-radius: 8px;
|
||||
padding: 14px 16px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.branch-panel__head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
|
||||
.branch-panel__label {
|
||||
font-weight: 700;
|
||||
font-size: 0.82rem;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.04em;
|
||||
color: var(--teal-dark);
|
||||
}
|
||||
|
||||
.branch-panel__origin {
|
||||
margin: 0 0 8px;
|
||||
font-size: 0.88rem;
|
||||
color: var(--ink);
|
||||
line-height: 1.45;
|
||||
}
|
||||
|
||||
.branch-panel__prior {
|
||||
margin-bottom: 10px;
|
||||
font-size: 0.87rem;
|
||||
}
|
||||
|
||||
.branch-panel__prior summary {
|
||||
cursor: pointer;
|
||||
color: var(--teal-dark);
|
||||
font-weight: 600;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.branch-panel__brief {
|
||||
margin-top: 6px;
|
||||
padding: 8px 10px;
|
||||
background: rgba(255,255,255,0.7);
|
||||
border-radius: 5px;
|
||||
font-size: 0.86rem;
|
||||
color: var(--muted);
|
||||
line-height: 1.5;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
|
||||
/* "Branch ↓" button on sub-Q cards */
|
||||
.dr-branch-btn {
|
||||
align-self: start;
|
||||
padding: 4px 10px;
|
||||
font-size: 0.78rem;
|
||||
font-weight: 700;
|
||||
background: transparent;
|
||||
border: 1px solid var(--teal);
|
||||
border-radius: 5px;
|
||||
color: var(--teal-dark);
|
||||
white-space: nowrap;
|
||||
transition: background 0.12s, color 0.12s;
|
||||
}
|
||||
|
||||
.dr-branch-btn:hover {
|
||||
background: var(--teal);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
/* ── Source modal: summary + chunk toggle + all-chunks ───────────────────── */
|
||||
.dr-modal-summary {
|
||||
font-size: 0.93rem;
|
||||
line-height: 1.65;
|
||||
color: var(--ink);
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
|
||||
.dr-modal-summary--empty em {
|
||||
color: var(--muted);
|
||||
font-size: 0.87rem;
|
||||
}
|
||||
|
||||
.dr-modal-chunk-toggle,
|
||||
.dr-modal-all-chunks {
|
||||
display: inline-block;
|
||||
margin-bottom: 8px;
|
||||
padding: 4px 10px;
|
||||
font-size: 0.8rem;
|
||||
font-weight: 600;
|
||||
background: transparent;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 5px;
|
||||
color: var(--teal-dark);
|
||||
cursor: pointer;
|
||||
transition: background 0.12s, border-color 0.12s;
|
||||
}
|
||||
|
||||
.dr-modal-chunk-toggle:hover,
|
||||
.dr-modal-all-chunks:hover {
|
||||
background: var(--soft-teal);
|
||||
border-color: var(--teal);
|
||||
}
|
||||
|
||||
.dr-modal-chunk-text {
|
||||
white-space: pre-wrap;
|
||||
font-size: 0.87rem;
|
||||
line-height: 1.65;
|
||||
color: var(--muted);
|
||||
background: #f8f9fb;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 6px;
|
||||
padding: 10px 12px;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.dr-modal-all-chunks {
|
||||
display: block;
|
||||
width: 100%;
|
||||
text-align: left;
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
||||
.dr-modal-chunks-list {
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
||||
.dr-modal-chunks-head {
|
||||
font-weight: 700;
|
||||
font-size: 0.82rem;
|
||||
color: var(--teal-dark);
|
||||
padding: 6px 0 8px;
|
||||
border-bottom: 1px solid var(--line);
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.dr-modal-chunk-item {
|
||||
padding: 8px 0;
|
||||
border-bottom: 1px solid var(--line);
|
||||
}
|
||||
|
||||
.dr-modal-chunk-item:last-child { border-bottom: 0; }
|
||||
|
||||
.dr-modal-chunk-idx {
|
||||
display: block;
|
||||
font-size: 0.78rem;
|
||||
font-weight: 700;
|
||||
color: var(--coral);
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.dr-modal-chunk-preview {
|
||||
margin: 0;
|
||||
font-size: 0.86rem;
|
||||
line-height: 1.5;
|
||||
color: var(--muted);
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
|
||||
+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';
|
||||
|
||||
+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' },
|
||||
@@ -63,6 +64,11 @@
|
||||
modalEyebrow: document.getElementById('drSourceModalEyebrow'),
|
||||
modalMeta: document.getElementById('drSourceModalMeta'),
|
||||
modalText: document.getElementById('drSourceModalText'),
|
||||
branchPanel: document.getElementById('drBranchPanel'),
|
||||
branchClear: document.getElementById('drBranchClear'),
|
||||
branchOrigin: document.getElementById('drBranchOrigin'),
|
||||
branchSummary: document.getElementById('drBranchSummary'),
|
||||
branchNotes: document.getElementById('drBranchNotes'),
|
||||
});
|
||||
|
||||
if (!els.form) return;
|
||||
@@ -72,7 +78,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 || '');
|
||||
});
|
||||
|
||||
// Pre-render placeholder trace
|
||||
renderTrace(STEP_LABELS.map((label) => ({ label, detail: 'Waiting…', status: 'idle' })));
|
||||
@@ -195,7 +206,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');
|
||||
}
|
||||
|
||||
@@ -257,6 +323,10 @@
|
||||
language: lang,
|
||||
controls: getControls(),
|
||||
};
|
||||
if (branchContext) {
|
||||
payload.prior_context = branchContext;
|
||||
payload.branch_notes = (els.branchNotes ? els.branchNotes.value : '').trim();
|
||||
}
|
||||
|
||||
const stepKeyToIndex = {
|
||||
interpretation: 0,
|
||||
@@ -348,6 +418,7 @@
|
||||
return;
|
||||
}
|
||||
|
||||
finalResult.query = query;
|
||||
lastResult = finalResult;
|
||||
const meta = finalResult.trace_metadata || {};
|
||||
const rc = meta.retrieval_counts || {};
|
||||
@@ -536,11 +607,40 @@
|
||||
<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>`;
|
||||
}
|
||||
|
||||
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 flashSource(n) {
|
||||
document.querySelectorAll('.dr-source-card.is-highlight').forEach((c) => c.classList.remove('is-highlight'));
|
||||
const target = document.querySelector(`.dr-source-card[data-source-n="${n}"]`);
|
||||
|
||||
@@ -129,6 +129,20 @@ require_once __DIR__ . '/includes/layout.php';
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="drBranchPanel" class="branch-panel is-hidden" aria-label="Branch context">
|
||||
<div class="branch-panel__head">
|
||||
<span class="branch-panel__label">Branching from sub-question</span>
|
||||
<button type="button" id="drBranchClear" class="upload-clear">× Clear branch</button>
|
||||
</div>
|
||||
<p id="drBranchOrigin" class="branch-panel__origin"></p>
|
||||
<details class="branch-panel__prior">
|
||||
<summary>Prior brief summary</summary>
|
||||
<div id="drBranchSummary" class="branch-panel__brief"></div>
|
||||
</details>
|
||||
<label class="input-label" for="drBranchNotes">Add your notes / context</label>
|
||||
<textarea id="drBranchNotes" rows="3" placeholder="Optional: add observations, corrections, or additional context for this branch…"></textarea>
|
||||
</div>
|
||||
|
||||
<label class="input-label" for="drInput">Question or pasted text</label>
|
||||
<textarea id="drInput" name="drInput" rows="8" placeholder="Describe the legal question, paste case notes, or both. The agent will research the corpus from 3–5 angles."></textarea>
|
||||
|
||||
|
||||
@@ -31,7 +31,9 @@ final class DbnDeepResearchAgent
|
||||
string $language,
|
||||
array $controls,
|
||||
?callable $emit = null,
|
||||
string $advocateRole = ''
|
||||
string $advocateRole = '',
|
||||
?array $priorContext = null,
|
||||
string $branchNotes = ''
|
||||
): array {
|
||||
$seedQuery = trim($seedQuery);
|
||||
$pastedText = trim($pastedText);
|
||||
@@ -82,7 +84,7 @@ final class DbnDeepResearchAgent
|
||||
// STEP 1: Query interpretation
|
||||
$emitRunning('interpretation', 'Query interpretation', 'Summarising the seed input…');
|
||||
$stepStart = microtime(true);
|
||||
$interpretation = $this->interpretSeed($seedDescription, $language, $advocateRole);
|
||||
$interpretation = $this->interpretSeed($seedDescription, $language, $advocateRole, $priorContext, $branchNotes);
|
||||
$this->stepTimings['interpretation'] = $this->elapsedMs($stepStart);
|
||||
$emitStep('interpretation', 'Query interpretation', $interpretation['detail'], 'complete');
|
||||
|
||||
@@ -284,6 +286,33 @@ final class DbnDeepResearchAgent
|
||||
$synthesisEngineLabel = $engine === 'azure_full' ? 'Azure gpt-4o' : ($engine === 'gpu' ? 'GPU qwen2.5:14b' : 'Azure gpt-4o-mini');
|
||||
$emitRunning('synthesis', 'Synthesis', sprintf('Synthesising cited brief with %s — this is the slowest step…', $synthesisEngineLabel));
|
||||
$stepStart = microtime(true);
|
||||
// Attach upload summaries (generated lazily) to numbered sources
|
||||
if (!empty($uploadedFiles) && !empty($numberedSources)) {
|
||||
$uploadSummaries = [];
|
||||
foreach ($uploadedFiles as $idx => $file) {
|
||||
$text = mb_substr((string)($file['text'] ?? ''), 0, 4000, 'UTF-8');
|
||||
$filename = (string)($file['filename'] ?? "file-{$idx}");
|
||||
if ($text === '') continue;
|
||||
try {
|
||||
$raw = $this->azure->chatText([
|
||||
['role' => 'system', 'content' => 'Return only a concise 3-4 sentence summary. No preamble.'],
|
||||
['role' => 'user', 'content' => "Summarise this document for a legal researcher.\n\nFilename: {$filename}\n\nContent:\n{$text}"],
|
||||
], ['temperature' => 0.1, 'max_tokens' => 200, 'timeout' => 20]);
|
||||
$uploadSummaries[$idx] = trim($raw);
|
||||
} catch (Throwable $e) {
|
||||
error_log('DBN upload summary gen failed for file ' . $idx . ': ' . $e->getMessage());
|
||||
$uploadSummaries[$idx] = null;
|
||||
}
|
||||
}
|
||||
foreach ($numberedSources as &$src) {
|
||||
if (($src['source_origin'] ?? '') !== 'upload') continue;
|
||||
if (preg_match('/^upload:(\d+):/', (string)($src['chunk_id'] ?? ''), $m)) {
|
||||
$src['summary'] = $uploadSummaries[(int)$m[1]] ?? null;
|
||||
}
|
||||
}
|
||||
unset($src);
|
||||
}
|
||||
|
||||
$synthesis = $this->synthesise(
|
||||
$seedDescription,
|
||||
$interpretation['brief'],
|
||||
@@ -292,7 +321,9 @@ final class DbnDeepResearchAgent
|
||||
$engine,
|
||||
$language,
|
||||
$controls['temperature'],
|
||||
$advocateRole
|
||||
$advocateRole,
|
||||
$priorContext,
|
||||
$branchNotes
|
||||
);
|
||||
$this->stepTimings['synthesis'] = $this->elapsedMs($stepStart);
|
||||
$emitStep(
|
||||
@@ -411,14 +442,30 @@ final class DbnDeepResearchAgent
|
||||
return implode("\n\n", $parts);
|
||||
}
|
||||
|
||||
private function interpretSeed(string $seedDescription, string $language, string $advocateRole = ''): array
|
||||
private function interpretSeed(string $seedDescription, string $language, string $advocateRole = '', ?array $priorContext = null, string $branchNotes = ''): array
|
||||
{
|
||||
$locale = $language === 'no' ? 'Norwegian' : 'English';
|
||||
$rolePrefix = $advocateRole !== ''
|
||||
? "You are preparing a case-research brief for: {$advocateRole}. Frame your interpretation to identify the strongest legal angles for this party.\n\n"
|
||||
: '';
|
||||
|
||||
$priorContextBlock = '';
|
||||
if (!empty($priorContext)) {
|
||||
$parts = ['Prior research context:'];
|
||||
if (!empty($priorContext['original_query'])) {
|
||||
$parts[] = 'Original question: ' . mb_substr((string)$priorContext['original_query'], 0, 300, 'UTF-8');
|
||||
}
|
||||
if (!empty($priorContext['what_we_found'])) {
|
||||
$parts[] = 'Key findings: ' . mb_substr((string)$priorContext['what_we_found'], 0, 400, 'UTF-8');
|
||||
}
|
||||
if ($branchNotes !== '') {
|
||||
$parts[] = 'Researcher notes: ' . mb_substr($branchNotes, 0, 300, 'UTF-8');
|
||||
}
|
||||
$priorContextBlock = implode("\n", $parts) . "\n\nNow investigate this branch:\n";
|
||||
}
|
||||
|
||||
$prompt = <<<PROMPT
|
||||
{$rolePrefix}You are reviewing the input below to set up a deep legal research pass against the Do Better Norge family-law corpus.
|
||||
{$rolePrefix}{$priorContextBlock}You are reviewing the input below to set up a deep legal research pass against the Do Better Norge family-law corpus.
|
||||
|
||||
Input:
|
||||
{$seedDescription}
|
||||
@@ -741,7 +788,8 @@ PROMPT;
|
||||
|
||||
$stmt = $ragDb->prepare("
|
||||
SELECT d.id, d.title, d.source_url, d.authority_type,
|
||||
d.publication_date, d.source_id, d.jurisdiction
|
||||
d.publication_date, d.source_id, d.jurisdiction,
|
||||
d.summary, LEFT(d.content, 4000) AS content_excerpt
|
||||
FROM documents d
|
||||
WHERE d.id IN ({$ph})
|
||||
");
|
||||
@@ -759,9 +807,30 @@ PROMPT;
|
||||
'publication_date' => $row['publication_date'] ?? null,
|
||||
'corpus_source_name' => 'Do Better Legal',
|
||||
'source_id' => $sid,
|
||||
'summary' => $row['summary'] ?? null,
|
||||
'content_excerpt' => (string)($row['content_excerpt'] ?? ''),
|
||||
'title' => (string)($row['title'] ?? ''),
|
||||
];
|
||||
}
|
||||
|
||||
// Lazily generate summaries for documents that don't have one yet
|
||||
$unsummarized = array_filter($docMeta, fn($m) => $m['summary'] === null && $m['content_excerpt'] !== '');
|
||||
foreach ($unsummarized as $dId => $m) {
|
||||
try {
|
||||
$raw = $this->azure->chatText([
|
||||
['role' => 'system', 'content' => 'Return only a concise 3-4 sentence summary. No preamble.'],
|
||||
['role' => 'user', 'content' => "Summarise this Norwegian family law document for a legal researcher.\nFocus on: which legal provisions it covers, its authority type, and what questions it helps answer.\n\nTitle: {$m['title']}\n\nContent:\n{$m['content_excerpt']}"],
|
||||
], ['temperature' => 0.1, 'max_tokens' => 200, 'timeout' => 25]);
|
||||
$summary = trim($raw);
|
||||
if ($summary !== '') {
|
||||
$ragDb->prepare("UPDATE documents SET summary = ? WHERE id = ?")->execute([$summary, $dId]);
|
||||
$docMeta[$dId]['summary'] = $summary;
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
error_log('DBN hydrateSourceUrls summary gen failed for doc ' . $dId . ': ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// Enrich with corpus source name from bnl_admin.corpus_sources
|
||||
if (!empty($sourceIds)) {
|
||||
$uSids = array_values(array_unique($sourceIds));
|
||||
@@ -795,6 +864,7 @@ PROMPT;
|
||||
$chunk['authority_label'] = $m['authority_label'] ?? $chunk['authority_label'];
|
||||
$chunk['corpus_source_name'] = $m['corpus_source_name'] ?? null;
|
||||
$chunk['publication_date'] = $m['publication_date'] ?? null;
|
||||
$chunk['summary'] = $m['summary'] ?? null;
|
||||
}
|
||||
unset($chunk);
|
||||
}
|
||||
@@ -868,7 +938,9 @@ PROMPT;
|
||||
string $engine,
|
||||
string $language,
|
||||
float $temperature,
|
||||
string $advocateRole = ''
|
||||
string $advocateRole = '',
|
||||
?array $priorContext = null,
|
||||
string $branchNotes = ''
|
||||
): array {
|
||||
$locale = $language === 'no' ? 'Norwegian' : 'English';
|
||||
|
||||
@@ -891,6 +963,23 @@ PROMPT;
|
||||
];
|
||||
}
|
||||
|
||||
$priorContextSection = '';
|
||||
if (!empty($priorContext)) {
|
||||
$prior = [];
|
||||
if (!empty($priorContext['original_query'])) {
|
||||
$prior[] = 'Original research question: ' . mb_substr((string)$priorContext['original_query'], 0, 300, 'UTF-8');
|
||||
}
|
||||
if (!empty($priorContext['brief_summary'])) {
|
||||
$prior[] = "Key findings from prior research:\n" . mb_substr((string)$priorContext['brief_summary'], 0, 600, 'UTF-8');
|
||||
}
|
||||
if ($branchNotes !== '') {
|
||||
$prior[] = 'Researcher notes: ' . mb_substr($branchNotes, 0, 300, 'UTF-8');
|
||||
}
|
||||
if ($prior) {
|
||||
$priorContextSection = "\nBackground from prior research:\n" . implode("\n", $prior) . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
$sourcesContext = [];
|
||||
foreach ($numberedSources as $s) {
|
||||
$sourcesContext[] = sprintf(
|
||||
@@ -926,7 +1015,7 @@ PROMPT;
|
||||
$prompt = <<<PROMPT
|
||||
You are Do Better Norge Legal Tools producing a legal preparation brief in {$locale}.
|
||||
Your client: {$advocateRole}
|
||||
|
||||
{$priorContextSection}
|
||||
You MUST ground every claim in the numbered sources below using inline `[n]` citation markers. Do NOT invent statutes, paragraph numbers, case names, dates, or parties.
|
||||
|
||||
User input:
|
||||
@@ -961,7 +1050,7 @@ PROMPT;
|
||||
} else {
|
||||
$prompt = <<<PROMPT
|
||||
You are Do Better Norge Legal Tools running a deep-research synthesis. You MUST ground every claim in the numbered sources below, using inline `[n]` citation markers that map to the source list. Do NOT cite a source you did not use. Do NOT invent statutes, paragraph numbers, case names, dates, or parties.
|
||||
|
||||
{$priorContextSection}
|
||||
User input:
|
||||
{$seedDescription}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user