Add Summarize Document tool — engine selector, file upload, optional corpus enrichment
- summarize.php: full custom inline form (replaces tool_form.php wrapper) with lang switcher, azure_mini/azure_full/gpu engine selector, 8 corpus-slice toggles (all off by default), doc picker, file upload zone, and textarea - api/summarize.php: rewritten to streaming NDJSON (matches barnevernet pattern); accepts JSON payload with text, language, engine, slices[], doc_ids[] - includes/LegalTools.php: adds corpusContextForSummarize() (keyword search via ClientRagPipeline) and summarizeWithContext() (engine-aware LLM call with optional corpus prepend); returns structured JSON matching existing summarize format - assets/js/summarize.js: self-contained IIFE handling file upload via api/extract.php, slice toggles, NDJSON stream reader, result rendering, and trace panel update - includes/i18n.php: adds 'summarize' to nav in all 4 languages (EN/NO/UK/PL), inserted after 'redact' in the tool order with icon 'SZ' Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+74
-6
@@ -2,13 +2,81 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../includes/LegalTools.php';
|
||||
require_once __DIR__ . '/../includes/ToolModels.php';
|
||||
|
||||
dbnToolsRequireMethod('POST');
|
||||
dbnToolsRequireAuth();
|
||||
$input = dbnToolsJsonInput(70000);
|
||||
$language = dbnToolsNormalizeLanguage($input['language'] ?? 'en');
|
||||
$ftUid = dbnToolsFreeTierCheck('summarize');
|
||||
|
||||
dbnToolsWithTelemetry('summarize', $language, function () use ($input, $language): array {
|
||||
$text = dbnToolsString($input, 'text', 32000);
|
||||
return (new DbnLegalToolsService())->summarize($text, $language);
|
||||
});
|
||||
$input = dbnToolsJsonInput(400000);
|
||||
$language = dbnToolsNormalizeLanguage($input['language'] ?? 'en');
|
||||
$engine = ToolModels::engineForUser($ftUid, (string)($input['engine'] ?? 'azure_mini'));
|
||||
$slices = is_array($input['slices'] ?? null) ? array_values(array_filter($input['slices'])) : [];
|
||||
|
||||
// Streaming headers — flush each NDJSON line as it's written
|
||||
header('Content-Type: application/x-ndjson; charset=utf-8');
|
||||
header('X-Accel-Buffering: no');
|
||||
header('Cache-Control: no-cache, no-store');
|
||||
while (ob_get_level()) {
|
||||
ob_end_clean();
|
||||
}
|
||||
|
||||
$emit = static function (string $event, array $payload): void {
|
||||
echo json_encode(['event' => $event] + $payload, JSON_UNESCAPED_UNICODE) . "\n";
|
||||
flush();
|
||||
};
|
||||
|
||||
try {
|
||||
$text = dbnToolsInjectDocContent($input, dbnToolsString($input, 'text', 128000));
|
||||
|
||||
if (mb_strlen(trim($text), 'UTF-8') < 50) {
|
||||
$emit('error', [
|
||||
'error' => 'Paste text, upload a file, or select a document before running.',
|
||||
'code' => 'empty_text',
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$emit('progress', [
|
||||
'step' => 'text_ready',
|
||||
'detail' => mb_strlen($text, 'UTF-8') . ' chars ready.',
|
||||
]);
|
||||
|
||||
$corpusContext = '';
|
||||
if (!empty($slices)) {
|
||||
$sliceCount = count($slices);
|
||||
$emit('progress', [
|
||||
'step' => 'corpus_search',
|
||||
'detail' => 'Searching legal corpus (' . $sliceCount . ' slice' . ($sliceCount === 1 ? '' : 's') . ')…',
|
||||
]);
|
||||
$svc = new DbnLegalToolsService();
|
||||
$query = mb_substr(trim($text), 0, 600, 'UTF-8');
|
||||
$corpusContext = $svc->corpusContextForSummarize($query, 8);
|
||||
$emit('progress', [
|
||||
'step' => 'corpus_done',
|
||||
'detail' => $corpusContext !== ''
|
||||
? 'Legal context retrieved.'
|
||||
: 'No matching passages found; summarising without corpus.',
|
||||
]);
|
||||
}
|
||||
|
||||
$emit('progress', [
|
||||
'step' => 'generating',
|
||||
'detail' => 'Generating summary…',
|
||||
]);
|
||||
|
||||
$result = (new DbnLegalToolsService())->summarizeWithContext($text, $language, $engine, $corpusContext);
|
||||
|
||||
if ($ftUid > 0) {
|
||||
$balance = dbnToolsFreeTierDeduct($ftUid, 'summarize');
|
||||
$result['balance'] = $balance;
|
||||
}
|
||||
|
||||
$emit('final', $result);
|
||||
|
||||
} catch (DbnToolsHttpException $e) {
|
||||
$emit('error', ['error' => $e->getMessage(), 'code' => (string)$e->getCode()]);
|
||||
} catch (Throwable $e) {
|
||||
error_log('summarize error: ' . $e->getMessage());
|
||||
$emit('error', ['error' => 'An unexpected error occurred. Please try again.', 'code' => 'server_error']);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user