Add Document Discrepancy Finder tool

8-step NDJSON-streaming pipeline that compares two Barnevernet documents:
classifies each doc, extracts parties and timelines, cross-references both
for contradictions/deletions/additions, retrieves corpus legal context, and
synthesises a full discrepancy report with tabbed UI.

New files: DiscrepancyAgent.php, api/discrepancy.php, discrepancy.php,
discrepancy.js. Modified: FreeTier.php (cost=4), i18n.php (all 4 langs),
tool-svgs.php (DC icon), tools.css (dc-* component styles).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-18 19:30:38 +02:00
parent 1246b7a804
commit e977bbb6b3
8 changed files with 2809 additions and 7 deletions
+161
View File
@@ -0,0 +1,161 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/bootstrap.php';
require_once __DIR__ . '/../includes/DiscrepancyAgent.php';
dbnToolsRequireMethod('POST');
dbnToolsRequireAuth();
$ftUid = dbnToolsFreeTierCheck('discrepancy');
$ftRemaining = dbnToolsFreeTierDeduct($ftUid, 'discrepancy');
@ini_set('output_buffering', '0');
@ini_set('zlib.output_compression', '0');
@ini_set('implicit_flush', '1');
while (ob_get_level() > 0) { @ob_end_clean(); }
ob_implicit_flush(true);
header('Content-Type: application/x-ndjson; charset=utf-8');
header('Cache-Control: no-store');
header('X-Accel-Buffering: no');
if ($ftRemaining >= 0) { header('X-Credits-Remaining: ' . $ftRemaining); }
$language = 'en';
$startTime = microtime(true);
$emit = function (string $event, array $payload = []) use ($startTime): void {
$payload['event'] = $event;
$payload['t_ms'] = (int)round((microtime(true) - $startTime) * 1000);
echo json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
@flush();
};
try {
// Parse payload (always multipart — two files required)
$payloadRaw = (string)($_POST['payload'] ?? '');
if ($payloadRaw === '') {
throw new DbnToolsHttpException('Missing payload field.', 422, 'missing_payload');
}
$input = json_decode($payloadRaw, true);
if (!is_array($input)) {
throw new DbnToolsHttpException('Invalid payload JSON.', 422, 'invalid_payload_json');
}
$language = dbnToolsNormalizeLanguage($input['language'] ?? 'en');
$engine = (string)($input['engine'] ?? 'azure_mini');
$sliceInput = $input['slices'] ?? [];
// Extract file A
$emit('progress', ['detail' => 'Reading Document A…']);
$fileEntryA = $_FILES['file_a'] ?? null;
if (!$fileEntryA || ($fileEntryA['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
throw new DbnToolsHttpException(
'Document A is required. Upload a PDF, DOCX, or TXT file.',
422, 'missing_file_a'
);
}
$extractedA = dbnToolsExtractUploadedFile([
'name' => $fileEntryA['name'] ?? '',
'type' => $fileEntryA['type'] ?? '',
'tmp_name' => $fileEntryA['tmp_name'] ?? '',
'error' => $fileEntryA['error'] ?? UPLOAD_ERR_NO_FILE,
'size' => $fileEntryA['size'] ?? 0,
]);
$fileA = [
'filename' => $extractedA['filename'],
'text' => $extractedA['text'],
'chars' => $extractedA['chars'],
'truncated' => $extractedA['truncated'],
];
$emit('progress', ['detail' => sprintf('Document A extracted: %s (%d chars%s)',
$extractedA['filename'], $extractedA['chars'],
!empty($extractedA['truncated']) ? ', truncated' : '')]);
// Extract file B
$emit('progress', ['detail' => 'Reading Document B…']);
$fileEntryB = $_FILES['file_b'] ?? null;
if (!$fileEntryB || ($fileEntryB['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
throw new DbnToolsHttpException(
'Document B is required. Upload a PDF, DOCX, or TXT file.',
422, 'missing_file_b'
);
}
$extractedB = dbnToolsExtractUploadedFile([
'name' => $fileEntryB['name'] ?? '',
'type' => $fileEntryB['type'] ?? '',
'tmp_name' => $fileEntryB['tmp_name'] ?? '',
'error' => $fileEntryB['error'] ?? UPLOAD_ERR_NO_FILE,
'size' => $fileEntryB['size'] ?? 0,
]);
$fileB = [
'filename' => $extractedB['filename'],
'text' => $extractedB['text'],
'chars' => $extractedB['chars'],
'truncated' => $extractedB['truncated'],
];
$emit('progress', ['detail' => sprintf('Document B extracted: %s (%d chars%s)',
$extractedB['filename'], $extractedB['chars'],
!empty($extractedB['truncated']) ? ', truncated' : '')]);
if (($fileA['text'] ?? '') === '') {
throw new DbnToolsHttpException('Could not extract text from Document A.', 422, 'empty_file_a');
}
if (($fileB['text'] ?? '') === '') {
throw new DbnToolsHttpException('Could not extract text from Document B.', 422, 'empty_file_b');
}
$emit('start', [
'engine' => $engine,
'language' => $language,
'file_a' => $fileA['filename'],
'file_b' => $fileB['filename'],
]);
$result = (new DbnDiscrepancyAgent())->run(
$fileA,
$fileB,
$engine,
$language,
is_array($sliceInput) ? $sliceInput : [],
$emit
);
$result['ok'] = true;
$result['latency_ms'] = (int)round((microtime(true) - $startTime) * 1000);
dbnToolsLogMetadata([
'tool' => 'discrepancy',
'language' => $language,
'ok' => true,
'latency_ms' => $result['latency_ms'],
'source_count' => (int)($result['trace_metadata']['source_count'] ?? 0),
'conflict_count' => (int)($result['trace_metadata']['conflict_count'] ?? 0),
'deleted_count' => (int)($result['trace_metadata']['deleted_count'] ?? 0),
'added_count' => (int)($result['trace_metadata']['added_count'] ?? 0),
'deployment' => $result['trace_metadata']['deployment'] ?? null,
]);
$emit('final', ['result' => $result]);
} catch (DbnToolsHttpException $e) {
$latency = (int)round((microtime(true) - $startTime) * 1000);
dbnToolsLogMetadata([
'tool' => 'discrepancy',
'language' => $language,
'ok' => false,
'latency_ms' => $latency,
'error_code' => $e->errorCode,
]);
$emit('error', ['code' => $e->errorCode, 'message' => $e->getMessage(), 'status' => $e->status]);
} catch (Throwable $e) {
error_log('DBN discrepancy fatal: ' . $e->getMessage());
$latency = (int)round((microtime(true) - $startTime) * 1000);
dbnToolsLogMetadata([
'tool' => 'discrepancy',
'language' => $language,
'ok' => false,
'latency_ms' => $latency,
'error_code' => 'internal_error',
]);
$emit('error', ['code' => 'internal_error', 'message' => 'The discrepancy finder could not complete this request.']);
}
+528
View File
@@ -6376,6 +6376,534 @@ body.lt-landing {
}
}
/* ─── Discrepancy Finder (dc-*) ──────────────────────────────────────────── */
/* Two upload zones side by side */
.dc-upload-pair {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 16px;
margin-bottom: 16px;
}
.dc-upload-slot {
display: flex;
flex-direction: column;
gap: 6px;
}
.dc-slot-hint {
font-weight: 400;
color: var(--muted);
font-size: 0.86em;
}
.dc-zone input[type="file"] {
position: absolute;
width: 0;
height: 0;
opacity: 0;
pointer-events: none;
}
/* Progressive doc meta cards */
.dc-doc-meta-pair {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px;
margin-bottom: 16px;
}
.dc-doc-meta-card {
background: var(--panel);
border: 1px solid var(--line);
border-radius: 10px;
padding: 14px 16px;
}
.dc-doc-meta-card__head {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 10px;
}
.dc-slot-label {
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.05em;
text-transform: uppercase;
color: var(--muted);
background: #f3f4f6;
border: 1px solid var(--line);
border-radius: 4px;
padding: 2px 7px;
}
.dc-slot-label--a { background: #e8f0fe; color: #2d5fa6; border-color: #c3d4f8; }
.dc-slot-label--b { background: var(--soft-coral); color: var(--coral); border-color: #f9c6ae; }
/* Parties preview (stream-time) */
.dc-parties-preview {
margin-bottom: 10px;
}
.dc-parties-chips {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-top: 6px;
}
.dc-party-chip {
display: inline-flex;
align-items: center;
gap: 4px;
font-size: 0.78rem;
font-weight: 600;
background: var(--soft-teal);
color: var(--teal-dark);
border: 1px solid #b2dbd6;
border-radius: 999px;
padding: 3px 10px;
}
.dc-party-chip--more {
background: #f3f4f6;
color: var(--muted);
border-color: var(--line);
}
.dc-parties-count {
font-size: 0.8rem;
color: var(--muted);
margin: 4px 0 0;
}
/* Timeline preview (stream-time) */
.dc-timeline-preview {
display: flex;
align-items: center;
gap: 8px;
font-size: 0.82rem;
color: var(--muted);
margin-bottom: 6px;
padding: 6px 10px;
background: #f7f8fb;
border: 1px solid var(--line);
border-radius: 6px;
}
.dc-timeline-preview strong {
color: var(--ink);
}
/* ── Tabs ─────────────────────────────────────────────────────────────────── */
.dc-tabs {
display: grid;
gap: 0;
}
.dc-tab-bar {
display: flex;
gap: 2px;
border-bottom: 2px solid var(--line);
margin-bottom: 16px;
overflow-x: auto;
}
.dc-tab {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 8px 14px;
font-size: 0.84rem;
font-weight: 600;
color: var(--muted);
border: none;
background: none;
cursor: pointer;
border-bottom: 2px solid transparent;
margin-bottom: -2px;
white-space: nowrap;
transition: color 100ms ease, border-color 100ms ease;
}
.dc-tab:hover { color: var(--ink); }
.dc-tab.is-active {
color: var(--teal-dark);
border-bottom-color: var(--teal);
}
.dc-tab-count {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 18px;
height: 18px;
padding: 0 5px;
border-radius: 999px;
font-size: 0.7rem;
font-weight: 800;
background: var(--line);
color: var(--muted);
}
.dc-tab.is-active .dc-tab-count {
background: var(--soft-teal);
color: var(--teal-dark);
}
.dc-tab-panel { display: none; }
.dc-tab-panel.is-active { display: block; }
/* ── Headline finding ─────────────────────────────────────────────────────── */
.dc-headline {
border-left: 4px solid var(--coral);
background: var(--soft-coral);
border-radius: 0 8px 8px 0;
padding: 14px 16px;
margin-bottom: 16px;
}
.dc-headline__label {
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.05em;
text-transform: uppercase;
color: var(--coral);
margin-bottom: 6px;
}
.dc-headline__text {
font-size: 1.0rem;
font-weight: 700;
color: var(--ink);
line-height: 1.5;
margin: 0;
}
/* ── Discrepancy list (Summary tab) ──────────────────────────────────────── */
.dc-discrepancies {
display: flex;
flex-direction: column;
gap: 10px;
}
.dc-discrepancy {
background: var(--panel);
border: 1px solid var(--line);
border-radius: 8px;
padding: 14px 16px;
}
.dc-discrepancy--contradiction { border-left: 3px solid var(--coral); }
.dc-discrepancy--deletion { border-left: 3px solid var(--amber); }
.dc-discrepancy--addition { border-left: 3px solid var(--teal); }
.dc-discrepancy--date_shift { border-left: 3px solid #8b5cf6; }
.dc-discrepancy--changed { border-left: 3px solid var(--amber); }
.dc-discrepancy__head {
display: flex;
align-items: flex-start;
gap: 10px;
margin-bottom: 10px;
flex-wrap: wrap;
}
.dc-cat-tag {
display: inline-block;
font-size: 0.68rem;
font-weight: 800;
letter-spacing: 0.05em;
text-transform: uppercase;
border-radius: 4px;
padding: 2px 7px;
white-space: nowrap;
}
.dc-cat-tag--contradiction { background: var(--soft-coral); color: var(--coral); }
.dc-cat-tag--deletion { background: #fffbeb; color: var(--amber); }
.dc-cat-tag--addition { background: var(--soft-teal); color: var(--teal-dark); }
.dc-cat-tag--date_shift { background: #ede9fe; color: #6d28d9; }
.dc-cat-tag--changed { background: #fffbeb; color: var(--amber); }
.dc-severity {
display: inline-block;
font-size: 0.68rem;
font-weight: 800;
letter-spacing: 0.04em;
text-transform: uppercase;
border-radius: 999px;
padding: 2px 9px;
white-space: nowrap;
}
.dc-sev--high { background: var(--soft-coral); color: var(--coral); border: 1px solid #f9c6ae; }
.dc-sev--medium { background: #fffbeb; color: var(--amber); border: 1px solid #fde68a; }
.dc-sev--low { background: #f3f4f6; color: var(--muted); border: 1px solid var(--line); }
.dc-discrepancy__compare {
display: grid;
grid-template-columns: 1fr auto 1fr;
gap: 8px;
align-items: start;
margin-bottom: 10px;
}
.dc-compare-col {
background: #f7f8fb;
border: 1px solid var(--line);
border-radius: 6px;
padding: 10px 12px;
font-size: 0.86rem;
line-height: 1.55;
color: var(--ink);
}
.dc-compare-col--a {
background: #f0f5ff;
border-color: #c3d4f8;
}
.dc-compare-col--b {
background: #fff5f0;
border-color: #f9c6ae;
}
.dc-compare-col__label {
font-size: 0.68rem;
font-weight: 800;
letter-spacing: 0.05em;
text-transform: uppercase;
margin-bottom: 5px;
}
.dc-compare-col--a .dc-compare-col__label { color: #2d5fa6; }
.dc-compare-col--b .dc-compare-col__label { color: var(--coral); }
.dc-compare-divider {
display: flex;
align-items: center;
justify-content: center;
font-size: 1.2rem;
color: var(--muted);
padding-top: 28px;
}
.dc-discrepancy__legal {
font-size: 0.82rem;
color: var(--muted);
font-style: italic;
line-height: 1.5;
border-top: 1px solid var(--line);
padding-top: 8px;
margin-top: 4px;
}
.dc-sig-badge {
display: inline-block;
font-size: 0.68rem;
font-weight: 700;
border-radius: 999px;
padding: 2px 8px;
margin-bottom: 6px;
}
.dc-sig--high { background: var(--soft-coral); color: var(--coral); }
.dc-sig--medium { background: #fffbeb; color: var(--amber); }
.dc-sig--low { background: #f3f4f6; color: var(--muted); }
/* ── Parties tab ─────────────────────────────────────────────────────────── */
.dc-party-list {
display: flex;
flex-direction: column;
gap: 6px;
}
.dc-party-row {
display: grid;
grid-template-columns: 1fr 1fr auto;
gap: 8px;
align-items: center;
background: var(--panel);
border: 1px solid var(--line);
border-radius: 6px;
padding: 10px 12px;
border-left: 3px solid var(--line);
}
.dc-party-row--removed { border-left-color: var(--coral); background: #fff5f0; }
.dc-party-row--added { border-left-color: var(--teal); background: #f0faf8; }
.dc-party-row--changed { border-left-color: var(--amber); background: #fffdf0; }
.dc-party-row__name {
font-weight: 700;
font-size: 0.88rem;
color: var(--ink);
}
.dc-party-row__role {
font-size: 0.82rem;
color: var(--muted);
}
.dc-party-row__sig {
font-size: 0.78rem;
color: var(--muted);
font-style: italic;
line-height: 1.4;
grid-column: 1 / -1;
padding-top: 4px;
border-top: 1px solid var(--line);
margin-top: 4px;
}
/* ── Timeline tab ────────────────────────────────────────────────────────── */
.dc-timeline-list {
display: flex;
flex-direction: column;
gap: 8px;
}
.dc-tl-item {
background: var(--panel);
border: 1px solid var(--line);
border-radius: 8px;
padding: 12px 14px;
border-left: 3px solid var(--line);
}
.dc-tl-item--conflict { border-left-color: var(--coral); }
.dc-tl-item--deleted { border-left-color: var(--amber); }
.dc-tl-item--added { border-left-color: var(--teal); }
.dc-tl-item--date_shift { border-left-color: #8b5cf6; }
.dc-tl-item__head {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
flex-wrap: wrap;
}
.dc-tl-date {
font-family: ui-monospace, "Cascadia Code", "Fira Code", monospace;
font-size: 0.78rem;
font-weight: 700;
color: var(--muted);
background: #f3f4f6;
border: 1px solid var(--line);
border-radius: 4px;
padding: 2px 7px;
}
.dc-tl-actor {
font-size: 0.78rem;
font-weight: 700;
color: var(--teal-dark);
text-transform: uppercase;
letter-spacing: 0.04em;
}
.dc-tl-desc {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-bottom: 8px;
}
.dc-tl-legal {
font-size: 0.8rem;
color: var(--muted);
font-style: italic;
line-height: 1.4;
border-top: 1px solid var(--line);
padding-top: 7px;
}
/* ── Narrative blocks ─────────────────────────────────────────────────────── */
.dc-narrative-block {
background: var(--panel);
border: 1px solid var(--line);
border-radius: 8px;
padding: 12px 14px;
border-left: 3px solid var(--line);
margin-bottom: 8px;
}
.dc-narrative-block--added { border-left-color: var(--teal); background: #f0faf8; }
.dc-narrative-block--removed { border-left-color: var(--coral); background: #fff5f0; }
/* ── Action list ─────────────────────────────────────────────────────────── */
.dc-action-list {
display: flex;
flex-direction: column;
gap: 6px;
list-style: none;
margin: 0;
padding: 0;
}
.dc-action-list li {
display: flex;
align-items: flex-start;
gap: 8px;
font-size: 0.88rem;
color: var(--ink);
line-height: 1.5;
padding: 8px 12px;
background: var(--soft-teal);
border: 1px solid #b2dbd6;
border-radius: 6px;
}
.dc-action-list li::before {
content: '→';
color: var(--teal);
font-weight: 800;
flex: 0 0 auto;
}
/* ── Disclaimer ──────────────────────────────────────────────────────────── */
.dc-disclaimer {
font-size: 0.76rem;
color: var(--muted);
font-style: italic;
line-height: 1.5;
padding: 10px 12px;
background: #f7f8fb;
border: 1px solid var(--line);
border-radius: 6px;
margin-top: 16px;
}
/* ── Responsive ──────────────────────────────────────────────────────────── */
@media (max-width: 780px) {
.dc-upload-pair { grid-template-columns: 1fr; }
.dc-doc-meta-pair { grid-template-columns: 1fr; }
.dc-discrepancy__compare { grid-template-columns: 1fr; }
.dc-compare-divider { padding-top: 0; }
.dc-party-row { grid-template-columns: 1fr 1fr; }
.dc-tl-desc { grid-template-columns: 1fr; }
}
@media (max-width: 520px) {
.dc-tab-bar { gap: 0; }
.dc-tab { padding: 8px 10px; font-size: 0.78rem; }
.dc-party-row { grid-template-columns: 1fr; }
}
/* Print styles */
@media print {
.tool-rail, .reasoning-panel, .topbar, .tool-form,
+882
View File
@@ -0,0 +1,882 @@
/* discrepancy.js — page-scoped UI for /discrepancy.php */
(function () {
'use strict';
const els = {};
let lang = window.DBN_TOOLS_LANG || localStorage.getItem('dbn-ui-lang') || 'en';
let fileA = null;
let fileB = null;
let lastResult = null;
const SLICE_DEFS = [
{ id: 'child_welfare', label: 'Child Welfare' },
{ id: 'echr', label: 'ECHR' },
{ id: 'family_core', label: 'Family Law Core' },
{ id: 'bufdir_guidance', label: 'Bufdir Guidance' },
{ id: 'norwegian_courts', label: 'Norwegian Courts' },
{ id: 'broader_legal', label: 'Broader Legal' },
];
const STEP_LABELS = [
'Classify documents',
'Extract parties',
'Build timelines',
'Cross-reference parties',
'Cross-reference timelines',
'Research questions',
'Retrieve legal context',
'Synthesize report',
];
const stepKeyToIndex = {
doc_classify: 0,
party_extract: 1,
timeline_extract: 2,
cross_parties: 3,
cross_timelines: 4,
sub_question_gen: 5,
retrieval: 6,
synthesis: 7,
};
document.addEventListener('DOMContentLoaded', () => {
if (!document.body.dataset.activeTool || document.body.dataset.activeTool !== 'discrepancy') return;
Object.assign(els, {
form: document.getElementById('dcForm'),
status: document.getElementById('dcStatus'),
runButton: document.getElementById('dcRunButton'),
results: document.getElementById('dcResults'),
traceList: document.getElementById('traceList'),
langButtons: Array.from(document.querySelectorAll('#dcLangSwitcher .lang-btn')),
engineRadios: Array.from(document.querySelectorAll('input[name="dcEngine"]')),
slices: Array.from(document.querySelectorAll('.adv-slice')),
// File A
zoneA: document.getElementById('dcZoneA'),
inputA: document.getElementById('dcInputA'),
promptA: document.getElementById('dcPromptA'),
fileInfoA: document.getElementById('dcFileInfoA'),
fileNameA: document.getElementById('dcFileNameA'),
clearA: document.getElementById('dcClearA'),
// File B
zoneB: document.getElementById('dcZoneB'),
inputB: document.getElementById('dcInputB'),
promptB: document.getElementById('dcPromptB'),
fileInfoB: document.getElementById('dcFileInfoB'),
fileNameB: document.getElementById('dcFileNameB'),
clearB: document.getElementById('dcClearB'),
// Source modal
modal: document.getElementById('dcSourceModal'),
modalClose: document.getElementById('dcSourceModalClose'),
modalTitle: document.getElementById('dcSourceModalTitle'),
modalEyebrow: document.getElementById('dcSourceModalEyebrow'),
modalMeta: document.getElementById('dcSourceModalMeta'),
modalText: document.getElementById('dcSourceModalText'),
});
if (!els.form) return;
bindLang();
bindSlices();
bindUploadZone('A');
bindUploadZone('B');
bindModal();
els.form.addEventListener('submit', onSubmit);
renderTrace(STEP_LABELS.map((label) => ({ label, detail: 'Waiting…', status: 'idle' })));
});
// ── Language ───────────────────────────────────────────────────────────────
function bindLang() {
els.langButtons.forEach((b) => {
b.classList.toggle('is-active', b.dataset.lang === lang);
b.addEventListener('click', () => {
els.langButtons.forEach((x) => x.classList.remove('is-active'));
b.classList.add('is-active');
lang = b.dataset.lang || 'en';
localStorage.setItem('dbn-ui-lang', lang);
});
});
}
// ── Corpus slice toggles ───────────────────────────────────────────────────
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 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;
}
// ── File upload zones ──────────────────────────────────────────────────────
function bindUploadZone(slot) {
const zone = els['zone' + slot];
const input = els['input' + slot];
const prompt = els['prompt' + slot];
const info = els['fileInfo' + slot];
const nameEl = els['fileName' + slot];
const clearEl = els['clear' + slot];
if (!zone) return;
const accept = (file) => {
if (!file) return;
if (file.size > 8 * 1024 * 1024) {
setStatus(`${file.name} exceeds the 8 MB limit.`, 'error');
return;
}
const ext = (file.name.split('.').pop() || '').toLowerCase();
if (!['pdf', 'docx', 'txt'].includes(ext)) {
setStatus(`${file.name} is not a supported file type (PDF, DOCX, TXT).`, 'error');
return;
}
if (slot === 'A') fileA = file;
else fileB = file;
nameEl.textContent = file.name;
prompt.classList.add('is-hidden');
info.classList.remove('is-hidden');
zone.classList.remove('is-drop');
setStatus('', '');
};
input.addEventListener('change', (e) => {
if (e.target.files && e.target.files[0]) accept(e.target.files[0]);
});
zone.addEventListener('dragover', (e) => { e.preventDefault(); zone.classList.add('is-drop'); });
zone.addEventListener('dragleave', () => zone.classList.remove('is-drop'));
zone.addEventListener('drop', (e) => {
e.preventDefault();
zone.classList.remove('is-drop');
const f = e.dataTransfer?.files?.[0];
if (f) accept(f);
});
clearEl?.addEventListener('click', () => {
if (slot === 'A') fileA = null;
else fileB = null;
input.value = '';
info.classList.add('is-hidden');
prompt.classList.remove('is-hidden');
});
}
// ── Form submission ────────────────────────────────────────────────────────
async function onSubmit(e) {
e.preventDefault();
if (!fileA) {
setStatus('Upload Document A (the earlier/original document) before running.', 'error');
return;
}
if (!fileB) {
setStatus('Upload Document B (the later/comparison document) before running.', 'error');
return;
}
const engine = (els.engineRadios.find((r) => r.checked) || {}).value || 'azure_mini';
const slices = getSelectedSlices();
const expectedDuration = engine === 'azure_full' ? '2-3 minutes'
: engine === 'gpu' ? '~90 seconds'
: '60-90 seconds';
setStatus(`Comparing documents… (${expectedDuration})`, 'busy');
els.runButton.disabled = true;
els.results.innerHTML = `<div class="empty-state"><h3>Analysing…</h3><p>Classifying both documents, extracting parties and timelines, then cross-referencing for discrepancies. Expect ${expectedDuration}.</p></div>`;
const stepState = STEP_LABELS.map((label) => ({ label, detail: 'Queued', status: 'idle' }));
renderTrace(stepState);
const payload = { engine, language: lang, slices };
const form = new FormData();
form.append('payload', JSON.stringify(payload));
form.append('file_a', fileA);
form.append('file_b', fileB);
let response;
try {
response = await fetch('api/discrepancy.php', { method: 'POST', body: form, credentials: 'same-origin' });
} catch (err) {
setStatus(`Network error: ${err.message || err}`, 'error');
els.runButton.disabled = false;
return;
}
if (!response.ok || !response.body) {
if (response.status === 402 || response.status === 429) {
const d = await response.json().catch(() => ({}));
if (typeof window.dbnFreeTierError === 'function') window.dbnFreeTierError(response.status, d);
} else {
setStatus(`Request failed (${response.status}).`, 'error');
}
els.runButton.disabled = false;
return;
}
const creditsRemaining = response.headers.get('X-Credits-Remaining');
if (creditsRemaining !== null && typeof window.dbnUpdateCredits === 'function') {
window.dbnUpdateCredits(parseInt(creditsRemaining, 10));
}
const reader = response.body.getReader();
const decoder = new TextDecoder('utf-8');
let buffer = '';
let finalResult = null;
let errorEvent = null;
// State for progressive rendering
let metaARendered = false;
let metaBRendered = false;
let partiesARendered = false;
let partiesBRendered = false;
let tlARendered = false;
let tlBRendered = false;
function handleStreamEvent(evt) {
if (!evt || !evt.event) return;
if (evt.event === 'progress') {
if (evt.detail) setStatus(evt.detail, 'busy');
return;
}
if (evt.event === 'start') {
setStatus(`Comparing ${escapeHtml(evt.file_a || 'A')}${escapeHtml(evt.file_b || 'B')}`, 'busy');
return;
}
if (evt.event === 'step') {
const idx = stepKeyToIndex[evt.step];
if (idx !== undefined) {
if (evt.status === 'running' && stepState[idx].status !== 'running') {
stepState[idx] = { label: evt.label || stepState[idx].label, detail: evt.detail || 'Running…', status: 'running' };
} else if (evt.status !== 'running') {
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 === 'doc_a_meta' && !metaARendered) {
renderDocMetaCard('A', evt.result || {});
metaARendered = true;
return;
}
if (evt.event === 'doc_b_meta' && !metaBRendered) {
renderDocMetaCard('B', evt.result || {});
metaBRendered = true;
return;
}
if (evt.event === 'parties_a' && !partiesARendered && Array.isArray(evt.parties)) {
renderPartiesPreview('A', evt.parties);
partiesARendered = true;
return;
}
if (evt.event === 'parties_b' && !partiesBRendered && Array.isArray(evt.parties)) {
renderPartiesPreview('B', evt.parties);
partiesBRendered = true;
return;
}
if (evt.event === 'timeline_a' && !tlARendered && Array.isArray(evt.events)) {
renderTimelinePreview('A', evt.events);
tlARendered = true;
return;
}
if (evt.event === 'timeline_b' && !tlBRendered && Array.isArray(evt.events)) {
renderTimelinePreview('B', evt.events);
tlBRendered = true;
return;
}
if (evt.event === 'subq') {
setStatus(`Retrieving ${evt.index}/${evt.total}: ${String(evt.question || '').slice(0, 80)}`, 'busy');
return;
}
if (evt.event === 'final') {
finalResult = evt.result;
return;
}
if (evt.event === 'error') {
errorEvent = evt;
return;
}
}
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 || {};
setStatus(
`Done · ${meta.conflict_count || 0} contradictions · ${meta.deleted_count || 0} deletions · ${meta.added_count || 0} additions · ${meta.source_count || 0} sources`,
'ok'
);
els.runButton.disabled = false;
renderTrace(finalResult.trace || []);
renderFinalResults(finalResult);
}
// ── Progressive rendering ──────────────────────────────────────────────────
function ensureResultsReady() {
const emptyState = els.results.querySelector('.empty-state');
if (emptyState) emptyState.remove();
// Ensure the doc-meta pair container exists
if (!els.results.querySelector('#dcDocMetaPair')) {
const pair = document.createElement('div');
pair.id = 'dcDocMetaPair';
pair.className = 'dc-doc-meta-pair';
els.results.insertBefore(pair, els.results.firstChild);
}
}
function renderDocMetaCard(slot, meta) {
ensureResultsReady();
const pair = els.results.querySelector('#dcDocMetaPair');
if (!pair) return;
const existing = pair.querySelector(`#dcMeta${slot}`);
if (existing) existing.remove();
const card = document.createElement('div');
card.id = `dcMeta${slot}`;
card.className = 'dc-doc-meta-card';
const fields = [
meta.doc_date ? ['Date', meta.doc_date] : null,
meta.issuing_authority ? ['Authority', meta.issuing_authority] : null,
meta.reference_number ? ['Ref', meta.reference_number] : null,
].filter(Boolean);
card.innerHTML = `
<div class="dc-doc-meta-card__head">
<span class="dc-slot-label">Document ${slot}</span>
<span class="bvj-doc-type-badge">${escapeHtml(meta.doc_type || ('Document ' + slot))}</span>
</div>
${fields.length ? `<div class="bvj-doc-meta__fields">
${fields.map(([k, v]) => `<span class="bvj-doc-meta__field"><strong>${escapeHtml(k)}:</strong> ${escapeHtml(String(v))}</span>`).join('')}
</div>` : ''}
`;
pair.appendChild(card);
}
function renderPartiesPreview(slot, parties) {
if (!parties.length) return;
ensureResultsReady();
const pair = els.results.querySelector('#dcDocMetaPair');
if (!pair) return;
const metaCard = pair.querySelector(`#dcMeta${slot}`);
if (!metaCard) return;
const existing = metaCard.querySelector('.dc-parties-preview');
if (existing) existing.remove();
const preview = document.createElement('div');
preview.className = 'dc-parties-preview';
preview.innerHTML = `<p class="dc-parties-count">${parties.length} party${parties.length === 1 ? '' : 'ies'} identified</p>
<div class="dc-parties-chips">
${parties.slice(0, 6).map((p) => `<span class="dc-party-chip">${escapeHtml(p.name || p.role || '?')}</span>`).join('')}
${parties.length > 6 ? `<span class="dc-party-chip dc-party-chip--more">+${parties.length - 6} more</span>` : ''}
</div>`;
metaCard.appendChild(preview);
}
function renderTimelinePreview(slot, events) {
if (!events.length) return;
ensureResultsReady();
const pair = els.results.querySelector('#dcDocMetaPair');
if (!pair) return;
const metaCard = pair.querySelector(`#dcMeta${slot}`);
if (!metaCard) return;
const existing = metaCard.querySelector('.dc-timeline-preview');
if (existing) existing.remove();
const highCount = events.filter((e) => e.significance === 'high').length;
const preview = document.createElement('div');
preview.className = 'dc-timeline-preview';
preview.innerHTML = `<p class="dc-parties-count">${events.length} events · ${highCount} high-significance</p>`;
metaCard.appendChild(preview);
}
// ── Final render ───────────────────────────────────────────────────────────
function renderFinalResults(data) {
const sources = data.sources || [];
const discrepancies = Array.isArray(data.critical_discrepancies) ? data.critical_discrepancies : [];
const actions = Array.isArray(data.recommended_actions) ? data.recommended_actions : [];
const uncertain = Array.isArray(data.what_remains_uncertain) ? data.what_remains_uncertain : [];
const partiesDiff = data.parties_diff || {};
const tlDiff = data.timeline_diff || {};
const headline = data.headline_finding || '';
const nameA = data.doc_a_name || 'Document A';
const nameB = data.doc_b_name || 'Document B';
// Remove progressive doc meta pair — we'll re-render from authoritative data
els.results.querySelector('#dcDocMetaPair')?.remove();
// Re-render doc meta pair from final data
renderDocMetaCard('A', data.doc_a_meta || {});
renderDocMetaCard('B', data.doc_b_meta || {});
if ((data.parties_a || []).length) renderPartiesPreview('A', data.parties_a);
if ((data.parties_b || []).length) renderPartiesPreview('B', data.parties_b);
if ((data.timeline_a || []).length) renderTimelinePreview('A', data.timeline_a);
if ((data.timeline_b || []).length) renderTimelinePreview('B', data.timeline_b);
// Build tabs
const conflicts = tlDiff.conflicts || [];
const deletedEvents = tlDiff.in_a_only || [];
const addedEvents = tlDiff.in_b_only || [];
const procGaps = tlDiff.procedural_gaps || [];
const narrative = tlDiff.narrative_shifts || {};
const pRemoved = partiesDiff.in_a_only || [];
const pAdded = partiesDiff.in_b_only || [];
const pChanged = partiesDiff.changed_between || [];
const totalDiscrepancies = discrepancies.length;
const tabCountStr = (n) => n > 0 ? ` <span class="dc-tab-count">${n}</span>` : '';
const finalHtml = `
<!-- Headline -->
${headline ? `<div class="dr-result-block dc-headline">
<h3 class="dc-headline__label">Key finding</h3>
<p class="dc-headline__text">${escapeHtml(headline)}</p>
</div>` : ''}
<!-- Tabs -->
<div class="dc-tabs">
<div class="dc-tab-bar" role="tablist">
<button type="button" class="dc-tab is-active" data-tab="summary" role="tab">Summary${tabCountStr(totalDiscrepancies)}</button>
<button type="button" class="dc-tab" data-tab="parties" role="tab">Parties${tabCountStr(pRemoved.length + pAdded.length + pChanged.length)}</button>
<button type="button" class="dc-tab" data-tab="timeline" role="tab">Timeline${tabCountStr(conflicts.length + deletedEvents.length + addedEvents.length)}</button>
<button type="button" class="dc-tab" data-tab="sources" role="tab">Legal context${tabCountStr(sources.length)}</button>
</div>
<!-- Summary tab -->
<div class="dc-tab-panel is-active" data-panel="summary">
${renderDiscrepanciesTab(discrepancies, sources)}
${actions.length ? `<div class="dr-result-block">
<h3 style="margin:0 0 10px;font-size:0.95rem">Recommended actions</h3>
<ol class="dc-action-list">
${actions.map((a) => `<li>${escapeHtml(String(a))}</li>`).join('')}
</ol>
</div>` : ''}
${narrative.summary ? `<div class="dr-result-block">
<h3 style="margin:0 0 8px;font-size:0.95rem">Narrative shift</h3>
<p style="margin:0 0 10px;line-height:1.55;color:var(--ink)">${escapeHtml(narrative.summary)}</p>
${(narrative.new_in_b || []).length ? `<div class="dc-narrative-block dc-narrative-block--added">
<strong>New in ${escapeHtml(nameB)}:</strong>
<ul>${(narrative.new_in_b || []).map((s) => `<li>${escapeHtml(String(s))}</li>`).join('')}</ul>
</div>` : ''}
${(narrative.removed_from_b || []).length ? `<div class="dc-narrative-block dc-narrative-block--removed">
<strong>Removed from ${escapeHtml(nameB)}:</strong>
<ul>${(narrative.removed_from_b || []).map((s) => `<li>${escapeHtml(String(s))}</li>`).join('')}</ul>
</div>` : ''}
</div>` : ''}
${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">
${uncertain.map((u) => `<li>${escapeHtml(String(u))}</li>`).join('')}
</ul>
</div>` : ''}
</div>
<!-- Parties tab -->
<div class="dc-tab-panel" data-panel="parties">
${renderPartiesTab(pRemoved, pAdded, pChanged, nameA, nameB)}
</div>
<!-- Timeline tab -->
<div class="dc-tab-panel" data-panel="timeline">
${renderTimelineTab(conflicts, deletedEvents, addedEvents, procGaps, nameA, nameB)}
</div>
<!-- Sources tab -->
<div class="dc-tab-panel" data-panel="sources">
${renderSourcesTab(sources)}
</div>
</div>
<p class="dc-disclaimer">${escapeHtml(data.disclaimer || 'For legal information and preparation only — not legal advice. Verify all findings with a qualified lawyer.')}</p>
`;
const finalContainer = document.createElement('div');
finalContainer.innerHTML = finalHtml;
while (finalContainer.firstChild) {
els.results.appendChild(finalContainer.firstChild);
}
// Bind tabs
els.results.querySelectorAll('.dc-tab').forEach((btn) => {
btn.addEventListener('click', () => {
const tab = btn.dataset.tab;
els.results.querySelectorAll('.dc-tab').forEach((b) => b.classList.remove('is-active'));
els.results.querySelectorAll('.dc-tab-panel').forEach((p) => p.classList.remove('is-active'));
btn.classList.add('is-active');
const panel = els.results.querySelector(`.dc-tab-panel[data-panel="${tab}"]`);
if (panel) panel.classList.add('is-active');
});
});
// 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);
});
});
}
// ── Tab content renderers ──────────────────────────────────────────────────
function renderDiscrepanciesTab(discrepancies, sources) {
if (!discrepancies.length) {
return '<div class="dr-result-block"><p style="color:var(--muted)"><em>No critical discrepancies were identified in the synthesis.</em></p></div>';
}
const sevClass = (s) => s === 'high' ? 'dc-sev--high' : (s === 'medium' ? 'dc-sev--medium' : 'dc-sev--low');
const catLabel = (c) => ({ timeline_conflict: 'Timeline', narrative_shift: 'Narrative', party_discrepancy: 'Party', procedural_gap: 'Procedure' }[c] || c);
return `<div class="dr-result-block">
<h3 style="margin:0 0 12px;font-size:0.95rem">Critical discrepancies (${discrepancies.length})</h3>
<div class="dc-discrepancies">
${discrepancies.map((d) => `<div class="dc-discrepancy">
<div class="dc-discrepancy__head">
<div>
<span class="dc-cat-tag">${escapeHtml(catLabel(d.category || ''))}</span>
<span class="dc-severity ${sevClass(d.significance || 'low')}">${escapeHtml(d.significance || 'low')}</span>
</div>
<div class="dc-discrepancy__title">${escapeHtml(d.title || '')}</div>
</div>
<div class="dc-discrepancy__compare">
<div class="dc-compare-col dc-compare-col--a">
<span class="dc-compare-label">Document A</span>
<p>${escapeHtml(d.document_a_says || '—')}</p>
</div>
<div class="dc-compare-divider" aria-hidden="true">≠</div>
<div class="dc-compare-col dc-compare-col--b">
<span class="dc-compare-label">Document B</span>
<p>${escapeHtml(d.document_b_says || '—')}</p>
</div>
</div>
${d.legal_relevance ? `<div class="dc-discrepancy__legal">
${renderInlineCitations(escapeHtml(d.legal_relevance), sources)}
</div>` : ''}
</div>`).join('')}
</div>
</div>`;
}
function renderPartiesTab(removed, added, changed, nameA, nameB) {
if (!removed.length && !added.length && !changed.length) {
return '<div class="dr-result-block"><p style="color:var(--muted)"><em>No party discrepancies identified between the two documents.</em></p></div>';
}
let html = '<div class="dr-result-block">';
if (removed.length) {
html += `<h3 style="margin:0 0 10px;font-size:0.92rem;color:#b41e1e">Removed from ${escapeHtml(nameB)} (${removed.length})</h3>
<div class="dc-party-list">
${removed.map((p) => `<div class="dc-party-row dc-party-row--removed">
<div class="dc-party-row__name">${escapeHtml(p.name || '?')}</div>
<div class="dc-party-row__role">${escapeHtml(p.role_in_a || '')}</div>
${p.significance ? `<div class="dc-party-row__sig">${escapeHtml(p.significance)}</div>` : ''}
</div>`).join('')}
</div>`;
}
if (added.length) {
html += `<h3 style="margin:16px 0 10px;font-size:0.92rem;color:var(--teal-dark)">Added in ${escapeHtml(nameB)} (${added.length})</h3>
<div class="dc-party-list">
${added.map((p) => `<div class="dc-party-row dc-party-row--added">
<div class="dc-party-row__name">${escapeHtml(p.name || '?')}</div>
<div class="dc-party-row__role">${escapeHtml(p.role_in_b || '')}</div>
${p.significance ? `<div class="dc-party-row__sig">${escapeHtml(p.significance)}</div>` : ''}
</div>`).join('')}
</div>`;
}
if (changed.length) {
html += `<h3 style="margin:16px 0 10px;font-size:0.92rem;color:var(--ink)">Changed between versions (${changed.length})</h3>
<div class="dc-party-list">
${changed.map((p) => `<div class="dc-party-row dc-party-row--changed">
<div class="dc-party-row__name">${escapeHtml(p.name || '?')}</div>
<div class="dc-compare-col dc-compare-col--a" style="font-size:0.82rem;padding:2px 0">${escapeHtml(p.in_a || '')}</div>
<div class="dc-compare-col dc-compare-col--b" style="font-size:0.82rem;padding:2px 0">${escapeHtml(p.in_b || '')}</div>
${p.significance ? `<div class="dc-party-row__sig">${escapeHtml(p.significance)}</div>` : ''}
</div>`).join('')}
</div>`;
}
html += '</div>';
return html;
}
function renderTimelineTab(conflicts, deleted, added, procGaps, nameA, nameB) {
if (!conflicts.length && !deleted.length && !added.length && !procGaps.length) {
return '<div class="dr-result-block"><p style="color:var(--muted)"><em>No timeline discrepancies identified between the two documents.</em></p></div>';
}
const sigClass = (s) => `dc-sig--${s === 'high' ? 'high' : (s === 'medium' ? 'medium' : 'low')}`;
let html = '';
if (conflicts.length) {
html += `<div class="dr-result-block">
<h3 style="margin:0 0 10px;font-size:0.92rem">Contradictions (${conflicts.length})</h3>
<div class="dc-timeline-list">
${conflicts.map((c) => `<div class="dc-tl-item dc-tl-item--conflict">
<div class="dc-tl-item__head">
<span class="dc-sig-badge ${sigClass(c.significance || 'low')}">${escapeHtml(c.significance || 'low')}</span>
${c.date_a || c.date_b ? `<span class="dc-tl-date">${escapeHtml(c.date_a || '?')} / ${escapeHtml(c.date_b || '?')}</span>` : ''}
</div>
<div class="dc-discrepancy__compare" style="margin-top:6px">
<div class="dc-compare-col dc-compare-col--a">
<span class="dc-compare-label">${escapeHtml(nameA)}</span>
<p>${escapeHtml(c.doc_a_says || '—')}</p>
</div>
<div class="dc-compare-divider" aria-hidden="true">≠</div>
<div class="dc-compare-col dc-compare-col--b">
<span class="dc-compare-label">${escapeHtml(nameB)}</span>
<p>${escapeHtml(c.doc_b_says || '—')}</p>
</div>
</div>
${c.legal_significance ? `<p class="dc-tl-legal">${escapeHtml(c.legal_significance)}</p>` : ''}
</div>`).join('')}
</div>
</div>`;
}
if (deleted.length) {
html += `<div class="dr-result-block">
<h3 style="margin:0 0 10px;font-size:0.92rem;color:#b41e1e">Deleted from ${escapeHtml(nameB)} (${deleted.length})</h3>
<div class="dc-timeline-list">
${deleted.map((ev) => `<div class="dc-tl-item dc-tl-item--deleted">
<div class="dc-tl-item__head">
<span class="dc-sig-badge ${sigClass(ev.significance || 'low')}">${escapeHtml(ev.significance || 'low')}</span>
${ev.date ? `<span class="dc-tl-date">${escapeHtml(ev.date)}</span>` : ''}
${ev.actor ? `<span class="dc-tl-actor">${escapeHtml(ev.actor)}</span>` : ''}
</div>
<p class="dc-tl-desc">${escapeHtml(ev.description || '')}</p>
${ev.legal_significance ? `<p class="dc-tl-legal">${escapeHtml(ev.legal_significance)}</p>` : ''}
</div>`).join('')}
</div>
</div>`;
}
if (added.length) {
html += `<div class="dr-result-block">
<h3 style="margin:0 0 10px;font-size:0.92rem;color:var(--teal-dark)">New in ${escapeHtml(nameB)} (${added.length})</h3>
<div class="dc-timeline-list">
${added.map((ev) => `<div class="dc-tl-item dc-tl-item--added">
<div class="dc-tl-item__head">
<span class="dc-sig-badge ${sigClass(ev.significance || 'low')}">${escapeHtml(ev.significance || 'low')}</span>
${ev.date ? `<span class="dc-tl-date">${escapeHtml(ev.date)}</span>` : ''}
${ev.actor ? `<span class="dc-tl-actor">${escapeHtml(ev.actor)}</span>` : ''}
</div>
<p class="dc-tl-desc">${escapeHtml(ev.description || '')}</p>
${ev.legal_significance ? `<p class="dc-tl-legal">${escapeHtml(ev.legal_significance)}</p>` : ''}
</div>`).join('')}
</div>
</div>`;
}
if (procGaps.length) {
html += `<div class="dr-result-block">
<h3 style="margin:0 0 10px;font-size:0.92rem;color:var(--muted)">Procedural gaps (${procGaps.length})</h3>
<ul style="padding-left:1.2em;margin:0;line-height:1.6">
${procGaps.map((g) => `<li>${escapeHtml(g.gap || '')} <span class="dc-sig-badge ${sigClass(g.significance || 'low')}" style="margin-left:4px">${escapeHtml(g.significance || 'low')}</span></li>`).join('')}
</ul>
</div>`;
}
return html;
}
function renderSourcesTab(sources) {
if (!sources.length) {
return '<div class="dr-result-block"><p style="color:var(--muted)"><em>No corpus sources retrieved. Enable corpus slices and re-run.</em></p></div>';
}
return `<div class="dr-result-block">
<div class="dr-sources-head">
<h3>Legal context sources (${sources.length})</h3>
<small>Click a card to expand · external link opens original source</small>
</div>
<div class="dr-source-list">
${sources.map((s) => renderSourceCard(s)).join('')}
</div>
</div>`;
}
function renderSourceCard(s) {
const score = s.reranker_score != null ? s.reranker_score : s.similarity;
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="dr-source-tag">${escapeHtml(s.package_or_corpus || 'corpus')}</span>
${s.authority_label ? `<span class="dr-source-tag">${escapeHtml(s.authority_label)}</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>
</div>
</div>`;
}
// ── Source modal ───────────────────────────────────────────────────────────
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 = 'Corpus source';
els.modalTitle.textContent = source.title || 'Source';
const metaRows = [
['Number', `[${source.n}]`],
source.section ? ['Section', source.section] : null,
['Corpus', source.package_or_corpus || '—'],
source.authority_label ? ['Authority', source.authority_label] : null,
source.similarity != null ? ['Similarity', String(source.similarity)] : null,
source.reranker_score != null ? ['Rerank score', String(source.reranker_score)] : null,
].filter(Boolean);
els.modalMeta.innerHTML = '<dl>' + metaRows.map(([k, v]) => `<dt>${escapeHtml(k)}</dt><dd>${escapeHtml(String(v))}</dd>`).join('') + '</dl>';
const chunkText = source.chunk_text || source.excerpt || '';
let html = chunkText
? `<button class="dr-modal-chunk-toggle" type="button">Show matching text ▼</button><div class="dr-modal-chunk-text is-hidden">${escapeHtml(chunkText)}</div>`
: '<em>No excerpt available.</em>';
els.modalText.innerHTML = html;
const toggle = els.modalText.querySelector('.dr-modal-chunk-toggle');
const div = els.modalText.querySelector('.dr-modal-chunk-text');
toggle?.addEventListener('click', () => {
const isHidden = div.classList.toggle('is-hidden');
toggle.textContent = isHidden ? 'Show matching text ▼' : 'Hide matching text ▲';
});
els.modal.classList.remove('is-hidden');
}
// ── Trace rendering ────────────────────────────────────────────────────────
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('');
}
// ── Utility ────────────────────────────────────────────────────────────────
function setStatus(message, kind) {
if (!els.status) return;
els.status.textContent = message;
els.status.style.color = kind === 'error' ? '#b41e1e'
: kind === 'ok' ? 'var(--teal-dark)'
: 'var(--muted)';
}
function renderInlineCitations(escapedHtml, sources) {
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 expandCiteGroup(group) {
const out = [];
group.split(',').forEach((part) => {
const range = part.trim().match(/^(\d+)\s*-\s*(\d+)$/);
if (range) {
for (let i = parseInt(range[1], 10); i <= parseInt(range[2], 10); 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, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;');
}
function truncate(s, n) {
if (!s || s.length <= n) return s || '';
return s.slice(0, n - 1) + '…';
}
})();
+176
View File
@@ -0,0 +1,176 @@
<?php
declare(strict_types=1);
$toolName = 'discrepancy';
$toolTitle = 'Discrepancy Finder';
$toolKind = 'Document comparison';
$toolBadge = 'Cross-document AI';
$extraScripts = ['assets/js/discrepancy.js'];
require_once __DIR__ . '/includes/layout.php';
?>
<form id="dcForm" class="tool-form deep-research" enctype="multipart/form-data">
<div class="lang-switcher" id="dcLangSwitcher" role="group" aria-label="UI language">
<button type="button" class="lang-btn is-active" data-lang="en">&#127468;&#127463; EN</button>
<button type="button" class="lang-btn" data-lang="no">&#127475;&#127476; NO</button>
<button type="button" class="lang-btn" data-lang="uk">&#127482;&#127462; UK</button>
<button type="button" class="lang-btn" data-lang="pl">&#127477;&#127473; PL</button>
</div>
<p class="upload-hint" style="margin-bottom:1.2rem">Upload two versions of the same Barnevernet document &mdash; or any two related documents &mdash; and the agent will find contradictions, deleted facts, new allegations, and party changes between them. Results include corpus-backed legal significance for each discrepancy.</p>
<!-- Two upload zones side by side -->
<div class="dc-upload-pair">
<div class="dc-upload-slot">
<p class="control-label">Document A <span class="dc-slot-hint">&mdash; Earlier / Original</span></p>
<div class="upload-zone dc-zone" id="dcZoneA" role="region" aria-label="Document A upload">
<input type="file" id="dcInputA" accept=".pdf,.docx,.txt" aria-label="Choose Document A">
<div id="dcPromptA" class="upload-prompt">
<span class="upload-icon" aria-hidden="true">&#8679;</span>
<p>Drop here or <label for="dcInputA" class="upload-browse">browse</label></p>
<p class="upload-hint"><strong>PDF</strong>, <strong>DOCX</strong>, <strong>TXT</strong> &mdash; max 8 MB</p>
</div>
<div id="dcFileInfoA" class="upload-file is-hidden">
<span id="dcFileNameA" class="upload-filename"></span>
<button type="button" id="dcClearA" class="upload-clear">&times;</button>
</div>
</div>
</div>
<div class="dc-upload-slot">
<p class="control-label">Document B <span class="dc-slot-hint">&mdash; Later / Comparison</span></p>
<div class="upload-zone dc-zone" id="dcZoneB" role="region" aria-label="Document B upload">
<input type="file" id="dcInputB" accept=".pdf,.docx,.txt" aria-label="Choose Document B">
<div id="dcPromptB" class="upload-prompt">
<span class="upload-icon" aria-hidden="true">&#8679;</span>
<p>Drop here or <label for="dcInputB" class="upload-browse">browse</label></p>
<p class="upload-hint"><strong>PDF</strong>, <strong>DOCX</strong>, <strong>TXT</strong> &mdash; max 8 MB</p>
</div>
<div id="dcFileInfoB" class="upload-file is-hidden">
<span id="dcFileNameB" class="upload-filename"></span>
<button type="button" id="dcClearB" class="upload-clear">&times;</button>
</div>
</div>
</div>
</div>
<div class="control-row" id="dcEngineControl">
<span class="control-label">Engine</span>
<label><input type="radio" name="dcEngine" value="azure_mini" checked> Azure gpt-4o-mini &#9733; <small class="control-hint">(~60-90s)</small></label>
<label><input type="radio" name="dcEngine" value="azure_full"> Azure gpt-4o <small class="control-hint">(best · ~2-3 min)</small></label>
<label><input type="radio" name="dcEngine" value="gpu"> GPU qwen2.5:14b <small class="control-hint">(local · ~90s)</small></label>
</div>
<p class="upload-hint">Engine applies to the final synthesis only. Document classification, party extraction, timelines, and cross-referencing always use azure-mini.</p>
<details class="advanced-panel" id="dcSlicePanel">
<summary class="advanced-toggle">Corpus slices <span class="control-hint">(used for legal significance context)</span></summary>
<p class="upload-hint" style="margin:8px 0">The corpus provides legal significance context for each discrepancy found. All four default slices cover the core Barnevernet framework.</p>
<div class="dr-slice-grid">
<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" aria-pressed="true">
<div class="dr-slice__head">
<span class="dr-slice__title">ECHR</span>
<span class="dr-slice__badge">on</span>
</div>
<p class="dr-slice__tagline">Art.&nbsp;8 family life, procedural fairness, HUDOC vs Norway</p>
</button>
<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="bufdir_guidance" aria-pressed="true">
<div class="dr-slice__head">
<span class="dr-slice__title">Bufdir Guidance</span>
<span class="dr-slice__badge">on</span>
</div>
<p class="dr-slice__tagline">Bufdir, Barneombudet, Statsforvalteren standards</p>
</button>
<button type="button" class="adv-slice" data-slice="norwegian_courts" aria-pressed="false">
<div class="dr-slice__head">
<span class="dr-slice__title">Norwegian Courts</span>
<span class="dr-slice__badge">off</span>
</div>
<p class="dr-slice__tagline">Høyesterett + Lagmannsrett family decisions</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</span>
<span class="dr-slice__badge">off</span>
</div>
<p class="dr-slice__tagline">NOUer, statutes, government background</p>
</button>
</div>
</details>
<div class="form-footer">
<p id="dcStatus" class="form-status" role="status" aria-live="polite"></p>
<button id="dcRunButton" type="submit">Find discrepancies</button>
</div>
</form>
<section id="dcResults" class="results deep-research-results" aria-live="polite">
<div class="empty-state">
<h3>Ready</h3>
<p>Upload two Barnevernet documents, then run. The agent will classify each document, extract parties and timelines, cross-reference them for discrepancies, and produce a corpus-backed legal significance report.</p>
<p class="upload-hint" style="margin-top:8px">Typical use: compare the original Bekymringsmelding against the later Vedtak, or compare two versions of a Barnevernet investigation report.</p>
</div>
</section>
<!-- Source modal -->
<div id="dcSourceModal" class="dr-source-modal is-hidden" role="dialog" aria-modal="true" aria-labelledby="dcSourceModalTitle">
<div class="dr-source-modal__dialog">
<header class="dr-source-modal__head">
<div>
<p class="eyebrow" id="dcSourceModalEyebrow">Source</p>
<h3 id="dcSourceModalTitle"></h3>
</div>
<button type="button" id="dcSourceModalClose" class="upload-clear" aria-label="Close">&times;</button>
</header>
<div class="dr-source-modal__body">
<aside class="dr-source-modal__meta" id="dcSourceModalMeta"></aside>
<div class="dr-source-modal__text" id="dcSourceModalText"></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>
<input type="radio" name="language" value="no">
<input type="radio" name="language" value="uk">
<input type="radio" name="language" value="pl">
</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'; ?>
File diff suppressed because it is too large Load Diff
+1
View File
@@ -25,6 +25,7 @@ final class FreeTier
'advocate' => 3,
'deep-research' => 5,
'transcribe' => 2, // flat rate; actual duration unknown upfront
'discrepancy' => 4, // 2 docs × 4 extraction steps + cross-ref + synthesis
];
/** Credit cost for a given tool slug. Returns 1 for unknown tools. */
+12 -7
View File
@@ -444,6 +444,7 @@ function dbnToolsLaunchedTools(?string $language = null): array
'barnevernet' => ['BVJ Analyzer', 'Barnevernet documents', 'Analyze child-welfare documents from your perspective with procedural red flags and citations.', 'Document + RAG'],
'advocate' => ['Advocate', 'Partisan brief', 'Choose who you represent and generate a source-grounded brief for that position.', 'ECHR + Lovdata'],
'deep-research' => ['Deep Research', 'Agent + RAG', 'Expand a question into research angles, search legal slices, and synthesize a cited brief.', 'Family-legal'],
'discrepancy' => ['Discrepancy Finder', 'Document comparison', 'Upload two versions of a Barnevernet document and find contradictions, deleted facts, and new allegations.', 'Cross-document AI'],
'corpus' => ['Corpus', 'Legal knowledge base', 'Inspect indexed sources, corpus health, legal categories, and retrieval behavior.', '~220 K passages'],
],
'no' => [
@@ -453,6 +454,7 @@ function dbnToolsLaunchedTools(?string $language = null): array
'barnevernet' => ['BVJ-analyse', 'Barnevernsdokumenter', 'Analyser barnevernsdokumenter fra ditt perspektiv med prosessuelle røde flagg og kilder.', 'Dokument + RAG'],
'advocate' => ['Advokatmodus', 'Partsinnlegg', 'Velg hvem du representerer og lag et kildebelagt innlegg for den posisjonen.', 'EMD + Lovdata'],
'deep-research' => ['Dyp research', 'Agent + RAG', 'Utvid et spørsmål til forskningsvinkler, søk juridiske kilder og lag et kildebelagt notat.', 'Familierett'],
'discrepancy' => ['Avviksfinner', 'Dokumentsammenligning', 'Last opp to versjoner av et barneverndokument og finn motsigelser, slettede fakta og nye påstander.', 'Kryssdokument AI'],
'corpus' => ['Korpus', 'Juridisk kunnskapsbase', 'Se indekserte kilder, korpushelse, juridiske kategorier og søkeoppsett.', '~220 K utdrag'],
],
'uk' => [
@@ -462,6 +464,7 @@ function dbnToolsLaunchedTools(?string $language = null): array
'barnevernet' => ['BVJ аналізатор', 'Документи Barnevernet', 'Аналізуйте документи захисту дітей з вашої позиції, з процесуальними ризиками та джерелами.', 'Документ + RAG'],
'advocate' => ['Адвокат', 'Позиційний бриф', 'Оберіть, кого представляєте, і створіть бриф із джерелами на підтримку цієї позиції.', 'ЄСПЛ + Lovdata'],
'deep-research' => ['Глибоке дослідження', 'Agent + RAG', 'Розгортає питання в дослідницькі напрями, шукає юридичні джерела та створює бриф.', 'Сімейне право'],
'discrepancy' => ['Пошук розбіжностей', 'Порівняння документів', 'Завантажте дві версії документа Barnevernet і знайдіть суперечності, видалені факти та нові твердження.', 'Міждокументний AI'],
'corpus' => ['Корпус', 'Юридична база знань', 'Переглядайте індексовані джерела, стан корпусу, категорії та поведінку пошуку.', '~220 тис. уривків'],
],
'pl' => [
@@ -471,20 +474,22 @@ function dbnToolsLaunchedTools(?string $language = null): array
'barnevernet' => ['Analizator BVJ', 'Dokumenty Barnevernet', 'Analizuj dokumenty opieki nad dziećmi z Twojej perspektywy, z ryzykami proceduralnymi i źródłami.', 'Dokument + RAG'],
'advocate' => ['Adwokat', 'Stronniczy brief', 'Wybierz, kogo reprezentujesz, i wygeneruj brief oparty na źródłach dla tej pozycji.', 'ETPC + Lovdata'],
'deep-research' => ['Głębokie badanie', 'Agent + RAG', 'Rozwija pytanie w kierunki badawcze, przeszukuje źródła prawne i tworzy brief z cytatami.', 'Prawo rodzinne'],
'discrepancy' => ['Wyszukiwacz rozbieżności', 'Porównanie dokumentów', 'Prześlij dwie wersje dokumentu Barnevernet i znajdź sprzeczności, usunięte fakty i nowe zarzuty.', 'AI Między-dokumentowe'],
'corpus' => ['Korpus', 'Prawna baza wiedzy', 'Sprawdzaj indeksowane źródła, stan korpusu, kategorie prawne i działanie wyszukiwania.', '~220 tys. fragmentów'],
],
];
$selected = $copy[$language] ?? $copy['en'];
$order = ['transcribe', 'timeline', 'redact', 'barnevernet', 'advocate', 'deep-research', 'corpus'];
$order = ['transcribe', 'timeline', 'redact', 'barnevernet', 'advocate', 'deep-research', 'discrepancy', 'corpus'];
$icons = [
'transcribe' => 'TR',
'timeline' => 'TL',
'redact' => 'RX',
'barnevernet' => 'BVJ',
'advocate' => 'ADV',
'transcribe' => 'TR',
'timeline' => 'TL',
'redact' => 'RX',
'barnevernet' => 'BVJ',
'advocate' => 'ADV',
'deep-research' => 'DR',
'corpus' => 'KB',
'discrepancy' => 'DC',
'corpus' => 'KB',
];
$out = [];
foreach ($order as $slug) {
+2
View File
@@ -16,5 +16,7 @@ $toolSvgs = [
'deep-research' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 480 178" preserveAspectRatio="xMidYMid slice" aria-hidden="true"><rect width="480" height="178" fill="#00205b"/><g stroke="rgba(244,197,66,0.24)" stroke-width="1.5" fill="none"><line x1="88" y1="48" x2="178" y2="88"/><line x1="88" y1="48" x2="88" y2="138"/><line x1="88" y1="138" x2="178" y2="88"/><line x1="178" y1="88" x2="268" y2="48"/><line x1="178" y1="88" x2="268" y2="138"/><line x1="268" y1="48" x2="268" y2="138"/><line x1="268" y1="48" x2="358" y2="68"/><line x1="268" y1="138" x2="358" y2="128"/></g><circle cx="88" cy="48" r="10" fill="rgba(244,197,66,0.68)"/><circle cx="88" cy="138" r="8" fill="rgba(255,255,255,0.46)"/><circle cx="178" cy="88" r="13" fill="#f4c542"/><circle cx="268" cy="48" r="9" fill="rgba(255,255,255,0.46)"/><circle cx="268" cy="138" r="8" fill="rgba(244,197,66,0.56)"/><circle cx="358" cy="68" r="9" fill="rgba(255,255,255,0.38)"/><circle cx="358" cy="128" r="8" fill="rgba(244,197,66,0.46)"/><circle cx="358" cy="88" r="44" fill="none" stroke="rgba(255,255,255,0.55)" stroke-width="3"/><line x1="392" y1="122" x2="424" y2="154" stroke="rgba(255,255,255,0.55)" stroke-width="4" stroke-linecap="round"/><circle cx="350" cy="80" r="6" fill="rgba(244,197,66,0.88)"/><circle cx="370" cy="96" r="5" fill="rgba(255,255,255,0.78)"/><line x1="350" y1="80" x2="370" y2="96" stroke="rgba(244,197,66,0.46)" stroke-width="1.5"/></svg>',
'discrepancy' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 480 178" preserveAspectRatio="xMidYMid slice" aria-hidden="true"><rect width="480" height="178" fill="#00205b"/><rect x="0" y="0" width="480" height="6" fill="rgba(186,12,47,0.18)"/><rect x="0" y="172" width="480" height="6" fill="rgba(186,12,47,0.18)"/><rect x="60" y="24" width="144" height="132" rx="4" fill="rgba(0,0,0,0.20)"/><rect x="57" y="21" width="144" height="132" rx="4" fill="#fff" opacity="0.97"/><rect x="57" y="21" width="144" height="26" rx="4" fill="#00205b"/><rect x="57" y="38" width="144" height="9" fill="#00205b"/><line x1="72" y1="32" x2="130" y2="32" stroke="rgba(255,255,255,0.58)" stroke-width="1.5"/><line x1="72" y1="60" x2="186" y2="60" stroke="rgba(0,0,0,0.09)" stroke-width="1.5"/><line x1="72" y1="72" x2="172" y2="72" stroke="rgba(0,0,0,0.09)" stroke-width="1.5"/><rect x="57" y="80" width="144" height="13" fill="rgba(244,197,66,0.20)"/><line x1="72" y1="87" x2="180" y2="87" stroke="rgba(244,197,66,0.68)" stroke-width="1.5"/><line x1="72" y1="103" x2="178" y2="103" stroke="rgba(0,0,0,0.09)" stroke-width="1.5"/><line x1="72" y1="115" x2="164" y2="115" stroke="rgba(0,0,0,0.09)" stroke-width="1.5"/><line x1="72" y1="127" x2="174" y2="127" stroke="rgba(186,12,47,0.28)" stroke-width="1.5" stroke-dasharray="22,4"/><line x1="72" y1="139" x2="148" y2="139" stroke="rgba(0,0,0,0.07)" stroke-width="1.5"/><text x="129" y="17" text-anchor="middle" font-family="monospace" font-size="8" fill="rgba(244,197,66,0.68)">DOC A</text><line x1="215" y1="87" x2="265" y2="87" stroke="rgba(186,12,47,0.70)" stroke-width="2" stroke-dasharray="5,3"/><polygon points="260,83 268,87 260,91" fill="rgba(186,12,47,0.70)"/><text x="240" y="82" text-anchor="middle" font-family="monospace" font-size="9" fill="rgba(186,12,47,0.82)">≠</text><line x1="215" y1="127" x2="265" y2="93" stroke="rgba(186,12,47,0.28)" stroke-width="1" stroke-dasharray="3,3"/><rect x="276" y="24" width="144" height="132" rx="4" fill="rgba(0,0,0,0.20)"/><rect x="273" y="21" width="144" height="132" rx="4" fill="#fff" opacity="0.97"/><rect x="273" y="21" width="144" height="26" rx="4" fill="#00205b"/><rect x="273" y="38" width="144" height="9" fill="#00205b"/><line x1="288" y1="32" x2="346" y2="32" stroke="rgba(255,255,255,0.58)" stroke-width="1.5"/><line x1="288" y1="60" x2="402" y2="60" stroke="rgba(0,0,0,0.09)" stroke-width="1.5"/><line x1="288" y1="72" x2="388" y2="72" stroke="rgba(0,0,0,0.09)" stroke-width="1.5"/><rect x="273" y="80" width="144" height="13" fill="rgba(186,12,47,0.13)"/><line x1="288" y1="87" x2="396" y2="87" stroke="rgba(186,12,47,0.55)" stroke-width="1.5"/><line x1="288" y1="103" x2="394" y2="103" stroke="rgba(0,0,0,0.09)" stroke-width="1.5"/><line x1="288" y1="115" x2="380" y2="115" stroke="rgba(0,0,0,0.09)" stroke-width="1.5"/><rect x="273" y="123" width="144" height="13" fill="rgba(244,197,66,0.10)"/><line x1="288" y1="130" x2="390" y2="130" stroke="rgba(244,197,66,0.42)" stroke-width="1.5"/><line x1="288" y1="143" x2="368" y2="143" stroke="rgba(0,0,0,0.07)" stroke-width="1.5"/><text x="345" y="17" text-anchor="middle" font-family="monospace" font-size="8" fill="rgba(244,197,66,0.68)">DOC B</text></svg>',
'corpus' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 480 178" preserveAspectRatio="xMidYMid slice" aria-hidden="true"><rect width="480" height="178" fill="#00205b"/><g stroke="rgba(244,197,66,0.28)" stroke-width="1.5" fill="none"><line x1="286" y1="54" x2="376" y2="36"/><line x1="286" y1="89" x2="390" y2="89"/><line x1="286" y1="124" x2="376" y2="143"/></g><circle cx="382" cy="36" r="16" fill="rgba(244,197,66,0.15)" stroke="rgba(244,197,66,0.38)" stroke-width="1.5"/><circle cx="396" cy="89" r="13" fill="rgba(255,255,255,0.06)" stroke="rgba(255,255,255,0.22)" stroke-width="1.5"/><circle cx="382" cy="143" r="16" fill="rgba(186,12,47,0.14)" stroke="rgba(186,12,47,0.34)" stroke-width="1.5"/><text x="382" y="40" text-anchor="middle" font-family="monospace" font-size="8" fill="rgba(244,197,66,0.72)">ECHR</text><text x="396" y="93" text-anchor="middle" font-family="monospace" font-size="7" fill="rgba(255,255,255,0.52)">LOV</text><text x="382" y="147" text-anchor="middle" font-family="monospace" font-size="7" fill="rgba(255,200,200,0.52)">BVJ</text><ellipse cx="196" cy="40" rx="78" ry="13" fill="rgba(0,18,56,0.94)" stroke="rgba(244,197,66,0.68)" stroke-width="1.5"/><rect x="118" y="40" width="156" height="26" fill="rgba(0,14,46,0.96)"/><ellipse cx="196" cy="66" rx="78" ry="13" fill="rgba(0,12,40,0.96)" stroke="rgba(244,197,66,0.48)" stroke-width="1.5"/><text x="196" y="58" text-anchor="middle" font-family="monospace" font-size="9" fill="rgba(244,197,66,0.62)">220K PASSAGES</text><ellipse cx="196" cy="78" rx="78" ry="13" fill="rgba(0,28,76,0.94)" stroke="rgba(255,255,255,0.36)" stroke-width="1.5"/><rect x="118" y="78" width="156" height="26" fill="rgba(0,12,46,0.96)"/><ellipse cx="196" cy="104" rx="78" ry="13" fill="rgba(0,10,38,0.96)" stroke="rgba(255,255,255,0.26)" stroke-width="1.5"/><text x="196" y="96" text-anchor="middle" font-family="monospace" font-size="9" fill="rgba(255,255,255,0.46)">QDRANT + AZURE</text><ellipse cx="196" cy="116" rx="78" ry="13" fill="rgba(186,12,47,0.18)" stroke="rgba(186,12,47,0.48)" stroke-width="1.5"/><rect x="118" y="116" width="156" height="20" fill="rgba(100,4,18,0.14)"/><ellipse cx="196" cy="136" rx="78" ry="13" fill="rgba(80,3,14,0.20)" stroke="rgba(186,12,47,0.32)" stroke-width="1.5"/><text x="196" y="130" text-anchor="middle" font-family="monospace" font-size="9" fill="rgba(255,170,170,0.48)">NOMIC EMBED</text></svg>',
];