timeline: remove GPU, add SSE status updates, DOCX export, single-file, engine-aware credits

- Remove GPU/cuttlefish engine from timeline.php, api/timeline.php, LegalTools.php, tools.js (all 4 langs)
- Add engine-aware credit cost: gpt-4o-mini=1 credit, gpt-4o=2 credits (matches redact pattern)
- Remove multiple attribute from file input (single document only)
- New api/timeline-stream.php: SSE endpoint emitting status events + final result
- New api/timeline-download.php: DOCX export of timeline events
- LegalTools::timeline() gains ?callable $onProgress for live status updates
- tools.js: spinner on run, SSE streaming fetch, Export to Word button
- Save to My Docs was already wired (showSaveResultButton at line 1136)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-25 09:32:28 +02:00
parent 4b8b675a64
commit d47024ed67
7 changed files with 406 additions and 43 deletions
+175
View File
@@ -0,0 +1,175 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/LegalTools.php';
dbnToolsRequireMethod('POST');
dbnToolsRequireAuth();
$input = dbnToolsJsonInput(200000);
$events = is_array($input['events'] ?? null) ? $input['events'] : [];
$whatFound = trim((string)($input['what_we_found'] ?? ''));
$format = (string)($input['format'] ?? 'docx');
if ($format !== 'docx') {
http_response_code(400);
echo json_encode(['error' => ['message' => 'Unsupported format.']]);
exit;
}
if (!$events) {
http_response_code(422);
echo json_encode(['error' => ['message' => 'No events provided.']]);
exit;
}
if (!class_exists('ZipArchive')) {
http_response_code(500);
echo json_encode(['error' => ['message' => 'ZipArchive extension not available.']]);
exit;
}
$text = buildTimelineText($events, $whatFound);
$docx = buildTimelineDocx($text);
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="timeline.docx"');
header('Content-Length: ' . strlen($docx));
header('Cache-Control: no-store');
echo $docx;
exit;
function buildTimelineText(array $events, string $whatFound): string
{
$lines = [];
if ($whatFound !== '') {
$lines[] = $whatFound;
$lines[] = '';
$lines[] = str_repeat("\u{2500}", 40);
$lines[] = '';
}
foreach ($events as $ev) {
$date = (string)($ev['date'] ?? 'unknown date');
$time = (string)($ev['time'] ?? '');
$actor = (string)($ev['actor'] ?? '');
$event = (string)($ev['event'] ?? '');
$excerpt = (string)($ev['source_excerpt'] ?? '');
$conf = (string)($ev['confidence'] ?? '');
$dateStr = $time !== '' ? "{$date} {$time}" : $date;
$header = $actor !== '' ? "[{$dateStr}] {$actor}" : "[{$dateStr}]";
if ($conf !== '' && $conf !== 'high') {
$header .= " ({$conf} confidence)";
}
$lines[] = $header;
if ($event !== '') $lines[] = $event;
if ($excerpt !== '') $lines[] = "Source: \u{201C}{$excerpt}\u{201D}";
$lines[] = '';
$lines[] = str_repeat("\u{2500}", 40);
$lines[] = '';
}
return implode("\n", $lines);
}
function buildTimelineDocx(string $text): string
{
$tmp = tempnam(sys_get_temp_dir(), 'dbn_tl_');
@unlink($tmp);
$tmp .= '.docx';
$zip = new ZipArchive();
$zip->open($tmp, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->addFromString('[Content_Types].xml', tlContentTypesXml());
$zip->addFromString('_rels/.rels', tlRelsXml());
$zip->addFromString('word/document.xml', tlDocumentXml($text));
$zip->addFromString('word/_rels/document.xml.rels', tlWordRelsXml());
$zip->addFromString('docProps/app.xml', tlAppXml());
$zip->addFromString('docProps/core.xml', tlCoreXml());
$zip->close();
$bytes = file_get_contents($tmp);
@unlink($tmp);
return $bytes;
}
function tlDocumentXml(string $text): string
{
$lines = explode("\n", str_replace("\r\n", "\n", str_replace("\r", "\n", $text)));
$paras = [];
foreach ($lines as $line) {
$safe = htmlspecialchars($line, ENT_XML1 | ENT_COMPAT, 'UTF-8');
if ($safe === '') {
$paras[] = '<w:p/>';
} elseif (preg_match('/^\[.+?\]/', $line)) {
// Event header line — bold
$paras[] = '<w:p><w:r><w:rPr><w:b/><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/><w:sz w:val="20"/></w:rPr>'
. '<w:t xml:space="preserve">' . $safe . '</w:t></w:r></w:p>';
} elseif (str_starts_with($line, str_repeat("\u{2500}", 5))) {
// Divider line — light grey
$paras[] = '<w:p><w:r><w:rPr><w:color w:val="AAAAAA"/><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/><w:sz w:val="16"/></w:rPr>'
. '<w:t xml:space="preserve">' . $safe . '</w:t></w:r></w:p>';
} else {
$paras[] = '<w:p><w:r><w:rPr><w:rFonts w:ascii="Calibri" w:hAnsi="Calibri"/><w:sz w:val="20"/></w:rPr>'
. '<w:t xml:space="preserve">' . $safe . '</w:t></w:r></w:p>';
}
}
return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
. '<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">'
. '<w:body>' . implode('', $paras)
. '<w:sectPr><w:pgSz w:w="12240" w:h="15840"/>'
. '<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440"/>'
. '</w:sectPr></w:body></w:document>';
}
function tlContentTypesXml(): string
{
return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
. '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">'
. '<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>'
. '<Default Extension="xml" ContentType="application/xml"/>'
. '<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>'
. '<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>'
. '<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>'
. '</Types>';
}
function tlRelsXml(): string
{
return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
. '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'
. '<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>'
. '<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>'
. '<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>'
. '</Relationships>';
}
function tlWordRelsXml(): string
{
return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
. '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"/>';
}
function tlAppXml(): string
{
return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
. '<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties">'
. '<Application>DoBetterNorge Timeline</Application>'
. '</Properties>';
}
function tlCoreXml(): string
{
$date = date('Y-m-d\TH:i:s\Z');
return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
. '<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties"'
. ' xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/"'
. ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
. '<dc:creator>DoBetterNorge</dc:creator>'
. '<dcterms:created xsi:type="dcterms:W3CDTF">' . $date . '</dcterms:created>'
. '</cp:coreProperties>';
}
+121
View File
@@ -0,0 +1,121 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/LegalTools.php';
require_once __DIR__ . '/../includes/ToolModels.php';
dbnToolsRequireMethod('POST');
dbnToolsRequireAuth();
// Parse input and run credit pre-check BEFORE emitting SSE headers so that
// auth/credit errors can still return JSON (dbnToolsError / dbnToolsAbort).
$input = dbnToolsJsonInput(400000);
$language = dbnToolsNormalizeLanguage($input['language'] ?? 'en');
$_validEngines = ['azure_mini', 'azure_full'];
$_engine = in_array((string)($input['engine'] ?? ''), $_validEngines, true)
? (string)$input['engine'] : 'azure_mini';
$_engineCredits = $_engine === 'azure_full' ? 2 : 1;
$ftUid = dbnToolsFreeTierCheckAmount('timeline', $_engineCredits);
// Only switch to SSE mode once auth + credit checks pass.
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache, no-transform');
header('X-Accel-Buffering: no');
while (ob_get_level() > 0) ob_end_flush();
ob_implicit_flush(true);
function sseEmit(string $event, array $data): void
{
echo "event: {$event}\n";
echo 'data: ' . json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n\n";
if (function_exists('flush')) @flush();
}
$start = microtime(true);
try {
$text = dbnToolsInjectDocContent($input, dbnToolsString($input, 'text', 128000, false));
if (mb_strlen(trim($text), 'UTF-8') < 10) {
sseEmit('error', ['code' => 'empty_text', 'message' => 'Paste text, upload a file, or select a document before running.']);
exit;
}
$validEngines = ['azure_mini', 'azure_full'];
$engine = in_array((string)($input['engine'] ?? ''), $validEngines, true)
? (string)$input['engine'] : 'azure_mini';
$engine = ToolModels::engineForUser($ftUid, $engine);
$validFocus = ['all', 'deadlines', 'hearings', 'cps'];
$focus = in_array((string)($input['focus'] ?? ''), $validFocus, true)
? (string)$input['focus'] : 'all';
$confidenceFilter = (string)($input['confidence_filter'] ?? '') === 'high_medium'
? 'high_medium' : 'all';
$includeRelative = ($input['include_relative'] ?? true) !== false;
$includeBackground = ($input['include_background'] ?? true) !== false;
$userNotes = dbnToolsString($input, 'user_notes', 2000, false);
$useMyCase = !empty($input['use_my_case']);
if ($useMyCase) {
$caseBlock = dbnToolsCaseContext(true, $text, 5);
if ($caseBlock !== '') {
$text = $text . "\n\n" . $caseBlock;
}
}
$result = (new DbnLegalToolsService())->timeline(
$text, $language, $engine, $focus, $confidenceFilter,
$includeRelative, $includeBackground, $userNotes,
fn(string $msg) => sseEmit('status', ['msg' => $msg])
);
$latency = (int)round((microtime(true) - $start) * 1000);
if ($ftUid > 0) {
$balance = dbnToolsFreeTierDeductAmount($ftUid, 'timeline', $_engineCredits);
$result['balance'] = $balance;
if (!headers_sent()) {
header('X-Credits-Remaining: ' . $balance);
}
}
$result['ok'] = true;
$result['latency_ms'] = $latency;
dbnToolsLogMetadata([
'tool' => 'timeline',
'language' => $language,
'ok' => true,
'latency_ms' => $latency,
'chunk_count' => (int)($result['trace_metadata']['chunk_count'] ?? 0),
'source_count' => (int)($result['trace_metadata']['source_count'] ?? 0),
'deployment' => $result['trace_metadata']['deployment'] ?? null,
]);
sseEmit('result', $result);
} catch (DbnToolsHttpException $e) {
$latency = (int)round((microtime(true) - $start) * 1000);
dbnToolsLogMetadata([
'tool' => 'timeline',
'language' => $language,
'ok' => false,
'latency_ms' => $latency,
'error_code' => $e->errorCode,
]);
sseEmit('error', ['code' => $e->errorCode, 'message' => $e->getMessage()]);
} catch (Throwable $e) {
$latency = (int)round((microtime(true) - $start) * 1000);
dbnToolsLogMetadata([
'tool' => 'timeline',
'language' => $language,
'ok' => false,
'latency_ms' => $latency,
'error_code' => 'internal_error',
]);
error_log('timeline-stream error: ' . $e->getMessage());
sseEmit('error', ['code' => 'internal_error', 'message' => 'The tool could not complete this request.']);
}
+8 -4
View File
@@ -6,8 +6,12 @@ require_once __DIR__ . '/../includes/ToolModels.php';
dbnToolsRequireMethod('POST');
dbnToolsRequireAuth();
$ftUid = dbnToolsFreeTierCheck('timeline');
$input = dbnToolsJsonInput(400000);
$_validEngines = ['azure_mini', 'azure_full'];
$_engine = in_array((string)($input['engine'] ?? ''), $_validEngines, true)
? (string)$input['engine'] : 'azure_mini';
$_engineCredits = $_engine === 'azure_full' ? 2 : 1;
$ftUid = dbnToolsFreeTierCheckAmount('timeline', $_engineCredits);
$language = dbnToolsNormalizeLanguage($input['language'] ?? 'en');
dbnToolsWithChargedTelemetry('timeline', $language, $ftUid, function () use ($input, $language, $ftUid): array {
@@ -16,7 +20,7 @@ dbnToolsWithChargedTelemetry('timeline', $language, $ftUid, function () use ($in
dbnToolsAbort('Paste text, upload a file, or select a document before running.', 422, 'empty_text');
}
$validEngines = ['azure_mini', 'azure_full', 'gpu'];
$validEngines = ['azure_mini', 'azure_full'];
$engine = in_array((string)($input['engine'] ?? ''), $validEngines, true)
? (string)$input['engine'] : 'azure_mini';
$engine = ToolModels::engineForUser($ftUid, $engine);
@@ -42,7 +46,7 @@ dbnToolsWithChargedTelemetry('timeline', $language, $ftUid, function () use ($in
}
}
$result = (new DbnLegalToolsService())->timeline($text, $language, $engine, $focus, $confidenceFilter, $includeRelative, $includeBackground, $userNotes);
$result = (new DbnLegalToolsService())->timeline($text, $language, $engine, $focus, $confidenceFilter, $includeRelative, $includeBackground, $userNotes, null);
return $result;
});
}, $_engineCredits);