Files
dobetternorge-tools/api/deep-research.php
T
daveadmin a1a7f442a7 Deep Research: NDJSON streaming so the connection survives long runs
Previously the endpoint returned a single JSON object at the end. Apache+
PHP-FPM buffers the entire body until PHP exits, so a 160s azure_full run
caused the browser to drop the fetch as "Failed to fetch" while the server
was still synthesising — the response then arrived to a dead socket.

Switch to application/x-ndjson with one event per line. The endpoint emits
'progress', 'start', 'step' (running/complete/warning/error), 'subq', and a
final 'final' event carrying the full result payload. Output buffering is
explicitly disabled so each line flushes through Apache as soon as the
agent emits it.

DbnDeepResearchAgent::run() now accepts an optional ?callable $emit and
fires step:running before each step + step:complete after, plus a subq
event per sub-question retrieval round.

JS reads response.body as a stream, splits on newlines, updates the
trace panel live, and renders the final result when the final event
arrives. Status pill shows live progress detail (e.g. "Synthesising with
Azure gpt-4o — this is the slowest step…").

Engine row in the form now shows expected duration per engine
(~15-45s mini, ~60-180s full, ~30-90s GPU) so users know what they're in
for before clicking Run.
2026-05-15 10:47:35 +02:00

156 lines
5.9 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/bootstrap.php';
require_once __DIR__ . '/../includes/DeepResearchAgent.php';
dbnToolsRequireMethod('POST');
dbnToolsRequireAuth();
// Stream-friendly response — defeat output buffering so the user's browser
// receives progress events while the agent runs (can take 60-180s for
// gpt-4o synthesis or multi-file ingest).
@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');
$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');
$seedQuery = trim((string)($input['query'] ?? ''));
$pastedText = trim((string)($input['paste_text'] ?? ''));
$sliceInput = $input['slices'] ?? [];
$engine = (string)($input['engine'] ?? 'azure_mini');
$controls = is_array($input['controls'] ?? null) ? $input['controls'] : [];
if (mb_strlen($seedQuery, 'UTF-8') > 4000) {
throw new DbnToolsHttpException('Query is too long.', 422, 'query_too_long');
}
if (mb_strlen($pastedText, 'UTF-8') > 64000) {
throw new DbnToolsHttpException('Pasted text is too long.', 422, 'paste_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' : ''
),
]);
}
}
$emit('start', [
'engine' => $engine,
'language' => $language,
'upload_count' => count($uploadedFiles),
]);
$result = (new DbnDeepResearchAgent())->run(
$seedQuery,
$pastedText,
$uploadedFiles,
is_array($sliceInput) ? $sliceInput : [],
$engine,
$language,
$controls,
$emit
);
$result['ok'] = true;
$result['latency_ms'] = (int)round((microtime(true) - $startTime) * 1000);
dbnToolsLogMetadata([
'tool' => 'deep_research',
'language' => $language,
'ok' => true,
'latency_ms' => $result['latency_ms'],
'chunk_count' => (int)($result['trace_metadata']['chunk_count'] ?? 0),
'source_count' => (int)($result['trace_metadata']['source_count'] ?? 0),
'deployment' => $result['trace_metadata']['deployment'] ?? null,
]);
$emit('final', ['result' => $result]);
} catch (DbnToolsHttpException $e) {
$latency = (int)round((microtime(true) - $startTime) * 1000);
dbnToolsLogMetadata([
'tool' => 'deep_research',
'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 deep research fatal: ' . $e->getMessage());
$latency = (int)round((microtime(true) - $startTime) * 1000);
dbnToolsLogMetadata([
'tool' => 'deep_research',
'language' => $language,
'ok' => false,
'latency_ms' => $latency,
'error_code' => 'internal_error',
]);
$emit('error', ['code' => 'internal_error', 'message' => 'The agent could not complete this request.']);
}