Files
dobetternorge-tools/api/barnevernet.php
T
daveadmin 2013648ee0 Add manual 'Save result' to all tools — replaces auto-save
All tool results can now be saved to My Case manually. Users click
'Save result', type a description, and confirm. This replaces the
previous silent auto-save on barnevernet/timeline/etc., giving users
control over what stays and what it's called (supports multiple runs
of the same tool with different titles).

- CaseResults: extend ELIGIBLE_TOOLS to include summarize, ask, redact,
  transcribe; add toolLabel/toolIcon entries; support explicit title
  via meta['title'] in save()
- api/case/save-result.php: new client-initiated save endpoint;
  accepts tool + title + input_payload + output_payload + meta
- Remove CaseResults::save() auto-save from barnevernet, deep-research,
  discrepancy, korrespond, timeline API endpoints
- tools.js: add showSaveResultButton() (exposed as window.dbnShowSaveResultButton);
  wire for ask, redact, timeline, transcribe (both file-upload and
  stored-audio paths)
- barnevernet.js: wire save button after final result render
- summarize.js: wire save button after renderFinal(); passes sumResults
  container so widget appears in the correct #sumResults div
- case-result.php: rich tool-specific rendering for summarize, ask,
  redact, transcribe, timeline; update re-run link map to include all
  new tools
- tools.css: styles for .save-result-widget and its states (idle,
  prompt, done, error)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-24 01:27:26 +02:00

178 lines
6.9 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/bootstrap.php';
require_once __DIR__ . '/../includes/BvjAnalyzerAgent.php';
require_once __DIR__ . '/../includes/ToolModels.php';
dbnToolsRequireMethod('POST');
dbnToolsRequireAuth();
$ftUid = dbnToolsFreeTierCheck('barnevernet');
$ftRemaining = dbnToolsFreeTierDeduct($ftUid, 'barnevernet');
@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 {
$isMultipart = stripos((string)($_SERVER['CONTENT_TYPE'] ?? ''), 'multipart/form-data') !== false;
if ($isMultipart) {
$payloadRaw = (string)($_POST['payload'] ?? '');
if ($payloadRaw === '') {
throw new DbnToolsHttpException('Multipart request missing payload.', 422, 'missing_payload');
}
$input = json_decode($payloadRaw, true);
if (!is_array($input)) {
throw new DbnToolsHttpException('Invalid payload JSON.', 422, 'invalid_payload_json');
}
} else {
$raw = file_get_contents('php://input');
if ($raw === false || strlen($raw) > 120000) {
throw new DbnToolsHttpException('Request body unreadable or too large.', 413, 'body_too_large');
}
$input = json_decode((string)$raw, true);
if (!is_array($input)) {
throw new DbnToolsHttpException('Request body must be valid JSON.', 400, 'invalid_json');
}
}
$language = dbnToolsNormalizeLanguage($input['language'] ?? 'en');
$advocateRole = trim((string)($input['advocate_role'] ?? ''));
$engine = ToolModels::engineForUser($ftUid, (string)($input['engine'] ?? 'azure_mini'));
$sliceInput = $input['slices'] ?? [];
$controls = is_array($input['controls'] ?? null) ? $input['controls'] : [];
$additionalNotes = mb_substr(trim((string)($input['additional_notes'] ?? '')), 0, 2000, 'UTF-8');
if (mb_strlen($advocateRole, 'UTF-8') > 200) {
throw new DbnToolsHttpException('advocate_role is too long.', 422, 'advocate_role_too_long');
}
if (mb_strlen($additionalNotes, 'UTF-8') > 2000) {
throw new DbnToolsHttpException('additional_notes is too long.', 422, 'notes_too_long');
}
$emit('progress', ['detail' => 'Reading upload(s)…']);
$uploadedFiles = [];
if (!empty($_FILES['files']) && is_array($_FILES['files']['tmp_name'] ?? null)) {
$count = count($_FILES['files']['tmp_name']);
if ($count > 5) {
throw new DbnToolsHttpException('At most 5 files can be uploaded per request.', 413, 'too_many_files');
}
for ($i = 0; $i < $count; $i++) {
$file = [
'name' => $_FILES['files']['name'][$i] ?? '',
'type' => $_FILES['files']['type'][$i] ?? '',
'tmp_name' => $_FILES['files']['tmp_name'][$i] ?? '',
'error' => $_FILES['files']['error'][$i] ?? UPLOAD_ERR_NO_FILE,
'size' => $_FILES['files']['size'][$i] ?? 0,
];
$extracted = dbnToolsExtractUploadedFile($file);
$uploadedFiles[] = [
'filename' => $extracted['filename'],
'text' => $extracted['text'],
'chars' => $extracted['chars'],
'truncated' => $extracted['truncated'],
];
$emit('progress', [
'detail' => sprintf('Extracted %s (%d chars%s)',
$extracted['filename'],
$extracted['chars'],
!empty($extracted['truncated']) ? ', truncated' : ''
),
]);
}
}
if (empty($uploadedFiles)) {
throw new DbnToolsHttpException(
'Upload at least one BVJ document (PDF, DOCX, or TXT) before running the analyzer.',
422, 'no_uploads'
);
}
$emit('start', [
'engine' => $engine,
'language' => $language,
'file_count' => count($uploadedFiles),
]);
// Optional: append user's case-context chunks to the last uploaded document text,
// so the agent reads them as supplementary background.
$useMyCase = !empty($input['use_my_case']);
if ($useMyCase && !empty($uploadedFiles)) {
$retrievalQuery = mb_substr((string)$uploadedFiles[0]['text'], 0, 2000, 'UTF-8');
$caseBlock = dbnToolsCaseContext(true, $retrievalQuery, 5);
if ($caseBlock !== '') {
$uploadedFiles[0]['text'] .= "\n\n" . $caseBlock;
}
}
$result = (new DbnBvjAnalyzerAgent())->run(
$uploadedFiles,
$advocateRole,
$engine,
$language,
is_array($sliceInput) ? $sliceInput : [],
$controls,
$additionalNotes,
$emit
);
$result['ok'] = true;
$result['latency_ms'] = (int)round((microtime(true) - $startTime) * 1000);
dbnToolsLogMetadata([
'tool' => 'bvj_analyzer',
'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,
'bvj_doc_type' => $result['doc_meta']['doc_type'] ?? null,
]);
$emit('final', ['result' => $result]);
} catch (DbnToolsHttpException $e) {
$latency = (int)round((microtime(true) - $startTime) * 1000);
dbnToolsLogMetadata([
'tool' => 'bvj_analyzer',
'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 BVJ analyzer fatal: ' . $e->getMessage());
$latency = (int)round((microtime(true) - $startTime) * 1000);
dbnToolsLogMetadata([
'tool' => 'bvj_analyzer',
'language' => $language,
'ok' => false,
'latency_ms' => $latency,
'error_code' => 'internal_error',
]);
$emit('error', ['code' => 'internal_error', 'message' => 'The analyzer could not complete this request.']);
}