feat: document & audio corpus picker for all tools

- Add "Select from My Docs" button to all text tool forms; free-tier
  users see an upgrade modal, paid (CaveauAI) users get a searchable
  multi-select modal backed by /api/dashboard/documents.php
- Add "Select from My Audio" picker on Transcribe with single-select
  and a "Save to My Audio" button for persisting uploaded clips
- New PHP helpers in bootstrap.php: dbnToolsFetchDocChunks,
  dbnToolsClientIdFromSession, dbnToolsInjectDocContent
- timeline, ask, redact APIs prepend selected document content
  (fetched from client_chunks SQL) before the textarea text
- api/dashboard/audio-upload.php stores audio files on server and
  creates a client_documents row with source_type='audio'
- api/transcribe.php falls back to stored audio via audio_doc_id POST
  field when no file is uploaded
- api/dashboard/documents.php supports ?source_type= filter
- tools.js: doc_ids added to JSON payload; stored-audio transcribe path
- New assets/css/doc-picker.css, assets/js/doc-picker.js
- SQL migration: scripts/sql/audio_docs_column.sql

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-23 21:38:04 +02:00
parent 58e1d1dae1
commit f383ad5b74
14 changed files with 857 additions and 15 deletions
+1 -1
View File
@@ -12,6 +12,6 @@ $input = dbnToolsJsonInput(25000);
$language = dbnToolsNormalizeLanguage($input['language'] ?? 'en');
dbnToolsWithTelemetry('ask', $language, function () use ($input, $language): array {
$question = dbnToolsString($input, 'question', 4000);
$question = dbnToolsInjectDocContent($input, dbnToolsString($input, 'question', 4000));
return (new DbnLegalToolsService())->ask($question, $language);
});
+85
View File
@@ -0,0 +1,85 @@
<?php
/**
* POST /api/dashboard/audio-upload.php
*
* Stores an uploaded audio file on the server and creates a client_documents
* row with source_type='audio' so it appears in the Audio picker on Transcribe.
*
* Request: multipart/form-data, field name "audio"
* Response: { ok: true, document: { id, title, file_size_bytes, audio_storage_path } }
*/
declare(strict_types=1);
require_once dirname(__DIR__, 2) . '/includes/bootstrap.php';
dbnToolsRequireAuth();
dbnToolsRequireMethod('POST');
try {
$tenant = dbnToolsEnsureDashboardTenant();
} catch (DbnToolsHttpException $e) {
dbnToolsError($e->getMessage(), $e->status, $e->errorCode);
}
$clientId = (int)$tenant['client_id'];
if (empty($_FILES['audio']) || $_FILES['audio']['error'] !== UPLOAD_ERR_OK) {
$code = $_FILES['audio']['error'] ?? -1;
$msgs = [
UPLOAD_ERR_INI_SIZE => 'File exceeds server upload limit.',
UPLOAD_ERR_FORM_SIZE => 'File exceeds form size limit.',
UPLOAD_ERR_PARTIAL => 'File was only partially uploaded.',
UPLOAD_ERR_NO_FILE => 'No audio file received.',
];
dbnToolsError($msgs[$code] ?? "Upload error (code {$code}).", 400, 'upload_error');
}
$file = $_FILES['audio'];
$maxBytes = 200 * 1024 * 1024;
if ($file['size'] > $maxBytes) {
dbnToolsError('File too large. Maximum 200 MB.', 413, 'file_too_large');
}
$allowedExts = ['mp3', 'wav', 'ogg', 'oga', 'm4a', 'mp4', 'flac', 'webm', 'aac'];
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
if (!in_array($ext, $allowedExts, true)) {
dbnToolsError("Unsupported format: .{$ext}. Use MP3, WAV, OGG, M4A, FLAC, or WebM.", 415, 'unsupported_format');
}
// Resolve storage directory
$storageRoot = dbnToolsEnv('AUDIO_STORAGE_ROOT', '/home/dobetternorge/audio-uploads');
$clientDir = rtrim($storageRoot, '/') . '/' . $clientId;
if (!is_dir($clientDir) && !mkdir($clientDir, 0750, true)) {
dbnToolsError('Could not create storage directory.', 500, 'storage_error');
}
$uniqueId = bin2hex(random_bytes(12));
$storagePath = $clientDir . '/' . $uniqueId . '.' . $ext;
if (!move_uploaded_file($file['tmp_name'], $storagePath)) {
dbnToolsError('Failed to store uploaded file.', 500, 'move_error');
}
$title = pathinfo($file['name'], PATHINFO_FILENAME);
$title = preg_replace('/[_\-]+/', ' ', $title);
$title = mb_substr(trim($title), 0, 200) ?: 'Audio ' . date('Y-m-d');
$db = dbnToolsDb();
$db->prepare(
'INSERT INTO client_documents
(client_id, title, source_type, audio_storage_path, status, file_size_bytes, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, NOW(), NOW())'
)->execute([$clientId, $title, 'audio', $storagePath, 'ready', (int)$file['size']]);
$docId = (int)$db->lastInsertId();
dbnToolsRespond([
'ok' => true,
'document' => [
'id' => $docId,
'title' => $title,
'file_size_bytes' => (int)$file['size'],
'audio_storage_path' => $storagePath,
'source_type' => 'audio',
],
]);
+6
View File
@@ -79,6 +79,12 @@ function respondList(PDO $db, int $clientId): void
$where[] = 'category = ?';
$params[] = $category;
}
$sourceType = trim((string)($_GET['source_type'] ?? ''));
$allowedSourceTypes = ['text', 'audio', 'url', 'tool-output', 'upload'];
if ($sourceType !== '' && in_array($sourceType, $allowedSourceTypes, true)) {
$where[] = 'source_type = ?';
$params[] = $sourceType;
}
$whereSql = 'WHERE ' . implode(' AND ', $where);
+1 -1
View File
@@ -11,7 +11,7 @@ if ($ftRemaining >= 0) { header('X-Credits-Remaining: ' . $ftRemaining); }
$input = dbnToolsJsonInput(400000);
dbnToolsWithTelemetry('redact', '', function () use ($input): array {
$text = dbnToolsString($input, 'text', 128000);
$text = dbnToolsInjectDocContent($input, dbnToolsString($input, 'text', 128000));
$mode = (string)($input['mode'] ?? 'standard');
$region = dbnToolsNormalizeRegion($input['region'] ?? 'nordic');
$language = dbnToolsNormalizeLanguage($input['language'] ?? 'en');
+1 -1
View File
@@ -14,7 +14,7 @@ $input = dbnToolsJsonInput(400000);
$language = dbnToolsNormalizeLanguage($input['language'] ?? 'en');
dbnToolsWithTelemetry('timeline', $language, function () use ($input, $language, $ftUid): array {
$text = dbnToolsString($input, 'text', 128000);
$text = dbnToolsInjectDocContent($input, dbnToolsString($input, 'text', 128000));
$validEngines = ['azure_mini', 'azure_full', 'gpu'];
$engine = in_array((string)($input['engine'] ?? ''), $validEngines, true)
+39 -9
View File
@@ -32,17 +32,47 @@ $postModel = in_array($_POST['post_model'] ?? '', $allowedPostModels, true)
? (string)($_POST['post_model'] ?? '')
: '';
// ── Validate upload ───────────────────────────────────────────────────────────
// ── Validate upload (or load from stored audio corpus) ────────────────────────
$storedAudioTmp = null;
if (empty($_FILES['audio']) || $_FILES['audio']['error'] !== UPLOAD_ERR_OK) {
$code = $_FILES['audio']['error'] ?? -1;
$map = [
UPLOAD_ERR_INI_SIZE => 'File exceeds server upload limit.',
UPLOAD_ERR_FORM_SIZE => 'File exceeds form size limit.',
UPLOAD_ERR_PARTIAL => 'File was only partially uploaded.',
UPLOAD_ERR_NO_FILE => 'No audio file received.',
];
dbnToolsError($map[$code] ?? "Upload error (code {$code}).", 400, 'upload_error');
// Check if the user picked a previously saved audio document
$audioDocId = (int)($_POST['audio_doc_id'] ?? 0);
if ($audioDocId > 0) {
$clientId = dbnToolsClientIdFromSession();
if ($clientId <= 0) {
dbnToolsError('No audio file received and no valid session for stored audio.', 400, 'upload_error');
}
$db = dbnToolsDb();
$row = $db->prepare(
'SELECT audio_storage_path, title FROM client_documents
WHERE id = ? AND client_id = ? AND source_type = ? AND status = ? LIMIT 1'
);
$row->execute([$audioDocId, $clientId, 'audio', 'ready']);
$audioRow = $row->fetch(PDO::FETCH_ASSOC);
if (!$audioRow || empty($audioRow['audio_storage_path']) || !is_readable((string)$audioRow['audio_storage_path'])) {
dbnToolsError('Stored audio file not found or not readable.', 404, 'audio_not_found');
}
// Synthesise a $_FILES-compatible entry pointing at the stored file
$storedAudioTmp = $audioRow['audio_storage_path'];
$_FILES['audio'] = [
'name' => basename($storedAudioTmp),
'tmp_name' => $storedAudioTmp,
'error' => UPLOAD_ERR_OK,
'size' => (int)filesize($storedAudioTmp),
'type' => mime_content_type($storedAudioTmp) ?: 'application/octet-stream',
];
} else {
$code = $_FILES['audio']['error'] ?? -1;
$map = [
UPLOAD_ERR_INI_SIZE => 'File exceeds server upload limit.',
UPLOAD_ERR_FORM_SIZE => 'File exceeds form size limit.',
UPLOAD_ERR_PARTIAL => 'File was only partially uploaded.',
UPLOAD_ERR_NO_FILE => 'No audio file received.',
];
dbnToolsError($map[$code] ?? "Upload error (code {$code}).", 400, 'upload_error');
}
}
$file = $_FILES['audio'];