Add Case Advocate tab — partisan brief grounded in Norwegian law
New /advocate.php tab: user selects who they represent (biological father, mother, foster carer, CWS, etc.) and the agent takes their side entirely. Adversarial sub-questions target supporting Lovdata statutes + ECHR precedents; synthesis returns client_strengths[] and opposing_weaknesses[] alongside the advocate brief. - DeepResearchAgent: add advocateRole param to run(), interpretSeed(), expandQueries(), synthesise(). Neutral path unchanged (empty string). - api/deep-research.php: extract + validate advocate_role from payload; telemetry logs tool='advocate' vs 'deep_research'. - advocate.php: new page with role dropdown (presets + custom), same corpus slices/engine/controls/upload zone as deep research. - assets/js/advocate.js: page-scoped JS; renders advocate banner, client strengths card (teal), advocate brief, opposing weaknesses card (amber), sub-Q cards, sources, uncertainty, next step. - assets/css/tools.css: append .adv-* rules (~120 lines). - includes/layout.php: add Advocate nav tab between Deep research and Summarize. - index.php: add Advocate cap-card tile. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+182
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
$toolName = 'advocate';
|
||||
$toolTitle = 'Case Advocate';
|
||||
$toolKind = 'Legal advocate';
|
||||
$toolBadge = 'Partisan brief';
|
||||
$extraScripts = ['assets/js/advocate.js'];
|
||||
require_once __DIR__ . '/includes/layout.php';
|
||||
?>
|
||||
<form id="advocateForm" class="tool-form deep-research" enctype="multipart/form-data">
|
||||
|
||||
<div class="lang-switcher" id="advLangSwitcher" role="group" aria-label="UI language">
|
||||
<button type="button" class="lang-btn is-active" data-lang="en">🇬🇧 EN</button>
|
||||
<button type="button" class="lang-btn" data-lang="no">🇳🇴 NO</button>
|
||||
</div>
|
||||
|
||||
<!-- Role selector — the defining field for advocate mode -->
|
||||
<div class="adv-role-row">
|
||||
<label class="control-label" for="advRoleSelect">Who are you representing?</label>
|
||||
<select id="advRoleSelect" class="adv-role-select" required>
|
||||
<option value="">— Select a role —</option>
|
||||
<option value="Biological mother">Biological mother</option>
|
||||
<option value="Biological father">Biological father</option>
|
||||
<option value="Both biological parents">Both biological parents</option>
|
||||
<option value="Foster carer / long-term placement">Foster carer / long-term placement</option>
|
||||
<option value="Adoptive parent">Adoptive parent</option>
|
||||
<option value="Child (via representative)">Child (via representative)</option>
|
||||
<option value="Extended family (grandparent, sibling, aunt/uncle)">Extended family (grandparent, sibling, aunt/uncle)</option>
|
||||
<option value="Child welfare services (Barnevernet)">Child welfare services (Barnevernet)</option>
|
||||
<option value="__other__">Other — describe below</option>
|
||||
</select>
|
||||
<input type="text" id="advRoleCustom" class="adv-role-custom is-hidden"
|
||||
placeholder="Describe the party you represent…" maxlength="200">
|
||||
<p class="upload-hint">The agent will frame every sub-question, retrieval pass, and the final brief to argue <em>for</em> the selected party, identifying weaknesses in the opposing position and citing Lovdata statutes, ECHR judgments, and Bufdir guidance.</p>
|
||||
</div>
|
||||
|
||||
<div class="control-row" id="advEngineControl">
|
||||
<span class="control-label">Engine</span>
|
||||
<label><input type="radio" name="advEngine" value="azure_mini" checked> Azure gpt-4o-mini ★ <small class="control-hint">(~15-45s)</small></label>
|
||||
<label><input type="radio" name="advEngine" value="azure_full"> Azure gpt-4o <small class="control-hint">(best · ~60-180s)</small></label>
|
||||
<label><input type="radio" name="advEngine" value="gpu"> GPU (cuttlefish) <small class="control-hint">(local · ~30-90s)</small></label>
|
||||
</div>
|
||||
<p class="upload-hint">Azure mini finishes fastest. Azure full produces the most thorough advocate brief. GPU keeps everything inside the BNL fleet.</p>
|
||||
|
||||
<div class="dr-slice-section">
|
||||
<p class="control-label">Corpus slices</p>
|
||||
<p class="upload-hint">Select which slices the agent searches when building your case. All three legal slices are on by default.</p>
|
||||
<div class="dr-slice-grid">
|
||||
<button type="button" class="adv-slice is-on" data-slice="family_core" aria-pressed="true">
|
||||
<div class="dr-slice__head">
|
||||
<span class="dr-slice__title">Family Law Core</span>
|
||||
<span class="dr-slice__badge">on</span>
|
||||
</div>
|
||||
<p class="dr-slice__tagline">Barneloven, custody, samvær, mediation</p>
|
||||
</button>
|
||||
<button type="button" class="adv-slice is-on" data-slice="child_welfare" aria-pressed="true">
|
||||
<div class="dr-slice__head">
|
||||
<span class="dr-slice__title">Child Welfare</span>
|
||||
<span class="dr-slice__badge">on</span>
|
||||
</div>
|
||||
<p class="dr-slice__tagline">Barnevern, omsorgsovertakelse, foster care</p>
|
||||
</button>
|
||||
<button type="button" class="adv-slice is-on" data-slice="echr_hague" aria-pressed="true">
|
||||
<div class="dr-slice__head">
|
||||
<span class="dr-slice__title">ECHR and Hague</span>
|
||||
<span class="dr-slice__badge">on</span>
|
||||
</div>
|
||||
<p class="dr-slice__tagline">Article 8, EMD, HCCH, cross-border family</p>
|
||||
</button>
|
||||
<button type="button" class="adv-slice" data-slice="broader_legal" aria-pressed="false">
|
||||
<div class="dr-slice__head">
|
||||
<span class="dr-slice__title">Broader Legal Support</span>
|
||||
<span class="dr-slice__badge">off</span>
|
||||
</div>
|
||||
<p class="dr-slice__tagline">Arbeidsmiljøloven, NOUer, statutes, government background</p>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<details class="advanced-panel" id="advAdvanced">
|
||||
<summary class="advanced-toggle">Advanced controls</summary>
|
||||
<div class="dr-control-grid">
|
||||
<div class="dr-control-card">
|
||||
<label>Sub-questions <span id="advSubQValue" class="dr-control-value">4</span></label>
|
||||
<input type="range" id="advSubQ" min="3" max="5" step="1" value="4">
|
||||
<small>How many angles the agent generates (each framed to strengthen your case).</small>
|
||||
</div>
|
||||
<div class="dr-control-card">
|
||||
<label>Chunks / sub-Q <span id="advChunkLimitValue" class="dr-control-value">6</span></label>
|
||||
<input type="range" id="advChunkLimit" min="4" max="10" step="1" value="6">
|
||||
<small>Corpus chunks retrieved per sub-question.</small>
|
||||
</div>
|
||||
<div class="dr-control-card">
|
||||
<label>Similarity floor <span id="advSimValue" class="dr-control-value">0.30</span></label>
|
||||
<input type="range" id="advSim" min="0.20" max="0.60" step="0.05" value="0.30">
|
||||
<small>Minimum similarity for uploaded-doc chunks to be included.</small>
|
||||
</div>
|
||||
<div class="dr-control-card">
|
||||
<label>Sources kept <span id="advTopKValue" class="dr-control-value">12</span></label>
|
||||
<input type="range" id="advTopK" min="8" max="14" step="1" value="12">
|
||||
<small>Top sources kept after dedupe + rerank to feed synthesis.</small>
|
||||
</div>
|
||||
<div class="dr-control-card">
|
||||
<label>Temperature <span id="advTempValue" class="dr-control-value">0.15</span></label>
|
||||
<input type="range" id="advTemp" min="0.05" max="0.40" step="0.05" value="0.15">
|
||||
<small>Keep low for grounded legal briefs.</small>
|
||||
</div>
|
||||
</div>
|
||||
</details>
|
||||
|
||||
<div class="upload-zone" id="advUploadZone" role="region" aria-label="File upload">
|
||||
<input type="file" id="advUploadInput" multiple accept=".pdf,.docx,.txt" aria-label="Choose files">
|
||||
<div id="advUploadPrompt" class="upload-prompt">
|
||||
<span class="upload-icon" aria-hidden="true">⇧</span>
|
||||
<p>Drop case files here, or <label for="advUploadInput" class="upload-browse">browse</label></p>
|
||||
<p class="upload-hint"><strong>PDF</strong>, <strong>DOCX</strong>, <strong>TXT</strong> — up to 5 files — chunked + embedded in memory only, never stored.</p>
|
||||
</div>
|
||||
<div id="advUploadFileInfo" class="upload-file is-hidden">
|
||||
<ul id="advUploadFileList" class="upload-file-list"></ul>
|
||||
<button type="button" id="advUploadClear" class="upload-clear">× Clear</button>
|
||||
</div>
|
||||
</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>
|
||||
|
||||
<div class="form-footer">
|
||||
<p id="advStatus" class="form-status" role="status" aria-live="polite"></p>
|
||||
<button id="advRunButton" type="submit">Research my case</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<section id="advResults" class="results deep-research-results" aria-live="polite">
|
||||
<div class="empty-state">
|
||||
<h3>Ready</h3>
|
||||
<p>Select who you are representing, describe the dispute, and optionally upload case documents. The agent will argue your side — identifying supporting statutes, ECHR judgments, and weaknesses in the opposing position.</p>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- Source modal -->
|
||||
<div id="advSourceModal" class="dr-source-modal is-hidden" role="dialog" aria-modal="true" aria-labelledby="advSourceModalTitle">
|
||||
<div class="dr-source-modal__dialog">
|
||||
<header class="dr-source-modal__head">
|
||||
<div>
|
||||
<p class="eyebrow" id="advSourceModalEyebrow">Source</p>
|
||||
<h3 id="advSourceModalTitle"></h3>
|
||||
</div>
|
||||
<button type="button" id="advSourceModalClose" class="upload-clear" aria-label="Close">×</button>
|
||||
</header>
|
||||
<div class="dr-source-modal__body">
|
||||
<aside class="dr-source-modal__meta" id="advSourceModalMeta"></aside>
|
||||
<div class="dr-source-modal__text" id="advSourceModalText"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Hidden stubs so tools.js element refs don't crash on this page -->
|
||||
<div class="is-hidden" id="languageControl" aria-hidden="true"><input type="radio" name="language" value="en" checked></div>
|
||||
<div class="is-hidden" id="redactionControl" aria-hidden="true"></div>
|
||||
<div class="is-hidden" id="audioZone" aria-hidden="true">
|
||||
<input type="file" id="audioInput" style="display:none">
|
||||
<div id="audioPrompt"></div>
|
||||
<div id="audioFileInfo"><ol id="audioQueueList"></ol><button type="button" id="audioClear"></button></div>
|
||||
</div>
|
||||
<div class="is-hidden" id="diarizeControl" aria-hidden="true">
|
||||
<input type="checkbox" id="diarizeCheck">
|
||||
<input type="number" id="numSpeakersInput">
|
||||
</div>
|
||||
<div class="is-hidden" id="transcribeLangControl" aria-hidden="true"><input type="radio" name="transcribeLang" value="no" checked></div>
|
||||
<div class="is-hidden" id="vocabControl" aria-hidden="true">
|
||||
<div id="vocabPresets"></div>
|
||||
<textarea id="initPromptInput"></textarea>
|
||||
</div>
|
||||
<div class="is-hidden" id="aliasSection" aria-hidden="true">
|
||||
<button type="button" id="addAliasRow"></button>
|
||||
<div id="aliasRows"></div>
|
||||
</div>
|
||||
<div class="is-hidden" id="exemptSection" aria-hidden="true">
|
||||
<button type="button" id="addExemptRow"></button>
|
||||
<div id="exemptRows"></div>
|
||||
</div>
|
||||
<?php require_once __DIR__ . '/includes/layout_footer.php'; ?>
|
||||
@@ -58,6 +58,10 @@ try {
|
||||
$sliceInput = $input['slices'] ?? [];
|
||||
$engine = (string)($input['engine'] ?? 'azure_mini');
|
||||
$controls = is_array($input['controls'] ?? null) ? $input['controls'] : [];
|
||||
$advocateRole = trim((string)($input['advocate_role'] ?? ''));
|
||||
if (mb_strlen($advocateRole, 'UTF-8') > 200) {
|
||||
throw new DbnToolsHttpException('advocate_role is too long.', 422, 'advocate_role_too_long');
|
||||
}
|
||||
|
||||
if (mb_strlen($seedQuery, 'UTF-8') > 4000) {
|
||||
throw new DbnToolsHttpException('Query is too long.', 422, 'query_too_long');
|
||||
@@ -113,20 +117,22 @@ try {
|
||||
$engine,
|
||||
$language,
|
||||
$controls,
|
||||
$emit
|
||||
$emit,
|
||||
$advocateRole
|
||||
);
|
||||
|
||||
$result['ok'] = true;
|
||||
$result['latency_ms'] = (int)round((microtime(true) - $startTime) * 1000);
|
||||
|
||||
dbnToolsLogMetadata([
|
||||
'tool' => 'deep_research',
|
||||
'tool' => $advocateRole !== '' ? 'advocate' : 'deep_research',
|
||||
'language' => $language,
|
||||
'ok' => true,
|
||||
'latency_ms' => $result['latency_ms'],
|
||||
'chunk_count' => (int)($result['trace_metadata']['chunk_count'] ?? 0),
|
||||
'source_count' => (int)($result['trace_metadata']['source_count'] ?? 0),
|
||||
'deployment' => $result['trace_metadata']['deployment'] ?? null,
|
||||
'advocate_role' => $advocateRole !== '' ? $advocateRole : null,
|
||||
]);
|
||||
|
||||
$emit('final', ['result' => $result]);
|
||||
|
||||
@@ -3052,3 +3052,164 @@ a.dr-source-title-link:hover {
|
||||
.source-expand-grid { grid-template-columns: 1fr; }
|
||||
.corpus-search-controls { flex-direction: column; align-items: flex-start; }
|
||||
}
|
||||
|
||||
/* =====================================================================
|
||||
Advocate tool (.adv-*)
|
||||
===================================================================== */
|
||||
|
||||
/* Role selector row */
|
||||
.adv-role-row {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
padding: 18px 20px;
|
||||
background: var(--soft-teal);
|
||||
border-radius: 8px;
|
||||
border: 1px solid color-mix(in srgb, var(--teal) 20%, transparent);
|
||||
margin-bottom: 18px;
|
||||
}
|
||||
.adv-role-row .control-label {
|
||||
font-size: 0.82rem;
|
||||
font-weight: 600;
|
||||
color: var(--teal-dark, var(--teal));
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
margin: 0;
|
||||
}
|
||||
.adv-role-select {
|
||||
appearance: none;
|
||||
background: var(--panel) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M0 0l6 8 6-8z' fill='%23666'/%3E%3C/svg%3E") no-repeat right 14px center;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 6px;
|
||||
padding: 10px 38px 10px 14px;
|
||||
font-size: 0.95rem;
|
||||
color: var(--ink);
|
||||
cursor: pointer;
|
||||
width: 100%;
|
||||
max-width: 480px;
|
||||
}
|
||||
.adv-role-select:focus { outline: 2px solid var(--teal); outline-offset: 2px; }
|
||||
.adv-role-custom {
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 6px;
|
||||
padding: 9px 14px;
|
||||
font-size: 0.93rem;
|
||||
color: var(--ink);
|
||||
width: 100%;
|
||||
max-width: 480px;
|
||||
background: var(--panel);
|
||||
}
|
||||
.adv-role-custom:focus { outline: 2px solid var(--teal); outline-offset: 2px; }
|
||||
|
||||
/* Advocate result banner */
|
||||
.adv-banner {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 8px 14px;
|
||||
padding: 14px 20px;
|
||||
background: var(--soft-teal);
|
||||
border-radius: 8px;
|
||||
border-left: 4px solid var(--teal);
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
.adv-banner__label {
|
||||
font-size: 0.75rem;
|
||||
font-weight: 700;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.06em;
|
||||
color: var(--teal-dark, var(--teal));
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.adv-banner__role {
|
||||
font-size: 1.05rem;
|
||||
color: var(--ink);
|
||||
}
|
||||
.adv-banner__note {
|
||||
font-size: 0.8rem;
|
||||
color: var(--muted);
|
||||
flex-basis: 100%;
|
||||
}
|
||||
|
||||
/* Client strengths card */
|
||||
.adv-strengths {
|
||||
background: var(--soft-teal);
|
||||
border-radius: 8px;
|
||||
border: 1px solid color-mix(in srgb, var(--teal) 20%, transparent);
|
||||
padding: 16px 20px;
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
.adv-strengths__head {
|
||||
font-size: 0.9rem;
|
||||
font-weight: 700;
|
||||
color: var(--teal-dark, var(--teal));
|
||||
margin: 0 0 10px;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.04em;
|
||||
}
|
||||
.adv-strengths__list {
|
||||
list-style: none;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 7px;
|
||||
}
|
||||
.adv-strengths__item {
|
||||
padding-left: 24px;
|
||||
position: relative;
|
||||
font-size: 0.88rem;
|
||||
color: var(--ink);
|
||||
line-height: 1.5;
|
||||
}
|
||||
.adv-strengths__item::before {
|
||||
content: '✓';
|
||||
position: absolute;
|
||||
left: 0;
|
||||
color: var(--teal);
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
/* Opposing weaknesses card */
|
||||
.adv-weaknesses {
|
||||
background: color-mix(in srgb, var(--amber, #c87f00) 10%, transparent);
|
||||
border-radius: 8px;
|
||||
border: 1px solid color-mix(in srgb, var(--amber, #c87f00) 25%, transparent);
|
||||
padding: 16px 20px;
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
.adv-weaknesses__head {
|
||||
font-size: 0.9rem;
|
||||
font-weight: 700;
|
||||
color: var(--amber-dark, #8a5700);
|
||||
margin: 0 0 10px;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.04em;
|
||||
}
|
||||
.adv-weaknesses__list {
|
||||
list-style: none;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 7px;
|
||||
}
|
||||
.adv-weaknesses__item {
|
||||
padding-left: 24px;
|
||||
position: relative;
|
||||
font-size: 0.88rem;
|
||||
color: var(--ink);
|
||||
line-height: 1.5;
|
||||
}
|
||||
.adv-weaknesses__item::before {
|
||||
content: '⚠';
|
||||
position: absolute;
|
||||
left: 0;
|
||||
font-size: 0.85em;
|
||||
color: var(--amber, #c87f00);
|
||||
}
|
||||
|
||||
@media (max-width: 760px) {
|
||||
.adv-role-select, .adv-role-custom { max-width: 100%; }
|
||||
.adv-banner { flex-direction: column; align-items: flex-start; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,693 @@
|
||||
/* advocate.js — page-scoped UI for /advocate.php */
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
const els = {};
|
||||
let lang = 'en';
|
||||
let uploadFiles = [];
|
||||
let lastResult = null;
|
||||
|
||||
const SLICE_DEFS = [
|
||||
{ id: 'family_core', label: 'Family Law Core' },
|
||||
{ id: 'child_welfare', label: 'Child Welfare' },
|
||||
{ id: 'echr_hague', label: 'ECHR and Hague' },
|
||||
{ id: 'broader_legal', label: 'Broader Legal Support' },
|
||||
];
|
||||
|
||||
const STEP_LABELS = [
|
||||
'Query interpretation',
|
||||
'Query expansion',
|
||||
'Slice resolution',
|
||||
'Upload indexing',
|
||||
'Retrieval',
|
||||
'Synthesis',
|
||||
'Citation confidence',
|
||||
];
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
if (!document.body.dataset.activeTool || document.body.dataset.activeTool !== 'advocate') return;
|
||||
|
||||
Object.assign(els, {
|
||||
form: document.getElementById('advocateForm'),
|
||||
input: document.getElementById('advInput'),
|
||||
status: document.getElementById('advStatus'),
|
||||
runButton: document.getElementById('advRunButton'),
|
||||
results: document.getElementById('advResults'),
|
||||
traceList: document.getElementById('traceList'),
|
||||
roleSelect: document.getElementById('advRoleSelect'),
|
||||
roleCustom: document.getElementById('advRoleCustom'),
|
||||
slices: Array.from(document.querySelectorAll('.adv-slice')),
|
||||
langButtons: Array.from(document.querySelectorAll('#advLangSwitcher .lang-btn')),
|
||||
engineRadios: Array.from(document.querySelectorAll('input[name="advEngine"]')),
|
||||
subQ: document.getElementById('advSubQ'),
|
||||
subQVal: document.getElementById('advSubQValue'),
|
||||
chunkLimit: document.getElementById('advChunkLimit'),
|
||||
chunkLimitVal: document.getElementById('advChunkLimitValue'),
|
||||
sim: document.getElementById('advSim'),
|
||||
simVal: document.getElementById('advSimValue'),
|
||||
topK: document.getElementById('advTopK'),
|
||||
topKVal: document.getElementById('advTopKValue'),
|
||||
temp: document.getElementById('advTemp'),
|
||||
tempVal: document.getElementById('advTempValue'),
|
||||
uploadZone: document.getElementById('advUploadZone'),
|
||||
uploadInput: document.getElementById('advUploadInput'),
|
||||
uploadPrompt: document.getElementById('advUploadPrompt'),
|
||||
uploadFileInfo: document.getElementById('advUploadFileInfo'),
|
||||
uploadFileList: document.getElementById('advUploadFileList'),
|
||||
uploadClear: document.getElementById('advUploadClear'),
|
||||
modal: document.getElementById('advSourceModal'),
|
||||
modalClose: document.getElementById('advSourceModalClose'),
|
||||
modalTitle: document.getElementById('advSourceModalTitle'),
|
||||
modalEyebrow: document.getElementById('advSourceModalEyebrow'),
|
||||
modalMeta: document.getElementById('advSourceModalMeta'),
|
||||
modalText: document.getElementById('advSourceModalText'),
|
||||
});
|
||||
|
||||
if (!els.form) return;
|
||||
|
||||
bindRole();
|
||||
bindSlices();
|
||||
bindLang();
|
||||
bindRanges();
|
||||
bindUpload();
|
||||
bindModal();
|
||||
els.form.addEventListener('submit', onSubmit);
|
||||
|
||||
renderTrace(STEP_LABELS.map((label) => ({ label, detail: 'Waiting…', status: 'idle' })));
|
||||
});
|
||||
|
||||
function bindRole() {
|
||||
if (!els.roleSelect) return;
|
||||
els.roleSelect.addEventListener('change', () => {
|
||||
const isOther = els.roleSelect.value === '__other__';
|
||||
els.roleCustom.classList.toggle('is-hidden', !isOther);
|
||||
if (isOther) els.roleCustom.focus();
|
||||
});
|
||||
}
|
||||
|
||||
function getAdvocateRole() {
|
||||
if (!els.roleSelect) return '';
|
||||
if (els.roleSelect.value === '__other__') {
|
||||
return (els.roleCustom ? els.roleCustom.value.trim() : '');
|
||||
}
|
||||
return els.roleSelect.value;
|
||||
}
|
||||
|
||||
function bindSlices() {
|
||||
els.slices.forEach((btn) => {
|
||||
btn.addEventListener('click', () => {
|
||||
const isOn = btn.classList.toggle('is-on');
|
||||
btn.setAttribute('aria-pressed', isOn ? 'true' : 'false');
|
||||
const badge = btn.querySelector('.dr-slice__badge');
|
||||
if (badge) badge.textContent = isOn ? 'on' : 'off';
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function bindLang() {
|
||||
els.langButtons.forEach((b) => {
|
||||
b.addEventListener('click', () => {
|
||||
els.langButtons.forEach((x) => x.classList.remove('is-active'));
|
||||
b.classList.add('is-active');
|
||||
lang = b.dataset.lang || 'en';
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function bindRanges() {
|
||||
const pairs = [
|
||||
[els.subQ, els.subQVal, (v) => v],
|
||||
[els.chunkLimit, els.chunkLimitVal, (v) => v],
|
||||
[els.sim, els.simVal, (v) => Number(v).toFixed(2)],
|
||||
[els.topK, els.topKVal, (v) => v],
|
||||
[els.temp, els.tempVal, (v) => Number(v).toFixed(2)],
|
||||
];
|
||||
pairs.forEach(([range, label, fmt]) => {
|
||||
if (!range || !label) return;
|
||||
const sync = () => { label.textContent = fmt(range.value); };
|
||||
range.addEventListener('input', sync);
|
||||
sync();
|
||||
});
|
||||
}
|
||||
|
||||
function bindUpload() {
|
||||
if (!els.uploadZone) return;
|
||||
const onFiles = (fileList) => {
|
||||
const files = Array.from(fileList || []).slice(0, 5);
|
||||
if (uploadFiles.length + files.length > 5) {
|
||||
setStatus('At most 5 files can be uploaded per request.', 'error');
|
||||
return;
|
||||
}
|
||||
files.forEach((f) => {
|
||||
if (f.size > 4 * 1024 * 1024) {
|
||||
setStatus(`${f.name} exceeds the 4 MB limit.`, 'error');
|
||||
return;
|
||||
}
|
||||
const ext = (f.name.split('.').pop() || '').toLowerCase();
|
||||
if (!['pdf', 'docx', 'txt'].includes(ext)) {
|
||||
setStatus(`${f.name} is not a supported file type.`, 'error');
|
||||
return;
|
||||
}
|
||||
uploadFiles.push(f);
|
||||
});
|
||||
renderUploadList();
|
||||
};
|
||||
els.uploadInput.addEventListener('change', (e) => onFiles(e.target.files));
|
||||
els.uploadZone.addEventListener('dragover', (e) => { e.preventDefault(); els.uploadZone.classList.add('is-drop'); });
|
||||
els.uploadZone.addEventListener('dragleave', () => els.uploadZone.classList.remove('is-drop'));
|
||||
els.uploadZone.addEventListener('drop', (e) => {
|
||||
e.preventDefault();
|
||||
els.uploadZone.classList.remove('is-drop');
|
||||
onFiles(e.dataTransfer?.files);
|
||||
});
|
||||
els.uploadClear?.addEventListener('click', () => {
|
||||
uploadFiles = [];
|
||||
els.uploadInput.value = '';
|
||||
renderUploadList();
|
||||
});
|
||||
}
|
||||
|
||||
function renderUploadList() {
|
||||
if (!uploadFiles.length) {
|
||||
els.uploadFileInfo.classList.add('is-hidden');
|
||||
els.uploadPrompt.classList.remove('is-hidden');
|
||||
return;
|
||||
}
|
||||
els.uploadPrompt.classList.add('is-hidden');
|
||||
els.uploadFileInfo.classList.remove('is-hidden');
|
||||
els.uploadFileList.innerHTML = uploadFiles.map((f) => {
|
||||
const kb = (f.size / 1024).toFixed(0);
|
||||
return `<li><span class="upload-filename">${escapeHtml(f.name)}</span><span class="upload-chars">${kb} KB</span></li>`;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
function bindModal() {
|
||||
els.modalClose?.addEventListener('click', closeModal);
|
||||
els.modal?.addEventListener('click', (e) => {
|
||||
if (e.target === els.modal) closeModal();
|
||||
});
|
||||
document.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Escape' && els.modal && !els.modal.classList.contains('is-hidden')) closeModal();
|
||||
});
|
||||
}
|
||||
|
||||
function closeModal() {
|
||||
els.modal?.classList.add('is-hidden');
|
||||
}
|
||||
|
||||
function openModal(source) {
|
||||
if (!source) return;
|
||||
els.modalEyebrow.textContent = source.source_origin === 'upload' ? 'Uploaded file' : 'Corpus source';
|
||||
els.modalTitle.textContent = source.title || 'Source';
|
||||
const metaRows = [
|
||||
['Number', `[${source.n}]`],
|
||||
source.section ? ['Section', source.section] : null,
|
||||
['Corpus / package', source.package_or_corpus || '—'],
|
||||
source.authority_type ? ['Authority', source.authority_type] : null,
|
||||
source.jurisdiction ? ['Jurisdiction', source.jurisdiction] : null,
|
||||
source.similarity != null ? ['Similarity', String(source.similarity)] : null,
|
||||
source.reranker_score != null ? ['Rerank score', String(source.reranker_score)] : null,
|
||||
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 || '';
|
||||
els.modal.classList.remove('is-hidden');
|
||||
}
|
||||
|
||||
function getSelectedSlices() {
|
||||
const out = {};
|
||||
SLICE_DEFS.forEach((s) => {
|
||||
const btn = els.slices.find((b) => b.dataset.slice === s.id);
|
||||
out[s.id] = !!(btn && btn.classList.contains('is-on'));
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
function getEngine() {
|
||||
const checked = els.engineRadios.find((r) => r.checked);
|
||||
return checked ? checked.value : 'azure_mini';
|
||||
}
|
||||
|
||||
function getControls() {
|
||||
return {
|
||||
sub_q_count: parseInt(els.subQ.value, 10),
|
||||
chunk_limit: parseInt(els.chunkLimit.value, 10),
|
||||
similarity_threshold: parseFloat(els.sim.value),
|
||||
reranker_top_k: parseInt(els.topK.value, 10),
|
||||
temperature: parseFloat(els.temp.value),
|
||||
};
|
||||
}
|
||||
|
||||
async function onSubmit(e) {
|
||||
e.preventDefault();
|
||||
|
||||
const advocateRole = getAdvocateRole();
|
||||
if (!advocateRole) {
|
||||
setStatus('Select who you are representing before running.', 'error');
|
||||
return;
|
||||
}
|
||||
const query = (els.input.value || '').trim();
|
||||
if (!query && uploadFiles.length === 0) {
|
||||
setStatus('Describe the case situation or upload a file before running.', 'error');
|
||||
return;
|
||||
}
|
||||
const slices = getSelectedSlices();
|
||||
if (!Object.values(slices).some(Boolean)) {
|
||||
setStatus('Enable at least one corpus slice.', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
const engine = getEngine();
|
||||
const expectedDuration = engine === 'azure_full'
|
||||
? '60–180 seconds with Azure gpt-4o'
|
||||
: (engine === 'gpu' ? '30–90 seconds on GPU' : '15–45 seconds with Azure gpt-4o-mini');
|
||||
|
||||
setStatus(`Building advocate brief for ${advocateRole}… (${expectedDuration})`, 'busy');
|
||||
els.runButton.disabled = true;
|
||||
els.results.innerHTML = `<div class="empty-state"><h3>Researching your case…</h3><p>The agent is generating adversarial sub-questions and retrieving from the legal corpus on behalf of <strong>${escapeHtml(advocateRole)}</strong>. Live progress in the right-hand panel. Expect ${expectedDuration}.</p></div>`;
|
||||
|
||||
const stepState = STEP_LABELS.map((label) => ({ label, detail: 'Queued', status: 'idle' }));
|
||||
renderTrace(stepState);
|
||||
|
||||
const payload = {
|
||||
query,
|
||||
paste_text: '',
|
||||
slices,
|
||||
engine,
|
||||
language: lang,
|
||||
controls: getControls(),
|
||||
advocate_role: advocateRole,
|
||||
};
|
||||
|
||||
const stepKeyToIndex = {
|
||||
interpretation: 0,
|
||||
expansion: 1,
|
||||
slice_resolution: 2,
|
||||
upload_indexing: 3,
|
||||
retrieval: 4,
|
||||
synthesis: 5,
|
||||
confidence: 6,
|
||||
};
|
||||
|
||||
let response;
|
||||
try {
|
||||
if (uploadFiles.length > 0) {
|
||||
const form = new FormData();
|
||||
form.append('payload', JSON.stringify(payload));
|
||||
uploadFiles.forEach((f) => form.append('files[]', f));
|
||||
response = await fetch('api/deep-research.php', { method: 'POST', body: form, credentials: 'same-origin' });
|
||||
} else {
|
||||
response = await fetch('api/deep-research.php', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
setStatus(`Network error: ${err.message || err}`, 'error');
|
||||
els.runButton.disabled = false;
|
||||
stepState[0] = { ...stepState[0], status: 'error', detail: String(err) };
|
||||
renderTrace(stepState);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!response.ok || !response.body) {
|
||||
setStatus(`Request failed (${response.status}).`, 'error');
|
||||
els.runButton.disabled = false;
|
||||
return;
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
let buffer = '';
|
||||
let finalResult = null;
|
||||
let errorEvent = null;
|
||||
|
||||
while (true) {
|
||||
let chunk;
|
||||
try {
|
||||
chunk = await reader.read();
|
||||
} catch (err) {
|
||||
setStatus(`Stream error: ${err.message || err}`, 'error');
|
||||
els.runButton.disabled = false;
|
||||
return;
|
||||
}
|
||||
const { done, value } = chunk;
|
||||
if (value) {
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
const lines = buffer.split('\n');
|
||||
buffer = lines.pop();
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed) continue;
|
||||
let evt;
|
||||
try { evt = JSON.parse(trimmed); } catch (_) { continue; }
|
||||
handleStreamEvent(evt);
|
||||
}
|
||||
}
|
||||
if (done) break;
|
||||
}
|
||||
|
||||
if (errorEvent) {
|
||||
setStatus(`${errorEvent.code}: ${errorEvent.message}`, 'error');
|
||||
els.runButton.disabled = false;
|
||||
const runningIdx = stepState.findIndex((s) => s.status === 'running');
|
||||
if (runningIdx >= 0) {
|
||||
stepState[runningIdx] = { ...stepState[runningIdx], status: 'error', detail: errorEvent.message };
|
||||
renderTrace(stepState);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!finalResult) {
|
||||
setStatus('Stream ended without a final result.', 'error');
|
||||
els.runButton.disabled = false;
|
||||
return;
|
||||
}
|
||||
|
||||
lastResult = finalResult;
|
||||
const meta = finalResult.trace_metadata || {};
|
||||
const rc = meta.retrieval_counts || {};
|
||||
const countSummary = (rc.post_filter_corpus != null)
|
||||
? `${rc.post_filter_corpus} corpus${rc.filtered_website ? ` (${rc.filtered_website} website filtered)` : ''}${rc.raw_upload ? ` + ${rc.raw_upload} upload` : ''}`
|
||||
: `${meta.source_count || 0} sources`;
|
||||
setStatus(
|
||||
`Done in ${Math.round((finalResult.latency_ms || 0) / 1000)} s · ${countSummary} · confidence ${meta.citation_confidence || '?'}`,
|
||||
'ok'
|
||||
);
|
||||
els.runButton.disabled = false;
|
||||
renderTrace(finalResult.trace || []);
|
||||
renderResults(finalResult);
|
||||
|
||||
function handleStreamEvent(evt) {
|
||||
if (!evt || !evt.event) return;
|
||||
if (evt.event === 'progress') {
|
||||
const d = evt.detail || '';
|
||||
if (d) setStatus(d, 'busy');
|
||||
return;
|
||||
}
|
||||
if (evt.event === 'start') {
|
||||
setStatus(`Running… engine=${evt.engine}, uploads=${evt.upload_count || 0}`, 'busy');
|
||||
return;
|
||||
}
|
||||
if (evt.event === 'step') {
|
||||
const idx = stepKeyToIndex[evt.step];
|
||||
if (idx === undefined) return;
|
||||
stepState[idx] = {
|
||||
label: evt.label || stepState[idx].label,
|
||||
detail: evt.detail || stepState[idx].detail,
|
||||
status: evt.status || stepState[idx].status,
|
||||
};
|
||||
renderTrace(stepState);
|
||||
return;
|
||||
}
|
||||
if (evt.event === 'subq') {
|
||||
setStatus(`Retrieving sub-question ${evt.index}/${evt.total}: ${evt.question.slice(0, 80)}${evt.question.length > 80 ? '…' : ''}`, 'busy');
|
||||
return;
|
||||
}
|
||||
if (evt.event === 'final') {
|
||||
finalResult = evt.result;
|
||||
return;
|
||||
}
|
||||
if (evt.event === 'error') {
|
||||
errorEvent = evt;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setStatus(message, kind) {
|
||||
els.status.textContent = message;
|
||||
els.status.style.color = kind === 'error' ? '#b41e1e' : (kind === 'ok' ? 'var(--teal-dark)' : 'var(--muted)');
|
||||
}
|
||||
|
||||
function renderTrace(steps) {
|
||||
if (!els.traceList) return;
|
||||
els.traceList.classList.add('is-rich');
|
||||
els.traceList.innerHTML = steps.map((step, i) => {
|
||||
const statusClass = step.status === 'running' ? 'is-running'
|
||||
: step.status === 'complete' ? 'is-done'
|
||||
: step.status === 'warning' ? 'is-warning'
|
||||
: step.status === 'error' ? 'is-error'
|
||||
: '';
|
||||
const marker = step.status === 'complete' ? '✓'
|
||||
: step.status === 'warning' ? '!'
|
||||
: step.status === 'error' ? '×'
|
||||
: (i + 1);
|
||||
return `<li class="trace-step ${statusClass}">
|
||||
<span class="trace-step__marker">${marker}</span>
|
||||
<div>
|
||||
<span class="trace-step__label">${escapeHtml(step.label || '')}</span>
|
||||
<span class="trace-step__detail">${escapeHtml(step.detail || '')}</span>
|
||||
</div>
|
||||
</li>`;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
function renderResults(data) {
|
||||
const sources = data.sources || [];
|
||||
const subs = data.sub_questions || [];
|
||||
const role = data.advocate_role || '';
|
||||
const strengths = Array.isArray(data.client_strengths) ? data.client_strengths : [];
|
||||
const weaknesses = Array.isArray(data.opposing_weaknesses) ? data.opposing_weaknesses : [];
|
||||
|
||||
// 1. Advocate banner
|
||||
const bannerHtml = role ? `
|
||||
<div class="adv-banner">
|
||||
<span class="adv-banner__label">Representing</span>
|
||||
<strong class="adv-banner__role">${escapeHtml(role)}</strong>
|
||||
<span class="adv-banner__note">Brief argues for this party · grounded in Norwegian law and ECHR authorities</span>
|
||||
</div>` : '';
|
||||
|
||||
// 2. Client strengths
|
||||
const strengthsHtml = strengths.length ? `
|
||||
<div class="dr-result-block adv-strengths">
|
||||
<h3 class="adv-strengths__head">Your strongest arguments</h3>
|
||||
<ul class="adv-strengths__list">
|
||||
${strengths.map((s) => `<li class="adv-strengths__item">${renderInlineCitations(escapeHtml(String(s)), sources)}</li>`).join('')}
|
||||
</ul>
|
||||
</div>` : '';
|
||||
|
||||
// 3. Brief
|
||||
const briefHtml = renderBrief(data.brief_markdown || '', sources);
|
||||
|
||||
// 4. Opposing weaknesses
|
||||
const weaknessesHtml = weaknesses.length ? `
|
||||
<div class="dr-result-block adv-weaknesses">
|
||||
<h3 class="adv-weaknesses__head">Gaps in the opposing position</h3>
|
||||
<ul class="adv-weaknesses__list">
|
||||
${weaknesses.map((w) => `<li class="adv-weaknesses__item">${renderInlineCitations(escapeHtml(String(w)), sources)}</li>`).join('')}
|
||||
</ul>
|
||||
</div>` : '';
|
||||
|
||||
// 5. Sub-Q report cards
|
||||
const subQReportsHtml = subs.length ? `
|
||||
<div class="dr-result-block">
|
||||
<div class="dr-sources-head">
|
||||
<h3>What each sub-question agent researched</h3>
|
||||
<small>${subs.length} sub-question${subs.length === 1 ? '' : 's'} framed for ${escapeHtml(role || 'your client')}</small>
|
||||
</div>
|
||||
<div class="dr-subq-list">
|
||||
${subs.map((sq, i) => renderSubQReport(sq, i)).join('')}
|
||||
</div>
|
||||
</div>` : '';
|
||||
|
||||
// 6. Sources
|
||||
const sourcesHtml = `
|
||||
<div class="dr-result-block">
|
||||
<div class="dr-sources-head">
|
||||
<h3>All sources (${sources.length})</h3>
|
||||
<small>Click a card to see the full chunk · external link opens the original article</small>
|
||||
</div>
|
||||
<div class="dr-source-list">
|
||||
${sources.map((s) => renderSourceCard(s)).join('')}
|
||||
</div>
|
||||
</div>`;
|
||||
|
||||
// 7. Uncertainty
|
||||
const uncertHtml = (data.what_remains_uncertain || []).length ? `
|
||||
<div class="dr-result-block">
|
||||
<h3 style="margin:0 0 8px;font-size:0.95rem;color:var(--muted)">What remains uncertain</h3>
|
||||
<ul style="padding-left:1.2em;margin:0;color:var(--muted);line-height:1.55">
|
||||
${(data.what_remains_uncertain || []).map((u) => `<li>${escapeHtml(String(u))}</li>`).join('')}
|
||||
</ul>
|
||||
</div>` : '';
|
||||
|
||||
// 8. Next step
|
||||
const nextHtml = data.next_practical_step ? `
|
||||
<div class="dr-result-block">
|
||||
<h3 style="margin:0 0 6px;font-size:0.95rem">Next practical step</h3>
|
||||
<p style="margin:0;color:var(--ink);line-height:1.5">${escapeHtml(data.next_practical_step)}</p>
|
||||
</div>` : '';
|
||||
|
||||
els.results.innerHTML = `
|
||||
${bannerHtml}
|
||||
${strengthsHtml}
|
||||
<div class="dr-result-block">
|
||||
<h3 style="margin:0 0 10px;font-size:1rem">Advocate brief</h3>
|
||||
<div class="dr-brief">${briefHtml}</div>
|
||||
</div>
|
||||
${weaknessesHtml}
|
||||
${subQReportsHtml}
|
||||
${sourcesHtml}
|
||||
${uncertHtml}
|
||||
${nextHtml}
|
||||
`;
|
||||
|
||||
// Bind source-card clicks
|
||||
els.results.querySelectorAll('.dr-source-card[data-source-n]').forEach((node) => {
|
||||
node.addEventListener('click', (e) => {
|
||||
if (e.target.closest('a')) return;
|
||||
const n = parseInt(node.dataset.sourceN, 10);
|
||||
const src = sources.find((s) => s.n === n);
|
||||
if (src) { openModal(src); flashSource(n); }
|
||||
});
|
||||
});
|
||||
// Bind inline citation markers
|
||||
els.results.querySelectorAll('.dr-cite[data-source-n]').forEach((node) => {
|
||||
node.addEventListener('click', (e) => {
|
||||
if (e.target.closest('a')) return;
|
||||
flashSource(parseInt(node.dataset.sourceN, 10));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Convert [n] markers already present in escaped HTML into clickable spans
|
||||
function renderInlineCitations(escapedHtml, sources) {
|
||||
const sourceSet = new Set((sources || []).map((s) => s.n));
|
||||
return escapedHtml.replace(/\[(\d+(?:\s*[-,]\s*\d+)*)\]/g, (_, group) => {
|
||||
const nums = expandCiteGroup(group);
|
||||
return nums.map((n) => `<span class="dr-cite" data-source-n="${n}" role="button" tabindex="0">${n}</span>`).join('');
|
||||
});
|
||||
}
|
||||
|
||||
function renderSubQReport(sq, idx) {
|
||||
const top = sq.top_sources || [];
|
||||
const sourceItems = top.length
|
||||
? top.map((s) => {
|
||||
const link = s.deep_link || s.source_url;
|
||||
const titleHtml = link
|
||||
? `<a href="${escapeHtml(link)}" target="_blank" rel="noopener" class="dr-mini-source__title">${escapeHtml(s.title || 'Untitled')} <span class="dr-external-link" aria-hidden="true">↗</span></a>`
|
||||
: `<span class="dr-mini-source__title">${escapeHtml(s.title || 'Untitled')}</span>`;
|
||||
const meta = [];
|
||||
if (s.section) meta.push(escapeHtml(s.section));
|
||||
if (s.authority_label) meta.push(escapeHtml(s.authority_label));
|
||||
if (s.source_origin === 'upload') meta.push('your upload');
|
||||
return `<li class="dr-mini-source">
|
||||
<span class="dr-mini-source__n">[${s.n ?? '?'}]</span>
|
||||
<div class="dr-mini-source__body">
|
||||
${titleHtml}
|
||||
${meta.length ? `<div class="dr-mini-source__meta">${meta.join(' · ')}</div>` : ''}
|
||||
<div class="dr-mini-source__excerpt">${escapeHtml(truncate(s.excerpt || '', 180))}</div>
|
||||
</div>
|
||||
</li>`;
|
||||
}).join('')
|
||||
: `<li class="dr-mini-source dr-mini-source--empty"><em>No sources retrieved for this sub-question.</em></li>`;
|
||||
|
||||
return `<div class="dr-subq-report">
|
||||
<div class="dr-subq-report__head">
|
||||
<span class="dr-subq-report__index">${escapeHtml(sq.id || ('q' + (idx + 1)))}</span>
|
||||
<div class="dr-subq-report__body">
|
||||
<div class="dr-subq-report__question">${escapeHtml(sq.question || '')}</div>
|
||||
${sq.rationale ? `<div class="dr-subq-report__rationale">${escapeHtml(sq.rationale)}</div>` : ''}
|
||||
</div>
|
||||
</div>
|
||||
<ul class="dr-mini-source-list">${sourceItems}</ul>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
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}"]`);
|
||||
if (target) {
|
||||
target.classList.add('is-highlight');
|
||||
target.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
||||
setTimeout(() => target.classList.remove('is-highlight'), 1800);
|
||||
}
|
||||
}
|
||||
|
||||
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';
|
||||
const originLabel = s.source_origin === 'upload' ? 'upload' : 'corpus';
|
||||
const link = s.deep_link || s.source_url;
|
||||
const titleHtml = link
|
||||
? `<a href="${escapeHtml(link)}" target="_blank" rel="noopener" class="dr-source-title-link">${escapeHtml(s.title || 'Untitled')} <span class="dr-external-link" aria-hidden="true">↗</span></a>`
|
||||
: `${escapeHtml(s.title || 'Untitled')}`;
|
||||
return `<div class="dr-source-card" data-source-n="${s.n}" role="button" tabindex="0">
|
||||
<span class="dr-source-number">${s.n}</span>
|
||||
<div class="dr-source-body">
|
||||
<div class="dr-source-title">${titleHtml}</div>
|
||||
${s.section ? `<div class="dr-source-meta"><span class="dr-source-tag">${escapeHtml(s.section)}</span></div>` : ''}
|
||||
<div class="dr-source-meta">
|
||||
<span class="${originTagClass}">${originLabel}</span>
|
||||
${s.authority_label ? `<span class="dr-source-tag">${escapeHtml(s.authority_label)}</span>` : ''}
|
||||
<span class="dr-source-tag dr-source-tag--score">${escapeHtml(s.package_or_corpus || '—')}</span>
|
||||
${(s.matched_sub_questions || []).map((q) => `<span class="dr-source-tag">${escapeHtml(q)}</span>`).join('')}
|
||||
</div>
|
||||
<p class="dr-source-excerpt">${escapeHtml(truncate(s.excerpt || '', 240))}</p>
|
||||
</div>
|
||||
<div class="dr-source-aside">
|
||||
<span>score<br><b>${score != null ? Number(score).toFixed(2) : '—'}</b></span>
|
||||
${s.reranker_score != null && s.similarity != null ? `<span>sim<br><b>${Number(s.similarity).toFixed(2)}</b></span>` : ''}
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function renderBrief(markdown, sources) {
|
||||
if (!markdown) return '<p><em>No brief was returned.</em></p>';
|
||||
const sourceSet = new Set((sources || []).map((s) => s.n));
|
||||
const escaped = escapeHtml(markdown);
|
||||
|
||||
const withCites = escaped.replace(/\[(\d+(?:\s*[-,]\s*\d+)*)\]/g, (_, group) => {
|
||||
const nums = expandCiteGroup(group);
|
||||
return nums.map((n) => `<span class="dr-cite" data-source-n="${n}" role="button" tabindex="0">${n}</span>`).join('');
|
||||
});
|
||||
|
||||
const withBold = withCites
|
||||
.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
|
||||
.replace(/(^|[^*])\*([^*]+)\*(?!\*)/g, '$1<em>$2</em>')
|
||||
.replace(/`([^`]+)`/g, '<code>$1</code>');
|
||||
|
||||
const paragraphs = withBold.split(/\n{2,}/).map((p) => {
|
||||
const t = p.trim();
|
||||
if (!t) return '';
|
||||
if (/^### /.test(t)) return `<h4 style="margin:14px 0 6px;color:var(--ink);font-size:1rem">${t.replace(/^### /, '')}</h4>`;
|
||||
return `<p>${t.replace(/\n/g, '<br>')}</p>`;
|
||||
}).join('');
|
||||
|
||||
return paragraphs;
|
||||
}
|
||||
|
||||
function expandCiteGroup(group) {
|
||||
const out = [];
|
||||
group.split(',').forEach((part) => {
|
||||
const range = part.trim().match(/^(\d+)\s*-\s*(\d+)$/);
|
||||
if (range) {
|
||||
const a = parseInt(range[1], 10);
|
||||
const b = parseInt(range[2], 10);
|
||||
for (let i = a; i <= b; i++) out.push(i);
|
||||
} else {
|
||||
const n = parseInt(part.trim(), 10);
|
||||
if (!Number.isNaN(n)) out.push(n);
|
||||
}
|
||||
});
|
||||
return Array.from(new Set(out));
|
||||
}
|
||||
|
||||
function escapeHtml(s) {
|
||||
return String(s)
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
|
||||
function truncate(s, n) {
|
||||
if (!s) return '';
|
||||
if (s.length <= n) return s;
|
||||
return s.slice(0, n - 1) + '…';
|
||||
}
|
||||
})();
|
||||
@@ -30,7 +30,8 @@ final class DbnDeepResearchAgent
|
||||
string $engine,
|
||||
string $language,
|
||||
array $controls,
|
||||
?callable $emit = null
|
||||
?callable $emit = null,
|
||||
string $advocateRole = ''
|
||||
): array {
|
||||
$seedQuery = trim($seedQuery);
|
||||
$pastedText = trim($pastedText);
|
||||
@@ -81,14 +82,14 @@ final class DbnDeepResearchAgent
|
||||
// STEP 1: Query interpretation
|
||||
$emitRunning('interpretation', 'Query interpretation', 'Summarising the seed input…');
|
||||
$stepStart = microtime(true);
|
||||
$interpretation = $this->interpretSeed($seedDescription, $language);
|
||||
$interpretation = $this->interpretSeed($seedDescription, $language, $advocateRole);
|
||||
$this->stepTimings['interpretation'] = $this->elapsedMs($stepStart);
|
||||
$emitStep('interpretation', 'Query interpretation', $interpretation['detail'], 'complete');
|
||||
|
||||
// STEP 2: Query expansion
|
||||
$emitRunning('expansion', 'Query expansion', 'Generating sub-questions…');
|
||||
$stepStart = microtime(true);
|
||||
$expansion = $this->expandQueries($seedDescription, $interpretation['brief'], $controls['sub_q_count'], $language);
|
||||
$expansion = $this->expandQueries($seedDescription, $interpretation['brief'], $controls['sub_q_count'], $language, $advocateRole);
|
||||
$this->stepTimings['expansion'] = $this->elapsedMs($stepStart);
|
||||
$subQuestions = $expansion['questions'];
|
||||
$expansionStatus = $expansion['fallback'] ? 'warning' : 'complete';
|
||||
@@ -290,7 +291,8 @@ final class DbnDeepResearchAgent
|
||||
$numberedSources,
|
||||
$engine,
|
||||
$language,
|
||||
$controls['temperature']
|
||||
$controls['temperature'],
|
||||
$advocateRole
|
||||
);
|
||||
$this->stepTimings['synthesis'] = $this->elapsedMs($stepStart);
|
||||
$emitStep(
|
||||
@@ -335,10 +337,14 @@ final class DbnDeepResearchAgent
|
||||
];
|
||||
}
|
||||
|
||||
$isAdvocate = $advocateRole !== '';
|
||||
return [
|
||||
'tool' => 'deep_research',
|
||||
'tool' => $isAdvocate ? 'advocate' : 'deep_research',
|
||||
'language' => $language,
|
||||
'advocate_role' => $isAdvocate ? $advocateRole : null,
|
||||
'brief_markdown' => (string)($synthesis['json']['brief_markdown'] ?? $synthesis['json']['answer'] ?? ''),
|
||||
'client_strengths' => $isAdvocate ? ($synthesis['json']['client_strengths'] ?? []) : null,
|
||||
'opposing_weaknesses' => $isAdvocate ? ($synthesis['json']['opposing_weaknesses'] ?? []) : null,
|
||||
'sub_questions' => $subQOut,
|
||||
'sources' => $numberedSources,
|
||||
'what_we_found' => (string)($synthesis['json']['what_we_found'] ?? ''),
|
||||
@@ -405,11 +411,14 @@ final class DbnDeepResearchAgent
|
||||
return implode("\n\n", $parts);
|
||||
}
|
||||
|
||||
private function interpretSeed(string $seedDescription, string $language): array
|
||||
private function interpretSeed(string $seedDescription, string $language, string $advocateRole = ''): 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"
|
||||
: '';
|
||||
$prompt = <<<PROMPT
|
||||
You are reviewing the input below to set up a deep legal research pass against the Do Better Norge family-law corpus.
|
||||
{$rolePrefix}You are reviewing the input below to set up a deep legal research pass against the Do Better Norge family-law corpus.
|
||||
|
||||
Input:
|
||||
{$seedDescription}
|
||||
@@ -445,9 +454,40 @@ PROMPT;
|
||||
];
|
||||
}
|
||||
|
||||
private function expandQueries(string $seedDescription, string $brief, int $targetCount, string $language): array
|
||||
private function expandQueries(string $seedDescription, string $brief, int $targetCount, string $language, string $advocateRole = ''): array
|
||||
{
|
||||
$locale = $language === 'no' ? 'Norwegian' : 'English';
|
||||
|
||||
if ($advocateRole !== '') {
|
||||
$prompt = <<<PROMPT
|
||||
You are a Norwegian family-law research assistant building a case for: {$advocateRole}.
|
||||
Generate exactly {$targetCount} targeted sub-questions designed to find:
|
||||
1. Lovdata statutes and ECHR/Hague precedents that support {$advocateRole}'s position.
|
||||
2. Procedural rights and obligations the opposing party must satisfy — failures here help {$advocateRole}.
|
||||
3. Case law that exposes weaknesses in the opposing party's likely arguments.
|
||||
4. Specific articles, paragraphs, or judgments {$advocateRole}'s representative should cite.
|
||||
|
||||
Research brief:
|
||||
{$brief}
|
||||
|
||||
Raw input:
|
||||
{$seedDescription}
|
||||
|
||||
Return JSON only in {$locale}:
|
||||
{
|
||||
"sub_questions": [
|
||||
{"id":"q1","question":"...","rationale":"how finding this strengthens {$advocateRole}'s case (≤ 140 chars)"}
|
||||
]
|
||||
}
|
||||
|
||||
Rules:
|
||||
- Exactly {$targetCount} sub-questions, no more, no fewer.
|
||||
- Every question must be answerable from Norwegian family-law, child-welfare, or ECHR/Hague sources.
|
||||
- Each question must cover a DIFFERENT angle (supporting statute, procedural right, opposing weakness, ECHR precedent, evidentiary frame).
|
||||
- Sub-questions must be self-contained — readable without the raw input.
|
||||
- Write the questions in {$locale}.
|
||||
PROMPT;
|
||||
} else {
|
||||
$prompt = <<<PROMPT
|
||||
You are decomposing a Do Better Norge legal-research request into {$targetCount} focused sub-questions that should each be answered by the legal corpus (Norwegian family law, child welfare, ECHR/Hague).
|
||||
|
||||
@@ -471,6 +511,7 @@ Rules:
|
||||
- Sub-questions must be self-contained — readable without seeing the seed text.
|
||||
- Write the questions in {$locale}.
|
||||
PROMPT;
|
||||
}
|
||||
|
||||
try {
|
||||
$raw = $this->azure->chatText([
|
||||
@@ -790,7 +831,8 @@ PROMPT;
|
||||
array $numberedSources,
|
||||
string $engine,
|
||||
string $language,
|
||||
float $temperature
|
||||
float $temperature,
|
||||
string $advocateRole = ''
|
||||
): array {
|
||||
$locale = $language === 'no' ? 'Norwegian' : 'English';
|
||||
|
||||
@@ -839,6 +881,43 @@ PROMPT;
|
||||
? '400-900 words, minimum 4 paragraphs, with clear paragraph breaks. Cover EACH sub-question above in its own paragraph.'
|
||||
: '250-450 words, 2-3 short paragraphs. Note when evidence is thin.';
|
||||
|
||||
if ($advocateRole !== '') {
|
||||
$prompt = <<<PROMPT
|
||||
You are Do Better Norge Legal Tools producing a legal preparation brief in {$locale}.
|
||||
Your client: {$advocateRole}
|
||||
|
||||
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:
|
||||
{$seedDescription}
|
||||
|
||||
Research brief:
|
||||
{$brief}
|
||||
{$subQText}
|
||||
|
||||
Sources ({$sourceCount} numbered):
|
||||
{$sourcesText}
|
||||
|
||||
Return JSON only in {$locale}:
|
||||
{
|
||||
"brief_markdown": "Partisan but factually grounded advocate brief. {$lengthGuidance} Structure: (1) {$advocateRole}'s core legal position, (2) Strongest supporting arguments with [n] citations, (3) Identified weaknesses in the opposing party's position with [n] citations, (4) Procedural rights and obligations {$advocateRole} should assert. End with a one-line caveat that this is legal preparation support, not final legal advice.",
|
||||
"client_strengths": ["3-6 strings — the strongest factual/legal points for {$advocateRole}, each anchored to at least one [n] source"],
|
||||
"opposing_weaknesses": ["2-5 strings — vulnerabilities in the opposing position supported by retrieved sources. Omit this array entirely if evidence is thin — do NOT invent weaknesses."],
|
||||
"what_we_found": "2-sentence summary of the most relevant retrieved authority for {$advocateRole}",
|
||||
"what_remains_uncertain": ["3-5 gaps where evidence is insufficient or law is unclear — be honest"],
|
||||
"next_practical_step": "one concrete action for {$advocateRole} to take next (legal filing, evidence gathering, consultation type, etc.)"
|
||||
}
|
||||
|
||||
Rules:
|
||||
- Every factual claim in `brief_markdown` must end with one or more `[n]` markers.
|
||||
- If no source supports a point, omit the point — DO NOT speculate.
|
||||
- Prefer citing statute sections (e.g. "Barneloven §43") and case names verbatim from source excerpts.
|
||||
- When multiple sources support the same point, cite all of them (e.g. `[2,4]`).
|
||||
- `opposing_weaknesses` must be omitted or empty when no retrieved source actually supports the identified weakness.
|
||||
- Respond in {$locale}.
|
||||
- Output valid JSON only — no markdown fences around the JSON object itself.
|
||||
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.
|
||||
|
||||
@@ -868,6 +947,7 @@ Rules:
|
||||
- Respond in {$locale}.
|
||||
- Output valid JSON only — no markdown fences around the JSON object itself.
|
||||
PROMPT;
|
||||
}
|
||||
|
||||
$messages = [
|
||||
['role' => 'system', 'content' => 'You return valid JSON only. No markdown fences.'],
|
||||
|
||||
@@ -12,6 +12,7 @@ $navItems = [
|
||||
'ask' => ['Ask', 'Source-grounded'],
|
||||
'search' => ['Search', 'Legal sources'],
|
||||
'deep-research' => ['Deep research', 'Agent + RAG'],
|
||||
'advocate' => ['Advocate', 'Take a side'],
|
||||
'summarize' => ['Summarize', 'Pasted text'],
|
||||
'timeline' => ['Timeline', 'Events'],
|
||||
'redact' => ['Redact', 'Privacy'],
|
||||
|
||||
@@ -91,7 +91,7 @@ if (dbnToolsIsAuthenticated()) {
|
||||
|
||||
<section class="cap-section">
|
||||
<div class="section-inner">
|
||||
<h2 class="section-heading">Seven tools, one suite</h2>
|
||||
<h2 class="section-heading">Eight tools, one suite</h2>
|
||||
<div class="cap-grid">
|
||||
<div class="cap-card">
|
||||
<span class="cap-label">Ask</span>
|
||||
@@ -108,6 +108,11 @@ if (dbnToolsIsAuthenticated()) {
|
||||
<h3>Deep research</h3>
|
||||
<p>Upload a case file or paste a question. An agent expands it into 3–5 angles, runs hybrid rank/rerank RAG across the corpus + your upload, and returns a cited brief.</p>
|
||||
</div>
|
||||
<div class="cap-card cap-card--featured">
|
||||
<span class="cap-label">Advocate</span>
|
||||
<h3>Case Advocate</h3>
|
||||
<p>Pick who you represent. The agent takes your side — generating adversarial sub-questions, identifying the opposing party’s weaknesses, and producing a partisan brief grounded in Lovdata statutes and ECHR judgments.</p>
|
||||
</div>
|
||||
<div class="cap-card">
|
||||
<span class="cap-label">Summarize</span>
|
||||
<h3>Summarize</h3>
|
||||
|
||||
Reference in New Issue
Block a user